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

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-02: Get National Category Names associated with selected State and
 * Operation Type
 *
 * @author Rumpal Sidhu
 * @version 1.0
 */
@Name("NMPT-02: Get National Category Names associated with selected State and Operation Type")
@Description("Return a list of national category names associated with selected state and operation type.")
@Path("m/nmpt/getcatfeatname/1.0")
@Polling(first = 10000, next = 2000)
@Resource(from = DBResources.class)

public class V1_0 extends ModelDataService {

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

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

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

    @Override
    protected void postProcess() throws ServiceException, JSONException {
        putResult("stateFIPS", stateFIPS, "State FIPS Code");
        putResult("operation_type_id", operationTypeId, "Operation Type Identifier");
        JSONArray resultArray = new JSONArray();
        for (NationalCategory category : list) {
            JSONArray resultArr = new JSONArray();
            resultArr.put(JSONUtils.dataDesc("national_category_id", category.id, "National Category Identifier"));
            resultArr.put(JSONUtils.dataDesc("national_category_name", category.name, "National Category Name"));
            resultArr.put(JSONUtils.dataDesc("national_category_displayOrder", category.displayOrder, "National Category Display Order In List"));
            resultArray.put(resultArr);
        }
        putResult("National Category List", resultArray);
    }

    static class NationalCategory {

        protected int id;
        protected String name;
        protected int displayOrder;

        public NationalCategory(int id, String name, int displayOrder) {
            this.id = id;
            this.name = name;
            this.displayOrder = displayOrder;
        }

    }
}