Search in sources :

Example 1 with BioModelRep

use of cbit.vcell.modeldb.BioModelRep in project vcell by virtualcell.

the class RestDatabaseService method query.

public BioModelRep[] query(BiomodelsServerResource resource, User vcellUser) throws SQLException, DataAccessException {
    if (vcellUser == null) {
        vcellUser = VCellApiApplication.DUMMY_USER;
    }
    String bioModelName = resource.getQueryValue(BiomodelsServerResource.PARAM_BM_NAME);
    Long bioModelID = resource.getLongQueryValue(BiomodelsServerResource.PARAM_BM_ID);
    Long savedLow = resource.getLongQueryValue(BiomodelsServerResource.PARAM_SAVED_LOW);
    Long savedHigh = resource.getLongQueryValue(BiomodelsServerResource.PARAM_SAVED_HIGH);
    Long startRowParam = resource.getLongQueryValue(BiomodelsServerResource.PARAM_START_ROW);
    Long maxRowsParam = resource.getLongQueryValue(BiomodelsServerResource.PARAM_MAX_ROWS);
    // it is ok if the category is null;
    String categoryParam = resource.getQueryValue(BiomodelsServerResource.PARAM_CATEGORY);
    // it is ok if the ownerName is null;
    String ownerParam = resource.getQueryValue(BiomodelsServerResource.PARAM_BM_OWNER);
    // it is ok if the orderBy is null;
    String orderByParam = resource.getQueryValue(BiomodelsServerResource.PARAM_ORDERBY);
    // default
    int startRow = 1;
    if (startRowParam != null) {
        startRow = startRowParam.intValue();
    }
    // default
    int maxRows = 10;
    if (maxRowsParam != null) {
        maxRows = maxRowsParam.intValue();
    }
    ArrayList<String> conditions = new ArrayList<String>();
    java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss", java.util.Locale.US);
    if (savedLow != null) {
        conditions.add("(" + BioModelTable.table.versionDate.getQualifiedColName() + " >= to_date('" + df.format(new Date(savedLow)) + "', 'mm/dd/yyyy HH24:MI:SS'))");
    }
    if (savedHigh != null) {
        conditions.add("(" + BioModelTable.table.versionDate.getQualifiedColName() + " <= to_date('" + df.format(new Date(savedHigh)) + "', 'mm/dd/yyyy HH24:MI:SS'))");
    }
    if (bioModelName != null && bioModelName.trim().length() > 0) {
        String pattern = bioModelName.trim();
        pattern = pattern.replace("  ", " ");
        pattern = pattern.replace("(", " ");
        pattern = pattern.replace(")", " ");
        pattern = pattern.replace("'", " ");
        pattern = pattern.replace("\"", " ");
        pattern = pattern.replace("*", "%");
        pattern = "%" + pattern + "%";
        pattern = pattern.replace("%%", "%");
        conditions.add("(" + "lower(" + BioModelTable.table.name.getQualifiedColName() + ")" + " like " + "lower('" + pattern + "')" + ")");
    }
    if (bioModelID != null) {
        conditions.add("(" + BioModelTable.table.id.getQualifiedColName() + " = " + bioModelID + ")");
    }
    // 
    if (categoryParam == null && ownerParam != null) {
        if (ownerParam.equals(VCellApiApplication.PSEUDOOWNER_PUBLIC)) {
            categoryParam = VCellApiApplication.CATEGORY_PUBLIC;
            ownerParam = null;
        } else if (ownerParam.equals(VCellApiApplication.PSEUDOOWNER_EDUCATION)) {
            categoryParam = VCellApiApplication.CATEGORY_EDUCATION;
            ownerParam = null;
        } else if (ownerParam.equals(VCellApiApplication.PSEUDOOWNER_SHARED)) {
            categoryParam = VCellApiApplication.CATEGORY_SHARED;
            ownerParam = null;
        } else if (ownerParam.equals(VCellApiApplication.PSEUDOOWNER_TUTORIAL)) {
            categoryParam = VCellApiApplication.CATEGORY_TUTORIAL;
            ownerParam = null;
        } else {
            categoryParam = VCellApiApplication.CATEGORY_ALL;
        }
    } else if (categoryParam == null && ownerParam == null) {
        categoryParam = VCellApiApplication.CATEGORY_ALL;
    }
    if (categoryParam.equals(VCellApiApplication.CATEGORY_MINE)) {
        // 
        // return all models owned by me (none if not authenticated).
        // 
        conditions.add("(" + UserTable.table.userid.getQualifiedColName() + " = '" + vcellUser.getName() + "')");
    } else if (categoryParam.equals(VCellApiApplication.CATEGORY_PUBLIC)) {
        // 
        // public models that aren't "Tutorial" or "Education"
        // 
        // we will display all public models (even if owned by this user)
        // 
        conditions.add("(" + BioModelTable.table.privacy.getQualifiedColName() + " = " + GroupAccess.GROUPACCESS_ALL + ")");
        if (ownerParam != null && ownerParam.trim().length() > 0) {
            conditions.add("(" + UserTable.table.userid.getQualifiedColName() + " = '" + ownerParam + "')");
        }
        conditions.add("(" + UserTable.table.userid.getQualifiedColName() + " != '" + VCellApiApplication.USERNAME_TUTORIAL + "')");
        conditions.add("(" + UserTable.table.userid.getQualifiedColName() + " != '" + VCellApiApplication.USERNAME_EDUCATION + "')");
    } else if (categoryParam.equals(VCellApiApplication.CATEGORY_EDUCATION)) {
        // 
        // public models from "Education"
        // 
        conditions.add("(" + UserTable.table.userid.getQualifiedColName() + " = '" + VCellApiApplication.USERNAME_EDUCATION + "')");
        conditions.add("(" + BioModelTable.table.privacy.getQualifiedColName() + " = " + GroupAccess.GROUPACCESS_ALL + ")");
    } else if (categoryParam.equals(VCellApiApplication.CATEGORY_TUTORIAL)) {
        // 
        // public models from "tutorial"
        // 
        conditions.add("(" + UserTable.table.userid.getQualifiedColName() + " = '" + VCellApiApplication.USERNAME_TUTORIAL + "')");
        conditions.add("(" + BioModelTable.table.privacy.getQualifiedColName() + " = " + GroupAccess.GROUPACCESS_ALL + ")");
    } else if (categoryParam.equals(VCellApiApplication.CATEGORY_SHARED)) {
        // 
        if (!vcellUser.compareEqual(VCellApiApplication.DUMMY_USER)) {
            conditions.add("(" + UserTable.table.userid.getQualifiedColName() + " != '" + vcellUser.getName() + "')");
        }
        if (ownerParam != null && ownerParam.trim().length() > 0) {
            conditions.add("(" + UserTable.table.userid.getQualifiedColName() + " = '" + ownerParam + "')");
        }
        conditions.add("(" + BioModelTable.table.privacy.getQualifiedColName() + " != " + GroupAccess.GROUPACCESS_ALL + ")");
        conditions.add("(" + BioModelTable.table.privacy.getQualifiedColName() + " != " + GroupAccess.GROUPACCESS_NONE + ")");
    } else if (categoryParam.equals(VCellApiApplication.CATEGORY_ALL)) {
        // 
        if (ownerParam != null && ownerParam.length() > 0) {
            conditions.add("(" + UserTable.table.userid.getQualifiedColName() + " = '" + ownerParam + "')");
        }
    }
    StringBuffer conditionsBuffer = new StringBuffer();
    for (String condition : conditions) {
        if (conditionsBuffer.length() > 0) {
            conditionsBuffer.append(" AND ");
        }
        conditionsBuffer.append(condition);
    }
    // default
    OrderBy orderBy = OrderBy.date_desc;
    if (orderByParam != null) {
        if (orderByParam.equals(BiomodelsServerResource.PARAM_ORDERBY_DATE_ASC)) {
            orderBy = OrderBy.date_asc;
        } else if (orderByParam.equals(BiomodelsServerResource.PARAM_ORDERBY_DATE_DESC)) {
            orderBy = OrderBy.date_desc;
        } else if (orderByParam.equals(BiomodelsServerResource.PARAM_ORDERBY_NAME_ASC)) {
            orderBy = OrderBy.name_asc;
        } else if (orderByParam.equals(BiomodelsServerResource.PARAM_ORDERBY_NAME_DESC)) {
            orderBy = OrderBy.name_desc;
        }
    }
    BioModelRep[] bioModelReps = databaseServerImpl.getBioModelReps(vcellUser, conditionsBuffer.toString(), orderBy, startRow, maxRows);
    for (BioModelRep bioModelRep : bioModelReps) {
        KeyValue[] simContextKeys = bioModelRep.getSimContextKeyList();
        for (KeyValue scKey : simContextKeys) {
            SimContextRep scRep = getSimContextRep(scKey);
            if (scRep != null) {
                bioModelRep.addSimContextRep(scRep);
            }
        }
        KeyValue[] simulationKeys = bioModelRep.getSimKeyList();
        for (KeyValue simKey : simulationKeys) {
            SimulationRep simulationRep = getSimulationRep(simKey);
            if (simulationRep != null) {
                bioModelRep.addSimulationRep(simulationRep);
            }
        }
    }
    return bioModelReps;
}
Also used : OrderBy(cbit.vcell.modeldb.DatabaseServerImpl.OrderBy) KeyValue(org.vcell.util.document.KeyValue) ArrayList(java.util.ArrayList) BioModelRep(cbit.vcell.modeldb.BioModelRep) BigString(org.vcell.util.BigString) Date(java.util.Date) SimulationContext(cbit.vcell.mapping.SimulationContext) SimContextRep(cbit.vcell.modeldb.SimContextRep) SimulationRep(cbit.vcell.modeldb.SimulationRep)

Example 2 with BioModelRep

use of cbit.vcell.modeldb.BioModelRep in project vcell by virtualcell.

the class RestDatabaseService method query.

public SimulationRepresentation query(BiomodelSimulationServerResource resource, User vcellUser) throws SQLException, DataAccessException, ExpressionException, XmlParseException, MappingException, MathException, MatrixException, ModelException {
    if (vcellUser == null) {
        vcellUser = VCellApiApplication.DUMMY_USER;
    }
    ArrayList<String> conditions = new ArrayList<String>();
    String bioModelID = (String) resource.getRequestAttributes().get(VCellApiApplication.BIOMODELID);
    if (bioModelID != null) {
        conditions.add("(" + BioModelTable.table.id.getQualifiedColName() + " = " + bioModelID + ")");
    } else {
        throw new RuntimeException(VCellApiApplication.BIOMODELID + " not specified");
    }
    StringBuffer conditionsBuffer = new StringBuffer();
    for (String condition : conditions) {
        if (conditionsBuffer.length() > 0) {
            conditionsBuffer.append(" AND ");
        }
        conditionsBuffer.append(condition);
    }
    int startRow = 1;
    int maxRows = 1;
    BioModelRep[] bioModelReps = databaseServerImpl.getBioModelReps(vcellUser, conditionsBuffer.toString(), null, startRow, maxRows);
    for (BioModelRep bioModelRep : bioModelReps) {
        KeyValue[] simContextKeys = bioModelRep.getSimContextKeyList();
        for (KeyValue scKey : simContextKeys) {
            SimContextRep scRep = getSimContextRep(scKey);
            if (scRep != null) {
                bioModelRep.addSimContextRep(scRep);
            }
        }
        KeyValue[] simulationKeys = bioModelRep.getSimKeyList();
        for (KeyValue simKey : simulationKeys) {
            SimulationRep simulationRep = getSimulationRep(simKey);
            if (simulationRep != null) {
                bioModelRep.addSimulationRep(simulationRep);
            }
        }
    }
    if (bioModelReps == null || bioModelReps.length != 1) {
        // 
        // try to determine if the current credentials are insufficient, try to fetch BioModel again with administrator privilege.
        // 
        User adminUser = new User(PropertyLoader.ADMINISTRATOR_ACCOUNT, new KeyValue(PropertyLoader.ADMINISTRATOR_ID));
        BioModelRep[] allBioModelReps = databaseServerImpl.getBioModelReps(adminUser, conditionsBuffer.toString(), null, startRow, 1);
        if (allBioModelReps != null && allBioModelReps.length >= 0) {
            throw new PermissionException("insufficient privilege to retrive BioModel " + bioModelID);
        } else {
            throw new RuntimeException("failed to get biomodel");
        }
    }
    String simulationId = (String) resource.getRequestAttributes().get(VCellApiApplication.SIMULATIONID);
    if (simulationId == null) {
        throw new RuntimeException(VCellApiApplication.SIMULATIONID + " not specified");
    }
    SimulationRep simRep = getSimulationRep(new KeyValue(simulationId));
    BigString bioModelXML = databaseServerImpl.getBioModelXML(vcellUser, bioModelReps[0].getBmKey());
    BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
    return new SimulationRepresentation(simRep, bioModel);
}
Also used : PermissionException(org.vcell.util.PermissionException) KeyValue(org.vcell.util.document.KeyValue) User(org.vcell.util.document.User) ArrayList(java.util.ArrayList) BioModelRep(cbit.vcell.modeldb.BioModelRep) BigString(org.vcell.util.BigString) BigString(org.vcell.util.BigString) SimulationRepresentation(org.vcell.rest.common.SimulationRepresentation) BioModel(cbit.vcell.biomodel.BioModel) XMLSource(cbit.vcell.xml.XMLSource) SimContextRep(cbit.vcell.modeldb.SimContextRep) SimulationRep(cbit.vcell.modeldb.SimulationRep)

Example 3 with BioModelRep

use of cbit.vcell.modeldb.BioModelRep in project vcell by virtualcell.

the class BiomodelServerResource method getBiomodelRepresentation.

private BiomodelRepresentation getBiomodelRepresentation(User vcellUser) {
    // if (!application.authenticate(getRequest(), getResponse())){
    // // not authenticated
    // return new SimulationTaskRepresentation[0];
    // }else{
    RestDatabaseService restDatabaseService = ((VCellApiApplication) getApplication()).getRestDatabaseService();
    try {
        BioModelRep bioModelRep = restDatabaseService.query(this, vcellUser);
        BiomodelRepresentation biomodelRep = new BiomodelRepresentation(bioModelRep);
        return biomodelRep;
    } catch (PermissionException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "permission denied to requested resource");
    } catch (ObjectNotFoundException e) {
        e.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "biomodel not found");
    } catch (Exception e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e.getMessage());
    }
// }
}
Also used : BiomodelRepresentation(org.vcell.rest.common.BiomodelRepresentation) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) VCellApiApplication(org.vcell.rest.VCellApiApplication) BioModelRep(cbit.vcell.modeldb.BioModelRep) ResourceException(org.restlet.resource.ResourceException) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) ResourceException(org.restlet.resource.ResourceException)

Example 4 with BioModelRep

use of cbit.vcell.modeldb.BioModelRep in project vcell by virtualcell.

the class BiomodelsServerResource method getBiomodelRepresentations.

private BiomodelRepresentation[] getBiomodelRepresentations(User vcellUser) {
    // if (!application.authenticate(getRequest(), getResponse())){
    // // not authenticated
    // return new SimulationTaskRepresentation[0];
    // }else{
    ArrayList<BiomodelRepresentation> biomodelReps = new ArrayList<BiomodelRepresentation>();
    RestDatabaseService restDatabaseService = ((VCellApiApplication) getApplication()).getRestDatabaseService();
    try {
        BioModelRep[] bioModelReps = restDatabaseService.query(this, vcellUser);
        for (BioModelRep bioModelRep : bioModelReps) {
            BiomodelRepresentation biomodelRep = new BiomodelRepresentation(bioModelRep);
            biomodelReps.add(biomodelRep);
        }
    } catch (PermissionException ee) {
        ee.printStackTrace();
        throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "not authorized");
    } catch (DataAccessException | SQLException | ExpressionException e) {
        e.printStackTrace();
        throw new RuntimeException("failed to retrieve biomodels from VCell Database : " + e.getMessage());
    }
    return biomodelReps.toArray(new BiomodelRepresentation[0]);
// }
}
Also used : BiomodelRepresentation(org.vcell.rest.common.BiomodelRepresentation) PermissionException(org.vcell.util.PermissionException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) BioModelRep(cbit.vcell.modeldb.BioModelRep) ExpressionException(cbit.vcell.parser.ExpressionException) VCellApiApplication(org.vcell.rest.VCellApiApplication) ResourceException(org.restlet.resource.ResourceException) DataAccessException(org.vcell.util.DataAccessException)

Example 5 with BioModelRep

use of cbit.vcell.modeldb.BioModelRep in project vcell by virtualcell.

the class RestDatabaseService method getBioModelRep.

public BioModelRep getBioModelRep(KeyValue bmKey, User vcellUser) throws SQLException, ObjectNotFoundException, DataAccessException {
    if (vcellUser == null) {
        vcellUser = VCellApiApplication.DUMMY_USER;
    }
    ArrayList<String> conditions = new ArrayList<String>();
    if (bmKey != null) {
        conditions.add("(" + BioModelTable.table.id.getQualifiedColName() + " = " + bmKey.toString() + ")");
    } else {
        throw new RuntimeException("bioModelID not specified");
    }
    StringBuffer conditionsBuffer = new StringBuffer();
    for (String condition : conditions) {
        if (conditionsBuffer.length() > 0) {
            conditionsBuffer.append(" AND ");
        }
        conditionsBuffer.append(condition);
    }
    int startRow = 1;
    int maxRows = 1;
    BioModelRep[] bioModelReps = databaseServerImpl.getBioModelReps(vcellUser, conditionsBuffer.toString(), null, startRow, maxRows);
    for (BioModelRep bioModelRep : bioModelReps) {
        KeyValue[] simContextKeys = bioModelRep.getSimContextKeyList();
        for (KeyValue scKey : simContextKeys) {
            SimContextRep scRep = getSimContextRep(scKey);
            if (scRep != null) {
                bioModelRep.addSimContextRep(scRep);
            }
        }
        KeyValue[] simulationKeys = bioModelRep.getSimKeyList();
        for (KeyValue simKey : simulationKeys) {
            SimulationRep simulationRep = getSimulationRep(simKey);
            if (simulationRep != null) {
                bioModelRep.addSimulationRep(simulationRep);
            }
        }
    }
    if (bioModelReps == null || bioModelReps.length != 1) {
        throw new ObjectNotFoundException("failed to get biomodel");
    }
    return bioModelReps[0];
}
Also used : KeyValue(org.vcell.util.document.KeyValue) ArrayList(java.util.ArrayList) BioModelRep(cbit.vcell.modeldb.BioModelRep) BigString(org.vcell.util.BigString) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) SimContextRep(cbit.vcell.modeldb.SimContextRep) SimulationRep(cbit.vcell.modeldb.SimulationRep)

Aggregations

BioModelRep (cbit.vcell.modeldb.BioModelRep)5 ArrayList (java.util.ArrayList)4 SimContextRep (cbit.vcell.modeldb.SimContextRep)3 SimulationRep (cbit.vcell.modeldb.SimulationRep)3 BigString (org.vcell.util.BigString)3 PermissionException (org.vcell.util.PermissionException)3 KeyValue (org.vcell.util.document.KeyValue)3 ResourceException (org.restlet.resource.ResourceException)2 VCellApiApplication (org.vcell.rest.VCellApiApplication)2 BiomodelRepresentation (org.vcell.rest.common.BiomodelRepresentation)2 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)2 BioModel (cbit.vcell.biomodel.BioModel)1 SimulationContext (cbit.vcell.mapping.SimulationContext)1 OrderBy (cbit.vcell.modeldb.DatabaseServerImpl.OrderBy)1 ExpressionException (cbit.vcell.parser.ExpressionException)1 XMLSource (cbit.vcell.xml.XMLSource)1 SQLException (java.sql.SQLException)1 Date (java.util.Date)1 SimulationRepresentation (org.vcell.rest.common.SimulationRepresentation)1 DataAccessException (org.vcell.util.DataAccessException)1