Search in sources :

Example 1 with QueryStructureByID

use of ambit2.db.search.structure.QueryStructureByID 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 QueryStructureByID

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

the class StructureQueryResource method createConvertor.

@Override
public RepresentationConvertor createConvertor(Variant variant) throws AmbitException, ResourceException {
    /* workaround for clients not being able to set accept headers */
    if ((queryObject == null) && !(variant.getMediaType().equals(MediaType.TEXT_HTML)))
        throw new NotFoundException();
    setTemplate(template);
    Form acceptform = getResourceRef(getRequest()).getQueryAsForm();
    String media = acceptform.getFirstValue("accept-header");
    if (media != null) {
        variant.setMediaType(new MediaType(media));
    }
    String filenamePrefix = getRequest().getResourceRef().getPath();
    if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_MDLSDF)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SDFReporter<QueryStructureByID>(template, getGroupProperties(), changeLineSeparators), ChemicalMediaType.CHEMICAL_MDLSDF, filenamePrefix);
    } else if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_MDLMOL)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SDFReporter<QueryStructureByID>(new Template(), getGroupProperties(), true, changeLineSeparators), ChemicalMediaType.CHEMICAL_MDLMOL, filenamePrefix);
    } else if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_CML)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new CMLReporter<QueryStructureByID>(), ChemicalMediaType.CHEMICAL_CML, filenamePrefix);
    } else if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_SMILES)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SmilesReporter<QueryStructureByID>(true, getTemplate()), ChemicalMediaType.CHEMICAL_SMILES, filenamePrefix);
    } else if (variant.getMediaType().equals(ChemicalMediaType.CHEMICAL_INCHI)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SmilesReporter<QueryStructureByID>(false, Mode.InChI, getTemplate()), ChemicalMediaType.CHEMICAL_INCHI, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_PDF)) {
        return new PDFConvertor<IStructureRecord, QueryStructureByID, PDFReporter<QueryStructureByID>>(new PDFReporter<QueryStructureByID>(getTemplate(), getGroupProperties()), filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.TEXT_PLAIN)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(createTXTReporter(), MediaType.TEXT_PLAIN);
    } else if (variant.getMediaType().equals(MediaType.TEXT_URI_LIST)) {
        ConformerURIReporter<QueryStructureByID> reporter = new ConformerURIReporter<QueryStructureByID>(getCompoundInDatasetPrefix(), getRequest(), queryObject.isPrescreen());
        return new StringConvertor(reporter, MediaType.TEXT_URI_LIST, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.IMAGE_PNG)) {
        return new ImageConvertor<IStructureRecord, QueryStructureByID>(new ImageReporter<QueryStructureByID>(MediaType.IMAGE_PNG.getMainType(), MediaType.IMAGE_PNG.getSubType()), MediaType.IMAGE_PNG);
    } else if (variant.getMediaType().equals(MediaType.IMAGE_GIF)) {
        return new ImageConvertor<IStructureRecord, QueryStructureByID>(new ImageReporter<QueryStructureByID>(MediaType.IMAGE_GIF.getMainType(), MediaType.IMAGE_GIF.getSubType()), MediaType.IMAGE_GIF);
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_JSON)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new CompoundJSONReporter(getTemplate(), getGroupProperties(), folders, bundles, getRequest(), getRequest().getRootRef().toString() + getCompoundInDatasetPrefix(), includeMol, null), MediaType.APPLICATION_JSON, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_JAVASCRIPT)) {
        String jsonpcallback = getParams().getFirstValue("jsonp");
        if (jsonpcallback == null)
            jsonpcallback = getParams().getFirstValue("callback");
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new CompoundJSONReporter(getTemplate(), getGroupProperties(), folders, bundles, getRequest(), getRequest().getRootRef().toString() + getCompoundInDatasetPrefix(), includeMol, jsonpcallback), MediaType.APPLICATION_JAVASCRIPT, filenamePrefix);
    } else if (variant.getMediaType().equals(ChemicalMediaType.WEKA_ARFF)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new ARFFResourceReporter(getTemplate(), getGroupProperties(), getRequest(), String.format("%s%s", getRequest().getRootRef(), getCompoundInDatasetPrefix())), ChemicalMediaType.WEKA_ARFF, filenamePrefix);
    } else if (variant.getMediaType().equals(ChemicalMediaType.THREECOL_ARFF)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new ARFF3ColResourceReporter(getTemplate(), getGroupProperties(), getRequest(), String.format("%s%s", getRequest().getRootRef(), getCompoundInDatasetPrefix())), ChemicalMediaType.THREECOL_ARFF, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.TEXT_CSV)) {
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(createCSVReporter(), MediaType.TEXT_CSV, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_RDF_XML)) {
        switch(rdfwriter) {
            case stax:
                {
                    return new RDFStaXConvertor<IStructureRecord, IQueryRetrieval<IStructureRecord>>(new DatasetRDFStaxReporter(getCompoundInDatasetPrefix(), getRequest(), getTemplate(), getGroupProperties()), filenamePrefix);
                }
            default:
                {
                    // jena
                    return new RDFJenaConvertor<IStructureRecord, IQueryRetrieval<IStructureRecord>>(new DatasetRDFReporter(getCompoundInDatasetPrefix(), getRequest(), variant.getMediaType(), getTemplate(), getGroupProperties()), variant.getMediaType(), filenamePrefix);
                }
        }
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_RDF_TURTLE) || variant.getMediaType().equals(MediaType.TEXT_RDF_N3) || variant.getMediaType().equals(MediaType.TEXT_RDF_NTRIPLES) || variant.getMediaType().equals(MediaType.APPLICATION_RDF_TRIG) || variant.getMediaType().equals(MediaType.APPLICATION_RDF_TRIX) || variant.getMediaType().equals(ChemicalMediaType.APPLICATION_JSONLD)) {
        return new RDFJenaConvertor<IStructureRecord, IQueryRetrieval<IStructureRecord>>(new DatasetRDFReporter(getCompoundInDatasetPrefix(), getRequest(), variant.getMediaType(), getTemplate(), getGroupProperties()), variant.getMediaType(), filenamePrefix);
    } else
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(new SDFReporter<QueryStructureByID>(template, getGroupProperties(), changeLineSeparators), ChemicalMediaType.CHEMICAL_MDLSDF, filenamePrefix);
}
Also used : Form(org.restlet.data.Form) OutputWriterConvertor(net.idea.restnet.db.convertors.OutputWriterConvertor) PDFReporter(ambit2.db.reporters.PDFReporter) NotFoundException(net.idea.modbcum.i.exceptions.NotFoundException) QueryStructureByID(ambit2.db.search.structure.QueryStructureByID) Template(ambit2.base.data.Template) IStructureRecord(ambit2.base.interfaces.IStructureRecord) ARFF3ColResourceReporter(ambit2.rest.dataset.ARFF3ColResourceReporter) SDFReporter(ambit2.db.reporters.SDFReporter) ChemicalMediaType(net.idea.restnet.c.ChemicalMediaType) MediaType(org.restlet.data.MediaType) RDFJenaConvertor(ambit2.rest.RDFJenaConvertor) CompoundJSONReporter(ambit2.rest.structure.CompoundJSONReporter) SmilesReporter(ambit2.db.reporters.SmilesReporter) DatasetRDFStaxReporter(ambit2.rest.dataset.DatasetRDFStaxReporter) StringConvertor(net.idea.restnet.c.StringConvertor) ImageReporter(ambit2.db.reporters.ImageReporter) RDFStaXConvertor(ambit2.rest.RDFStaXConvertor) ARFFResourceReporter(ambit2.rest.dataset.ARFFResourceReporter) ConformerURIReporter(ambit2.rest.structure.ConformerURIReporter) ImageConvertor(ambit2.rest.ImageConvertor) DatasetRDFReporter(ambit2.rest.dataset.DatasetRDFReporter)

Example 3 with QueryStructureByID

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

the class NanoMaterialResource method createQueryByID.

@Override
protected QueryStructureByID createQueryByID(IStructureRecord record) {
    QueryStructureByID query = super.createQueryByID(record);
    query.setNanomaterial(true);
    return query;
}
Also used : QueryStructureByID(ambit2.db.search.structure.QueryStructureByID)

Example 4 with QueryStructureByID

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

the class ConformerResource method createQuery.

@Override
protected QueryStructureByID createQuery(Context context, Request request, Response response) throws ResourceException {
    media = getMediaParameter(request);
    Form form = request.getResourceRef().getQueryAsForm();
    try {
        headless = Boolean.parseBoolean(form.getFirstValue("headless"));
    } catch (Exception x) {
        headless = false;
    }
    try {
        includeMol = "true".equals(form.getFirstValue("mol"));
    } catch (Exception x) {
        includeMol = false;
    }
    try {
        setTemplate(createTemplate(context, request, response));
        IStructureRecord record = new StructureRecord();
        try {
            record.setIdchemical(Integer.parseInt(Reference.decode(request.getAttributes().get(DataResources.idcompound_resource).toString())));
        } catch (NumberFormatException x) {
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Invalid resource id", x);
        }
        QueryStructureByID query = new QueryStructureByID();
        query.setPageSize(-1);
        Object idconformer = request.getAttributes().get(DataResources.idconformer_resource);
        try {
            record.setIdstructure(Integer.parseInt(Reference.decode(idconformer.toString())));
            query.setChemicalsOnly(false);
        } catch (Exception x) {
            record.setIdstructure(-1);
            query.setChemicalsOnly(true);
            query.setPageSize(-1);
            query.setValue(record);
        }
        query.setValue(record);
        return query;
    } catch (Exception x) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Invalid resource id", x);
    }
}
Also used : IStructureRecord(ambit2.base.interfaces.IStructureRecord) Form(org.restlet.data.Form) IStructureRecord(ambit2.base.interfaces.IStructureRecord) StructureRecord(ambit2.base.data.StructureRecord) ResourceException(org.restlet.resource.ResourceException) ResourceException(org.restlet.resource.ResourceException) QueryStructureByID(ambit2.db.search.structure.QueryStructureByID)

Example 5 with QueryStructureByID

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

the class AbstractPairwiseResource method createConvertor.

@Override
public RepresentationConvertor createConvertor(Variant variant) throws AmbitException, ResourceException {
    /* workaround for clients not being able to set accept headers */
    if ((queryObject == null) && !(variant.getMediaType().equals(MediaType.TEXT_HTML)))
        throw new NotFoundException();
    // setTemplate(template);
    Form acceptform = getResourceRef(getRequest()).getQueryAsForm();
    String media = acceptform.getFirstValue("accept-header");
    if (media != null) {
        variant.setMediaType(new MediaType(media));
    }
    String filenamePrefix = getRequest().getResourceRef().getPath();
    if (variant.getMediaType().equals(MediaType.TEXT_HTML)) {
        Dimension d = new Dimension(150, 150);
        Form form = getResourceRef(getRequest()).getQueryAsForm();
        try {
            d.width = Integer.parseInt(form.getFirstValue("w").toString());
        } catch (Exception x) {
        }
        try {
            d.height = Integer.parseInt(form.getFirstValue("h").toString());
        } catch (Exception x) {
        }
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(createHTMLReporter(d), MediaType.TEXT_HTML);
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_JAVASCRIPT)) {
        String jsonpcallback = getParams().getFirstValue("jsonp");
        if (jsonpcallback == null)
            jsonpcallback = getParams().getFirstValue("callback");
        return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(createJSONReporter(jsonpcallback), MediaType.APPLICATION_JSON, filenamePrefix);
    }
    return new OutputWriterConvertor<IStructureRecord, QueryStructureByID>(createJSONReporter(), MediaType.APPLICATION_JSON, filenamePrefix);
}
Also used : IStructureRecord(ambit2.base.interfaces.IStructureRecord) Form(org.restlet.data.Form) OutputWriterConvertor(net.idea.restnet.db.convertors.OutputWriterConvertor) NotFoundException(net.idea.modbcum.i.exceptions.NotFoundException) MediaType(org.restlet.data.MediaType) Dimension(java.awt.Dimension) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) NotFoundException(net.idea.modbcum.i.exceptions.NotFoundException) InvalidResourceIDException(ambit2.rest.error.InvalidResourceIDException) QueryStructureByID(ambit2.db.search.structure.QueryStructureByID)

Aggregations

QueryStructureByID (ambit2.db.search.structure.QueryStructureByID)21 IStructureRecord (ambit2.base.interfaces.IStructureRecord)10 Form (org.restlet.data.Form)10 ResourceException (org.restlet.resource.ResourceException)7 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)5 StructureRecord (ambit2.base.data.StructureRecord)4 QueryCombinedStructure (ambit2.db.search.structure.QueryCombinedStructure)4 NotFoundException (net.idea.modbcum.i.exceptions.NotFoundException)4 StringConvertor (net.idea.restnet.c.StringConvertor)4 OutputWriterConvertor (net.idea.restnet.db.convertors.OutputWriterConvertor)4 MediaType (org.restlet.data.MediaType)4 Template (ambit2.base.data.Template)3 ARFF3ColResourceReporter (ambit2.rest.dataset.ARFF3ColResourceReporter)3 InvalidResourceIDException (ambit2.rest.error.InvalidResourceIDException)3 ChemicalMediaType (net.idea.restnet.c.ChemicalMediaType)3 Test (org.junit.Test)3 ISourceDataset (ambit2.base.data.ISourceDataset)2 SourceDataset (ambit2.base.data.SourceDataset)2 SubstanceEndpointsBundle (ambit2.base.data.substance.SubstanceEndpointsBundle)2 SessionID (ambit2.db.SessionID)2