V1_0.java [src/java/m/wqm/wqm25_pestmitgntechsfull] Revision:   Date:
/**
 * @author anvesh
 */
package m.wqm.wqm25_pestmitgntechsfull;

import csip.ModelDataService;
import csip.annotations.Resource;
import java.util.ArrayList;
import javax.ws.rs.Path;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.api.server.ServiceException;
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 org.codehaus.jettison.json.JSONException;
import wqm.utils.DBQueries;
import wqm.utils.DBResources;

@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<Result1> result1 = new ArrayList<>();

    @Override
    protected void doProcess() throws ServiceException, SQLException {
        try (Connection conn = resources().getJDBC(DBResources.WQM_READONLY_ID);
                Statement statement = conn.createStatement();
                ResultSet results = statement.executeQuery(DBQueries.WQM25Query())) {
            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 Result1(ipm_technique_score_id,
                        ipm_technique_id, ipm_technique_score, ipm_technique_name, wqm_concern));
            }
        }
    }

    @Override
    protected void postProcess() throws JSONException {
        JSONArray result1Arr = new JSONArray();
        for (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"));
        }
        results().put("operation", result1Arr);
    }

    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;

        }
    }

}