V1_0.java [src/java/m/crp/wepsmetadata] Revision: default  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.wepsmetadata;

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.WEPSMetaData;
import data.models.WEPSModelArchive;
import csip.annotations.Description;
import csip.annotations.Name;

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

    protected static String dbLocation = Config.getString("weps.archive.mongodb.uri", "mongodb://csip.engr.colostate.edu:8095/8083_iet_csip-weps");
    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("weps_archive_mongodb")){
          dbLocation = params.getString("weps_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 WEPS model in the archive for that suid and mongodb location.");
        }

        model = tArchive.get(0);

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

    @Override
    public void doProcess() throws ServiceException {
        ((WEPSModelArchive)model).calcWEPSMetaData();

        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();
        WEPSMetaData wepsMetaData = ((WEPSModelArchive)model).getWEPSMetaData();
        if (null != wepsMetaData) {
          results.put("meta_model_data", wepsMetaData.toJSON());
        }else{
          throw new ServiceException("No meta data was returned from the WEPSMetaData() function.");
        }
        
    }
}