use of ambit2.db.search.IStoredQuery in project ambit-mirror by ideaconsult.
the class StructureRelationJSONReporter method header.
@Override
public void header(Writer output, Q query) {
try {
output.write("{\n");
Reference datasetURI = baseReference.clone();
datasetURI.addSegment(OpenTox.URI.dataset.name());
Object dataset = (query instanceof AbstractQuery) ? ((AbstractQuery) query).getValue() : null;
if (dataset instanceof IStoredQuery)
datasetURI.addSegment(String.format("R%d", ((IStoredQuery) dataset).getID()));
else if (dataset instanceof SourceDataset)
datasetURI.addSegment(Integer.toString(((ISourceDataset) dataset).getID()));
output.write(String.format("\"%s\":\"%s\",\n", jsonFeature.datasetURI.jsonname(), datasetURI));
output.write("\"links\":[");
} catch (Exception x) {
x.printStackTrace();
}
}
use of ambit2.db.search.IStoredQuery in project ambit-mirror by ideaconsult.
the class CallableQueryResultsCreator method createQueryResults.
protected TaskResult createQueryResults(IQueryObject<IStructureRecord> target) throws Exception {
Connection connection = null;
UpdateExecutor xx = new UpdateExecutor();
try {
DBConnection dbc = new DBConnection(context);
connection = dbc.getConnection();
SessionID session = new SessionID();
session.setName(folder);
CreateQueryFolder qfolder = new CreateQueryFolder();
qfolder.setObject(session);
xx.setConnection(connection);
xx.process(qfolder);
ProcessorCreateQuery p = new ProcessorCreateQuery();
p.setQueryName(queryName == null ? UUID.randomUUID().toString() : queryName);
p.setProfile(template);
p.setDelete(clearPreviousContent);
p.setCopy(true);
p.setStoredQuery(storedQuery);
p.setSession(session);
p.setConnection(connection);
IStoredQuery q = p.process((IQueryObject<IStructureRecord>) target);
return new TaskResult(String.format("%s/dataset/%s%d", applicationRootReference, DatasetStructuresResource.QR_PREFIX, q.getId()));
} catch (Exception x) {
Context.getCurrentLogger().log(Level.SEVERE, x.getMessage(), x);
throw x;
} finally {
Context.getCurrentLogger().fine("Done");
try {
xx.close();
} catch (Exception x) {
}
try {
connection.close();
} catch (Exception x) {
Context.getCurrentLogger().warning(x.getMessage());
}
}
}
use of ambit2.db.search.IStoredQuery 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.IStoredQuery in project ambit-mirror by ideaconsult.
the class QueryPairwiseTanimotoAndProperty method getSQL.
@Override
public String getSQL() throws AmbitException {
if ((property == null) || (property.getId() <= 0))
return super.getSQL();
ISourceDataset q1 = (getFieldname() != null) ? getFieldname() : getValue();
ISourceDataset q2 = (getValue() != null) ? getValue() : getFieldname();
if (q1 instanceof IStoredQuery) {
if (q2 instanceof IStoredQuery)
return sql_rdataset;
else
return sql_dataset_rdataset;
} else if (q2 instanceof IStoredQuery)
return sql_dataset_rdataset;
else
return sql_dataset;
}
use of ambit2.db.search.IStoredQuery in project ambit-mirror by ideaconsult.
the class ProcessorCreateQueryTest method test.
@Test
public void test() throws Exception {
setUpDatabaseFromResource("ambit2/db/processors/test/descriptors-datasets.xml");
IQueryObject q = new QueryStructureByID(100211, 100215);
q.setPageSize(1000);
IDatabaseConnection c = getConnection();
ProcessorCreateSession ps = new ProcessorCreateSession();
ps.setConnection(c.getConnection());
SessionID s = new SessionID();
s = ps.process(s);
c = getConnection();
ProcessorCreateQuery pq = new ProcessorCreateQuery();
pq.setSession(s);
pq.setConnection(c.getConnection());
IStoredQuery storedQuery = pq.process(q);
c = getConnection();
ITable table = c.createQueryTable("EXPECTED", String.format("SELECT idsessions,title from sessions where title='%s'", s.getName()));
Assert.assertEquals(1, table.getRowCount());
Assert.assertEquals(s.getName(), table.getValue(0, "title").toString());
Assert.assertEquals(3, storedQuery.getRows());
c.close();
}
Aggregations