V1_0.java [src/java/m/crp/wepsmetadata] Revision: 57250261f8dd22e26a3b05bbd9b109c64c04d323  Date: Fri Dec 06 13:37:31 MST 2019
/*
 * $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 archives.FileArchive;
import archives.MongoArchive;
import 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 models.ModelArchive;
import models.WEPSModelArchive;
import oms3.annotations.Description;
import oms3.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.metadata.mongodb", "mongodb://csip.engr.colostate.edu:8095/8084_ftm_csip-weps");
    protected static String serviceName = Config.getString("weps.metadata.service", "http://csip.engr.colostate.edu:8084/csip-weps/m/weps/5.0");
    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 (dbLocation.contains("mongodb://")) {
            mongoArchive = new MongoArchive(dbLocation);
        } else {
            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 (!model.getService().equalsIgnoreCase(serviceName)) {
            throw new ServiceException("Could not find a WEPS model in the archive for that suid and mongodb location:  Found another type of model for that suid with the service of: " + model.getService());
        }

        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();
        results.put("meta_model_data", ((WEPSModelArchive)model).getWEPSMetaData().toJSON());
    }
}