use of org.vcell.rest.VCellApiApplication in project vcell by virtualcell.
the class PublicationServerResource method get_json.
@Override
public PublicationRepresentation get_json() {
VCellApiApplication application = ((VCellApiApplication) getApplication());
User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.prohibitInvalidCredentials);
Object pubIdObj = getRequestAttributes().get(VCellApiApplication.PUBLICATIONID);
PublicationRepresentation publicationRep = getPublicationRepresentation(((VCellApiApplication) getApplication()).getRestDatabaseService(), vcellUser, new KeyValue(pubIdObj.toString()));
if (publicationRep != null) {
return publicationRep;
}
throw new RuntimeException("publication not found");
}
use of org.vcell.rest.VCellApiApplication in project vcell by virtualcell.
the class PublicationServerResource method get_html.
@Override
public Representation get_html() {
VCellApiApplication application = ((VCellApiApplication) getApplication());
User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
PublicationRepresentation publication = null;
Map<String, Object> dataModel = new HashMap<String, Object>();
Object pubIdObj = getRequestAttributes().get(VCellApiApplication.PUBLICATIONID);
if (pubIdObj != null) {
publication = getPublicationRepresentation(((VCellApiApplication) getApplication()).getRestDatabaseService(), vcellUser, new KeyValue(pubIdObj.toString()));
} else {
throw new RuntimeException("publication not found");
}
// +"?"+VCellApiApplication.REDIRECTURL_FORMNAME+"="+getRequest().getResourceRef().toUrl());
dataModel.put("loginurl", "/" + VCellApiApplication.LOGINFORM);
dataModel.put("logouturl", "/" + VCellApiApplication.LOGOUT + "?" + VCellApiApplication.REDIRECTURL_FORMNAME + "=" + Reference.encode(getRequest().getResourceRef().toUrl().toString()));
if (vcellUser != null) {
dataModel.put("userid", vcellUser.getName());
}
dataModel.put("pubId", getQueryValue(VCellApiApplication.PUBLICATIONID));
dataModel.put("publication", publication);
Gson gson = new Gson();
dataModel.put("jsonResponse", gson.toJson(publication));
Configuration templateConfiguration = application.getTemplateConfiguration();
Representation formFtl = new ClientResource(LocalReference.createClapReference("/publication.ftl")).get();
TemplateRepresentation templateRepresentation = new TemplateRepresentation(formFtl, templateConfiguration, dataModel, MediaType.TEXT_HTML);
return templateRepresentation;
}
use of org.vcell.rest.VCellApiApplication in project vcell by virtualcell.
the class PublicationServerResource method handleForm.
@Post
public Representation handleForm(Representation entity) {
try {
String myHost = getHostRef().getHostDomain();
String allowedPublicationHost = PropertyLoader.getProperty(PropertyLoader.vcellPublicationHost, "");
if (!allowedPublicationHost.equals(myHost)) {
throw new PermissionException("Host '" + myHost + "' not allowed to edit publications");
}
Form form = new Form(entity);
VCellApiApplication application = ((VCellApiApplication) getApplication());
User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
if (vcellUser == null) {
throw new PermissionException("must be authenticated to edit publication info");
}
// The form contains input with names "user" and "password"
String pubop = form.getFirstValue("pubop");
// String password = form.getFirstValue("password");
Map<String, Object> dataModel = new HashMap<String, Object>();
if (pubop == null) {
return new StringRepresentation(("value of publication 'operation' cannot be null"));
} else if (pubop.equals("editNew")) {
PublicationRepresentation value = new PublicationRepresentation();
value.pubKey = AUTOMATICALLY_GENERATED;
dataModel.put("publicationRepr", value);
} else if (pubop.equals("editWithKey")) {
Object pubidObj = getRequestAttributes().get(VCellApiApplication.PUBLICATIONID);
PublicationRepresentation publication = getPublicationRepresentation(((VCellApiApplication) getApplication()).getRestDatabaseService(), vcellUser, new KeyValue(pubidObj.toString()));
dataModel.put("publicationRepr", publication);
} else if (pubop.equals("applyEdit")) {
SimpleDateFormat sdf = new java.text.SimpleDateFormat("MM/dd/yyyy", java.util.Locale.US);
// PublicationRepresentation publication = getPublicationRepresentation(((VCellApiApplication)getApplication()).getRestDatabaseService(),vcellUser,new KeyValue(form.getFirstValue("pubid")));
String[] authors = StringUtils.split(form.getFirstValue("authors"), ";");
for (int i = 0; i < authors.length; i++) {
authors[i] = authors[i].trim();
}
String thePubID = form.getFirstValue("pubId");
BioModelReferenceRep[] bioModelReferenceReps = (BioModelReferenceRep[]) convertToReferenceRep(form.getFirstValue("biomodelReferences"), BioModelReferenceRep.class);
MathModelReferenceRep[] mathModelReferenceReps = (MathModelReferenceRep[]) convertToReferenceRep(form.getFirstValue("mathmodelReferences"), MathModelReferenceRep.class);
PublicationRep publicationRep = new PublicationRep((thePubID == null || thePubID.equals(AUTOMATICALLY_GENERATED) ? null : new KeyValue(Integer.valueOf(thePubID).toString())), form.getFirstValue("title"), authors, (form.getFirstValue("year") == null || form.getFirstValue("year").trim().length() == 0 ? null : Integer.valueOf(form.getFirstValue("year"))), form.getFirstValue("citation"), form.getFirstValue("pubmedid"), form.getFirstValue("doi"), form.getFirstValue("endnoteid"), form.getFirstValue("url"), bioModelReferenceReps, mathModelReferenceReps, form.getFirstValue("wittid"), (form.getFirstValue("pubdate") == null || form.getFirstValue("pubdate").trim().length() == 0 ? null : sdf.parse(form.getFirstValue("pubdate"))));
KeyValue savedOrEditedPubID = ((VCellApiApplication) getApplication()).getRestDatabaseService().savePublicationRep(publicationRep, vcellUser);
// String address = getRequest().getClientInfo().getAddress();
// int port = getRequest().getClientInfo().getPort();
StringRepresentation s = getPubInfoHtml(myHost, savedOrEditedPubID);
return s;
} else if (pubop.equals("makepublic")) {
String[] bmKeys = form.getValuesArray("bmpublic");
KeyValue[] publishTheseBiomodels = new KeyValue[(bmKeys == null ? 0 : bmKeys.length)];
for (int i = 0; i < publishTheseBiomodels.length; i++) {
publishTheseBiomodels[i] = new KeyValue(bmKeys[i]);
}
String[] mmKeys = form.getValuesArray("mmpublic");
KeyValue[] publishTheseMathmodels = new KeyValue[(mmKeys == null ? 0 : mmKeys.length)];
for (int i = 0; i < publishTheseMathmodels.length; i++) {
publishTheseMathmodels[i] = new KeyValue(mmKeys[i]);
}
if (publishTheseBiomodels.length > 0 || publishTheseMathmodels.length > 0) {
((VCellApiApplication) getApplication()).getRestDatabaseService().publishDirectly(publishTheseBiomodels, publishTheseMathmodels, vcellUser);
}
Object pubidObj = getRequestAttributes().get(VCellApiApplication.PUBLICATIONID);
StringRepresentation s = getPubInfoHtml(myHost, new KeyValue(pubidObj.toString()));
return s;
} else {
return new StringRepresentation(("value of pubop=" + pubop + " not expected").toCharArray());
}
Configuration templateConfiguration = application.getTemplateConfiguration();
Representation myRepresentation = new ClientResource(LocalReference.createClapReference("/newpublication.ftl")).get();
TemplateRepresentation templateRepresentation = new TemplateRepresentation(myRepresentation, templateConfiguration, dataModel, MediaType.TEXT_HTML);
return templateRepresentation;
} catch (Exception e) {
return new StringRepresentation(e.getClass().getName() + " " + e.getMessage());
}
}
use of org.vcell.rest.VCellApiApplication in project vcell by virtualcell.
the class BiomodelSBMLServerResource method getBiomodelSBML.
private String getBiomodelSBML(User vcellUser) {
RestDatabaseService restDatabaseService = ((VCellApiApplication) getApplication()).getRestDatabaseService();
try {
// Make temporary resource compatible with restDatabaseService so we can re-use
BiomodelVCMLServerResource bmsr = new BiomodelVCMLServerResource() {
@Override
public Map<String, Object> getRequestAttributes() {
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put(VCellApiApplication.BIOMODELID, BiomodelSBMLServerResource.this.biomodelid);
return hashMap;
}
@Override
public Request getRequest() {
// TODO Auto-generated method stub
return BiomodelSBMLServerResource.this.getRequest();
}
};
String biomodelVCML = restDatabaseService.query(bmsr, vcellUser);
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(biomodelVCML));
// public SBMLExporter(BioModel argBioModel, int argSbmlLevel, int argSbmlVersion, boolean isSpatial) {
SimulationContext simulationContext = null;
if (appName != null) {
simulationContext = bioModel.getSimulationContext(appName);
} else {
simulationContext = bioModel.getSimulationContext(0);
}
SBMLExporter sbmlExporter = new SBMLExporter(simulationContext, 3, 1, simulationContext.getGeometryContext().getGeometry().getDimension() > 0);
return sbmlExporter.getSBMLString();
} catch (PermissionException e) {
e.printStackTrace();
throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "permission denied to requested resource");
} catch (Exception e) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
}
}
use of org.vcell.rest.VCellApiApplication 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