Search in sources :

Example 1 with IdentifiableProperty

use of org.hisp.dhis.common.IdentifiableProperty in project dhis2-core by dhis2.

the class DefaultAdxDataService method parseADXDataValueToDxf.

private void parseADXDataValueToDxf(XMLReader adxReader, XMLStreamWriter dxfWriter, Map<String, String> groupAttributes, ImportOptions importOptions, CachingMap<String, DataElement> dataElementMap, IdentifiableObjectCallable<DataElement> dataElementCallable) throws XMLStreamException, AdxException {
    Map<String, String> dvAttributes = adxReader.readAttributes();
    log.debug("Processing data value: " + dvAttributes);
    if (!dvAttributes.containsKey(AdxDataService.DATAELEMENT)) {
        throw new AdxException(AdxDataService.DATAELEMENT + " attribute is required on 'dataValue'");
    }
    if (!dvAttributes.containsKey(AdxDataService.VALUE)) {
        throw new AdxException(AdxDataService.VALUE + " attribute is required on 'dataValue'");
    }
    IdentifiableProperty dataElementIdScheme = importOptions.getIdSchemes().getDataElementIdScheme().getIdentifiableProperty();
    String dataElementStr = trimToNull(dvAttributes.get(AdxDataService.DATAELEMENT));
    final DataElement dataElement = dataElementMap.get(dataElementStr, dataElementCallable.setId(dataElementStr));
    if (dataElement == null) {
        throw new AdxException(dvAttributes.get(AdxDataService.DATAELEMENT), "No matching dataElement");
    }
    // process ADX data value attributes
    if (!dvAttributes.containsKey(AdxDataService.CATOPTCOMBO)) {
        log.debug("No category option combo present");
        //TODO expand to allow for category combos part of DataSetElements.
        DataElementCategoryCombo categoryCombo = dataElement.getDataElementCategoryCombo();
        convertAttributesToDxf(dvAttributes, AdxDataService.CATOPTCOMBO, categoryCombo, dataElementIdScheme);
    }
    // 'annotation' element
    if (!dataElement.getValueType().isNumeric()) {
        adxReader.moveToStartElement(AdxDataService.ANNOTATION, AdxDataService.DATAVALUE);
        if (adxReader.isStartElement(AdxDataService.ANNOTATION)) {
            String textValue = adxReader.getElementValue();
            dvAttributes.put(AdxDataService.VALUE, textValue);
        } else {
            throw new AdxException(dvAttributes.get(AdxDataService.DATAELEMENT), "DataElement expects text annotation");
        }
    }
    log.debug("Processing data value as DXF: " + dvAttributes);
    dxfWriter.writeStartElement("dataValue");
    // write the group attributes through to DXF stream
    for (String attribute : groupAttributes.keySet()) {
        dxfWriter.writeAttribute(attribute, groupAttributes.get(attribute));
    }
    // pass through the remaining attributes to DXF
    for (String attribute : dvAttributes.keySet()) {
        dxfWriter.writeAttribute(attribute, dvAttributes.get(attribute));
    }
    // dataValue
    dxfWriter.writeEndElement();
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) IdentifiableProperty(org.hisp.dhis.common.IdentifiableProperty)

Example 2 with IdentifiableProperty

use of org.hisp.dhis.common.IdentifiableProperty in project dhis2-core by dhis2.

the class DefaultAdxDataService method parseAdxGroupToDxf.

// -------------------------------------------------------------------------
// Utility methods
// -------------------------------------------------------------------------
private List<ImportConflict> parseAdxGroupToDxf(XMLReader adxReader, XMLStreamWriter dxfWriter, ImportOptions importOptions, CachingMap<String, DataSet> dataSetMap, IdentifiableObjectCallable<DataSet> dataSetCallable, CachingMap<String, DataElement> dataElementMap, IdentifiableObjectCallable<DataElement> dataElementCallable) throws XMLStreamException, AdxException {
    List<ImportConflict> adxConflicts = new LinkedList<>();
    IdentifiableProperty dataElementIdScheme = importOptions.getIdSchemes().getDataElementIdScheme().getIdentifiableProperty();
    Map<String, String> groupAttributes = adxReader.readAttributes();
    if (!groupAttributes.containsKey(AdxDataService.PERIOD)) {
        throw new AdxException(AdxDataService.PERIOD + " attribute is required on 'group'");
    }
    if (!groupAttributes.containsKey(AdxDataService.ORGUNIT)) {
        throw new AdxException(AdxDataService.ORGUNIT + " attribute is required on 'group'");
    }
    // translate ADX period to DXF
    String periodStr = groupAttributes.get(AdxDataService.PERIOD);
    groupAttributes.remove(AdxDataService.PERIOD);
    Period period = AdxPeriod.parse(periodStr);
    groupAttributes.put(AdxDataService.PERIOD, period.getIsoDate());
    // process ADX group attributes
    if (!groupAttributes.containsKey(AdxDataService.ATTOPTCOMBO) && groupAttributes.containsKey(AdxDataService.DATASET)) {
        log.debug("No attribute option combo present, check data set for attribute category combo");
        String dataSetStr = trimToNull(groupAttributes.get(AdxDataService.DATASET));
        final DataSet dataSet = dataSetMap.get(dataSetStr, dataSetCallable.setId(dataSetStr));
        if (dataSet == null) {
            throw new AdxException("No data set matching identifier: " + groupAttributes.get(AdxDataService.DATASET));
        }
        groupAttributes.put(AdxDataService.DATASET, dataSet.getUid());
        DataElementCategoryCombo attributeCombo = dataSet.getCategoryCombo();
        convertAttributesToDxf(groupAttributes, AdxDataService.ATTOPTCOMBO, attributeCombo, dataElementIdScheme);
    }
    // process the dataValues
    while (adxReader.moveToStartElement(AdxDataService.DATAVALUE, AdxDataService.GROUP)) {
        try {
            parseADXDataValueToDxf(adxReader, dxfWriter, groupAttributes, importOptions, dataElementMap, dataElementCallable);
        } catch (AdxException ex) {
            adxConflicts.add(ex.getImportConflict());
            log.info("ADX data value conflict: " + ex.getImportConflict());
        }
    }
    return adxConflicts;
}
Also used : DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) DataSet(org.hisp.dhis.dataset.DataSet) IdentifiableProperty(org.hisp.dhis.common.IdentifiableProperty) Period(org.hisp.dhis.period.Period) LinkedList(java.util.LinkedList) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Aggregations

IdentifiableProperty (org.hisp.dhis.common.IdentifiableProperty)2 DataElementCategoryCombo (org.hisp.dhis.dataelement.DataElementCategoryCombo)2 LinkedList (java.util.LinkedList)1 DataElement (org.hisp.dhis.dataelement.DataElement)1 DataSet (org.hisp.dhis.dataset.DataSet)1 ImportConflict (org.hisp.dhis.dxf2.importsummary.ImportConflict)1 Period (org.hisp.dhis.period.Period)1