use of cbit.vcell.modeldb.SimulationRep 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;
}
use of cbit.vcell.modeldb.SimulationRep in project vcell by virtualcell.
the class RestDatabaseService method saveSimulation.
public SimulationSaveResponse saveSimulation(BiomodelSimulationSaveServerResource resource, User vcellUser, List<OverrideRepresentation> overrideRepresentations) throws PermissionException, ObjectNotFoundException, DataAccessException, SQLException, XmlParseException, PropertyVetoException, MappingException, ExpressionException {
String simId = resource.getAttribute(VCellApiApplication.SIMULATIONID);
KeyValue simKey = new KeyValue(simId);
SimulationRep simRep = getSimulationRep(simKey);
if (simRep == null) {
throw new ObjectNotFoundException("Simulation with key " + simKey + " not found");
}
boolean myModel = simRep.getOwner().compareEqual(vcellUser);
// get the bioModel
String biomodelId = resource.getAttribute(VCellApiApplication.BIOMODELID);
KeyValue biomodelKey = new KeyValue(biomodelId);
BigString bioModelXML = this.databaseServerImpl.getBioModelXML(vcellUser, biomodelKey);
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
// copy the simulation as new
Simulation origSimulation = null;
for (Simulation sim : bioModel.getSimulations()) {
if (sim.getKey().equals(simKey)) {
origSimulation = sim;
}
}
if (origSimulation == null) {
throw new RuntimeException("cannot find original Simulation");
}
SimulationContext simContext = bioModel.getSimulationContext(origSimulation);
Simulation newUnsavedSimulation = simContext.copySimulation(origSimulation);
// make appropriate changes
// MATH OVERRIDES
MathOverrides mathOverrides = new MathOverrides(newUnsavedSimulation);
for (OverrideRepresentation overrideRep : overrideRepresentations) {
overrideRep.applyMathOverrides(mathOverrides);
}
newUnsavedSimulation.setMathOverrides(mathOverrides);
// save bioModel
String editedBioModelXML = XmlHelper.bioModelToXML(bioModel);
ServerDocumentManager serverDocumentManager = new ServerDocumentManager(this.databaseServerImpl);
String modelName = bioModel.getName();
if (!myModel) {
modelName = modelName + "_" + Math.abs(new Random().nextInt());
}
String newBioModelXML = serverDocumentManager.saveBioModel(new QueryHashtable(), vcellUser, editedBioModelXML, modelName, null);
BioModel savedBioModel = XmlHelper.XMLToBioModel(new XMLSource(newBioModelXML));
Simulation savedSimulation = null;
for (Simulation sim : savedBioModel.getSimulations()) {
if (sim.getName().equals(newUnsavedSimulation.getName())) {
savedSimulation = sim;
}
}
if (savedSimulation == null) {
throw new RuntimeException("cannot find new Simulation");
}
return new SimulationSaveResponse(savedBioModel, savedSimulation);
}
use of cbit.vcell.modeldb.SimulationRep in project vcell by virtualcell.
the class RestDatabaseService method getDataSetMetadata.
public DataSetMetadata getDataSetMetadata(SimDataServerResource resource, User vcellUser) throws ObjectNotFoundException, DataAccessException, SQLException {
if (vcellUser == null) {
vcellUser = VCellApiApplication.DUMMY_USER;
}
UserLoginInfo userLoginInfo = new UserLoginInfo(vcellUser.getName(), null);
// resource.getRequestAttributes().get(VCellApiApplication.SIMDATAID);
String simId = resource.getAttribute(VCellApiApplication.SIMDATAID);
KeyValue simKey = new KeyValue(simId);
SimulationRep simRep = getSimulationRep(simKey);
if (simRep == null) {
throw new ObjectNotFoundException("Simulation with key " + simKey + " not found");
}
User owner = simRep.getOwner();
int jobIndex = 0;
VCMessageSession rpcSession = vcMessagingService.createProducerSession();
try {
RpcDataServerProxy rpcDataServerProxy = new RpcDataServerProxy(userLoginInfo, rpcSession);
VCSimulationIdentifier vcSimID = new VCSimulationIdentifier(simKey, owner);
VCDataIdentifier vcdID = new VCSimulationDataIdentifier(vcSimID, jobIndex);
DataSetMetadata dataSetMetadata = rpcDataServerProxy.getDataSetMetadata(vcdID);
return dataSetMetadata;
} finally {
rpcSession.close();
}
}
use of cbit.vcell.modeldb.SimulationRep 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);
}
use of cbit.vcell.modeldb.SimulationRep in project vcell by virtualcell.
the class RestDatabaseService method query.
public SimulationStatusRepresentation[] query(SimulationStatusServerResource resource, User vcellUser) throws SQLException, DataAccessException {
if (vcellUser == null) {
vcellUser = VCellApiApplication.DUMMY_USER;
}
String userID = vcellUser.getName();
SimpleJobStatusQuerySpec simQuerySpec = new SimpleJobStatusQuerySpec();
simQuerySpec.userid = userID;
simQuerySpec.simId = resource.getLongQueryValue(SimulationStatusServerResource.PARAM_SIM_ID);
String hasData = resource.getQueryValue(SimulationStatusServerResource.PARAM_HAS_DATA);
if (hasData != null && hasData.equals("yes")) {
simQuerySpec.hasData = true;
} else if (hasData != null && hasData.equals("no")) {
simQuerySpec.hasData = false;
} else {
simQuerySpec.hasData = null;
}
simQuerySpec.waiting = resource.getBooleanQueryValue(SimulationStatusServerResource.PARAM_STATUS_ACTIVE, false);
simQuerySpec.queued = resource.getBooleanQueryValue(SimulationStatusServerResource.PARAM_STATUS_ACTIVE, false);
simQuerySpec.dispatched = resource.getBooleanQueryValue(SimulationStatusServerResource.PARAM_STATUS_ACTIVE, false);
simQuerySpec.running = resource.getBooleanQueryValue(SimulationStatusServerResource.PARAM_STATUS_ACTIVE, false);
simQuerySpec.completed = resource.getBooleanQueryValue(SimulationStatusServerResource.PARAM_STATUS_COMPLETED, false);
simQuerySpec.failed = resource.getBooleanQueryValue(SimulationStatusServerResource.PARAM_STATUS_FAILED, false);
simQuerySpec.stopped = resource.getBooleanQueryValue(SimulationStatusServerResource.PARAM_STATUS_STOPPED, false);
simQuerySpec.submitLowMS = resource.getLongQueryValue(SimulationStatusServerResource.PARAM_SUBMIT_LOW);
simQuerySpec.submitHighMS = resource.getLongQueryValue(SimulationStatusServerResource.PARAM_SUBMIT_HIGH);
simQuerySpec.startLowMS = resource.getLongQueryValue(SimulationStatusServerResource.PARAM_START_LOW);
simQuerySpec.startHighMS = resource.getLongQueryValue(SimulationStatusServerResource.PARAM_START_HIGH);
simQuerySpec.endLowMS = resource.getLongQueryValue(SimulationStatusServerResource.PARAM_END_LOW);
simQuerySpec.endHighMS = resource.getLongQueryValue(SimulationStatusServerResource.PARAM_END_HIGH);
Long startRowParam = resource.getLongQueryValue(SimulationStatusServerResource.PARAM_START_ROW);
// default
simQuerySpec.startRow = 1;
if (startRowParam != null) {
simQuerySpec.startRow = startRowParam.intValue();
}
Long maxRowsParam = resource.getLongQueryValue(SimulationTasksServerResource.PARAM_MAX_ROWS);
// default
simQuerySpec.maxRows = 10;
if (maxRowsParam != null) {
simQuerySpec.maxRows = maxRowsParam.intValue();
}
SimulationStatus[] simStatuses = null;
HashMap<KeyValue, SimulationDocumentLink> simDocLinks = new HashMap<KeyValue, SimulationDocumentLink>();
//
// ask server for simJobStatuses with above query spec.
// find set of simulation IDs from the result set of simJobStatus
// ask server for simulationStatuses from list of sim IDs.
//
VCMessageSession rpcSession = vcMessagingService.createProducerSession();
try {
UserLoginInfo userLoginInfo = new UserLoginInfo(vcellUser.getName(), null);
try {
userLoginInfo.setUser(vcellUser);
} catch (Exception e) {
e.printStackTrace();
throw new DataAccessException(e.getMessage());
}
RpcSimServerProxy rpcSimServerProxy = new RpcSimServerProxy(userLoginInfo, rpcSession);
SimpleJobStatus[] simpleJobStatusArray = rpcSimServerProxy.getSimpleJobStatus(vcellUser, simQuerySpec);
// gather unique simIDs and go back and ask server for SimulationStatuses
for (SimpleJobStatus simpleJobStatus : simpleJobStatusArray) {
KeyValue simulationKey = simpleJobStatus.jobStatus.getVCSimulationIdentifier().getSimulationKey();
SimulationDocumentLink simulationDocumentLink = simpleJobStatus.simulationDocumentLink;
simDocLinks.put(simulationKey, simulationDocumentLink);
}
KeyValue[] simKeys = simDocLinks.keySet().toArray(new KeyValue[0]);
if (simKeys.length > 0) {
simStatuses = rpcSimServerProxy.getSimulationStatus(vcellUser, simKeys);
}
} finally {
rpcSession.close();
}
ArrayList<SimulationStatusRepresentation> simStatusReps = new ArrayList<SimulationStatusRepresentation>();
for (int i = 0; simStatuses != null && i < simStatuses.length; i++) {
KeyValue simulationKey = simStatuses[i].getVCSimulationIdentifier().getSimulationKey();
SimulationRep simRep = getSimulationRep(simulationKey);
try {
SimulationRepresentation simRepresentation = new SimulationRepresentation(simRep, simDocLinks.get(simulationKey));
simStatusReps.add(new SimulationStatusRepresentation(simRepresentation, simStatuses[i]));
} catch (ExpressionException e) {
e.printStackTrace(System.out);
}
}
return simStatusReps.toArray(new SimulationStatusRepresentation[0]);
}
Aggregations