Search in sources :

Example 31 with VCellApiApplication

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");
}
Also used : PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) User(org.vcell.util.document.User) KeyValue(org.vcell.util.document.KeyValue) VCellApiApplication(org.vcell.rest.VCellApiApplication)

Example 32 with VCellApiApplication

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;
}
Also used : TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) User(org.vcell.util.document.User) KeyValue(org.vcell.util.document.KeyValue) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) MathmodelReferenceRepresentation(org.vcell.rest.common.MathmodelReferenceRepresentation) BiomodelReferenceRepresentation(org.vcell.rest.common.BiomodelReferenceRepresentation) StringRepresentation(org.restlet.representation.StringRepresentation) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) Representation(org.restlet.representation.Representation) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource)

Example 33 with VCellApiApplication

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());
    }
}
Also used : PermissionException(org.vcell.util.PermissionException) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) User(org.vcell.util.document.User) KeyValue(org.vcell.util.document.KeyValue) Configuration(freemarker.template.Configuration) Form(org.restlet.data.Form) HashMap(java.util.HashMap) SimpleDateFormat(java.text.SimpleDateFormat) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) BioModelReferenceRep(cbit.vcell.modeldb.BioModelReferenceRep) PublicationRep(cbit.vcell.modeldb.PublicationRep) StringRepresentation(org.restlet.representation.StringRepresentation) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource) MathModelReferenceRep(cbit.vcell.modeldb.MathModelReferenceRep) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) MathmodelReferenceRepresentation(org.vcell.rest.common.MathmodelReferenceRepresentation) BiomodelReferenceRepresentation(org.vcell.rest.common.BiomodelReferenceRepresentation) StringRepresentation(org.restlet.representation.StringRepresentation) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) Representation(org.restlet.representation.Representation) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) ResourceException(org.restlet.resource.ResourceException) SimpleDateFormat(java.text.SimpleDateFormat) Post(org.restlet.resource.Post)

Example 34 with VCellApiApplication

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());
    }
}
Also used : PermissionException(org.vcell.util.PermissionException) HashMap(java.util.HashMap) SBMLExporter(org.vcell.sbml.vcell.SBMLExporter) SimulationContext(cbit.vcell.mapping.SimulationContext) PermissionException(org.vcell.util.PermissionException) ResourceException(org.restlet.resource.ResourceException) BioModel(cbit.vcell.biomodel.BioModel) VCellApiApplication(org.vcell.rest.VCellApiApplication) ResourceException(org.restlet.resource.ResourceException) XMLSource(cbit.vcell.xml.XMLSource)

Example 35 with VCellApiApplication

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());
    }
}
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

VCellApiApplication (org.vcell.rest.VCellApiApplication)53 User (org.vcell.util.document.User)34 ResourceException (org.restlet.resource.ResourceException)21 PermissionException (org.vcell.util.PermissionException)20 Gson (com.google.gson.Gson)14 Representation (org.restlet.representation.Representation)14 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)14 HashMap (java.util.HashMap)13 Configuration (freemarker.template.Configuration)12 TemplateRepresentation (org.restlet.ext.freemarker.TemplateRepresentation)11 ClientResource (org.restlet.resource.ClientResource)11 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)9 Form (org.restlet.data.Form)8 StringRepresentation (org.restlet.representation.StringRepresentation)8 BioModel (cbit.vcell.biomodel.BioModel)6 XMLSource (cbit.vcell.xml.XMLSource)6 SQLException (java.sql.SQLException)6 ArrayList (java.util.ArrayList)6 BiomodelRepresentation (org.vcell.rest.common.BiomodelRepresentation)6 PublicationRepresentation (org.vcell.rest.common.PublicationRepresentation)6