V1_0.java [src/java/m/comet/comet05_practparams] Revision: default  Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2017, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package m.comet.comet05_practparams;

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 javax.ws.rs.Path;
import m.comet.utils.DBQueries;
import m.comet.utils.DBResources;
import csip.annotations.Description;
import csip.annotations.Name;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import static m.comet.utils.DBResources.CRDB;

/**
 * COMET-05: Get Conservation Practices and Information to Display in CDSI
 * Application
 *
 * @author rumpal
 * @version 1.0
 */
@Name("COMET-05: Get Conservation Practices and Information to Display in CDSI Application")
@Description("This service returns the list of conservation practices that can "
        + "be applied to reduce greenhouse gas emissions and sequester carbon, i"
        + "ncluding relevant data to aid the CDSI application user in selectinsg "
        + "practices for a conservation plan.")
@Path("m/comet/cometpractparams/1.0")
@Polling(first = 10000, next = 2000)
@Resource(from = DBResources.class)
@Deprecated
public class V1_0 extends ModelDataService {

    private Integer practiceId;
    private Integer practiceScore;
    private ArrayList<PracticeScore> practiceScoreList = new ArrayList<>();

    @Override
    public void preProcess() throws ServiceException {
        practiceId = parameter().getInt("practice_id", -1);
        practiceScore = parameter().getInt("comet_practice_score", -1);
    }

    @Override
    public void doProcess() throws ServiceException, SQLException {
        practiceId = practiceId == -1 ? null : practiceId;
        practiceScore = practiceScore == -1 ? null : practiceScore;
        try (Connection sdmConnection = resources().getJDBC(CRDB);
                Statement statement = sdmConnection.createStatement();
                ResultSet resultSet = statement.executeQuery(DBQueries.COMET05Query01(practiceId, practiceScore));) {
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String code = resultSet.getString("pract_code");
                String name = resultSet.getString("pract_name");
                String benifit = resultSet.getString("pract_benefit");
                int score = resultSet.getInt("score");
                practiceScoreList.add(new PracticeScore(id, code, name, benifit, score));
            }
        }
    }

    @Override
    public void postProcess() throws JSONException {
        if (practiceScoreList.isEmpty()) {
            results().put("message", "Not a COMET practice or insufficient domain data to compute result.");
        } else {
            JSONArray array = new JSONArray();
            for (PracticeScore ps : practiceScoreList) {
                JSONArray arr = new JSONArray();
                arr.put(JSONUtils.dataDesc("practice_id", ps.id, "Conservation Practice Identifier"));
                arr.put(JSONUtils.dataDesc("practice_code", ps.code, "Conservation Practice Code"));
                arr.put(JSONUtils.dataDesc("practice_name", ps.name, "Conservation Practice Name"));
                arr.put(JSONUtils.dataDesc("comet_practice_score", ps.score, "COMET Practice Score reflecting impact on mitigating greenhouse gas emissions and sequesting carbon"));
                arr.put(JSONUtils.dataDesc("beneficial_attributes", ps.benefit, "Benefits for mitigating greenhouse gas emissions and sequesting carbon"));
                array.put(arr);
            }
            results().put("Practice Score List", array);
        }
    }

    static class PracticeScore {

        int id;
        String code;
        String name;
        String benefit;
        int score;

        public PracticeScore(int id, String code, String name, String benefit, int score) {
            this.id = id;
            this.code = code;
            this.name = name;
            this.benefit = benefit;
            this.score = score;
        }
    }
}