Search in sources :

Example 61 with CategoryCombo

use of org.hisp.dhis.category.CategoryCombo in project dhis2-core by dhis2.

the class LoadFormAction method getSortedCategoryCombos.

private List<CategoryCombo> getSortedCategoryCombos(Set<CategoryCombo> categoryCombos) {
    List<CategoryCombo> listCategoryCombos = new ArrayList<>(categoryCombos);
    Collections.sort(listCategoryCombos, new CategoryComboSizeNameComparator());
    return listCategoryCombos;
}
Also used : CategoryCombo(org.hisp.dhis.category.CategoryCombo) CategoryComboSizeNameComparator(org.hisp.dhis.category.comparator.CategoryComboSizeNameComparator) ArrayList(java.util.ArrayList)

Example 62 with CategoryCombo

use of org.hisp.dhis.category.CategoryCombo 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.getDataElementCategoryCombo(dataSet));
    orderedCategoryCombos = getCategoryCombos(dataElements, dataSet);
    User currentUser = currentUserService.getCurrentUser();
    for (CategoryCombo categoryCombo : orderedCategoryCombos) {
        List<CategoryOptionCombo> optionCombos = categoryCombo.getSortedOptionCombos();
        orderedCategoryOptionCombos.put(categoryCombo.getId(), optionCombos);
        addOptionAccess(currentUser, optionComboAccessMap, 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<Long, List<CategoryOption>> optionsMap = new HashMap<>();
        for (Category 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<Long, Integer> catRepeat = new HashMap<>();
        Map<Long, Collection<Integer>> colRepeat = new HashMap<>();
        int catColSpan = optionCombos.size();
        for (Category 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.setCode(dataSet.getCode());
        dataSetCopy.setName(dataSet.getName());
        dataSetCopy.setShortName(dataSet.getShortName());
        dataSetCopy.setRenderAsTabs(dataSet.isRenderAsTabs());
        dataSetCopy.setRenderHorizontally(dataSet.isRenderHorizontally());
        dataSetCopy.setDataElementDecoration(dataSet.isDataElementDecoration());
        dataSetCopy.setCompulsoryDataElementOperands(dataSet.getCompulsoryDataElementOperands());
        for (int i = 0; i < orderedCategoryCombos.size(); i++) {
            CategoryCombo 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));
            if (i == 0) {
                section.setIndicators(new ArrayList<>(dataSet.getIndicators()));
            }
        }
        dataSet = dataSetCopy;
        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(dataSet);
        formType = FormType.SECTION_MULTIORG;
    }
    getSectionForm(dataSet);
    return formType.toString();
}
Also used : ListMap(org.hisp.dhis.common.ListMap) AggregateAccessManager(org.hisp.dhis.datavalue.AggregateAccessManager) SectionUtils(org.hisp.dhis.dxf2.util.SectionUtils) SectionOrderComparator(org.hisp.dhis.dataset.comparator.SectionOrderComparator) CategoryOption(org.hisp.dhis.category.CategoryOption) DataSet(org.hisp.dhis.dataset.DataSet) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) I18n(org.hisp.dhis.i18n.I18n) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) DataElement(org.hisp.dhis.dataelement.DataElement) CategoryComboSizeNameComparator(org.hisp.dhis.category.comparator.CategoryComboSizeNameComparator) Map(java.util.Map) User(org.hisp.dhis.user.User) 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) Category(org.hisp.dhis.category.Category) DataEntryFormService(org.hisp.dhis.dataentryform.DataEntryFormService) Collectors(java.util.stream.Collectors) Section(org.hisp.dhis.dataset.Section) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) List(java.util.List) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CategoryCombo(org.hisp.dhis.category.CategoryCombo) CodeGenerator(org.hisp.dhis.common.CodeGenerator) DataSetService(org.hisp.dhis.dataset.DataSetService) Action(com.opensymphony.xwork2.Action) Collections(java.util.Collections) User(org.hisp.dhis.user.User) Category(org.hisp.dhis.category.Category) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DataSet(org.hisp.dhis.dataset.DataSet) ArrayList(java.util.ArrayList) DataElement(org.hisp.dhis.dataelement.DataElement) CategoryCombo(org.hisp.dhis.category.CategoryCombo) ArrayList(java.util.ArrayList) List(java.util.List) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) FormType(org.hisp.dhis.dataset.FormType) Section(org.hisp.dhis.dataset.Section) Collection(java.util.Collection) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 63 with CategoryCombo

use of org.hisp.dhis.category.CategoryCombo 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<>();
    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 " + dataSetCallable.getIdScheme().name().toLowerCase() + " '" + groupAttributes.get(AdxDataService.DATASET) + "'");
        }
        groupAttributes.put(AdxDataService.DATASET, dataSet.getUid());
        CategoryCombo attributeCombo = dataSet.getCategoryCombo();
        convertAttributesToDxf(groupAttributes, AdxDataService.ATTOPTCOMBO, attributeCombo, importOptions.getIdSchemes());
    }
    // process the dataValues
    while (adxReader.moveToStartElement(AdxDataService.DATAVALUE, AdxDataService.GROUP)) {
        try {
            parseAdxDataValueToDxf(adxReader, dxfWriter, groupAttributes, importOptions, dataElementMap, dataElementCallable);
        } catch (AdxException ex) {
            adxConflicts.add(new ImportConflict(ex.getObject(), ex.getMessage()));
            log.info("ADX data value conflict: {} {}", ex.getObject(), ex.getMessage());
        }
    }
    return adxConflicts;
}
Also used : DataSet(org.hisp.dhis.dataset.DataSet) CategoryCombo(org.hisp.dhis.category.CategoryCombo) Period(org.hisp.dhis.period.Period) LinkedList(java.util.LinkedList) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 64 with CategoryCombo

use of org.hisp.dhis.category.CategoryCombo in project dhis2-core by dhis2.

the class DefaultCompleteDataSetRegistrationExchangeService method validateAocMatchesDataSetCc.

private static void validateAocMatchesDataSetCc(MetadataProperties mdProps) throws ImportConflictException {
    // TODO MdCache?
    CategoryCombo aocCC = mdProps.attrOptCombo.getCategoryCombo();
    CategoryCombo 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 : CategoryCombo(org.hisp.dhis.category.CategoryCombo) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 65 with CategoryCombo

use of org.hisp.dhis.category.CategoryCombo in project dhis2-core by dhis2.

the class AttributeOptionComboLoader method bind.

private CategoryOptionCombo bind(String key, ResultSet rs) throws SQLException {
    CategoryOptionCombo categoryOptionCombo = new CategoryOptionCombo();
    categoryOptionCombo.setId(rs.getLong(key));
    categoryOptionCombo.setUid(rs.getString("uid"));
    categoryOptionCombo.setCode(rs.getString("code"));
    categoryOptionCombo.setIgnoreApproval(rs.getBoolean("ignoreapproval"));
    categoryOptionCombo.setName(rs.getString("name"));
    String cat_ids = rs.getString("cat_ids");
    if (!ObjectUtils.isEmpty(cat_ids)) {
        categoryOptionCombo.setCategoryOptions(Arrays.stream(cat_ids.split(",")).map(coid -> getCategoryOption(IdScheme.ID, coid)).collect(Collectors.toSet()));
    }
    CategoryCombo categoryCombo = new CategoryCombo();
    categoryCombo.setUid(rs.getString("cc_uid"));
    categoryCombo.setName(rs.getString("cc_name"));
    categoryOptionCombo.setCategoryCombo(categoryCombo);
    return categoryOptionCombo;
}
Also used : CategoryCombo(org.hisp.dhis.category.CategoryCombo) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Aggregations

CategoryCombo (org.hisp.dhis.category.CategoryCombo)114 Test (org.junit.jupiter.api.Test)66 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)53 CategoryOption (org.hisp.dhis.category.CategoryOption)47 Category (org.hisp.dhis.category.Category)41 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)39 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)36 Program (org.hisp.dhis.program.Program)31 Event (org.hisp.dhis.tracker.domain.Event)27 TrackedEntityType (org.hisp.dhis.trackedentity.TrackedEntityType)23 BeforeEach (org.junit.jupiter.api.BeforeEach)23 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)22 Collectors (java.util.stream.Collectors)21 ProgramInstance (org.hisp.dhis.program.ProgramInstance)21 Collections (java.util.Collections)20 CategoryService (org.hisp.dhis.category.CategoryService)20 CodeGenerator (org.hisp.dhis.common.CodeGenerator)20 ProgramStage (org.hisp.dhis.program.ProgramStage)20 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)20 Sets (com.google.common.collect.Sets)19