V1_0.java [src/java/m/crp/weppmetadata] Revision:   Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2019, 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.crp.weppmetadata;

import data.archives.MongoArchive;
import data.archives.ServiceArchive;
import csip.Config;
import csip.ModelDataService;
import csip.PayloadParameter;
import csip.PayloadResults;
import csip.ServiceException;
import java.util.ArrayList;
import java.util.logging.Level;
import javax.ws.rs.Path;
import data.models.ModelArchive;
import data.models.WEPPMetaData;
import data.models.WEPPModelArchive;
import csip.annotations.Description;
import csip.annotations.Name;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
@Name("WEPPMetaData")
@Description("Extracts Metadata from WEPP model runs into sets of meta-model parameters")
@Path("m/weppmetadata/1.0")
public class V1_0 extends ModelDataService {

  protected static String dbLocation = Config.getString("wepp.archive.mongodb.uri", "mongodb://csip.engr.colostate.edu:8095/8083_iet_csip-wepp");
  protected String suid = "";
  protected ServiceArchive mongoArchive;
  protected ModelArchive model;

  @Override
  public void preProcess() throws Exception {
    PayloadParameter params = this.parameter();
    ArrayList<ModelArchive> results = null;

    if (params.has("suid")) {
      suid = params.getString("suid");
    } else {
      throw new ServiceException("Service requires an input suid value");
    }
    if (params.has("wepp_archive_mongodb")) {
      dbLocation = params.getString("wepp_archive_mongodb");
    }

    if (dbLocation.contains("mongodb://")) {
      mongoArchive = new MongoArchive(dbLocation);
    } else {
      throw new ServiceException("No valid mongodb archive location was specified.  The location, " + dbLocation + ", specified, was not amongodb location.");// mongoArchive = new FileArchive(dbLocation);
    }

    ArrayList<ModelArchive> tArchive = mongoArchive.getArchivesByFilter("_id", suid, 1);

    if (tArchive.isEmpty()) {
      throw new ServiceException("Could not find a WEPP model in the archive for that suid and mongodb location.");
    }

    model = tArchive.get(0);

    if (((WEPPModelArchive) model).badModelRun()) {
      throw new ServiceException("Found a WEPP model in the archive, however the model was invalid: " + ((WEPPModelArchive) model).badModelMessage());
    }
  }

  @Override
  public void doProcess() throws ServiceException {
    ((WEPPModelArchive) model).calcWEPPMetaData();

    try {
      mongoArchive.shutdown();
    } catch (Exception ex) {
      LOG.log(Level.SEVERE, "Could not shutdown mongo archive at: " + dbLocation, ex);
    }
  }

  @Override
  public void postProcess() throws Exception {
    PayloadResults results = results();
    WEPPMetaData weppMetaData = ((WEPPModelArchive) model).getWEPPMetaData();
    if (null != weppMetaData) {
      results.put("meta_model_data", weppMetaData.toJSON());
    } else {
      throw new ServiceException("No meta data was returned from the WEPPMetaData() function.");
    }

  }
}