Search in sources :

Example 1 with OverrideRepresentation

use of org.vcell.rest.common.OverrideRepresentation in project vcell by virtualcell.

the class RestDatabaseService method saveSimulation.

public SimulationSaveResponse saveSimulation(BiomodelSimulationSaveServerResource resource, User vcellUser, List<OverrideRepresentation> overrideRepresentations) throws PermissionException, ObjectNotFoundException, DataAccessException, SQLException, XmlParseException, PropertyVetoException, MappingException, ExpressionException {
    String simId = resource.getAttribute(VCellApiApplication.SIMULATIONID);
    KeyValue simKey = new KeyValue(simId);
    SimulationRep simRep = getSimulationRep(simKey);
    if (simRep == null) {
        throw new ObjectNotFoundException("Simulation with key " + simKey + " not found");
    }
    boolean myModel = simRep.getOwner().compareEqual(vcellUser);
    // get the bioModel
    String biomodelId = resource.getAttribute(VCellApiApplication.BIOMODELID);
    KeyValue biomodelKey = new KeyValue(biomodelId);
    BigString bioModelXML = this.databaseServerImpl.getBioModelXML(vcellUser, biomodelKey);
    BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
    // copy the simulation as new
    Simulation origSimulation = null;
    for (Simulation sim : bioModel.getSimulations()) {
        if (sim.getKey().equals(simKey)) {
            origSimulation = sim;
        }
    }
    if (origSimulation == null) {
        throw new RuntimeException("cannot find original Simulation");
    }
    SimulationContext simContext = bioModel.getSimulationContext(origSimulation);
    Simulation newUnsavedSimulation = simContext.copySimulation(origSimulation);
    // make appropriate changes
    // MATH OVERRIDES
    MathOverrides mathOverrides = new MathOverrides(newUnsavedSimulation);
    for (OverrideRepresentation overrideRep : overrideRepresentations) {
        overrideRep.applyMathOverrides(mathOverrides);
    }
    newUnsavedSimulation.setMathOverrides(mathOverrides);
    // save bioModel
    String editedBioModelXML = XmlHelper.bioModelToXML(bioModel);
    ServerDocumentManager serverDocumentManager = new ServerDocumentManager(this.databaseServerImpl);
    String modelName = bioModel.getName();
    if (!myModel) {
        modelName = modelName + "_" + Math.abs(new Random().nextInt());
    }
    String newBioModelXML = serverDocumentManager.saveBioModel(new QueryHashtable(), vcellUser, editedBioModelXML, modelName, null);
    BioModel savedBioModel = XmlHelper.XMLToBioModel(new XMLSource(newBioModelXML));
    Simulation savedSimulation = null;
    for (Simulation sim : savedBioModel.getSimulations()) {
        if (sim.getName().equals(newUnsavedSimulation.getName())) {
            savedSimulation = sim;
        }
    }
    if (savedSimulation == null) {
        throw new RuntimeException("cannot find new Simulation");
    }
    return new SimulationSaveResponse(savedBioModel, savedSimulation);
}
Also used : QueryHashtable(cbit.sql.QueryHashtable) KeyValue(org.vcell.util.document.KeyValue) BigString(org.vcell.util.BigString) SimulationContext(cbit.vcell.mapping.SimulationContext) BigString(org.vcell.util.BigString) OverrideRepresentation(org.vcell.rest.common.OverrideRepresentation) ServerDocumentManager(cbit.vcell.modeldb.ServerDocumentManager) MathOverrides(cbit.vcell.solver.MathOverrides) Simulation(cbit.vcell.solver.Simulation) Random(java.util.Random) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) BioModel(cbit.vcell.biomodel.BioModel) XMLSource(cbit.vcell.xml.XMLSource) SimulationRep(cbit.vcell.modeldb.SimulationRep)

Example 2 with OverrideRepresentation

use of org.vcell.rest.common.OverrideRepresentation in project vcell by virtualcell.

the class BiomodelSimulationSaveServerResource method save.

@Override
public void save(JsonRepresentation jsonOverrides) throws JSONException {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.prohibitInvalidCredentials);
    ArrayList<OverrideRepresentation> overrideRepresentations = new ArrayList<OverrideRepresentation>();
    if (jsonOverrides != null && jsonOverrides.getMediaType().isCompatible(MediaType.APPLICATION_JSON)) {
        JSONObject obj = jsonOverrides.getJsonObject();
        JSONArray overrideArray = obj.getJSONArray("overrides");
        for (int i = 0; i < overrideArray.length(); i++) {
            JSONObject overrideObj = overrideArray.getJSONObject(i);
            String name = overrideObj.getString("name");
            String type = overrideObj.getString("type");
            int cardinality = overrideObj.getInt("cardinality");
            double[] values = new double[0];
            if (overrideObj.has("values")) {
                JSONArray valuesArray = overrideObj.getJSONArray("values");
                values = new double[valuesArray.length()];
                for (int j = 0; j < valuesArray.length(); j++) {
                    values[j] = valuesArray.getDouble(j);
                }
            }
            String expression = null;
            if (overrideObj.has("expression")) {
                expression = overrideObj.getString("expression");
            }
            OverrideRepresentation overrideRep = new OverrideRepresentation(name, type, cardinality, values, expression);
            overrideRepresentations.add(overrideRep);
        }
    }
    RestDatabaseService restDatabaseService = application.getRestDatabaseService();
    try {
        if (vcellUser == null) {
            throw new PermissionException("must be authenticated to copy simulation");
        }
        SimulationSaveResponse simulationSavedResponse = restDatabaseService.saveSimulation(this, vcellUser, overrideRepresentations);
        JSONObject responseJson = new JSONObject();
        String redirectURL = "/" + VCellApiApplication.BIOMODEL + "/" + simulationSavedResponse.newBioModel.getVersion().getVersionKey() + "/" + VCellApiApplication.SIMULATION + "/" + simulationSavedResponse.newSimulation.getKey().toString();
        responseJson.put("redirect", redirectURL);
        responseJson.put("status", "simulation saved");
        JsonRepresentation representation = new JsonRepresentation(responseJson);
        redirectSeeOther(redirectURL);
    // return representation;
    } catch (PermissionException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "not authorized to save simulation");
    } catch (ObjectNotFoundException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "simulation not found");
    } catch (Exception e) {
        e.printStackTrace(System.out);
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
    }
}
Also used : PermissionException(org.vcell.util.PermissionException) User(org.vcell.util.document.User) SimulationSaveResponse(org.vcell.rest.server.RestDatabaseService.SimulationSaveResponse) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) OverrideRepresentation(org.vcell.rest.common.OverrideRepresentation) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) JSONException(org.json.JSONException) ResourceException(org.restlet.resource.ResourceException) JSONObject(org.json.JSONObject) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) VCellApiApplication(org.vcell.rest.VCellApiApplication) ResourceException(org.restlet.resource.ResourceException) JsonRepresentation(org.restlet.ext.json.JsonRepresentation)

Aggregations

OverrideRepresentation (org.vcell.rest.common.OverrideRepresentation)2 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)2 QueryHashtable (cbit.sql.QueryHashtable)1 BioModel (cbit.vcell.biomodel.BioModel)1 SimulationContext (cbit.vcell.mapping.SimulationContext)1 ServerDocumentManager (cbit.vcell.modeldb.ServerDocumentManager)1 SimulationRep (cbit.vcell.modeldb.SimulationRep)1 MathOverrides (cbit.vcell.solver.MathOverrides)1 Simulation (cbit.vcell.solver.Simulation)1 XMLSource (cbit.vcell.xml.XMLSource)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)1 ResourceException (org.restlet.resource.ResourceException)1 VCellApiApplication (org.vcell.rest.VCellApiApplication)1 SimulationSaveResponse (org.vcell.rest.server.RestDatabaseService.SimulationSaveResponse)1 BigString (org.vcell.util.BigString)1