V1_0.java [src/java/d/crlmod/crop] Revision:   Date:
/*
 * $Id: 1.0+65 V1_0.java 402d39c37049 2021-12-29 od $
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2024, 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 d.crlmod.crop;

import crlmod.LMODData;
import crlmod.ServiceResources;
import static crlmod.ServiceResources.*;
import crlmod.nodes.Crop;
import crlmod.utils.Utils;
import csip.ModelDataService;
import csip.api.server.ServiceException;
import csip.annotations.*;
import static csip.annotations.State.RELEASED;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import javax.ws.rs.Path;

@Name("crops")
@Description("Deprectaed.  This service is no longer supported.")
@VersionInfo("1.0")
@State(RELEASED)
@Path("d/crop/1.0")
@Resource(from = ServiceResources.class)
public class V1_0 extends ModelDataService {

  LMODData lmodData = new LMODData();

  private final String OBJECT_KEY = "veg";
  private final String OBJECT_NAME = "Crop";

  @Override
  protected void doProcess() throws Exception {
    try (Connection connection = resources().getJDBC(getJDBCId())) {
      getCrops(connection);
    }
  }

  @Override
  protected void postProcess() throws Exception {
    results().put("lmod", lmodData.getJson());
  }

  ////////////////////////////////////////////////////////////////////////////
  private void getCrops(Connection connection) throws Exception {
    List<String> params = new ArrayList<>();

    String query = "SELECT file_name, file_key FROM file_metadata \n";
    query += Utils.createWhere(params, OBJECT_KEY, OBJECT_NAME, parameter(), LOG);

    String countQuery = "SELECT Count(*) AS 'Count' FROM file_metadata \n";
    countQuery += Utils.createWhere(new ArrayList<>(), OBJECT_KEY, OBJECT_NAME, parameter(), LOG); // dummy list

    try (Statement statement = connection.createStatement()) {
      try (ResultSet resultSet = statement.executeQuery(countQuery)) {
        resultSet.next();
        lmodData.cropCount = resultSet.getInt("Count");
      }
      query += Utils.createOffsetAndLimits(params, parameter(), LOG, OBJECT_NAME, OBJECT_KEY);

      LOG.log(Level.INFO, "Crop: " + query);
      try (ResultSet resultSet = statement.executeQuery(query)) {
        if (!resultSet.next()) {
          String errors = "";
          errors = "No crops found with ";
          for (int i = 0; i < params.size(); i++) {
            errors += params.get(i);
            if (i < params.size() - 1) {
              errors += "and ";
            }
          }
          throw new ServiceException(errors);
        }
        do {
          BigInteger file_key = resultSet.getBigDecimal("file_key").toBigIntegerExact();
          Crop crop = new Crop(file_key);
          lmodData.addCrop(crop);
        } while (resultSet.next());
      }
      statement.clearBatch();
    }
  }

  protected String getJDBCId() {
    return LMOD_ID;
  }

  @Override
  protected Map<String, Object> getConfigInfo() {
    return new LinkedHashMap<String, Object>() {
      {
        put(getJDBCId(), resources().getResolved(getJDBCId()));
      }
    };
  }

}