Search in sources :

Example 11 with QuantityData

use of org.n52.io.response.dataset.quantity.QuantityData in project series-rest-api by 52North.

the class PDFReportGenerator method encodeAndWriteTo.

@Override
public void encodeAndWriteTo(DataCollection<QuantityData> data, OutputStream stream) throws IoParseException {
    try {
        generateOutput(data);
        DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
        Configuration cfg = cfgBuilder.build(document.newInputStream());
        FopFactory fopFactory = new FopFactoryBuilder(baseURI).setConfiguration(cfg).build();
        final String mimeType = MimeType.APPLICATION_PDF.getMimeType();
        Fop fop = fopFactory.newFop(mimeType, stream);
        //FopFactory fopFactory = FopFactory.newInstance(cfg);
        //Fop fop = fopFactory.newFop(APPLICATION_PDF.getMimeType(), stream);
        //FopFactory fopFactory = fopFactoryBuilder.build();
        //Fop fop = fopFactory.newFop(APPLICATION_PDF.getMimeType(), stream);
        // Create PDF via XSLT transformation
        TransformerFactory transFact = TransformerFactory.newInstance();
        StreamSource transformationRule = getTransforamtionRule();
        Transformer transformer = transFact.newTransformer(transformationRule);
        Source source = new StreamSource(document.newInputStream());
        Result result = new SAXResult(fop.getDefaultHandler());
        if (LOGGER.isDebugEnabled()) {
            try {
                File tempFile = File.createTempFile(TEMP_FILE_PREFIX, ".xml");
                StreamResult debugResult = new StreamResult(tempFile);
                transformer.transform(source, debugResult);
                String xslResult = XmlObject.Factory.parse(tempFile).xmlText();
                LOGGER.debug("xsl-fo input (locale '{}'): {}", i18n.getTwoDigitsLanguageCode(), xslResult);
            } catch (IOException | TransformerException | XmlException e) {
                LOGGER.error("Could not debug XSL result output!", e);
            }
        }
        // XXX debug, diagram is not embedded
        transformer.transform(source, result);
    } catch (FOPException e) {
        throw new IoParseException("Failed to create Formatting Object Processor (FOP)", e);
    } catch (SAXException | ConfigurationException | IOException e) {
        throw new IoParseException("Failed to read config for Formatting Object Processor (FOP)", e);
    } catch (TransformerConfigurationException e) {
        throw new IoParseException("Invalid transform configuration. Inspect xslt!", e);
    } catch (TransformerException e) {
        throw new IoParseException("Could not generate PDF report!", e);
    }
}
Also used : IoParseException(org.n52.io.IoParseException) DefaultConfigurationBuilder(org.apache.avalon.framework.configuration.DefaultConfigurationBuilder) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Configuration(org.apache.avalon.framework.configuration.Configuration) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Fop(org.apache.fop.apps.Fop) StreamSource(javax.xml.transform.stream.StreamSource) FopFactory(org.apache.fop.apps.FopFactory) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) SAXException(org.xml.sax.SAXException) FopFactoryBuilder(org.apache.fop.apps.FopFactoryBuilder) FOPException(org.apache.fop.apps.FOPException) SAXResult(javax.xml.transform.sax.SAXResult) ConfigurationException(org.apache.avalon.framework.configuration.ConfigurationException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) XmlException(org.apache.xmlbeans.XmlException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 12 with QuantityData

use of org.n52.io.response.dataset.quantity.QuantityData in project series-rest-api by 52North.

the class PDFReportGenerator method generateOutput.

public void generateOutput(DataCollection<QuantityData> data) throws IoParseException {
    try {
        generateTimeseriesChart(data);
        generateTimeseriesMetadata();
    } catch (IOException e) {
        throw new IoParseException("Error handling (temp) file!", e);
    }
}
Also used : IoParseException(org.n52.io.IoParseException) IOException(java.io.IOException)

Example 13 with QuantityData

use of org.n52.io.response.dataset.quantity.QuantityData in project series-rest-api by 52North.

the class PDFReportGenerator method addDataTable.

private void addDataTable(TimeSeries timeseries, TimeseriesMetadataOutput metadata, TvpDataCollection<QuantityData> dataCollection) {
    TableType dataTable = timeseries.addNewTable();
    // TODO add language context
    dataTable.setLeftColHeader("Date");
    dataTable.setRightColHeader(createValueTableHeader(metadata));
    QuantityData data = dataCollection.getSeries(metadata.getId());
    for (QuantityValue valueEntry : data.getValues()) {
        Entry entry = dataTable.addNewEntry();
        // TODO update TableType schema to allow start/end time
        entry.setTime(new DateTime(valueEntry.getTimestamp()).toString());
        entry.setValue(Double.toString(valueEntry.getValue()));
    }
}
Also used : Entry(org.n52.oxf.TableType.Entry) TableType(org.n52.oxf.TableType) QuantityValue(org.n52.io.response.dataset.quantity.QuantityValue) QuantityData(org.n52.io.response.dataset.quantity.QuantityData) DateTime(org.joda.time.DateTime)

Example 14 with QuantityData

use of org.n52.io.response.dataset.quantity.QuantityData in project series-rest-api by 52North.

the class TimeseriesDataController method getTimeseriesData.

private DataCollection<QuantityData> getTimeseriesData(RequestSimpleParameterSet parameters) {
    Stopwatch stopwatch = Stopwatch.startStopwatch();
    DataCollection<QuantityData> timeseriesData = parameters.isGeneralize() ? new GeneralizingQuantityService(timeseriesDataService).getData(parameters) : timeseriesDataService.getData(parameters);
    LOGGER.debug("Processing request took {} seconds.", stopwatch.stopInSeconds());
    return timeseriesData;
}
Also used : Stopwatch(org.n52.web.common.Stopwatch) GeneralizingQuantityService(org.n52.io.quantity.generalize.GeneralizingQuantityService) QuantityData(org.n52.io.response.dataset.quantity.QuantityData)

Example 15 with QuantityData

use of org.n52.io.response.dataset.quantity.QuantityData in project series-rest-api by 52North.

the class QuantityIoFactory method createHandler.

@Override
public IoHandler<QuantityData> createHandler(String outputMimeType) {
    IoParameters parameters = getParameters();
    MimeType mimeType = MimeType.toInstance(outputMimeType);
    if (mimeType == MimeType.IMAGE_PNG) {
        return createMultiChartRenderer(mimeType);
    } else if (mimeType == MimeType.APPLICATION_PDF) {
        ChartIoHandler imgRenderer = createMultiChartRenderer(mimeType);
        PDFReportGenerator reportGenerator = new PDFReportGenerator(getRequestParameters(), createProcessChain(), imgRenderer);
        reportGenerator.setBaseURI(getBasePath());
        return reportGenerator;
    } else if (mimeType == MimeType.TEXT_CSV || mimeType == MimeType.APPLICATION_ZIP) {
        QuantityCsvIoHandler handler = new QuantityCsvIoHandler(getRequestParameters(), createProcessChain(), getMetadatas());
        handler.setTokenSeparator(parameters.getOther("tokenSeparator"));
        boolean zipOutput = parameters.getAsBoolean(MimeType.APPLICATION_ZIP.name());
        handler.setZipOutput(zipOutput || mimeType == MimeType.APPLICATION_ZIP);
        boolean byteOderMark = Boolean.parseBoolean(parameters.getOther("bom"));
        handler.setIncludeByteOrderMark(byteOderMark);
        return handler;
    }
    String msg = "The requested media type '" + outputMimeType + "' is not supported.";
    IllegalArgumentException exception = new IllegalArgumentException(msg);
    throw exception;
}
Also used : QuantityCsvIoHandler(org.n52.io.quantity.csv.QuantityCsvIoHandler) ChartIoHandler(org.n52.io.quantity.img.ChartIoHandler) IoParameters(org.n52.io.request.IoParameters) PDFReportGenerator(org.n52.io.quantity.report.PDFReportGenerator) MimeType(org.n52.io.MimeType)

Aggregations

QuantityData (org.n52.io.response.dataset.quantity.QuantityData)14 QuantityValue (org.n52.io.response.dataset.quantity.QuantityValue)7 IOException (java.io.IOException)3 IoParseException (org.n52.io.IoParseException)3 QuantityDatasetMetadata (org.n52.io.response.dataset.quantity.QuantityDatasetMetadata)3 Coordinate (com.vividsolutions.jts.geom.Coordinate)2 ArrayList (java.util.ArrayList)2 DateTime (org.joda.time.DateTime)2 GeneralizingQuantityService (org.n52.io.quantity.generalize.GeneralizingQuantityService)2 DatasetOutput (org.n52.io.response.dataset.DatasetOutput)2 TvpDataCollection (org.n52.io.series.TvpDataCollection)2 Line2D (java.awt.geom.Line2D)1 File (java.io.File)1 Result (javax.xml.transform.Result)1 Source (javax.xml.transform.Source)1 Transformer (javax.xml.transform.Transformer)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 SAXResult (javax.xml.transform.sax.SAXResult)1