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

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-04: Get RHEM Surface Texture Classes Choice List.")
@Description("Get and return a list of RHEM surface soil texture classes.")
@Path("m/rhem/getsurftexclasses/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, ClassNotFoundException {
        try (Connection connection = getResourceJDBC(MSSQL_RHEM);
                Statement statement = connection.createStatement();) {
            String query = "SELECT text_id, text_abreviation, text_label "
                    + "FROM rhem.d_rhem_text_lookup "
                    + "WHERE text_id "
                    + "IN (SELECT DISTINCT(text_class_id) "
                    + "FROM rhem.d_rhem_texture_class_subclass "
                    + "WHERE obsolete = 'false');";

            try (ResultSet resultSet = statement.executeQuery(query)) {
                while (resultSet.next()) {
                    int id = resultSet.getInt("text_id");
                    String kind = "texture class";
                    int sequence = id;
                    String dataTextEntry = resultSet.getString("text_abreviation");
                    String label = resultSet.getString("text_label");
                    choiceList.add(new ChoiceList(id, kind, sequence,
                            dataTextEntry, label));
                }
            }
        } catch (SQLException se) {
            LOG.log(Level.SEVERE, "RHEM-06: 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_data_entry_text", list.getChoiceDataEntryText(), "Choice Data Text Entry"));
                choiceListArr.put(JSONUtils.dataDesc("choice_label", list.getChoiceLabel(), "Choice Label"));
                resultArr.put(JSONUtils.dataDesc("surface_texture_classes", choiceListArr, "Surface Texture Classes Choice List"));
            }
            putResult("choice_lists", resultArr, "List");
        } catch (JSONException ex) {
            LOG.log(Level.SEVERE, "RHEM-06: Error in processing the response JSON.", ex);
            throw new ServiceException("Error in processing the response JSON.", ex);
        }
    }

    static class ChoiceList {

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

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

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

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

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

        public String getChoiceDataEntryText() {
            return this.dataEntryText;
        }

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