Search in sources :

Example 1 with ReadDataset

use of ambit2.db.update.dataset.ReadDataset in project ambit-mirror by ideaconsult.

the class MetadatasetResource method getQuery.

protected IQueryRetrieval<M> getQuery(Context context, Request request, Response response, boolean IDcanBeEmpty) throws ResourceException {
    Form form = getResourceRef(request).getQueryAsForm();
    try {
        headless = Boolean.parseBoolean(form.getFirstValue("headless"));
    } catch (Exception x) {
        headless = false;
    }
    AbstractReadDataset query = null;
    structureParam = getStructureParameter();
    StringCondition condition;
    try {
        condition = StringCondition.getInstance(form.getFirstValue(QueryResource.condition));
    } catch (Exception x) {
        condition = StringCondition.getInstance(StringCondition.C_EQ);
    }
    Property property = new Property(null);
    property.setClazz(null);
    property.setLabel(null);
    property.setReference(null);
    for (search_features sf : search_features.values()) {
        Object id = form.getFirstValue(sf.name());
        if (id != null) {
            // because we are not storing full local references!
            if (search_features.feature_hassource.equals(sf)) {
                String parent = getRequest().getRootRef().toString();
                int p = id.toString().indexOf(parent);
                if (p >= 0) {
                    // yet one more hack ... should store at least prefixes
                    id = id.toString().substring(p + parent.length()).replace("/algorithm/", "").replace("/model/", "");
                }
            }
            sf.setProperty(property, id);
            if (query == null) {
                query = new QueryDatasetByFeatures(property, condition);
                ((QueryDatasetByFeatures) query).setStructure(structureParam);
            }
        }
    }
    if (query == null) {
        query = new ReadDataset();
        query.setValue(null);
    }
    Object id = request.getAttributes().get(DatasetStructuresResource.datasetKey);
    if (id != null)
        try {
            Integer idnum = new Integer(Reference.decode(id.toString()));
            dataset = (M) new SourceDataset();
            dataset.setID(idnum);
            query.setValue(dataset);
        } catch (NumberFormatException x) {
            if (id.toString().startsWith(DatasetStructuresResource.QR_PREFIX)) {
                String key = id.toString().substring(DatasetStructuresResource.QR_PREFIX.length());
                try {
                    IQueryRetrieval<M> q = (IQueryRetrieval<M>) new ReadStoredQuery(Integer.parseInt(key.toString()));
                    return q;
                } catch (NumberFormatException xx) {
                    throw new InvalidResourceIDException(id);
                }
            } else {
                dataset = (M) new SourceDataset();
                dataset.setName(id.toString());
                query.setValue(dataset);
            }
        } catch (Exception x) {
            throw new InvalidResourceIDException(id);
        }
    if (!IDcanBeEmpty && (query.getValue() == null))
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Empty dataset ID!");
    return query;
}
Also used : QueryDatasetByFeatures(ambit2.db.update.dataset.QueryDatasetByFeatures) Form(org.restlet.data.Form) AbstractReadDataset(ambit2.db.update.dataset.AbstractReadDataset) IQueryRetrieval(net.idea.modbcum.i.IQueryRetrieval) ResourceException(org.restlet.resource.ResourceException) InvalidResourceIDException(ambit2.rest.error.InvalidResourceIDException) StringCondition(net.idea.modbcum.q.conditions.StringCondition) ReadStoredQuery(ambit2.db.update.storedquery.ReadStoredQuery) ISourceDataset(ambit2.base.data.ISourceDataset) SourceDataset(ambit2.base.data.SourceDataset) ResourceException(org.restlet.resource.ResourceException) InvalidResourceIDException(ambit2.rest.error.InvalidResourceIDException) Property(ambit2.base.data.Property) ReadDataset(ambit2.db.update.dataset.ReadDataset) AbstractReadDataset(ambit2.db.update.dataset.AbstractReadDataset)

Example 2 with ReadDataset

use of ambit2.db.update.dataset.ReadDataset 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));
    }
}
Also used : QueryExecutor(ambit2.db.search.QueryExecutor) ResultSet(java.sql.ResultSet) TaskResult(ambit2.rest.task.TaskResult) SourceDataset(ambit2.base.data.SourceDataset) ResourceException(org.restlet.resource.ResourceException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) ReadDataset(ambit2.db.update.dataset.ReadDataset)

Example 3 with ReadDataset

use of ambit2.db.update.dataset.ReadDataset 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;
    }
}
Also used : RDFIteratingReader(ambit2.rest.dataset.RDFIteratingReader) ArrayList(java.util.ArrayList) RepositoryWriter(ambit2.db.processors.RepositoryWriter) IStructureRecord(ambit2.base.interfaces.IStructureRecord) ISourceDataset(ambit2.base.data.ISourceDataset) ProcessorsChain(net.idea.modbcum.i.processors.ProcessorsChain) IInputState(ambit2.core.io.IInputState) QueryExecutor(ambit2.db.search.QueryExecutor) ResultSet(java.sql.ResultSet) ResourceException(org.restlet.resource.ResourceException) Status(org.restlet.data.Status) IIteratingChemObjectReader(org.openscience.cdk.io.iterator.IIteratingChemObjectReader) IBatchStatistics(net.idea.modbcum.i.batch.IBatchStatistics) IProcessor(net.idea.modbcum.i.processors.IProcessor) BatchDBProcessor(ambit2.db.processors.BatchDBProcessor) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) CDKException(org.openscience.cdk.exception.CDKException) ResourceException(org.restlet.resource.ResourceException) ConformerURIReporter(ambit2.rest.structure.ConformerURIReporter) ISourceDataset(ambit2.base.data.ISourceDataset) SourceDataset(ambit2.base.data.SourceDataset) File(java.io.File) FileInputState(ambit2.core.io.FileInputState) ReadDataset(ambit2.db.update.dataset.ReadDataset) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DatasetURIReporter(ambit2.rest.dataset.DatasetURIReporter)

Example 4 with ReadDataset

use of ambit2.db.update.dataset.ReadDataset 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;
}
Also used : DBConnection(ambit2.rest.DBConnection) QueryExecutor(ambit2.db.search.QueryExecutor) Connection(java.sql.Connection) DBConnection(ambit2.rest.DBConnection) ResultSet(java.sql.ResultSet) ReadDataset(ambit2.db.update.dataset.ReadDataset) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 5 with ReadDataset

use of ambit2.db.update.dataset.ReadDataset in project ambit-mirror by ideaconsult.

the class DbStructureWriter method writeDataset.

protected void writeDataset(IStructureRecord structure) throws SQLException, AmbitException, OperationNotSupportedException {
    if (getDataset() == null)
        setDataset(new SourceDataset("Default"));
    if (getDataset().getID() <= 0) {
        if (readDatasetQuery == null)
            readDatasetQuery = new ReadDataset();
        readDatasetQuery.setValue(getDataset());
        readDatasetQuery.setCondition(StringCondition.getInstance(StringCondition.C_EQ));
        readDatasetQuery.setPageSize(2);
        readDatasetQuery.setPage(0);
        ResultSet rs = null;
        try {
            rs = queryexec.process(readDatasetQuery);
            // should be 1 only
            int record = 0;
            while (rs.next()) {
                SourceDataset d = readDatasetQuery.getObject(rs);
                getDataset().setID(d.getID());
                record++;
            }
            if (record > 1)
                // smth is wrong
                getDataset().setID(-1);
        } catch (Exception x) {
            getDataset().setID(-1);
        }
    }
    datasetAddStruc.setObject(structure);
    datasetAddStruc.setGroup(dataset);
    try {
        exec.process(datasetAddStruc);
    } catch (Exception x) {
        logger.log(Level.SEVERE, String.format("Error %s adding structure /compound/%d/conformer/%d to dataset [%d] %s", x.getMessage(), structure.getIdchemical(), structure.getIdstructure(), dataset.getId(), dataset.getName()));
        throw new AmbitException(x);
    }
    propertyWriter.setDataset(dataset);
}
Also used : ResultSet(java.sql.ResultSet) SourceDataset(ambit2.base.data.SourceDataset) ReadDataset(ambit2.db.update.dataset.ReadDataset) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException) SQLException(java.sql.SQLException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) DbAmbitException(net.idea.modbcum.i.exceptions.DbAmbitException)

Aggregations

SourceDataset (ambit2.base.data.SourceDataset)6 ReadDataset (ambit2.db.update.dataset.ReadDataset)6 ResultSet (java.sql.ResultSet)5 ResourceException (org.restlet.resource.ResourceException)5 ISourceDataset (ambit2.base.data.ISourceDataset)4 QueryExecutor (ambit2.db.search.QueryExecutor)4 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)4 DBConnection (ambit2.rest.DBConnection)2 TaskResult (ambit2.rest.task.TaskResult)2 Connection (java.sql.Connection)2 IQueryRetrieval (net.idea.modbcum.i.IQueryRetrieval)2 Property (ambit2.base.data.Property)1 IStructureRecord (ambit2.base.interfaces.IStructureRecord)1 FileInputState (ambit2.core.io.FileInputState)1 IInputState (ambit2.core.io.IInputState)1 BatchDBProcessor (ambit2.db.processors.BatchDBProcessor)1 RepositoryWriter (ambit2.db.processors.RepositoryWriter)1 IStoredQuery (ambit2.db.search.IStoredQuery)1 StoredQuery (ambit2.db.search.StoredQuery)1 AbstractReadDataset (ambit2.db.update.dataset.AbstractReadDataset)1