V1_0.java [src/java/m/nmpt/nmpt04_getsetbackdist] 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.nmpt.nmpt04_getsetbackdist;

import csip.annotations.Polling;
import csip.annotations.Resource;
import csip.ModelDataService;
import csip.ServiceException;
import csip.utils.JSONUtils;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import javax.ws.rs.Path;
import nmpt.utils.DBQueries;
import nmpt.utils.DBResources;
import static nmpt.utils.DBResources.CRDB;
import csip.annotations.Description;
import csip.annotations.Name;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;

/**
 * NMPT-04: Get State Feature Names, Setback Descriptions and Distances
 * associated with selected State, Operation Type, and National Standard Feature
 * Type
 *
 * @author Rumpal Sidhu
 * @version 1.0
 */
@Name("NMPT-04: Get State Feature Names, Setback Descriptions and Distances "
        + "associated with selected State, Operation Type, and National Standard Feature Type")
@Description("Return a list of state feature names, setback descriptions and "
        + "distances associated with selected state, operation type and national standard feature type.")
@Path("m/nmpt/getsetbackdist/1.0")
@Polling(first = 10000, next = 2000)
@Resource(from = DBResources.class)

public class V1_0 extends ModelDataService {

    private String stateFIPS;
    private int operationTypeId;
    private int nationalFeatureId;
    private ArrayList<NationalFeature> list = new ArrayList<>();

    @Override
    protected void preProcess() throws ServiceException {
        stateFIPS = getStringParam("stateFIPS");
        operationTypeId = getIntParam("operation_type_id");
        nationalFeatureId = getIntParam("national_feature_stnd_id");
    }

    @Override
    protected void doProcess() throws ServiceException, SQLException {
        try (Connection connection = getResourceJDBC(CRDB);
                Statement statement = connection.createStatement();
                ResultSet resultSet = statement.executeQuery(DBQueries.NMPT04Query01(stateFIPS,
                        operationTypeId, nationalFeatureId,
                        new Date(Calendar.getInstance().getTimeInMillis())));) {
            while (resultSet.next()) {
                int stateFeatureId = resultSet.getInt("state_feature_id");
                String stateFeatureName = resultSet.getString("state_feature_name");
                String stateFeatureNotes = resultSet.getString("state_feature_notes");
                int setbackDescriptionId = resultSet.getInt("setback_description_id");
                String setbackDescription = resultSet.getString("setback_description");
                String setbackDescriptionNotes = resultSet.getString("setback_description_notes");
                int setbackDistanceId = resultSet.getInt("setback_distance_id");
                int setbackDistance = resultSet.getInt("setback_distance");
                String setbackDistanceNotes = resultSet.getString("setback_distance_notes");
                list.add(new NationalFeature(stateFeatureId, stateFeatureName, stateFeatureNotes,
                        setbackDescriptionId, setbackDescription, setbackDescriptionNotes,
                        setbackDistanceId, setbackDistance, setbackDistanceNotes));
            }
        }
    }

    @Override
    protected void postProcess() throws ServiceException, JSONException {
        JSONArray resultArray = new JSONArray();
        for (NationalFeature feature : list) {
            JSONArray resultArr = new JSONArray();
            resultArr.put(JSONUtils.dataDesc("stateFIPS", stateFIPS, "State FIPS Code"));
            resultArr.put(JSONUtils.dataDesc("operation_type_id", operationTypeId, "Operation Type Identifier"));
            resultArr.put(JSONUtils.dataDesc("national_feature_stnd_id", nationalFeatureId, "National Standard Feature Identifier"));
            resultArr.put(JSONUtils.dataDesc("state_feature_id", feature.stateFeatureId, "State Feature Identifier"));
            resultArr.put(JSONUtils.dataDesc("state_feature_name", feature.stateFeatureName, "State Feature Name"));
            resultArr.put(JSONUtils.dataDesc("state_feature_notes", feature.stateFeatureNotes, "State Feature Notes"));
            resultArr.put(JSONUtils.dataDesc("setback_description_id", feature.setbackDescriptionId, "Setback Description Identifier"));
            resultArr.put(JSONUtils.dataDesc("setback_description", feature.setbackDescription, "Application Criteria Name"));
            resultArr.put(JSONUtils.dataDesc("setback_description_notes", feature.setbackDescriptionNotes, "Setback Description Notes"));
            resultArr.put(JSONUtils.dataDesc("setback_distance_id", feature.setbackDistanceId, "Setback Distance Identifier"));
            resultArr.put(JSONUtils.dataDesc("setback_distance", feature.setbackDistance, "Distance in feet required for manure application setback from feature"));
            resultArr.put(JSONUtils.dataDesc("setback_distance_notes", feature.setbackDistanceNotes, "Setback Distance Notes"));
            resultArray.put(resultArr);
        }
        putResult("National Category List", resultArray);
    }

    static class NationalFeature {

        protected int stateFeatureId;
        protected String stateFeatureName;
        protected String stateFeatureNotes;
        protected int setbackDescriptionId;
        protected String setbackDescription;
        protected String setbackDescriptionNotes;
        protected int setbackDistanceId;
        protected int setbackDistance;
        protected String setbackDistanceNotes;

        public NationalFeature(int stateFeatureId, String stateFeatureName, String stateFeatureNotes,
                int setbackDescriptionId, String setbackDescription, String setbackDescriptionNotes,
                int setbackDistanceId, int setbackDistance, String setbackDistanceNotes) {
            this.stateFeatureId = stateFeatureId;
            this.stateFeatureName = stateFeatureName;
            this.stateFeatureNotes = stateFeatureNotes;
            this.setbackDescriptionId = setbackDescriptionId;
            this.setbackDescription = setbackDescription;
            this.setbackDescriptionNotes = setbackDescriptionNotes;
            this.setbackDistanceId = setbackDistanceId;
            this.setbackDistance = setbackDistance;
            this.setbackDistanceNotes = setbackDistanceNotes;
        }
    }
}