use of ambit2.base.processors.ProcessorException 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.base.processors.ProcessorException in project ambit-mirror by ideaconsult.
the class QueryExecutor method process.
public synchronized ResultSet process(Q target) throws AmbitException {
long now = System.currentTimeMillis();
Connection c = getConnection();
if (c == null)
throw new AmbitException("no connection");
ResultSet rs = null;
try {
List<QueryParam> params = target.getParameters();
if (params == null) {
statement = c.createStatement(getResultType(), getResultTypeConcurency());
String sql = getSQL(target);
rs = statement.executeQuery(sql);
} else {
String sql = getSQL(target);
sresults = getCachedStatement(sql);
if (sresults == null) {
if (target instanceof IStoredProcStatement) {
sresults = c.prepareCall(sql);
} else {
sresults = c.prepareStatement(sql, getResultType(), getResultTypeConcurency());
sresults.setFetchDirection(ResultSet.FETCH_FORWARD);
sresults.setFetchSize(Integer.MIN_VALUE);
}
if (isUseCache())
addStatementToCache(sql, sresults);
} else {
sresults.clearParameters();
}
QueryExecutor.setParameters(sresults, params);
// logger.log(Level.FINEST,sql);
rs = sresults.executeQuery();
}
} catch (Exception x) {
try {
logger.log(Level.SEVERE, x.getMessage() + " " + sresults);
} catch (Exception xx) {
}
throw new ProcessorException(this, x);
} catch (Throwable x) {
throw new ProcessorException(this, x.getMessage());
} finally {
}
return rs;
}
use of ambit2.base.processors.ProcessorException in project ambit-mirror by ideaconsult.
the class HTTPRequest method process.
public Result process(Target target) throws AmbitException {
int retry = 0;
int status = 200;
setCancelled(false);
while (!isCancelled()) {
try {
URL url = new URL(getUrl());
URLConnection connection = url.openConnection();
if (connection instanceof HttpURLConnection) {
HttpURLConnection hc = ((HttpURLConnection) connection);
hc.setReadTimeout(timeout);
hc.setConnectTimeout(timeout);
hc.setRequestMethod(httpMethod);
hc.setDoOutput(true);
prepareOutput(target, hc.getOutputStream());
InputStream in = hc.getInputStream();
Result result = parseInput(target, hc.getInputStream());
in.close();
status = hc.getResponseCode();
switch(status) {
case 200:
return result;
case 404:
return null;
case 500:
{
retry++;
setCancelled(retry >= 10);
}
default:
{
throw new ProcessorException(this, String.format("STATUS: %d", status));
}
}
} else
return null;
} catch (SocketTimeoutException x) {
retry++;
setCancelled(retry >= maxretry);
if (isCancelled())
if (maxretry > 1)
throw new ProcessorException(this, "Maximum retry count reached " + maxretry);
else
throw new ProcessorException(this, x);
} catch (FileNotFoundException x) {
throw new ambit2.pubchem.FileNotFoundException(this, x);
} catch (IOException x) {
if (x.getMessage().indexOf("Server returned HTTP response code: 4") >= 0)
setCancelled(true);
throw new ProcessorException(this, x);
} catch (Exception x) {
throw new ProcessorException(this, x);
} finally {
}
}
if (isCancelled())
throw new ProcessorException(this, "Maximum retry count reached " + maxretry);
else
throw new ProcessorException(this, "STATUS: " + status);
}
use of ambit2.base.processors.ProcessorException in project ambit-mirror by ideaconsult.
the class NCISearchProcessor method parseInput.
@Override
protected String parseInput(String target, InputStream in) throws ProcessorException {
StringBuilder builder = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append((getOption().equals(METHODS.sdf)) ? '\n' : '\t');
}
return builder.toString();
} catch (Exception x) {
throw new ProcessorException(this, x);
}
}
use of ambit2.base.processors.ProcessorException in project ambit-mirror by ideaconsult.
the class PUGProcessor method createDownloadHTTPRequest.
protected HTTPRequest<List<IStructureRecord>, List<IStructureRecord>> createDownloadHTTPRequest(List<IStructureRecord> record) {
HTTPRequest<List<IStructureRecord>, List<IStructureRecord>> request = new HTTPRequest<List<IStructureRecord>, List<IStructureRecord>>() {
@Override
protected List<IStructureRecord> parseInput(List<IStructureRecord> target, InputStream in) throws ProcessorException {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new InputStreamReader(in)));
doc.normalize();
return parseOutput(doc);
} catch (IOException x) {
throw new ProcessorException(this, x);
} catch (SAXException x) {
throw new ProcessorException(this, x);
} catch (ParserConfigurationException x) {
throw new ProcessorException(this, x);
}
}
@Override
protected void prepareOutput(List<IStructureRecord> target, OutputStream out) throws ProcessorException {
try {
Writer w = new OutputStreamWriter(out);
createDownloadRequest(target, w);
w.flush();
w.close();
} catch (ParserConfigurationException x) {
throw new ProcessorException(this, x);
} catch (TransformerException x) {
throw new ProcessorException(this, x);
} catch (IOException x) {
throw new ProcessorException(this, x);
}
}
};
request.setUrl(pugURL);
return request;
}
Aggregations