Search in sources :

Example 21 with DataElementCategoryCombo

use of org.hisp.dhis.dataelement.DataElementCategoryCombo in project dhis2-core by dhis2.

the class LoadFormAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    dataSet = dataSetService.getDataSet(dataSetId);
    if (dataSet == null) {
        return INPUT;
    }
    FormType formType = dataSet.getFormType();
    if (formType.isCustom() && dataSet.hasDataEntryForm()) {
        dataEntryForm = dataSet.getDataEntryForm();
        customDataEntryFormCode = dataEntryFormService.prepareDataEntryFormForEntry(dataEntryForm, dataSet, i18n);
        return formType.toString();
    }
    // ---------------------------------------------------------------------
    // Section / default form
    // ---------------------------------------------------------------------
    List<DataElement> dataElements = new ArrayList<>(dataSet.getDataElements());
    if (dataElements.isEmpty()) {
        return INPUT;
    }
    Collections.sort(dataElements);
    orderedDataElements = ListMap.getListMap(dataElements, de -> de.getCategoryCombo(dataSet));
    orderedCategoryCombos = getDataElementCategoryCombos(dataElements, dataSet);
    for (DataElementCategoryCombo categoryCombo : orderedCategoryCombos) {
        List<DataElementCategoryOptionCombo> optionCombos = categoryCombo.getSortedOptionCombos();
        orderedCategoryOptionCombos.put(categoryCombo.getId(), optionCombos);
        // -----------------------------------------------------------------
        // Perform ordering of categories and their options so that they
        // could be displayed as in the paper form. Note that the total
        // number of entry cells to be generated are the multiple of options
        // from each category.
        // -----------------------------------------------------------------
        numberOfTotalColumns.put(categoryCombo.getId(), optionCombos.size());
        orderedCategories.put(categoryCombo.getId(), categoryCombo.getCategories());
        Map<Integer, List<DataElementCategoryOption>> optionsMap = new HashMap<>();
        for (DataElementCategory category : categoryCombo.getCategories()) {
            optionsMap.put(category.getId(), category.getCategoryOptions());
        }
        orderedOptionsMap.put(categoryCombo.getId(), optionsMap);
        // -----------------------------------------------------------------
        // Calculating the number of times each category should be repeated
        // -----------------------------------------------------------------
        Map<Integer, Integer> catRepeat = new HashMap<>();
        Map<Integer, Collection<Integer>> colRepeat = new HashMap<>();
        int catColSpan = optionCombos.size();
        for (DataElementCategory cat : categoryCombo.getCategories()) {
            int categoryOptionSize = cat.getCategoryOptions().size();
            if (categoryOptionSize > 0 && catColSpan >= categoryOptionSize) {
                catColSpan = catColSpan / categoryOptionSize;
                int total = optionCombos.size() / (catColSpan * categoryOptionSize);
                Collection<Integer> cols = new ArrayList<>(total);
                for (int i = 0; i < total; i++) {
                    cols.add(i);
                }
                colRepeat.put(cat.getId(), cols);
                catRepeat.put(cat.getId(), catColSpan);
            }
        }
        catColRepeat.put(categoryCombo.getId(), colRepeat);
    }
    // ---------------------------------------------------------------------
    // Get data entry form
    // ---------------------------------------------------------------------
    DataSet dsOriginal = dataSet;
    if (dataSet.getFormType().isDefault()) {
        DataSet dataSetCopy = new DataSet();
        dataSetCopy.setUid(dataSet.getUid());
        dataSetCopy.setName(dataSet.getName());
        dataSetCopy.setShortName(dataSet.getShortName());
        dataSetCopy.setRenderAsTabs(dataSet.isRenderAsTabs());
        dataSetCopy.setRenderHorizontally(dataSet.isRenderHorizontally());
        dataSetCopy.setDataElementDecoration(dataSet.isDataElementDecoration());
        dataSet = dataSetCopy;
        for (int i = 0; i < orderedCategoryCombos.size(); i++) {
            DataElementCategoryCombo categoryCombo = orderedCategoryCombos.get(i);
            String name = !categoryCombo.isDefault() ? categoryCombo.getName() : dataSetCopy.getName();
            Section section = new Section();
            section.setUid(CodeGenerator.generateUid());
            section.setId(i);
            section.setName(name);
            section.setSortOrder(i);
            section.setDataSet(dataSetCopy);
            dataSetCopy.getSections().add(section);
            section.getDataElements().addAll(orderedDataElements.get(categoryCombo));
            section.setIndicators(new ArrayList<>(dataSet.getIndicators()));
        }
        formType = FormType.SECTION;
    }
    if (CodeGenerator.isValidUid(multiOrganisationUnit)) {
        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(multiOrganisationUnit);
        List<OrganisationUnit> organisationUnitChildren = new ArrayList<>();
        for (OrganisationUnit child : organisationUnit.getChildren()) {
            if (child.getDataSets().contains(dsOriginal)) {
                organisationUnitChildren.add(child);
            }
        }
        Collections.sort(organisationUnitChildren);
        organisationUnits.addAll(organisationUnitChildren);
        getSectionForm(dataElements, dataSet);
        formType = FormType.SECTION_MULTIORG;
    }
    getSectionForm(dataElements, dataSet);
    return formType.toString();
}
Also used : ListMap(org.hisp.dhis.common.ListMap) SectionOrderComparator(org.hisp.dhis.dataset.comparator.SectionOrderComparator) DataSet(org.hisp.dhis.dataset.DataSet) HashMap(java.util.HashMap) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) I18n(org.hisp.dhis.i18n.I18n) ArrayList(java.util.ArrayList) DataElement(org.hisp.dhis.dataelement.DataElement) HashSet(java.util.HashSet) DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) Map(java.util.Map) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) DataEntryForm(org.hisp.dhis.dataentryform.DataEntryForm) FormType(org.hisp.dhis.dataset.FormType) Collection(java.util.Collection) Set(java.util.Set) DataEntryFormService(org.hisp.dhis.dataentryform.DataEntryFormService) DataElementCategoryOption(org.hisp.dhis.dataelement.DataElementCategoryOption) DataElementCategoryComboSizeComparator(org.hisp.dhis.dataelement.comparator.DataElementCategoryComboSizeComparator) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) Section(org.hisp.dhis.dataset.Section) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) List(java.util.List) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Action(com.opensymphony.xwork2.Action) DataSetService(org.hisp.dhis.dataset.DataSetService) Collections(java.util.Collections) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) HashMap(java.util.HashMap) DataSet(org.hisp.dhis.dataset.DataSet) FormType(org.hisp.dhis.dataset.FormType) ArrayList(java.util.ArrayList) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) Section(org.hisp.dhis.dataset.Section) DataElement(org.hisp.dhis.dataelement.DataElement) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 22 with DataElementCategoryCombo

use of org.hisp.dhis.dataelement.DataElementCategoryCombo in project dhis2-core by dhis2.

the class EventQueryParamsTest method testGetDuplicateQueryItems.

@Test
public void testGetDuplicateQueryItems() {
    QueryItem iA = new QueryItem(createDataElement('A', new DataElementCategoryCombo()));
    QueryItem iB = new QueryItem(createDataElement('B', new DataElementCategoryCombo()));
    QueryItem iC = new QueryItem(createDataElement('B', new DataElementCategoryCombo()));
    QueryItem iD = new QueryItem(createDataElement('D', new DataElementCategoryCombo()));
    EventQueryParams params = new EventQueryParams.Builder().addItem(iA).addItem(iB).addItem(iC).addItem(iD).build();
    List<QueryItem> duplicates = params.getDuplicateQueryItems();
    assertEquals(1, duplicates.size());
    assertTrue(duplicates.contains(iC));
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) Test(org.junit.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 23 with DataElementCategoryCombo

use of org.hisp.dhis.dataelement.DataElementCategoryCombo in project dhis2-core by dhis2.

the class DefaultCompleteDataSetRegistrationExchangeService method validateAocMatchesDataSetCc.

private static void validateAocMatchesDataSetCc(MetaDataProperties mdProps) throws ImportConflictException {
    // TODO MdCache?
    DataElementCategoryCombo aocCC = mdProps.attrOptCombo.getCategoryCombo();
    DataElementCategoryCombo dsCc = mdProps.dataSet.getCategoryCombo();
    if (!aocCC.equals(dsCc)) {
        throw new ImportConflictException(new ImportConflict(aocCC.getUid(), String.format("Attribute option combo: %s must have category combo: %s", aocCC.getUid(), dsCc.getUid())));
    }
}
Also used : DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 24 with DataElementCategoryCombo

use of org.hisp.dhis.dataelement.DataElementCategoryCombo 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)

Example 25 with DataElementCategoryCombo

use of org.hisp.dhis.dataelement.DataElementCategoryCombo 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

DataElementCategoryCombo (org.hisp.dhis.dataelement.DataElementCategoryCombo)32 DataElement (org.hisp.dhis.dataelement.DataElement)14 DataSet (org.hisp.dhis.dataset.DataSet)12 ArrayList (java.util.ArrayList)11 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)10 DataElementCategory (org.hisp.dhis.dataelement.DataElementCategory)8 DataElementCategoryOption (org.hisp.dhis.dataelement.DataElementCategoryOption)8 Section (org.hisp.dhis.dataset.Section)7 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)7 HashSet (java.util.HashSet)6 Test (org.junit.Test)6 HashMap (java.util.HashMap)5 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)5 List (java.util.List)4 Map (java.util.Map)4 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)3 SectionOrderComparator (org.hisp.dhis.dataset.comparator.SectionOrderComparator)3 OptionSet (org.hisp.dhis.option.OptionSet)3 Period (org.hisp.dhis.period.Period)3 Program (org.hisp.dhis.program.Program)3