use of ambit2.db.search.QueryExecutor in project ambit-mirror by ideaconsult.
the class UserByURIResource method readBundle.
protected SubstanceEndpointsBundle readBundle(int idbundle) throws ResourceException {
SubstanceEndpointsBundle dataset = new SubstanceEndpointsBundle();
dataset.setID(idbundle);
Connection c = null;
ResultSet rs = null;
QueryExecutor xx = null;
try {
DBConnection dbc = new DBConnection(getContext(), getAmbitConfigFile());
c = dbc.getConnection();
Set<_published_status> status = new TreeSet<_published_status>();
status.add(_published_status.published);
ReadBundle read = new ReadBundle(status);
read.setValue(dataset);
xx = new QueryExecutor();
xx.setConnection(c);
rs = xx.process(read);
while (rs.next()) {
dataset = read.getObject(rs);
}
return dataset;
} catch (Exception x) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, x.getMessage(), x);
} finally {
try {
rs.close();
} catch (Exception x) {
}
try {
c.close();
} catch (Exception x) {
}
try {
xx.close();
} catch (Exception x) {
}
}
}
use of ambit2.db.search.QueryExecutor in project ambit-mirror by ideaconsult.
the class CallableSubstanceImporter method createReference.
@Override
protected TaskResult createReference(Connection connection) throws Exception {
if ((importedRecord.getIdsubstance() > 0) || (importedRecord.getSubstanceUUID() != null)) {
try {
batch.close();
} catch (Exception xx) {
}
try {
if (file != null && file.exists())
file.delete();
} catch (Exception x) {
}
return new TaskResult(substanceReporter.getURI(importedRecord));
} else {
SourceDataset newDataset = dataset;
if (newDataset.getId() <= 0) {
ReadDataset q = new ReadDataset();
q.setValue(newDataset);
QueryExecutor<ReadDataset> x = new QueryExecutor<ReadDataset>();
x.setConnection(connection);
ResultSet rs = x.process(q);
while (rs.next()) {
newDataset = q.getObject(rs);
if (newDataset.getId() > 0)
break;
}
x.closeResults(rs);
x.setConnection(null);
}
if (newDataset == null || newDataset.getId() <= 0)
throw new ResourceException(Status.SUCCESS_NO_CONTENT);
try {
batch.close();
} catch (Exception xx) {
}
try {
if (file != null && file.exists())
file.delete();
} catch (Exception x) {
}
return new TaskResult(datasetURIReporter.getURI(newDataset));
}
}
use of ambit2.db.search.QueryExecutor in project ambit-mirror by ideaconsult.
the class CallableFileImport method importFile.
public TaskResult importFile(File file) throws Exception {
try {
// if target dataset is not defined, create new dataset
final SourceDataset dataset = targetDataset != null ? targetDataset : datasetMeta(file);
if (targetDataset == null)
dataset.setId(-1);
final BatchDBProcessor<String> batch = new BatchDBProcessor<String>() {
/**
*/
private static final long serialVersionUID = -7971761364143510120L;
@Override
public Iterator<String> getIterator(IInputState target) throws AmbitException {
try {
File file = ((FileInputState) target).getFile();
RDFIteratingReader i = getRDFIterator(file, getReporter().getBaseReference().toString());
if (i == null) {
IIteratingChemObjectReader ni = getNanoCMLIterator(file, getReporter().getBaseReference().toString());
if (ni == null)
return super.getIterator(target);
else
return ni;
} else {
/*
* RDFMetaDatasetIterator datasets = null; try {
* datasets = new
* RDFMetaDatasetIterator(i.getJenaModel());
* datasets
* .setBaseReference(getReporter().getBaseReference
* ()); while (datasets.hasNext()) { SourceDataset d
* = datasets.next(); dataset.setId(d.getId());
* dataset.setName(d.getName());
* dataset.setTitle(d.getTitle());
* dataset.setURL(d.getURL()); } } catch (Exception
* x) { x.printStackTrace(); } finally { try {
* datasets.close();} catch (Exception x) {} }
*/
return i;
}
} catch (AmbitException x) {
throw x;
} catch (Exception x) {
throw new AmbitException(x);
}
}
@Override
public void onItemProcessed(String input, Object output, IBatchStatistics stats) {
super.onItemProcessed(input, output, stats);
if (firstCompoundOnly && (stats.getRecords(RECORDS_STATS.RECORDS_PROCESSED) >= 1)) {
cancelled = true;
if (output != null)
if ((output instanceof ArrayList) && ((ArrayList) output).size() > 0) {
if (((ArrayList) output).get(0) instanceof IStructureRecord)
recordImported = (IStructureRecord) ((ArrayList) output).get(0);
} else if (output instanceof IStructureRecord)
recordImported = (IStructureRecord) output;
}
}
};
batch.setReference(dataset.getReference());
batch.setConnection(connection);
final RepositoryWriter writer = new RepositoryWriter();
writer.setUseExistingStructure(isPropertyOnly());
writer.setPropertyKey(getMatcher());
writer.setDataset(dataset);
final ProcessorsChain<String, IBatchStatistics, IProcessor> chain = new ProcessorsChain<String, IBatchStatistics, IProcessor>();
chain.add(writer);
batch.setProcessorChain(chain);
writer.setConnection(connection);
FileInputState fin = new FileInputState(file);
IBatchStatistics stats = batch.process(fin);
if (firstCompoundOnly) {
if (recordImported == null)
throw new Exception("No compound imported");
if (compoundReporter == null)
compoundReporter = new ConformerURIReporter("", null, false);
try {
batch.close();
} catch (Exception xx) {
}
return new TaskResult(compoundReporter.getURI(recordImported));
} else {
ReadDataset q = new ReadDataset();
q.setValue(dataset);
QueryExecutor<ReadDataset> x = new QueryExecutor<ReadDataset>();
x.setConnection(connection);
ResultSet rs = x.process(q);
ISourceDataset newDataset = null;
while (rs.next()) {
newDataset = q.getObject(rs);
break;
}
x.closeResults(rs);
x.setConnection(null);
if (newDataset == null)
throw new ResourceException(Status.SUCCESS_NO_CONTENT);
if (reporter == null)
reporter = new DatasetURIReporter<IQueryRetrieval<ISourceDataset>, ISourceDataset>();
try {
batch.close();
} catch (Exception xx) {
}
return new TaskResult(reporter.getURI(newDataset));
}
} catch (ResourceException x) {
throw x;
} catch (Exception x) {
throw new ResourceException(new Status(Status.SERVER_ERROR_INTERNAL, x.getMessage()));
} finally {
try {
connection.close();
} catch (Exception x) {
}
connection = null;
}
}
use of ambit2.db.search.QueryExecutor in project ambit-mirror by ideaconsult.
the class CallableFinder method getDataset.
protected SourceDataset getDataset(SourceDataset dataset) {
QueryExecutor exec = new QueryExecutor();
Connection connection = null;
try {
ReadDataset q = new ReadDataset();
q.setPage(0);
q.setPageSize(1);
q.setValue(dataset);
DBConnection dbc = new DBConnection(context);
connection = dbc.getConnection();
exec.setConnection(connection);
ResultSet rs = exec.process(q);
while (rs.next()) {
dataset = q.getObject(rs);
}
try {
exec.closeResults(rs);
} catch (Exception x) {
}
;
} catch (Exception x) {
// if failed will return the original dataset
} finally {
try {
exec.close();
} catch (Exception x) {
}
;
try {
connection.close();
} catch (Exception x) {
}
;
}
return dataset;
}
use of ambit2.db.search.QueryExecutor in project ambit-mirror by ideaconsult.
the class ProcessorStructureRetrieval method process.
public IStructureRecord process(IStructureRecord target) throws AmbitException {
if (exec == null) {
exec = new QueryExecutor<IQueryObject<IStructureRecord>>();
exec.setConnection(getConnection());
exec.setCloseConnection(false);
exec.setUseCache(true);
}
if (query instanceof AbstractQuery) {
((AbstractQuery) query).setValue(target);
// ((AbstractQuery)query).setCondition(NumberCondition.getInstance());
}
ResultSet rs = null;
try {
rs = exec.process(query);
if (query instanceof IMultiRetrieval)
return (IStructureRecord) ((IMultiRetrieval) query).getMultiObject(rs);
else
return selectResult(target, rs);
} catch (Exception x) {
throw new AmbitException(query.getSQL(), x);
} finally {
try {
exec.closeResults(rs);
} catch (SQLException x) {
logger.log(Level.WARNING, x.getMessage(), x);
}
}
}
Aggregations