Search in sources :

Example 1 with IStoredQuery

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();
    }
}
Also used : IStoredQuery(ambit2.db.search.IStoredQuery) ISourceDataset(ambit2.base.data.ISourceDataset) Reference(org.restlet.data.Reference) AbstractQuery(ambit2.db.search.AbstractQuery) ISourceDataset(ambit2.base.data.ISourceDataset) SourceDataset(ambit2.base.data.SourceDataset) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException)

Example 2 with IStoredQuery

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());
        }
    }
}
Also used : DBConnection(ambit2.rest.DBConnection) IStoredQuery(ambit2.db.search.IStoredQuery) IStructureRecord(ambit2.base.interfaces.IStructureRecord) ProcessorCreateQuery(ambit2.db.processors.ProcessorCreateQuery) UpdateExecutor(ambit2.db.UpdateExecutor) Connection(java.sql.Connection) DBConnection(ambit2.rest.DBConnection) CreateQueryFolder(ambit2.db.update.queryfolder.CreateQueryFolder) SessionID(ambit2.db.SessionID) ResourceException(org.restlet.resource.ResourceException)

Example 3 with IStoredQuery

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) {
        }
    }
}
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 4 with IStoredQuery

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;
}
Also used : IStoredQuery(ambit2.db.search.IStoredQuery) ISourceDataset(ambit2.base.data.ISourceDataset)

Example 5 with IStoredQuery

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();
}
Also used : ProcessorCreateSession(ambit2.db.processors.ProcessorCreateSession) IStoredQuery(ambit2.db.search.IStoredQuery) ProcessorCreateQuery(ambit2.db.processors.ProcessorCreateQuery) ITable(org.dbunit.dataset.ITable) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) IQueryObject(net.idea.modbcum.i.IQueryObject) SessionID(ambit2.db.SessionID) QueryStructureByID(ambit2.db.search.structure.QueryStructureByID) Test(org.junit.Test)

Aggregations

IStoredQuery (ambit2.db.search.IStoredQuery)37 StoredQuery (ambit2.db.search.StoredQuery)19 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)9 SessionID (ambit2.db.SessionID)8 DbAmbitException (net.idea.modbcum.i.exceptions.DbAmbitException)7 ISourceDataset (ambit2.base.data.ISourceDataset)6 DeleteStoredQuery (ambit2.db.update.storedquery.DeleteStoredQuery)6 Connection (java.sql.Connection)6 Test (org.junit.Test)6 ProcessorCreateQuery (ambit2.db.processors.ProcessorCreateQuery)5 CreateStoredQuery (ambit2.db.update.storedquery.CreateStoredQuery)5 IStructureRecord (ambit2.base.interfaces.IStructureRecord)4 QueryStoredResults (ambit2.db.search.structure.QueryStoredResults)4 UpdateStoredQuery (ambit2.db.update.storedquery.UpdateStoredQuery)4 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)4 ResourceException (org.restlet.resource.ResourceException)4 SourceDataset (ambit2.base.data.SourceDataset)3 ProcessorCreateSession (ambit2.db.processors.ProcessorCreateSession)3 QueryStructureByID (ambit2.db.search.structure.QueryStructureByID)3 QueryDeleteTemplate (ambit2.db.update.storedquery.QueryDeleteTemplate)3