V1_0.java [src/java/d/crlmod/residue] Revision: default  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.residue;

import crlmod.LMODData;
import crlmod.ServiceResources;
import static crlmod.ServiceResources.*;
import crlmod.nodes.Residue;
import crlmod.utils.Utils;
import csip.ModelDataService;
import csip.api.server.ServiceException;
import csip.annotations.*;
import static csip.annotations.State.RELEASED;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
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;
import org.codehaus.jettison.json.JSONException;

/**
 *
 * @author User
 */
@Name("residues")
@Description("lmod residues")
@VersionInfo("1.0")
@State(RELEASED)
@Path("d/residue/1.0")
@Resource(from = ServiceResources.class)
public class V1_0 extends ModelDataService {

  LMODData lmodData = new LMODData();

  private static final String OBJECT_KEY = "rsd";
  private static final String OBJECT_NAME = "Residue";

  @Override
  protected void doProcess() throws Exception {
    getResidues();
  }

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

  ////////////////////////////////////////////////////////////////////////////
  private void getResidues() throws JSONException, SQLException, ServiceException {
    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' \n"
        + "FROM file_metadata \n";
    countQuery += Utils.createWhere(new ArrayList<String>(), OBJECT_KEY, OBJECT_NAME, parameter(), LOG);

    try (Connection connection = resources().getJDBC(getJDBCId())) {
      try (Statement statement = connection.createStatement()) {
        try (ResultSet resultSet = statement.executeQuery(countQuery)) {
          resultSet.next();
          lmodData.residueCount = resultSet.getInt("Count");
        }

        query += "ORDER BY file_key \n";

        String off = parameter().getString(KEY_OFFSET, "0");
        if (!off.equals("") && Utils.isNumeric(off)) {
          params.add("offset " + off + " ");
          query += "OFFSET " + off + " ROWS\n";
        } else {
          query += "OFFSET 0 ROWS\n";
          params.add("default offset of 0");
          LOG.log(Level.INFO, "Managements: no offset");
        }

        String lim = parameter().getString(KEY_LIMIT, "");
        if (lim.equals("all")) {
          LOG.log(Level.INFO, "Residues: all Residues requested");
        } else if (!lim.equals("") && Utils.isNumeric(lim)) {
          int limInt = -1;
          try {
            limInt = Integer.parseInt(lim);
          } catch (NumberFormatException ex) {
            throw new ServiceException("Invalid number of records requested.  Limit must be a positive integer between 1 and 100.");
          }
          if (limInt < 1) {
            throw new ServiceException("Residues: limit specified is out of range.  Limit must be a positive integer between 1 and 100.");
          }
          if (limInt > 100) {
            limInt = 100;
            LOG.log(Level.INFO, "Residues: limit specified is out of range, adjusting to 100.");
            params.add("limit automatically adjusted to 100.  This service only allows 100 rows to be requested at a time.");
          }
          params.add("limit " + limInt + "");
          query += "FETCH NEXT " + limInt + " ROWS ONLY\n";
        } else if (lim.equals("")) {
          LOG.log(Level.INFO, "Residues: no limit specified adujsting to 1");
          query += "FETCH NEXT 1 ROWS ONLY\n";
        } else {
          throw new ServiceException("Residues: Error - limit specified is not numeric.  Limit must be a positive integer");
        }

        LOG.log(Level.INFO, "Residue: {0}", query);
        try (ResultSet resultSet = statement.executeQuery(query)) {
          if (!resultSet.next()) {
            String errors = "";
            errors = "No residues 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 {
            BigDecimal file_key = resultSet.getBigDecimal("file_key");
            Residue res = new Residue();
            res.id = file_key.toBigInteger();
            lmodData.addResidue(res);
          } 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()));
      }
    };
  }
}