V2_0.java [src/java/d/crlmod/residue] Revision: default  Date:
/*
 * $Id: 1.0+69 V2_0.java 68122e0cd0dd 2023-02-02 casesp $
 *
 * 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.Statement;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Path;

/**
 *
 * @author Lucas Yaege
 */
@Name("residue")
@Description("Residue service for the CRLMOD_2018 Database.  Results from this service are returned in the rotation v1 format.")
@VersionInfo("2.0")
@State(RELEASED)
@Path("d/residue/2.0")
@Resource(from = ServiceResources.class)
public class V2_0 extends ModelDataService {
//TODO: Refactor for residues.

  LMODData lmodData = new LMODData();

  private final String OBJECT_PREFIX = "res";
  private final String OBJECT_NAME = "Residue";
  private boolean nativeFormats = false;
  private boolean namesOnly = false;

  @Override
  protected void doProcess() throws Exception {
    //does the user want just names?
    namesOnly = parameter().getBoolean(KEY_NAMESONLY, false);
    //does the user want param_sets, or just base data?
    nativeFormats = parameter().getBoolean(KEY_NATIVEFORMATS, false);
    getResidues();
  }

  @Override
  protected void postProcess() throws Exception {
    if (namesOnly) {
      for (Residue res : lmodData.residues) {
        res.id = null;
      }
    }
    results().put("lmod", lmodData.getJson());
  }

  private void getResidues() throws Exception {
    List<String> params = new ArrayList<>();
    String query = "SELECT res_id, res_name, res_group1, res_group2, res_group3, res_group4, res_group5,"
        + "weps_res_param_set, wepp_res_param_set\n"
        + "FROM residues \n"
        + "     LEFT JOIN weps_residues on (fk_weps_res_id = weps_res_id)\n"
        + "     LEFT JOIN wepp_residues on (fk_wepp_res_id = wepp_res_id)\n";

    query += Utils.createWhere(params, OBJECT_PREFIX, OBJECT_NAME, parameter(), LOG);

    String countQuery = "SELECT Count(*) AS 'Count' \n"
        + "FROM residues \n";
    countQuery += Utils.createWhere(new ArrayList<String>(), OBJECT_PREFIX, 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 += Utils.createOffsetAndLimits(params, parameter(), LOG, OBJECT_NAME, OBJECT_PREFIX);

        try (ResultSet resultSet = statement.executeQuery(query)) {
          if (!resultSet.next()) {
            String 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 res_id = resultSet.getBigDecimal("res_id");
            String res_name = resultSet.getString("res_name");
            String res_group1 = resultSet.getString("res_group1");
            String res_group2 = resultSet.getString("res_group2");
            String res_group3 = resultSet.getString("res_group3");
            String res_group4 = resultSet.getString("res_group4");
            String res_group5 = resultSet.getString("res_group5");

            String weps_param_set = resultSet.getString("weps_res_param_set");
            String wepp_param_set = resultSet.getString("wepp_res_param_set");

            if (weps_param_set == null)
              weps_param_set = "No Matching WEPS data is available at this time";

            if (wepp_param_set == null)
              wepp_param_set = "No Matching WEPP data is available at this time";

            weps_param_set = filterXML(weps_param_set);
            wepp_param_set = filterXML(wepp_param_set);

            Residue res = new Residue();
            res.id = res_id.toBigInteger();
            res.name = res_name;
            res.resGroup1 = res_group1;
            res.resGroup2 = res_group2;
            res.resGroup3 = res_group3;
            res.resGroup4 = res_group4;
            res.resGroup5 = res_group5;

            if (nativeFormats) {
              res.wepsResParamSet = weps_param_set;
              res.weppResParamSet = wepp_param_set;
            }
            lmodData.addResidue(res);
          } while (resultSet.next());
        }
        statement.clearBatch();
      }
    }
  }

  /**
   * This filter was required in order to attempt to fix non UTF-8 encoded XML
   * data provided by ARS.
   *
   * @param value
   * @return
   */
  protected String filterXML(String value) {
    return value.replaceAll("\n", "").replaceAll("\t", "");
  }

  protected String getJDBCId() {
    return CR_LMOD_2018_ID;
  }

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