Search in sources :

Example 1 with XMLWriter

use of org.hisp.staxwax.writer.XMLWriter in project dhis2-core by dhis2.

the class GridUtils method toXml.

/**
     * Writes an XML representation of the given Grid to the given OutputStream.
     */
public static void toXml(Grid grid, OutputStream out) {
    XMLWriter writer = XMLFactory.getXMLWriter(out);
    writer.openDocument();
    writer.openElement(ATTR_GRID, ATTR_TITLE, grid.getTitle(), ATTR_SUBTITLE, grid.getSubtitle(), ATTR_WIDTH, String.valueOf(grid.getWidth()), ATTR_HEIGHT, String.valueOf(grid.getHeight()));
    writer.openElement(ATTR_HEADERS);
    for (GridHeader header : grid.getHeaders()) {
        writer.writeElement(ATTR_HEADER, null, ATTR_NAME, header.getName(), ATTR_COLUMN, header.getColumn(), ATTR_TYPE, header.getType(), ATTR_HIDDEN, String.valueOf(header.isHidden()), ATTR_META, String.valueOf(header.isMeta()));
    }
    writer.closeElement();
    writer.openElement(ATTR_ROWS);
    for (List<Object> row : grid.getRows()) {
        writer.openElement(ATTR_ROW);
        for (Object field : row) {
            writer.writeElement(ATTR_FIELD, field != null ? String.valueOf(field) : EMPTY);
        }
        writer.closeElement();
    }
    writer.closeElement();
    writer.closeElement();
    writer.closeDocument();
}
Also used : XMLWriter(org.hisp.staxwax.writer.XMLWriter) GridHeader(org.hisp.dhis.common.GridHeader)

Example 2 with XMLWriter

use of org.hisp.staxwax.writer.XMLWriter in project dhis2-core by dhis2.

the class DefaultAdxDataService method writeDataValueSet.

@Override
public void writeDataValueSet(DataExportParams params, OutputStream out) throws AdxException {
    dataValueSetService.decideAccess(params);
    dataValueSetService.validate(params);
    XMLWriter adxWriter = XMLFactory.getXMLWriter(out);
    adxWriter.openElement(AdxDataService.ROOT);
    adxWriter.writeAttribute("xmlns", AdxDataService.NAMESPACE);
    for (DataSet dataSet : params.getDataSets()) {
        AdxDataSetMetadata metadata = new AdxDataSetMetadata(dataSet);
        DataElementCategoryCombo categoryCombo = dataSet.getCategoryCombo();
        for (DataElementCategoryOptionCombo aoc : categoryCombo.getOptionCombos()) {
            Map<String, String> attributeDimensions = metadata.getExplodedCategoryAttributes(aoc.getId());
            for (OrganisationUnit orgUnit : params.getOrganisationUnits()) {
                for (Period period : params.getPeriods()) {
                    adxWriter.openElement(AdxDataService.GROUP);
                    adxWriter.writeAttribute(AdxDataService.DATASET, dataSet.getCode());
                    adxWriter.writeAttribute(AdxDataService.PERIOD, AdxPeriod.serialize(period));
                    adxWriter.writeAttribute(AdxDataService.ORGUNIT, orgUnit.getCode());
                    for (String attribute : attributeDimensions.keySet()) {
                        adxWriter.writeAttribute(attribute, attributeDimensions.get(attribute));
                    }
                    for (DataValue dv : dataValueService.getDataValues(orgUnit, period, dataSet.getDataElements(), aoc)) {
                        adxWriter.openElement(AdxDataService.DATAVALUE);
                        adxWriter.writeAttribute(AdxDataService.DATAELEMENT, dv.getDataElement().getCode());
                        DataElementCategoryOptionCombo coc = dv.getCategoryOptionCombo();
                        Map<String, String> categoryDimensions = metadata.getExplodedCategoryAttributes(coc.getId());
                        for (String attribute : categoryDimensions.keySet()) {
                            adxWriter.writeAttribute(attribute, categoryDimensions.get(attribute));
                        }
                        if (dv.getDataElement().getValueType().isNumeric()) {
                            adxWriter.writeAttribute(AdxDataService.VALUE, dv.getValue());
                        } else {
                            adxWriter.writeAttribute(AdxDataService.VALUE, "0");
                            adxWriter.openElement(AdxDataService.ANNOTATION);
                            adxWriter.writeCharacters(dv.getValue());
                            // ANNOTATION
                            adxWriter.closeElement();
                        }
                        // DATAVALUE
                        adxWriter.closeElement();
                    }
                    // GROUP
                    adxWriter.closeElement();
                }
            }
        }
    }
    // ADX
    adxWriter.closeElement();
    adxWriter.closeWriter();
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) DataSet(org.hisp.dhis.dataset.DataSet) DataValue(org.hisp.dhis.datavalue.DataValue) Period(org.hisp.dhis.period.Period) XMLWriter(org.hisp.staxwax.writer.XMLWriter) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Aggregations

XMLWriter (org.hisp.staxwax.writer.XMLWriter)2 GridHeader (org.hisp.dhis.common.GridHeader)1 DataElementCategoryCombo (org.hisp.dhis.dataelement.DataElementCategoryCombo)1 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)1 DataSet (org.hisp.dhis.dataset.DataSet)1 DataValue (org.hisp.dhis.datavalue.DataValue)1 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)1 Period (org.hisp.dhis.period.Period)1