use of ambit2.db.UpdateExecutor 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.UpdateExecutor 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.UpdateExecutor in project ambit-mirror by ideaconsult.
the class SubstanceWriterTest method delete.
public int delete(SubstanceEndpointsBundle bundle, IRawReader<IStructureRecord> reader, Connection connection) throws Exception {
/*
* DBSubstanceWriter writer; if (bundle != null) writer = new
* DBBundleStudyWriter(bundle, DBSubstanceWriter.datasetMeta(), new
* SubstanceRecord()); else writer = new
* DBSubstanceWriter(DBSubstanceWriter.datasetMeta(), new
* SubstanceRecord(), clearMeasurements, clearComposition);
* writer.setSplitRecord(splitRecord); writer.setConnection(connection);
* writer.open();
*/
UpdateExecutor<IQueryUpdate> writer = new UpdateExecutor<IQueryUpdate>();
writer.setConnection(connection);
DeleteMatrixValue q = new DeleteMatrixValue();
q.setGroup(bundle);
int records = 0;
while (reader.hasNext()) {
Object record = reader.next();
if (record == null)
continue;
Assert.assertTrue(record instanceof SubstanceRecord);
Assert.assertEquals("IUC4-efdb21bb-e79f-3286-a988-b6f6944d3734", ((SubstanceRecord) record).getSubstanceUUID());
for (ProtocolApplication pa : ((SubstanceRecord) record).getMeasurements()) {
Assert.assertTrue(pa instanceof ProtocolApplicationAnnotated);
// System.out.println(((ProtocolApplicationAnnotated) pa)getRecords_to_delete());
ProtocolApplicationAnnotated paa = (ProtocolApplicationAnnotated) pa;
List<ValueAnnotated> vaa = paa.getRecords_to_delete();
for (ValueAnnotated va : vaa) {
q.setObject(va);
writer.process(q);
}
}
// writer.setImportedRecord((SubstanceRecord) record);
// writer.process((IStructureRecord) record);
records++;
}
writer.close();
return records;
}
use of ambit2.db.UpdateExecutor in project ambit-mirror by ideaconsult.
the class CallableWekaModelCreator method createReference.
@Override
protected TaskResult createReference(Connection connection) throws Exception {
builder.setTrainingData(((RDFInstancesParser) batch).getInstances());
UpdateExecutor<CreateModel> x = new UpdateExecutor<CreateModel>();
try {
model = createModel();
CreateModel update = new CreateModel(model);
x.setConnection(connection);
x.process(update);
writeAnnotations(model.getPredicted(), x);
return new TaskResult(builder.getModelReporter().getURI(model));
} catch (WekaException e) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage(), e);
} catch (AmbitException e) {
Context.getCurrentLogger().severe(e.getMessage());
if ((e.getCause() != null) && (e.getCause() instanceof WekaException))
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e.getCause().getMessage(), e.getCause());
else
throw e;
} catch (Exception e) {
Context.getCurrentLogger().severe(e.getMessage());
throw e;
} finally {
try {
x.close();
} catch (Exception xx) {
}
}
}
use of ambit2.db.UpdateExecutor in project ambit-mirror by ideaconsult.
the class CallableWafflesModelCreator method createReference.
/**
* TODO download ARFF directly, instead of reading Weka instances in memory
* protected AbstractBatchProcessor createBatch(Object target) throws Exception{
* if (target == null) throw new Exception("No dataset URI");
*
* File.createTempFile(prefix, suffix);
* DownloadTool.download(url, file);
* builder.setTrainingData(trainingData);
* return null;
* }
*/
@Override
protected TaskResult createReference(Connection connection) throws Exception {
File trainingData = File.createTempFile("wfltrain_", ".arff");
trainingData.deleteOnExit();
Instances instances = ((RDFInstancesParser) batch).getInstances();
instances.deleteAttributeAt(0);
// sort attributes by name (i.e. attribute URI).
List<Attribute> sorted = new ArrayList<Attribute>();
for (int i = 0; i < instances.numAttributes(); i++) sorted.add(instances.attribute(i));
Collections.sort(sorted, new Comparator<Attribute>() {
@Override
public int compare(Attribute o1, Attribute o2) {
return o1.name().compareTo(o2.toString());
}
});
StringBuilder order = null;
for (int i = 0; i < sorted.size(); i++) {
if (order == null)
order = new StringBuilder();
else
order.append(",");
order.append(sorted.get(i).index() + 1);
}
Reorder reorder = new Reorder();
String[] options = new String[2];
options[0] = "-R";
options[1] = order.toString();
reorder.setOptions(options);
reorder.setInputFormat(instances);
instances = Filter.useFilter(instances, reorder);
SparseToNonSparse sp = new SparseToNonSparse();
sp.setInputFormat(instances);
Instances newInstances = Filter.useFilter(instances, sp);
ArffSaver saver = new ArffSaver();
saver.setInstances(newInstances);
saver.setFile(trainingData);
saver.writeBatch();
// Leave the header only
newInstances.delete();
builder.setHeader(newInstances);
builder.setTrainingData(trainingData);
UpdateExecutor<CreateModel> x = new UpdateExecutor<CreateModel>();
try {
model = createModel();
// trainingData.delete();
CreateModel update = new CreateModel(model);
x.setConnection(connection);
x.process(update);
writeAnnotations(model.getPredicted(), x);
return new TaskResult(builder.getModelReporter().getURI(model));
} catch (WekaException e) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage(), e);
} catch (Exception e) {
Context.getCurrentLogger().severe(e.getMessage());
throw e;
} finally {
try {
x.close();
} catch (Exception xx) {
}
}
}
Aggregations