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

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-03: Get National Standard Feature Names associated with selected State,
 * Operation Type and National Category Type
 *
 * @author Rumpal Sidhu
 * @version 1.0
 */
@Name("NMPT-03: Get National Standard Feature Names associated with selected "
        + "State, Operation Type and National Category Type")
@Description("Return a list of national standard features names associated with "
        + "selected state, operation type and national category type.")
@Path("m/nmpt/getnatfeatname/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 nationalCategoryId;
    private ArrayList<NationalFeature> list = new ArrayList<>();

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

    @Override
    protected void doProcess() throws ServiceException, SQLException {
        try (Connection connection = getResourceJDBC(CRDB);
                Statement statement = connection.createStatement();
                ResultSet resultSet = statement.executeQuery(DBQueries.NMPT03Query01(stateFIPS,
                        operationTypeId, nationalCategoryId,
                        new Date(Calendar.getInstance().getTimeInMillis())));) {
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int feature_num = resultSet.getInt("feature_num");
                list.add(new NationalFeature(id, name, feature_num));
            }
        }
    }

    @Override
    protected void postProcess() throws ServiceException, JSONException {
        putResult("stateFIPS", stateFIPS, "State FIPS Code");
        putResult("operation_type_id", operationTypeId, "Operation Type Identifier");
        putResult("national_category_id", nationalCategoryId, "National Category Identifier");
        JSONArray resultArray = new JSONArray();
        for (NationalFeature feature : list) {
            JSONArray resultArr = new JSONArray();
            resultArr.put(JSONUtils.dataDesc("national_feature_stnd_id", feature.id, "National Standard Feature Identifier"));
            resultArr.put(JSONUtils.dataDesc("national_feature_stnd_name", feature.name, "National Standard Feature Name"));
            resultArr.put(JSONUtils.dataDesc("national_feature_stnd_order", feature.featureNum, "National Standard Feature Order In List"));
            resultArray.put(resultArr);
        }
        putResult("National Category List", resultArray);
    }

    static class NationalFeature {

        protected int id;
        protected String name;
        protected int featureNum;

        public NationalFeature(int id, String name, int featureNum) {
            this.id = id;
            this.name = name;
            this.featureNum = featureNum;
        }
    }
}