V1_0.java [src/java/m/rhem/rhem05_slopeShapes] Revision: aecfd5ebf9fab244ef52c5ee6fbe4fb1fcdbfa8a  Date: Tue Oct 04 14:59:19 MDT 2016
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package m.rhem.rhem05_slopeShapes;

import csip.ModelDataService;
import csip.ServiceException;
import csip.annotations.Polling;
import csip.annotations.Resource;
import csip.utils.JSONUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import javax.ws.rs.Path;
import oms3.annotations.Description;
import oms3.annotations.Name;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import rhem.utils.DBResources;
import static rhem.utils.DBResources.MSSQL_RHEM;

/**
 * @version 1.0
 * @author rumpal
 */
@Name("RHEM-05: Get RHEM Slope Shape Choice List.")
@Description("Get and return a list of RHEM slope shapes.")
@Path("m/rhem/getslopeshapes/1.0")
@Polling(first = 10000, next = 2000)
@Resource(from = DBResources.class)

public class V1_0 extends ModelDataService {

    private ArrayList<ChoiceList> choiceList;

    @Override
    public void preProcess() throws ServiceException {
        choiceList = new ArrayList<>();
    }

    @Override
    public void doProcess() throws ServiceException {

        try (Connection conn = getResourceJDBC(MSSQL_RHEM);
                Statement statement = conn.createStatement();) {
            String query = "SELECT choice_id, choice_label "
                    + "FROM rhem.d_rhem_slope_shape "
                    + "WHERE obsolete = 'false';";

            try (ResultSet resultSet = statement.executeQuery(query)) {
                while (resultSet.next()) {
                    int id = resultSet.getInt("choice_id");
                    String kind = "slope shape";
                    int sequence = id;
                    String label = resultSet.getString("choice_label");
                    choiceList.add(new ChoiceList(id, kind, sequence, label));
                }
            }
        } catch (ServiceException | SQLException se) {
            LOG.log(Level.SEVERE, "RHEM-07: SQLException.", se);
            throw new ServiceException("SQL Problem.", se);
        }
    }

    @Override
    public void postProcess() throws Exception {
        try {
            JSONArray resultArr = new JSONArray();
            for (ChoiceList list : choiceList) {
                JSONArray choiceListArr = new JSONArray();
                choiceListArr.put(JSONUtils.dataDesc("choice_id", list.getChoiceId(), "Choice Identifier"));
                choiceListArr.put(JSONUtils.dataDesc("choice_kind", list.getChoiceKind(), "Choice Kind"));
                choiceListArr.put(JSONUtils.dataDesc("choice_sequence", list.getChoiceSequence(), "Choice Sequence"));
                choiceListArr.put(JSONUtils.dataDesc("choice_label", list.getChoiceLabel(), "Choice Label"));
                resultArr.put(JSONUtils.dataDesc("slope_shape", choiceListArr, "Slope Shape"));
            }
            putResult("choice_lists", resultArr, "Slope Shape Choice List");
        } catch (JSONException ex) {
            LOG.log(Level.SEVERE, "RHEM-07: Error in processing the reponse JSON.", ex);
            throw new ServiceException("Error in processing the reponse JSON.", ex);
        }
    }

    static class ChoiceList {

        protected int id;
        protected String kind;
        protected int sequence;
        protected String label;

        public ChoiceList(int id, String kind, int sequence, String label) {
            this.id = id;
            this.kind = kind;
            this.sequence = sequence;
            this.label = label;
        }

        public int getChoiceId() {
            return this.id;
        }

        public String getChoiceKind() {
            return this.kind;
        }

        public int getChoiceSequence() {
            return this.sequence;
        }

        public String getChoiceLabel() {
            return this.label;
        }
    }
}