Search in sources :

Example 1 with OutputStreamConvertor

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

the class AssayTemplateResource method createConvertor.

@Override
public IProcessor<Q, Representation> createConvertor(Variant variant) throws AmbitException, ResourceException {
    /* workaround for clients not being able to set accept headers */
    Form acceptform = getResourceRef(getRequest()).getQueryAsForm();
    String media = acceptform.getFirstValue("accept-header");
    if (media != null)
        variant.setMediaType(new MediaType(media));
    if (variant.getMediaType().equals(MediaType.APPLICATION_MSOFFICE_XLSX)) {
        AssayTemplateEntrySpreadsheetReporter reporter = new AssayTemplateEntrySpreadsheetReporter(settings.getTemplatesType(), settings.getLayout_raw_data(), settings.getNumber_of_experiments(), settings.getNumber_of_endpoints(), settings.getNumber_of_replicates(), settings.getNumber_of_timepoints(), settings.getNumber_of_concentration());
        return new OutputStreamConvertor(reporter, MediaType.APPLICATION_MSOFFICE_XLSX, settings.getOutputFileName()) {

            protected void setDisposition(Representation rep) {
                super.setDisposition(rep);
                rep.setDownloadName(settings.getOutputFileName());
            }
        };
    } else {
        // json by default
        return new StringConvertor(new AssayTemplateEntryJSONReporter(getRequest()), MediaType.APPLICATION_JSON, null);
    }
}
Also used : OutputStreamConvertor(ambit2.rest.OutputStreamConvertor) Form(org.restlet.data.Form) StringConvertor(net.idea.restnet.c.StringConvertor) MediaType(org.restlet.data.MediaType) Representation(org.restlet.representation.Representation)

Example 2 with OutputStreamConvertor

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

the class SubstanceResource method createConvertor.

@Override
public IProcessor<Q, Representation> createConvertor(Variant variant) throws AmbitException, ResourceException {
    /* workaround for clients not being able to set accept headers */
    Form acceptform = getResourceRef(getRequest()).getQueryAsForm();
    String configResource = String.format("config-%s.js", ((IFreeMarkerApplication) getApplication()).getProfile());
    try {
        retrieveStudySummary = Boolean.parseBoolean(acceptform.getFirstValue("studysummary"));
    } catch (Exception x) {
        retrieveStudySummary = false;
    }
    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_URI_LIST)) {
        QueryURIReporter r = (QueryURIReporter) getURIReporter(getRequest());
        return new StringConvertor(r, MediaType.TEXT_URI_LIST, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.IMAGE_PNG)) {
        Dimension d = new Dimension(250, 250);
        try {
            d.width = Integer.parseInt(acceptform.getFirstValue("w").toString());
        } catch (Exception x) {
        }
        try {
            d.height = Integer.parseInt(acceptform.getFirstValue("h").toString());
        } catch (Exception x) {
        }
        return new ImageConvertor(new ImageReporter(variant.getMediaType().getMainType(), variant.getMediaType().getSubType(), d), variant.getMediaType());
    } else if (variant.getMediaType().equals(MediaType.TEXT_CSV)) {
        SubstanceCSVReporter csvreporter = new SubstanceCSVReporter(getRequest(), bundles);
        return new OutputWriterConvertor<SubstanceRecord, Q>(csvreporter, MediaType.TEXT_CSV, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_MSOFFICE_XLSX)) {
        SubstanceRecordXLSXReporter xlsxreporter = new SubstanceRecordXLSXReporter(getRequest().getRootRef().toString(), false, bundles, configResource);
        return new OutputStreamConvertor<SubstanceRecord, Q>(xlsxreporter, MediaType.APPLICATION_MSOFFICE_XLSX, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_EXCEL)) {
        SubstanceRecordXLSXReporter xlsxreporter = new SubstanceRecordXLSXReporter(getRequest().getRootRef().toString(), true, bundles, configResource);
        return new OutputStreamConvertor<SubstanceRecord, Q>(xlsxreporter, MediaType.APPLICATION_EXCEL, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_RDF_XML) || // filenamePrefix);
    variant.getMediaType().equals(MediaType.APPLICATION_RDF_TURTLE) || variant.getMediaType().equals(MediaType.TEXT_RDF_NTRIPLES) || variant.getMediaType().equals(MediaType.TEXT_RDF_N3) || variant.getMediaType().equals(ChemicalMediaType.APPLICATION_JSONLD)) {
        SubstanceRecordAnnotationProcessor annotator = null;
        try {
            annotator = new SubstanceRecordAnnotationProcessor(new File(((AmbitFreeMarkerApplication) getApplication()).getProperties().getMapFolder()), false);
        } catch (Exception x) {
            Logger.getGlobal().log(Level.WARNING, x.getMessage());
            annotator = null;
        }
        return new RDFJenaConvertor(new SubstanceRDFReporter(getRequest(), variant.getMediaType(), annotator), variant.getMediaType(), filenamePrefix) {

            @Override
            protected OntModel createOutput(IQueryRetrieval query) throws AmbitException {
                try {
                    OntModel jenaModel = OT.createModel();
                    jenaModel.setNsPrefix("sio", "http://semanticscience.org/resource/");
                    jenaModel.setNsPrefix("obo", "http://purl.obolibrary.org/obo/");
                    jenaModel.setNsPrefix("bao", "http://www.bioassayontology.org/bao#");
                    jenaModel.setNsPrefix("npo", "http://purl.bioontology.org/ontology/npo/");
                    jenaModel.setNsPrefix("enm", "http://purl.enanomapper.org/onto/");
                    return jenaModel;
                } catch (Exception x) {
                    throw new AmbitException(x);
                }
            }
        };
    } else if (variant.getMediaType().equals(ISAJSON)) {
        return new OutputStreamConvertor<SubstanceRecord, Q>(createISAReporter(getRequest()), MediaType.APPLICATION_JAVASCRIPT, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_JAVASCRIPT)) {
        return createJSONReporter(filenamePrefix);
    } else
        // json by default
        return createJSONReporter(filenamePrefix);
}
Also used : Form(org.restlet.data.Form) OutputWriterConvertor(net.idea.restnet.db.convertors.OutputWriterConvertor) StringConvertor(net.idea.restnet.c.StringConvertor) ImageReporter(ambit2.db.reporters.ImageReporter) SubstanceRecord(ambit2.base.data.SubstanceRecord) Dimension(java.awt.Dimension) IQueryRetrieval(net.idea.modbcum.i.IQueryRetrieval) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) PatternSyntaxException(java.util.regex.PatternSyntaxException) SQLException(java.sql.SQLException) ProcessorException(ambit2.base.processors.ProcessorException) OutputStreamConvertor(ambit2.rest.OutputStreamConvertor) SubstanceRecordXLSXReporter(ambit2.db.reporters.xlsx.SubstanceRecordXLSXReporter) ImageConvertor(ambit2.rest.ImageConvertor) SubstanceRecordAnnotationProcessor(ambit2.base.ro.SubstanceRecordAnnotationProcessor) ChemicalMediaType(net.idea.restnet.c.ChemicalMediaType) MediaType(org.restlet.data.MediaType) OntModel(com.hp.hpl.jena.ontology.OntModel) RDFJenaConvertor(ambit2.rest.RDFJenaConvertor) QueryURIReporter(net.idea.restnet.db.QueryURIReporter) File(java.io.File) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 3 with OutputStreamConvertor

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

the class SubstanceDatasetResource method createXLSXReporter.

protected IProcessor<Q, Representation> createXLSXReporter(MediaType media, boolean hssf, String filenamePrefix) {
    groupProperties.add(new SubstancePublicName());
    groupProperties.add(new SubstanceName());
    groupProperties.add(new SubstanceUUID());
    groupProperties.add(new SubstanceOwner());
    String jsonpcallback = getParams().getFirstValue("jsonp");
    if (jsonpcallback == null)
        jsonpcallback = getParams().getFirstValue("callback");
    String configResource = String.format("config-%s.js", ((IFreeMarkerApplication) getApplication()).getProfile());
    return new OutputStreamConvertor(new StructureRecordXLSXReporter(getRequest().getRootRef().toString(), hssf, getTemplate(), getGroupProperties(), getBundles(), null, true, configResource) {

        @Override
        protected void configurePropertyProcessors() {
            getCompositionProcessors(getProcessors());
            getProcessors().add(getPropertyProcessors(false, false));
        }

        @Override
        protected void initColumns(int afterCol) {
            for (I5_ROOT_OBJECTS section : I5_ROOT_OBJECTS.values()) {
                if (section.isIUCLID5() && section.isScientificPart() && section.isSupported() && !section.isNanoMaterialTemplate()) {
                    int last = mergedProperties.size();
                    mergedProperties.put("http://www.opentox.org/echaEndpoints.owl#" + section.name(), last);
                    Cell hcell = sheet.getRow(0).createCell(last + afterCol);
                    hcell.setCellStyle(hstyle);
                    hcell.setCellType(CellType.STRING);
                    hcell.setCellValue(section.getNumber() + ". " + section.getTitle());
                    sheet.autoSizeColumn(hcell.getColumnIndex(), true);
                    // initially hide all columns, unhide if data is added
                    // to
                    sheet.setColumnHidden(hcell.getColumnIndex(), true);
                }
            }
        }
    }, media, filenamePrefix);
}
Also used : SubstanceName(ambit2.base.data.substance.SubstanceName) OutputStreamConvertor(ambit2.rest.OutputStreamConvertor) SubstanceOwner(ambit2.base.data.substance.SubstanceOwner) SubstanceUUID(ambit2.base.data.substance.SubstanceUUID) I5_ROOT_OBJECTS(net.idea.i5.io.I5_ROOT_OBJECTS) StructureRecordXLSXReporter(ambit2.db.reporters.xlsx.StructureRecordXLSXReporter) Cell(org.apache.poi.ss.usermodel.Cell) SubstancePublicName(ambit2.base.data.substance.SubstancePublicName)

Example 4 with OutputStreamConvertor

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

the class BundleSubstanceResource method createConvertor.

@Override
public IProcessor<Q, Representation> createConvertor(Variant variant) throws AmbitException, ResourceException {
    /* workaround for clients not being able to set accept headers */
    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.APPLICATION_JSON)) {
        SubstanceJSONReporter cmpreporter = new SubstanceJSONReporter(getRequest(), null, bundles, null, false);
        return new OutputWriterConvertor<SubstanceRecord, Q>(cmpreporter, MediaType.APPLICATION_JSON, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.IMAGE_PNG)) {
        Dimension d = new Dimension(250, 250);
        try {
            d.width = Integer.parseInt(acceptform.getFirstValue("w").toString());
        } catch (Exception x) {
        }
        try {
            d.height = Integer.parseInt(acceptform.getFirstValue("h").toString());
        } catch (Exception x) {
        }
        return new ImageConvertor(new ImageReporter(variant.getMediaType().getMainType(), variant.getMediaType().getSubType(), d), variant.getMediaType());
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_MSOFFICE_XLSX)) {
        return new OutputStreamConvertor<SubstanceRecord, Q>(createXLSXReporter(getRequest(), false), MediaType.APPLICATION_MSOFFICE_XLSX, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_EXCEL)) {
        return new OutputStreamConvertor<SubstanceRecord, Q>(createXLSXReporter(getRequest(), false), MediaType.APPLICATION_EXCEL, filenamePrefix);
    } else if (isRDFMediaType(variant.getMediaType())) {
        return new RDFJenaConvertor(new SubstanceRDFReporter(getRequest(), variant.getMediaType()), variant.getMediaType(), filenamePrefix) {

            @Override
            protected OntModel createOutput(IQueryRetrieval query) throws AmbitException {
                try {
                    OntModel jenaModel = OT.createModel();
                    jenaModel.setNsPrefix("sio", "http://semanticscience.org/resource/");
                    jenaModel.setNsPrefix("obo", "http://purl.obolibrary.org/obo/");
                    jenaModel.setNsPrefix("bao", "http://www.bioassayontology.org/bao#");
                    jenaModel.setNsPrefix("npo", "http://purl.bioontology.org/ontology/npo/");
                    jenaModel.setNsPrefix("enm", "http://purl.enanomapper.org/onto/");
                    return jenaModel;
                } catch (Exception x) {
                    throw new AmbitException(x);
                }
            }
        };
    } else if (variant.getMediaType().equals(MediaType.TEXT_CSV)) {
        return new OutputWriterConvertor<SubstanceRecord, Q>(new SubstanceCSVReporter(getRequest(), bundles), MediaType.TEXT_CSV, filenamePrefix);
    } else if (variant.getMediaType().equals(MediaType.APPLICATION_JAVASCRIPT)) {
        String jsonpcallback = getParams().getFirstValue("jsonp");
        if (jsonpcallback == null)
            jsonpcallback = getParams().getFirstValue("callback");
        return new OutputWriterConvertor<SubstanceRecord, Q>(createJSONReporter(getRequest(), jsonpcallback), MediaType.APPLICATION_JAVASCRIPT, filenamePrefix);
    } else if (variant.getMediaType().equals(ISAJSON)) {
        return new OutputStreamConvertor<SubstanceRecord, Q>(createISAReporter(getRequest()), MediaType.APPLICATION_JAVASCRIPT, filenamePrefix);
    } else {
        // json by default
        return new OutputWriterConvertor<SubstanceRecord, Q>(createJSONReporter(getRequest(), null), MediaType.APPLICATION_JSON, filenamePrefix);
    }
}
Also used : SubstanceCSVReporter(ambit2.rest.substance.SubstanceCSVReporter) Form(org.restlet.data.Form) OutputWriterConvertor(net.idea.restnet.db.convertors.OutputWriterConvertor) ImageReporter(ambit2.db.reporters.ImageReporter) SubstanceRecord(ambit2.base.data.SubstanceRecord) Dimension(java.awt.Dimension) IQueryRetrieval(net.idea.modbcum.i.IQueryRetrieval) SubstanceRDFReporter(ambit2.rest.substance.SubstanceRDFReporter) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) ResourceException(org.restlet.resource.ResourceException) SubstanceJSONReporter(ambit2.rest.substance.SubstanceJSONReporter) OutputStreamConvertor(ambit2.rest.OutputStreamConvertor) ImageConvertor(ambit2.rest.ImageConvertor) ChemicalMediaType(net.idea.restnet.c.ChemicalMediaType) MediaType(org.restlet.data.MediaType) OntModel(com.hp.hpl.jena.ontology.OntModel) RDFJenaConvertor(ambit2.rest.RDFJenaConvertor) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Aggregations

OutputStreamConvertor (ambit2.rest.OutputStreamConvertor)4 Form (org.restlet.data.Form)3 MediaType (org.restlet.data.MediaType)3 SubstanceRecord (ambit2.base.data.SubstanceRecord)2 ImageReporter (ambit2.db.reporters.ImageReporter)2 ImageConvertor (ambit2.rest.ImageConvertor)2 RDFJenaConvertor (ambit2.rest.RDFJenaConvertor)2 OntModel (com.hp.hpl.jena.ontology.OntModel)2 Dimension (java.awt.Dimension)2 IQueryRetrieval (net.idea.modbcum.i.IQueryRetrieval)2 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)2 ChemicalMediaType (net.idea.restnet.c.ChemicalMediaType)2 StringConvertor (net.idea.restnet.c.StringConvertor)2 OutputWriterConvertor (net.idea.restnet.db.convertors.OutputWriterConvertor)2 ResourceException (org.restlet.resource.ResourceException)2 SubstanceName (ambit2.base.data.substance.SubstanceName)1 SubstanceOwner (ambit2.base.data.substance.SubstanceOwner)1 SubstancePublicName (ambit2.base.data.substance.SubstancePublicName)1 SubstanceUUID (ambit2.base.data.substance.SubstanceUUID)1 ProcessorException (ambit2.base.processors.ProcessorException)1