use of org.vcell.rest.server.RestDatabaseService.SimulationSaveResponse 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());
}
}
Aggregations