V1_0.java [src/java/d/crlmod/dataversion] 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.dataversion;

import crlmod.ServiceResources;
import static crlmod.ServiceResources.CR_LMOD_ID;
import csip.ModelDataService;
import csip.api.server.ServiceException;
import csip.annotations.*;
import static csip.annotations.State.RELEASED;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import javax.ws.rs.Path;

@Name("dataversion")
@Description("lmod data version")
@VersionInfo("1.0")
@State(RELEASED)
@Path("d/dataversion/1.0")
@Resource(from = ServiceResources.class)
public class V1_0 extends ModelDataService {

  @Override
  protected void doProcess() throws Exception {
    String dv = getDataVersion();
    results().put("dataversion", dv);
  }

  ////////////////////////////////////////////////////////////////////////////
  private String getDataVersion() throws Exception {
    try (Connection connection = resources().getJDBC(getJDBCId()); Statement statement = connection.createStatement()) {
      String query = "SELECT version FROM version;";
      LOG.log(Level.INFO, "dataVersion: " + query);
      try (ResultSet resultSet = statement.executeQuery(query)) {
        if (!resultSet.next())
          throw new ServiceException("No version data available for LMOD");

        return resultSet.getString("version");
      }
    }
  }

  protected String getJDBCId() {
    return CR_LMOD_ID;
  }

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

}