V1_0.java [src/java/m/wqm/ipmscoresfull] Revision: 9dc6694d09df0e64f5859cb3a7250a48cb93ac9d  Date: Thu May 26 15:22:20 MDT 2016
package m.wqm.ipmscoresfull;

/**
 * @author anvesh, rumpal
 */
import csip.ModelDataService;
import csip.ServiceException;
import csip.annotations.Resource;
import java.util.ArrayList;
import javax.ws.rs.Path;
import oms3.annotations.Description;
import oms3.annotations.Name;
import org.codehaus.jettison.json.JSONArray;
import csip.utils.JSONUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import org.codehaus.jettison.json.JSONException;
import wqm.utils.DBResources;
import static wqm.utils.DBResources.WQM_ID;

@Name("WQM-24: IPM Scores")
@Description("This service returns all rows and columns from the wqm_ipm_scores table.")
@Path("m/ipmscoresfull/1.0")
@Resource(from = DBResources.class)

public class V1_0 extends ModelDataService {

    private ArrayList<m.wqm.ipmscoresfull.V1_0.Result1> result1;

    @Override
    protected void doProcess() throws Exception {
        this.result1 = new ArrayList<>();

        try (Connection connection = getResourceJDBC(WQM_ID);
                Statement statement = connection.createStatement()) {
            connection.setAutoCommit(false);
            String query = "SELECT * FROM wqm.wqm_ipm_scores";
            ResultSet results = statement.executeQuery(query);
            while (results.next()) {
                int ipm_score_id = results.getInt("ipm_score_id");
                int ipm_mitigation_score = results.getInt("ipm_mitigation_score");
                String resource_concern = results.getString("resource_concern");
                String ipm_level = results.getString("ipm_level");
                String wqm_concern = results.getString("wqm_concern");
                String ipm_level_name = results.getString("ipm_level_name");

                this.result1.add(new m.wqm.ipmscoresfull.V1_0.Result1(ipm_score_id, ipm_mitigation_score,
                        resource_concern, ipm_level, wqm_concern, ipm_level_name));

            }

        } catch (SQLException se) {
            LOG.log(Level.SEVERE, "Did not open database for WQM-24!", se);
            throw new ServiceException("SQL problem", se);
        }
    }

    @Override
    //writing the results back to JSON
    protected void postProcess() throws Exception {
        try {
            JSONArray result1Arr = new JSONArray();
            for (m.wqm.ipmscoresfull.V1_0.Result1 rs1 : result1) {
                JSONArray tmpArr = new JSONArray();
                tmpArr.put(JSONUtils.dataDesc("ipm_score_id", rs1.ipm_score_id, "ipm_score_id"));
                tmpArr.put(JSONUtils.dataDesc("ipm_mitigation_score", rs1.ipm_mitigation_score, "ipm_mitigation_score"));
                tmpArr.put(JSONUtils.dataDesc("resource_concern", rs1.resource_concern, "NRCS resource concern"));
                tmpArr.put(JSONUtils.dataDesc("ipm_level", rs1.ipm_level, "Treatment level of integrated pest management:  I (Basic), II (Intermediate), or III (Advanced)"));
                tmpArr.put(JSONUtils.dataDesc("wqm_concern", rs1.wqm_concern, "WQM resource concern:  pesticide leaching, pesticide solution runoff, pesticide adsorbed runoff, pesticide drift"));
                tmpArr.put(JSONUtils.dataDesc("ipm_level_name", rs1.ipm_level_name, "Treatment level of integrated pest management:  Basic, Intermediate, Advanced"));
                result1Arr.put(JSONUtils.dataDesc("IPM  Scores", tmpArr, "IPMScores"));
            }
            putResult("operation", result1Arr);
        } catch (JSONException ex) {
            LOG.log(Level.SEVERE, "Error in processing the response JSON for WQM-24!", ex);
            throw new ServiceException("Error in processing the response JSON.", ex);
        }
    }

    static class Result1 {

        int ipm_score_id;
        int ipm_mitigation_score;
        String resource_concern;
        String ipm_level;
        String wqm_concern;
        String ipm_level_name;

        public Result1(
                int ipm_score_id,
                int ipm_mitigation_score,
                String resource_concern,
                String ipm_level,
                String wqm_concern,
                String ipm_level_name) {
            this.ipm_score_id = ipm_score_id;
            this.ipm_mitigation_score = ipm_mitigation_score;
            this.resource_concern = resource_concern;
            this.ipm_level = ipm_level;
            this.wqm_concern = wqm_concern;
            this.ipm_level_name = ipm_level_name;

        }
    }

}