V1_0.java [src/java/m/svap/svap06_svapstreamflow] Revision: default  Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2017, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package m.svap.svap06_svapstreamflow;

import csip.annotations.Polling;
import csip.ModelDataService;
import csip.ServiceException;
import WaterData.WaterData;
import WaterData.WaterDataException;
import WaterData.WaterDataInterface;
import javax.ws.rs.Path;
import csip.annotations.Description;
import csip.annotations.Name;

/**
 * SVAP-06: Get Stream Gauge Location and Discharge for Stream Reach Description
 *
 * @author Robert Streetman
 * @author Rumpal Sidhu
 * @version 1.0
 */
@Name("SVAP-06: Get Stream Gauge Location and Discharge for Stream Reach Description")
@Description("This service returns the discharge in cubic feet per second for "
        + "the most recent date from a representative stream gauging station.")
@Path("m/svap/svapstreamflow/1.0")
@Polling(first = 10000, next = 2000)

public class V1_0 extends ModelDataService {

    private String stationId, lastDate, lastData, organization;

    @Override
    protected void preProcess() throws ServiceException {
        stationId = parameter().getString("station_id");
        organization = parameter().getString("organization");
        validateSource();
    }

    @Override
    protected void doProcess() throws ServiceException, WaterDataException {
        String[][] flowData = null;

        if (organization.equals("STORET")) {
            WaterDataInterface waterData = WaterData.getNewWaterDataInterface(organization, null, null);
            flowData = waterData.extractFlowData_formatted(null, organization, stationId, "", "");

        } else {
            WaterDataInterface waterData = WaterData.getNewWaterDataInterface(organization, null, null);
            flowData = waterData.extractFlowData_formatted(null, organization, stationId, "", "");

        }

        if (flowData != null && flowData.length > 0) {
            lastDate = flowData[flowData.length - 1][0];
            lastData = flowData[flowData.length - 1][1];
        } else {
            results().put("message", "No data found, please check the station id");
        }
    }

    @Override
    protected void postProcess() {
        results().put("station_id", stationId, "Station Identifier");
        results().put("date_latest_measurement", lastDate, "Date of Latest Station Measurement.");
        results().put("latest_measurement", lastData, "Data from Latest Station Measurement.", "cubic feet per second (cfs)");
    }

    private void validateSource() throws ServiceException {
        if (!organization.equals("USGS") && !organization.equals("STORET") && !organization.equals("CDSN")) {
            throw new ServiceException("Please check the organization name.");
        }
    }
}