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

import csip.annotations.Polling;
import csip.annotations.Resource;
import csip.ModelDataService;
import csip.ServiceException;
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 csip.annotations.Description;
import csip.annotations.Name;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import svap.utils.DBQueries;
import svap.utils.DBResources;
import static svap.utils.DBResources.CRDB;

/**
 * SVAP-10a: Get Stream Assessment Elements for Field Assessment
 *
 * @author Robert Streetman
 * @author Rumpal Sidhu
 * @version 1.0
 */
@Name("SVAP-10a:  Get Stream Assessment Elements for Field Assessment")
@Description("This service returns assessment element identifiers, names, and "
    + "descriptions for populating a SVAP application choice list.")
@Path("m/svap/svapelements/1.0")
@Polling(first = 10000, next = 2000)
@Resource(from = DBResources.class)

public class V1_0 extends ModelDataService {

  private ArrayList<Element> elementList = new ArrayList();


  @Override
  protected void doProcess() throws ServiceException, SQLException {
    try (Connection connection = resources().getJDBC(CRDB);
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(DBQueries.SVAP10aQuery01());) {
      while (resultSet.next()) {
        int id = resultSet.getInt("id");
        String name = resultSet.getString("name");
        String description = resultSet.getString("description");
        elementList.add(new Element(id, name, description));
      }
    }
  }


  @Override
  protected void postProcess() throws ServiceException, JSONException {
    JSONArray elementsArr = new JSONArray();
    for (Element element : elementList) {
      JSONArray elemArr = new JSONArray();
      elemArr.put(JSONUtils.dataDesc("element_id", element.getId(), "Element Identifier"));
      elemArr.put(JSONUtils.dataDesc("element_name", element.getName(), "Element Name"));
      elemArr.put(JSONUtils.dataDesc("element_description", element.getDescription(), "Element Description"));
      elementsArr.put(elemArr);
    }
    results().put("elements", elementsArr);
  }

  static class Element {

    protected int id;
    protected String name;
    protected String description;


    public Element(int id, String name, String description) {
      this.id = id;
      this.name = name;
      this.description = description;
    }


    public int getId() {
      return id;
    }


    public String getName() {
      return name;
    }


    public String getDescription() {
      return description;
    }

  }
}