Search in sources :

Example 1 with ProcessorException

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) {
        }
    }
}
Also used : IStoredQuery(ambit2.db.search.IStoredQuery) StoredQuery(ambit2.db.search.StoredQuery) CreateStoredQuery(ambit2.db.update.storedquery.CreateStoredQuery) ReadStoredQuery(ambit2.db.update.storedquery.ReadStoredQuery) ProcessorException(ambit2.base.processors.ProcessorException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) IQueryRetrieval(net.idea.modbcum.i.IQueryRetrieval) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException) SQLException(java.sql.SQLException) ProcessorException(ambit2.base.processors.ProcessorException) IStoredQuery(ambit2.db.search.IStoredQuery) QueryExecutor(ambit2.db.search.QueryExecutor) ReadStoredQuery(ambit2.db.update.storedquery.ReadStoredQuery) ResultSet(java.sql.ResultSet) QueryStoredResults(ambit2.db.search.structure.QueryStoredResults) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException)

Example 2 with ProcessorException

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;
}
Also used : ProcessorException(ambit2.base.processors.ProcessorException) QueryParam(net.idea.modbcum.i.query.QueryParam) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) IStoredProcStatement(net.idea.modbcum.i.IStoredProcStatement) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException) ProcessorException(ambit2.base.processors.ProcessorException) SQLException(java.sql.SQLException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException)

Example 3 with ProcessorException

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);
}
Also used : ProcessorException(ambit2.base.processors.ProcessorException) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ProcessorException(ambit2.base.processors.ProcessorException) FileNotFoundException(java.io.FileNotFoundException) HttpURLConnection(java.net.HttpURLConnection) SocketTimeoutException(java.net.SocketTimeoutException)

Example 4 with ProcessorException

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);
    }
}
Also used : ProcessorException(ambit2.base.processors.ProcessorException) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ProcessorException(ambit2.base.processors.ProcessorException)

Example 5 with ProcessorException

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;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ProcessorException(ambit2.base.processors.ProcessorException) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) IStructureRecord(ambit2.base.interfaces.IStructureRecord) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) OutputStreamWriter(java.io.OutputStreamWriter) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) TransformerException(javax.xml.transform.TransformerException)

Aggregations

ProcessorException (ambit2.base.processors.ProcessorException)19 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)12 IStructureRecord (ambit2.base.interfaces.IStructureRecord)8 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)7 InputStreamReader (java.io.InputStreamReader)6 Connection (java.sql.Connection)5 SQLException (java.sql.SQLException)5 NodeList (org.w3c.dom.NodeList)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 DbAmbitException (net.idea.modbcum.i.exceptions.DbAmbitException)4 StructureRecord (ambit2.base.data.StructureRecord)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 PreparedStatement (java.sql.PreparedStatement)3 List (java.util.List)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)3 Document (org.w3c.dom.Document)3