Search in sources :

Example 1 with SimDataValuesRepresentation

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

the class SimDataValuesServerResource method get_html.

@Override
public Representation get_html() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
    SimDataValuesRepresentation simDataValues = getSimDataValuesRepresentation(vcellUser);
    Map<String, Object> dataModel = new HashMap<String, Object>();
    // +"?"+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("userId", getAttribute(PARAM_USER));
    dataModel.put("simId", getQueryValue(PARAM_SIM_ID));
    Long startRowParam = getLongQueryValue(PARAM_START_ROW);
    if (startRowParam != null) {
        dataModel.put("startRow", startRowParam);
    } else {
        dataModel.put("startRow", 1);
    }
    Long maxRowsParam = getLongQueryValue(PARAM_MAX_ROWS);
    if (maxRowsParam != null) {
        dataModel.put("maxRows", maxRowsParam);
    } else {
        dataModel.put("maxRows", 10);
    }
    dataModel.put("simdatavalues", simDataValues);
    int numVars = simDataValues.getVariables().length;
    if (numVars > 1) {
        StringBuffer buffer = new StringBuffer();
        String firstRow = "\"";
        for (int i = 0; i < numVars; i++) {
            firstRow += simDataValues.getVariables()[i].getName();
            if (i < numVars - 1) {
                firstRow += ",";
            }
        }
        firstRow += "\\n\" + \n";
        buffer.append(firstRow);
        int numTimes = simDataValues.getVariables()[0].getValues().length;
        for (int t = 0; t < numTimes; t++) {
            String row = "\"";
            for (int v = 0; v < numVars; v++) {
                row += simDataValues.getVariables()[v].getValues()[t];
                if (v < numVars - 1) {
                    row += ",";
                }
            }
            row += "\\n\"";
            if (t < numTimes - 1) {
                row += " + \n";
            }
            buffer.append(row);
        }
        String csvdata = buffer.toString();
        // String csvdata = "\"t,x,y\\n\" + \n" +
        // "\"0,0,0\\n\" + \n" +
        // "\"1,1,1\\n\" + \n" +
        // "\"2,2,4\\n\" + \n" +
        // "\"3,3,9\\n\" + \n" +
        // "\"4,4,16\\n\" + \n" +
        // "\"5,5,25\\n\"";
        dataModel.put("csvdata", csvdata);
    }
    Gson gson = new Gson();
    dataModel.put("jsonResponse", gson.toJson(simDataValues));
    Configuration templateConfiguration = application.getTemplateConfiguration();
    Representation formFtl = new ClientResource(LocalReference.createClapReference("/simdatavalues.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) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) SimDataValuesRepresentation(org.vcell.rest.common.SimDataValuesRepresentation) Representation(org.restlet.representation.Representation) SimDataValuesRepresentation(org.vcell.rest.common.SimDataValuesRepresentation) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource)

Example 2 with SimDataValuesRepresentation

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

the class SimDataValuesServerResource method getSimDataValuesRepresentation.

private SimDataValuesRepresentation getSimDataValuesRepresentation(User vcellUser) {
    // if (!application.authenticate(getRequest(), getResponse())){
    // // not authenticated
    // return new SimulationTaskRepresentation[0];
    // }else{
    RestDatabaseService restDatabaseService = ((VCellApiApplication) getApplication()).getRestDatabaseService();
    try {
        DataSetTimeSeries dataSetTimeSeries = restDatabaseService.getDataSetTimeSeries(this, vcellUser);
        SimDataValuesRepresentation simDataRepresentation = new SimDataValuesRepresentation(dataSetTimeSeries);
        return simDataRepresentation;
    } catch (PermissionException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "not authorized to stop simulation");
    } catch (ObjectNotFoundException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "simulation not found");
    } catch (Exception e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
    }
// }
}
Also used : PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) VCellApiApplication(org.vcell.rest.VCellApiApplication) ResourceException(org.restlet.resource.ResourceException) DataSetTimeSeries(cbit.vcell.simdata.DataSetTimeSeries) SimDataValuesRepresentation(org.vcell.rest.common.SimDataValuesRepresentation) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) ResourceException(org.restlet.resource.ResourceException)

Aggregations

VCellApiApplication (org.vcell.rest.VCellApiApplication)2 SimDataValuesRepresentation (org.vcell.rest.common.SimDataValuesRepresentation)2 DataSetTimeSeries (cbit.vcell.simdata.DataSetTimeSeries)1 Gson (com.google.gson.Gson)1 Configuration (freemarker.template.Configuration)1 HashMap (java.util.HashMap)1 TemplateRepresentation (org.restlet.ext.freemarker.TemplateRepresentation)1 Representation (org.restlet.representation.Representation)1 ClientResource (org.restlet.resource.ClientResource)1 ResourceException (org.restlet.resource.ResourceException)1 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)1 PermissionException (org.vcell.util.PermissionException)1 User (org.vcell.util.document.User)1