Search in sources :

Example 1 with QueryStoredResults

use of ambit2.db.search.structure.QueryStoredResults in project ambit-mirror by ideaconsult.

the class FilteredDatasetResource method createQuery.

@Override
protected Q createQuery(Context context, Request request, Response response) throws ResourceException {
    Template filter = null;
    Form form = getResourceRef(getRequest()).getQueryAsForm();
    try {
        includeMol = "true".equals(form.getFirstValue("mol"));
    } catch (Exception x) {
        includeMol = false;
    }
    Object dataset = form.getFirstValue(OpenTox.params.dataset_uri.toString());
    String[] filteruris = OpenTox.params.filter.getValuesArray(form);
    Object condition = form.getFirstValue(OpenTox.params.condition.toString());
    SetCondition.conditions sc = condition == null ? SetCondition.conditions.in : "no".equals(condition) ? SetCondition.conditions.not_in : SetCondition.conditions.in;
    // if (dataset==null) throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,"Empty dataset");
    if (filteruris == null)
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "No features to filter");
    setTemplate(createTemplate(context, request, response));
    try {
        filter = createTemplate(context, request, response, filteruris);
        if ((filter == null) || (filter.size() == 0))
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "No features to filter");
        if (dataset == null) {
            QueryStructureByProperty q = new QueryStructureByProperty();
            q.setFieldname(filter);
            q.setCondition(new SetCondition(sc));
            setPaging(form, q);
            return (Q) q;
        }
        Object q = CallableQueryProcessor.getQueryObject(new Reference(dataset.toString()), getRequest().getRootRef(), getApplication().getContext(), getCookies(), getAgent(), getRequest().getResourceRef().toString());
        if (q == null) {
            throw new ResourceException(Status.SERVER_ERROR_NOT_IMPLEMENTED, "Processing foreign datasets not implemented!");
        } else if (q instanceof QueryDatasetByID) {
            QueryStructureByPropertyInDataset qz = new QueryStructureByPropertyInDataset();
            qz.setValue((QueryDatasetByID) q);
            qz.setFieldname(filter);
            qz.setCondition(new SetCondition(sc));
            setPaging(form, qz);
            return (Q) qz;
        } else if (q instanceof QueryStoredResults) {
            QueryStructuresByPropertyInResults qz = new QueryStructuresByPropertyInResults();
            qz.setValue((QueryStoredResults) q);
            qz.setFieldname(filter);
            qz.setCondition(new SetCondition(sc));
            setPaging(form, qz);
            return (Q) qz;
        } else if (q instanceof QueryStructureByID) {
            QueryStructureByPropertyInCompounds qz = new QueryStructureByPropertyInCompounds();
            qz.setValue((QueryStructureByID) q);
            qz.setFieldname(filter);
            qz.setCondition(new SetCondition(sc));
            setPaging(form, qz);
            return (Q) qz;
        } else
            throw new ResourceException(Status.SERVER_ERROR_NOT_IMPLEMENTED, dataset.toString() + " " + q.getClass().getName() + " not implemented!");
    } catch (ResourceException x) {
        throw x;
    } catch (Exception x) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x.getMessage(), x);
    }
}
Also used : Form(org.restlet.data.Form) Reference(org.restlet.data.Reference) QueryStructureByProperty(ambit2.db.search.structure.byproperty.QueryStructureByProperty) SetCondition(net.idea.modbcum.q.conditions.SetCondition) QueryStructureByPropertyInDataset(ambit2.db.search.structure.byproperty.QueryStructureByPropertyInDataset) QueryDatasetByID(ambit2.db.search.structure.QueryDatasetByID) ResourceException(org.restlet.resource.ResourceException) QueryStructureByID(ambit2.db.search.structure.QueryStructureByID) Template(ambit2.base.data.Template) QueryStructureByPropertyInCompounds(ambit2.db.search.structure.byproperty.QueryStructureByPropertyInCompounds) QueryStructuresByPropertyInResults(ambit2.db.search.structure.byproperty.QueryStructuresByPropertyInResults) QueryStoredResults(ambit2.db.search.structure.QueryStoredResults) ResourceException(org.restlet.resource.ResourceException)

Example 2 with QueryStoredResults

use of ambit2.db.search.structure.QueryStoredResults 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 3 with QueryStoredResults

use of ambit2.db.search.structure.QueryStoredResults in project ambit-mirror by ideaconsult.

the class StoredQueryTableModel method selectRecords.

protected void selectRecords(SELECTION_MODE mode) throws Exception, SQLException {
    IStoredQuery query = null;
    if (storedResults == null)
        return;
    if (storedResults instanceof QueryStoredResults)
        query = ((QueryStoredResults) storedResults).getFieldname();
    else if (storedResults instanceof IStoredQuery)
        query = (IStoredQuery) storedResults;
    else
        return;
    UpdateSelectedRecords r = new UpdateSelectedRecords();
    r.setObject(mode);
    r.setGroup(query);
    int updated = updateExecutor.process(r);
    if (updated > 0)
        setQuery(getQuery());
}
Also used : IStoredQuery(ambit2.db.search.IStoredQuery) UpdateSelectedRecords(ambit2.db.update.storedquery.UpdateSelectedRecords) QueryStoredResults(ambit2.db.search.structure.QueryStoredResults)

Example 4 with QueryStoredResults

use of ambit2.db.search.structure.QueryStoredResults in project ambit-mirror by ideaconsult.

the class StoredQueryTableModelTest method createQuery.

/*
	public void test() throws Exception {
		final IStoredQuery query = new StoredQuery();
		query.setId(3);
		final StoredQueryTableModel queryModel = new StoredQueryTableModel();
		Profile profile = new Profile();
		Property prop = Property.getInstance("CAS","CAS");
		prop.setLabel("CAS RN");
		prop.setClazz(String.class);
		prop.setEnabled(true);
		profile.add(prop);
		queryModel.setProfile(profile);
	
	}
*/
@Override
protected QueryStoredResults createQuery() throws Exception {
    QueryStoredResults q = new QueryStoredResults();
    q.setFieldname(new StoredQuery(1));
    return q;
}
Also used : StoredQuery(ambit2.db.search.StoredQuery) QueryStoredResults(ambit2.db.search.structure.QueryStoredResults)

Example 5 with QueryStoredResults

use of ambit2.db.search.structure.QueryStoredResults in project ambit-mirror by ideaconsult.

the class DatasetStructuresResource method getQueryById.

protected Q getQueryById(String key) throws ResourceException {
    if (key.startsWith(QR_PREFIX)) {
        key = key.substring(QR_PREFIX.length());
        try {
            queryResultsID = Integer.parseInt(key.toString());
        } catch (NumberFormatException x) {
            throw new InvalidResourceIDException(key);
        }
    } else
        return getDatasetByName(key);
    if ((property > 0) && (search != null)) {
        Property p = new Property("");
        p.setId(property);
        DatasetQueryFieldGeneric q = getSearchQuery(search, p, cond);
        StoredQuery d = new StoredQuery(Integer.parseInt(key.toString()));
        q.setFieldname(d);
        Form form = getResourceRef(getRequest()).getQueryAsForm();
        setPaging(form, q);
        return (Q) q;
    } else {
        QueryStoredResults q = new QueryStoredResults();
        q.setChemicalsOnly(true);
        q.setFieldname(new StoredQuery(Integer.parseInt(key.toString())));
        q.setValue(null);
        return (Q) q;
    }
}
Also used : StoredQuery(ambit2.db.search.StoredQuery) Form(org.restlet.data.Form) DatasetQueryFieldGeneric(ambit2.db.update.dataset.DatasetQueryFieldGeneric) QueryStoredResults(ambit2.db.search.structure.QueryStoredResults) InvalidResourceIDException(ambit2.rest.error.InvalidResourceIDException) Property(ambit2.base.data.Property)

Aggregations

QueryStoredResults (ambit2.db.search.structure.QueryStoredResults)11 StoredQuery (ambit2.db.search.StoredQuery)5 IStoredQuery (ambit2.db.search.IStoredQuery)3 QueryCombinedStructure (ambit2.db.search.structure.QueryCombinedStructure)3 QueryStructure (ambit2.db.search.structure.QueryStructure)3 QueryDataset (ambit2.db.search.structure.QueryDataset)2 QueryField (ambit2.db.search.structure.QueryField)2 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)2 DbAmbitException (net.idea.modbcum.i.exceptions.DbAmbitException)2 Form (org.restlet.data.Form)2 Property (ambit2.base.data.Property)1 Template (ambit2.base.data.Template)1 IAmbitEditor (ambit2.base.interfaces.IAmbitEditor)1 IStructureRecord (ambit2.base.interfaces.IStructureRecord)1 ProcessorException (ambit2.base.processors.ProcessorException)1 ProcessorSetQueryScope (ambit2.db.processors.ProcessorSetQueryScope)1 QueryExecutor (ambit2.db.search.QueryExecutor)1 QueryDatasetByID (ambit2.db.search.structure.QueryDatasetByID)1 QueryFieldNumeric (ambit2.db.search.structure.QueryFieldNumeric)1 QueryPrescreenBitSet (ambit2.db.search.structure.QueryPrescreenBitSet)1