Search in sources :

Example 1 with RDFMetaDatasetIterator

use of ambit2.rest.rdf.RDFMetaDatasetIterator in project ambit-mirror by ideaconsult.

the class MetadatasetResource method createObjectIterator.

@Override
protected RDFObjectIterator<M> createObjectIterator(Representation entity) throws ResourceException {
    RDFMetaDatasetIterator iterator = new RDFMetaDatasetIterator(entity, entity.getMediaType()) {

        @Override
        protected ISourceDataset createRecord() {
            return new SourceDataset();
        }
    };
    iterator.setForceReadRDFLocalObjects(true);
    iterator.setBaseReference(getRequest().getRootRef());
    return iterator;
}
Also used : ISourceDataset(ambit2.base.data.ISourceDataset) SourceDataset(ambit2.base.data.SourceDataset) RDFMetaDatasetIterator(ambit2.rest.rdf.RDFMetaDatasetIterator)

Example 2 with RDFMetaDatasetIterator

use of ambit2.rest.rdf.RDFMetaDatasetIterator 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 3 with RDFMetaDatasetIterator

use of ambit2.rest.rdf.RDFMetaDatasetIterator in project ambit-mirror by ideaconsult.

the class MetaDatasetResourceTest method testRDFXML.

@Test
public void testRDFXML() throws Exception {
    Reference ref = new Reference(String.format("http://localhost:%d/dataset/1/metadata", port));
    RDFMetaDatasetIterator<ISourceDataset> iterator = new RDFMetaDatasetIterator<ISourceDataset>(ref, "test") {

        @Override
        protected ISourceDataset createRecord() {
            return new SourceDataset();
        }
    };
    iterator.setBaseReference(new Reference(String.format("http://localhost:%d", port)));
    int count = 0;
    while (iterator.hasNext()) {
        ISourceDataset target = iterator.next();
        Assert.assertEquals(1, target.getID());
        Assert.assertEquals("Dbunit dataset", target.getName());
        Assert.assertEquals("XLogP", target.getSource());
        if (target instanceof SourceDataset)
            Assert.assertEquals("XLogP reference", ((SourceDataset) target).getURL());
        count++;
    }
    Assert.assertEquals(1, count);
    iterator.close();
}
Also used : ISourceDataset(ambit2.base.data.ISourceDataset) Reference(org.restlet.data.Reference) ISourceDataset(ambit2.base.data.ISourceDataset) SourceDataset(ambit2.base.data.SourceDataset) RDFMetaDatasetIterator(ambit2.rest.rdf.RDFMetaDatasetIterator) Test(org.junit.Test) ResourceTest(ambit2.rest.test.ResourceTest)

Example 4 with RDFMetaDatasetIterator

use of ambit2.rest.rdf.RDFMetaDatasetIterator in project ambit-mirror by ideaconsult.

the class MetaRDatasetResourceTest method testRDFXML.

@Test
public void testRDFXML() throws Exception {
    Reference ref = new Reference(String.format("http://localhost:%d/dataset/R1/metadata", port));
    RDFMetaDatasetIterator<ISourceDataset> iterator = new RDFMetaDatasetIterator<ISourceDataset>(ref, "test") {

        @Override
        protected ISourceDataset createRecord() {
            return new SourceDataset();
        }
    };
    iterator.setBaseReference(new Reference(String.format("http://localhost:%d", port)));
    int count = 0;
    while (iterator.hasNext()) {
        ISourceDataset target = iterator.next();
        Assert.assertEquals(1, target.getID());
        Assert.assertEquals("test query", target.getName());
        // Assert.assertEquals("XLogP", target.getTitle());
        // Assert.assertEquals("XLogP", target.getURL());
        count++;
    }
    Assert.assertEquals(1, count);
    iterator.close();
}
Also used : ISourceDataset(ambit2.base.data.ISourceDataset) Reference(org.restlet.data.Reference) ISourceDataset(ambit2.base.data.ISourceDataset) SourceDataset(ambit2.base.data.SourceDataset) RDFMetaDatasetIterator(ambit2.rest.rdf.RDFMetaDatasetIterator) Test(org.junit.Test) ResourceTest(ambit2.rest.test.ResourceTest)

Aggregations

ISourceDataset (ambit2.base.data.ISourceDataset)4 SourceDataset (ambit2.base.data.SourceDataset)4 RDFMetaDatasetIterator (ambit2.rest.rdf.RDFMetaDatasetIterator)3 ResourceTest (ambit2.rest.test.ResourceTest)2 Test (org.junit.Test)2 Reference (org.restlet.data.Reference)2 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 QueryExecutor (ambit2.db.search.QueryExecutor)1 ReadDataset (ambit2.db.update.dataset.ReadDataset)1 DatasetURIReporter (ambit2.rest.dataset.DatasetURIReporter)1 RDFIteratingReader (ambit2.rest.dataset.RDFIteratingReader)1 ConformerURIReporter (ambit2.rest.structure.ConformerURIReporter)1 File (java.io.File)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 IBatchStatistics (net.idea.modbcum.i.batch.IBatchStatistics)1