V1_0.java [src/java/m/wqm/pestmitgntechsfull] Revision: 2515c84ddbdf9dd482b16c880995fa43e75d802e  Date: Thu May 26 15:55:20 MDT 2016
/**
 * @author anvesh
 */
package m.wqm.pestmitgntechsfull;

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-25: Pesticide Migration Techniques")
@Description("This service returns all rows and columns from the wqm_ipm_technique_scores table.")
@Path("m/pestmigtechsfull/1.0")
@Resource(from = DBResources.class)

public class V1_0 extends ModelDataService {

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

    @Override
    protected void doProcess() throws Exception {
        result1 = new ArrayList<>();
        try (Connection conn = getResourceJDBC(WQM_ID);
                Statement statement = conn.createStatement()) {
            String query = "SELECT * FROM wqm.wqm_ipm_technique_scores";
            try (ResultSet results = statement.executeQuery(query)) {
                while (results.next()) {
                    int ipm_technique_score_id = results.getInt("ipm_technique_score_id");
                    int ipm_technique_id = results.getInt("ipm_technique_id");
                    int ipm_technique_score = results.getInt("ipm_technique_score");
                    String ipm_technique_name = results.getString("ipm_technique_name");
                    String wqm_concern = results.getString("wqm_concern");

                    result1.add(new m.wqm.pestmitgntechsfull.V1_0.Result1(ipm_technique_score_id,
                            ipm_technique_id, ipm_technique_score, ipm_technique_name, wqm_concern));
                }
            }
        } catch (SQLException se) {
            LOG.log(Level.SEVERE, "Did not open database for WQM-25!", 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.pestmitgntechsfull.V1_0.Result1 rs1 : result1) {
                JSONArray tmpArr = new JSONArray();
                tmpArr.put(JSONUtils.dataDesc("ipm_technique_score_id", rs1.ipm_technique_score_id, "ipm_technique_score_id"));
                tmpArr.put(JSONUtils.dataDesc("ipm_technique_id", rs1.ipm_technique_id, "ipm_technique_id"));
                tmpArr.put(JSONUtils.dataDesc("ipm_technique_score", rs1.ipm_technique_score, "ipm_technique_score"));
                tmpArr.put(JSONUtils.dataDesc("ipm_technique_name", rs1.ipm_technique_name, "Name of the integrated pest management technique"));
                tmpArr.put(JSONUtils.dataDesc("wqm_concern", rs1.wqm_concern, "WQM resource concern:  pesticide leaching, pesticide solution runoff, pesticide adsorbed runoff, pesticide drift"));
                result1Arr.put(JSONUtils.dataDesc("IPM techn Scores", tmpArr, "IPMTechnScores"));
            }
            putResult("operation", result1Arr);
        } catch (JSONException ex) {
            LOG.log(Level.SEVERE, "Error in processing the response JSON for WQM-25!", ex);
            throw new ServiceException("Error in processing the response JSON.", ex);
        }
    }

    static class Result1 {

        int ipm_technique_score_id;
        int ipm_technique_id;
        int ipm_technique_score;
        String ipm_technique_name;
        String wqm_concern;

        public Result1(
                int ipm_technique_score_id,
                int ipm_technique_id,
                int ipm_technique_score,
                String ipm_technique_name,
                String wqm_concern) {
            this.ipm_technique_score_id = ipm_technique_score_id;
            this.ipm_technique_id = ipm_technique_id;
            this.ipm_technique_score = ipm_technique_score;
            this.ipm_technique_name = ipm_technique_name;
            this.wqm_concern = wqm_concern;

        }
    }

}