use of ambit2.db.search.structure.QueryStoredResults in project ambit-mirror by ideaconsult.
the class FilteredDatasetResource method createQuery.
@Override
protected Q createQuery(Context context, Request request, Response response) throws ResourceException {
Template filter = null;
Form form = getResourceRef(getRequest()).getQueryAsForm();
try {
includeMol = "true".equals(form.getFirstValue("mol"));
} catch (Exception x) {
includeMol = false;
}
Object dataset = form.getFirstValue(OpenTox.params.dataset_uri.toString());
String[] filteruris = OpenTox.params.filter.getValuesArray(form);
Object condition = form.getFirstValue(OpenTox.params.condition.toString());
SetCondition.conditions sc = condition == null ? SetCondition.conditions.in : "no".equals(condition) ? SetCondition.conditions.not_in : SetCondition.conditions.in;
// if (dataset==null) throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,"Empty dataset");
if (filteruris == null)
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "No features to filter");
setTemplate(createTemplate(context, request, response));
try {
filter = createTemplate(context, request, response, filteruris);
if ((filter == null) || (filter.size() == 0))
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "No features to filter");
if (dataset == null) {
QueryStructureByProperty q = new QueryStructureByProperty();
q.setFieldname(filter);
q.setCondition(new SetCondition(sc));
setPaging(form, q);
return (Q) q;
}
Object q = CallableQueryProcessor.getQueryObject(new Reference(dataset.toString()), getRequest().getRootRef(), getApplication().getContext(), getCookies(), getAgent(), getRequest().getResourceRef().toString());
if (q == null) {
throw new ResourceException(Status.SERVER_ERROR_NOT_IMPLEMENTED, "Processing foreign datasets not implemented!");
} else if (q instanceof QueryDatasetByID) {
QueryStructureByPropertyInDataset qz = new QueryStructureByPropertyInDataset();
qz.setValue((QueryDatasetByID) q);
qz.setFieldname(filter);
qz.setCondition(new SetCondition(sc));
setPaging(form, qz);
return (Q) qz;
} else if (q instanceof QueryStoredResults) {
QueryStructuresByPropertyInResults qz = new QueryStructuresByPropertyInResults();
qz.setValue((QueryStoredResults) q);
qz.setFieldname(filter);
qz.setCondition(new SetCondition(sc));
setPaging(form, qz);
return (Q) qz;
} else if (q instanceof QueryStructureByID) {
QueryStructureByPropertyInCompounds qz = new QueryStructureByPropertyInCompounds();
qz.setValue((QueryStructureByID) q);
qz.setFieldname(filter);
qz.setCondition(new SetCondition(sc));
setPaging(form, qz);
return (Q) qz;
} else
throw new ResourceException(Status.SERVER_ERROR_NOT_IMPLEMENTED, dataset.toString() + " " + q.getClass().getName() + " not implemented!");
} catch (ResourceException x) {
throw x;
} catch (Exception x) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x.getMessage(), x);
}
}
use of ambit2.db.search.structure.QueryStoredResults in project ambit-mirror by ideaconsult.
the class ProcessorCreateQuery method process.
public IStoredQuery process(IQueryObject<IStructureRecord> target) throws AmbitException {
if (target == null)
throw new AmbitException("Undefined query!");
if (qexec == null) {
qexec = new QueryExecutor();
qexec.setCloseConnection(false);
}
qexec.setConnection(connection);
try {
if (result == null) {
if (!copy & (target instanceof QueryStoredResults))
return ((QueryStoredResults) target).getFieldname();
result = new StoredQuery(-1);
result.setName(queryName == null ? UUID.randomUUID().toString() : queryName);
} else {
if (target instanceof QueryStoredResults) {
if ((((QueryStoredResults) target).getId() == result.getId()) && (result.getId() > 0))
// if we are copying to the same query , don't delete!
delete = false;
}
}
result.setQuery(target);
connection.setAutoCommit(false);
// create entry in the query table
if (result.getId() <= 0) {
// read by name and folder title
ReadStoredQuery findIfExists = new ReadStoredQuery();
findIfExists.setValue(result);
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
findIfExists.setFieldname(getSession().getName());
ResultSet found = null;
try {
found = qexec.process(findIfExists);
while (found.next()) {
IStoredQuery q = findIfExists.getObject(found);
result.setID(q.getID());
break;
}
} catch (Exception x) {
logger.warning(x.getMessage());
} finally {
try {
found.close();
} catch (Exception x) {
}
}
// if still not found, add it! TODO refactor it to use UpdateExecutor
if (result.getId() <= 0) {
PreparedStatement s = connection.prepareStatement(CreateStoredQuery.sql_byname, Statement.RETURN_GENERATED_KEYS);
s.setNull(1, Types.INTEGER);
if (result.getName().length() > 255)
s.setString(2, result.getName().substring(0, 255));
else
s.setString(2, result.getName());
try {
s.setString(3, result.getQuery().toString() == null ? "Results" : result.getQuery().toString());
} catch (Exception x) {
s.setString(3, "Results");
}
s.setString(4, getSession().getName());
// execute
if (s.executeUpdate() > 0) {
ResultSet rss = s.getGeneratedKeys();
while (rss.next()) result.setId(new Integer(rss.getInt(1)));
rss.close();
}
s.close();
}
}
// sometimes we want to remove the old content
if (delete && (result.getId() > 0)) {
PreparedStatement s = null;
try {
s = connection.prepareStatement("Delete from query_results where idquery=?");
s.setInt(1, result.getId());
s.executeUpdate();
} catch (Exception x) {
} finally {
try {
if (s != null)
s.close();
} catch (Exception x) {
}
}
}
if (result.getId() > 0) {
int rows = 0;
if ((result.getQuery() instanceof IQueryRetrieval) && ((IQueryRetrieval) result.getQuery()).isPrescreen()) {
rows = insertScreenedResults(result, (IQueryRetrieval<IStructureRecord>) result.getQuery());
} else {
rows = insertResults(result);
if (rows > 0)
connection.commit();
else
connection.rollback();
}
insertProfile(result, profile);
}
return result;
} catch (Exception x) {
try {
connection.rollback();
} catch (SQLException xx) {
}
throw new ProcessorException(this, x);
} finally {
try {
close();
} catch (Exception e) {
}
}
}
use of ambit2.db.search.structure.QueryStoredResults in project ambit-mirror by ideaconsult.
the class StoredQueryTableModel method selectRecords.
protected void selectRecords(SELECTION_MODE mode) throws Exception, SQLException {
IStoredQuery query = null;
if (storedResults == null)
return;
if (storedResults instanceof QueryStoredResults)
query = ((QueryStoredResults) storedResults).getFieldname();
else if (storedResults instanceof IStoredQuery)
query = (IStoredQuery) storedResults;
else
return;
UpdateSelectedRecords r = new UpdateSelectedRecords();
r.setObject(mode);
r.setGroup(query);
int updated = updateExecutor.process(r);
if (updated > 0)
setQuery(getQuery());
}
use of ambit2.db.search.structure.QueryStoredResults in project ambit-mirror by ideaconsult.
the class StoredQueryTableModelTest method createQuery.
/*
public void test() throws Exception {
final IStoredQuery query = new StoredQuery();
query.setId(3);
final StoredQueryTableModel queryModel = new StoredQueryTableModel();
Profile profile = new Profile();
Property prop = Property.getInstance("CAS","CAS");
prop.setLabel("CAS RN");
prop.setClazz(String.class);
prop.setEnabled(true);
profile.add(prop);
queryModel.setProfile(profile);
}
*/
@Override
protected QueryStoredResults createQuery() throws Exception {
QueryStoredResults q = new QueryStoredResults();
q.setFieldname(new StoredQuery(1));
return q;
}
use of ambit2.db.search.structure.QueryStoredResults in project ambit-mirror by ideaconsult.
the class DatasetStructuresResource method getQueryById.
protected Q getQueryById(String key) throws ResourceException {
if (key.startsWith(QR_PREFIX)) {
key = key.substring(QR_PREFIX.length());
try {
queryResultsID = Integer.parseInt(key.toString());
} catch (NumberFormatException x) {
throw new InvalidResourceIDException(key);
}
} else
return getDatasetByName(key);
if ((property > 0) && (search != null)) {
Property p = new Property("");
p.setId(property);
DatasetQueryFieldGeneric q = getSearchQuery(search, p, cond);
StoredQuery d = new StoredQuery(Integer.parseInt(key.toString()));
q.setFieldname(d);
Form form = getResourceRef(getRequest()).getQueryAsForm();
setPaging(form, q);
return (Q) q;
} else {
QueryStoredResults q = new QueryStoredResults();
q.setChemicalsOnly(true);
q.setFieldname(new StoredQuery(Integer.parseInt(key.toString())));
q.setValue(null);
return (Q) q;
}
}
Aggregations