Search in sources :

Example 66 with DataElementOperand

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

the class SchemaIdResponseMapperTest method testGetSchemeIdResponseMapWhenOutputDataElementIdSchemeIsSetToUidForDataValueSet.

@Test
void testGetSchemeIdResponseMapWhenOutputDataElementIdSchemeIsSetToUidForDataValueSet() {
    // Given
    final List<DataElementOperand> dataElementOperandsStub = stubDataElementOperands();
    final OrganisationUnit orUnitStub = stubOrgUnit();
    final Period periodStub = stubPeriod();
    final DataQueryParams theDataQueryParams = stubQueryParams(dataElementOperandsStub, orUnitStub, periodStub, DATA_VALUE_SET);
    theDataQueryParams.setOutputDataElementIdScheme(UID);
    // When
    final Map<String, String> responseMap = schemaIdResponseMapper.getSchemeIdResponseMap(theDataQueryParams);
    // Then
    final String orgUnitUid = orUnitStub.getUid();
    final String periodIsoDate = periodStub.getIsoDate();
    final DataElement dataElementA = dataElementOperandsStub.get(0).getDataElement();
    final DataElement dataElementB = dataElementOperandsStub.get(1).getDataElement();
    final CategoryOptionCombo categoryOptionComboC = dataElementOperandsStub.get(0).getCategoryOptionCombo();
    assertThat(responseMap.get(orgUnitUid), is(equalTo(orUnitStub.getUid())));
    assertThat(responseMap.get(periodIsoDate), is(equalTo(periodStub.getUid())));
    assertThat(responseMap.get(dataElementA.getUid()), is(emptyOrNullString()));
    assertThat(responseMap.get(dataElementB.getUid()), is(emptyOrNullString()));
    assertThat(responseMap.get(categoryOptionComboC.getUid()), is(emptyOrNullString()));
    assertThat(responseMap.get(categoryOptionComboC.getUid()), is(emptyOrNullString()));
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DataElement(org.hisp.dhis.dataelement.DataElement) Period(org.hisp.dhis.period.Period) Matchers.emptyOrNullString(org.hamcrest.Matchers.emptyOrNullString) PeriodType.getPeriodFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodFromIsoString) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) Test(org.junit.jupiter.api.Test)

Example 67 with DataElementOperand

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

the class SchemaIdResponseMapperTest method testGetSchemeIdResponseMapWhenOutputDataElementAndOrgUnitIdSchemeOverrideOutputOrgUnitIdSchemeForDataValueSet.

@Test
void testGetSchemeIdResponseMapWhenOutputDataElementAndOrgUnitIdSchemeOverrideOutputOrgUnitIdSchemeForDataValueSet() {
    // Given
    final List<DataElementOperand> dataElementOperandsStub = stubDataElementOperands();
    final OrganisationUnit orUnitStub = stubOrgUnit();
    final Period periodStub = stubPeriod();
    final DataQueryParams theDataQueryParams = stubQueryParams(dataElementOperandsStub, orUnitStub, periodStub, DATA_VALUE_SET);
    theDataQueryParams.setOutputIdScheme(NAME);
    // Overriding output id schema and setting CODE for Data
    // Element/Operands
    theDataQueryParams.setOutputDataElementIdScheme(CODE);
    // Overriding output id schema and setting ID for Org Unit
    theDataQueryParams.setOutputOrgUnitIdScheme(ID);
    // When
    final Map<String, String> responseMap = schemaIdResponseMapper.getSchemeIdResponseMap(theDataQueryParams);
    // Then
    final String orgUnitUid = orUnitStub.getUid();
    final String periodIsoDate = periodStub.getIsoDate();
    final DataElement dataElementA = dataElementOperandsStub.get(0).getDataElement();
    final DataElement dataElementB = dataElementOperandsStub.get(1).getDataElement();
    final CategoryOptionCombo categoryOptionComboC = dataElementOperandsStub.get(0).getCategoryOptionCombo();
    assertThat(responseMap.get(orgUnitUid), is(equalTo(valueOf(orUnitStub.getId()))));
    assertThat(responseMap.get(periodIsoDate), is(equalTo(periodStub.getName())));
    assertThat(responseMap.get(dataElementA.getUid()), is(equalTo(dataElementA.getCode())));
    assertThat(responseMap.get(dataElementB.getUid()), is(equalTo(dataElementB.getCode())));
    assertThat(responseMap.get(categoryOptionComboC.getUid()), is(equalTo(categoryOptionComboC.getCode())));
    assertThat(responseMap.get(categoryOptionComboC.getUid()), is(equalTo(categoryOptionComboC.getCode())));
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DataElement(org.hisp.dhis.dataelement.DataElement) Period(org.hisp.dhis.period.Period) Matchers.emptyOrNullString(org.hamcrest.Matchers.emptyOrNullString) PeriodType.getPeriodFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodFromIsoString) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) Test(org.junit.jupiter.api.Test)

Example 68 with DataElementOperand

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

the class DataSetDeletionHandler method deleteDataElement.

private void deleteDataElement(DataElement dataElement) {
    Iterator<DataSetElement> elements = dataElement.getDataSetElements().iterator();
    while (elements.hasNext()) {
        DataSetElement element = elements.next();
        elements.remove();
        dataElement.removeDataSetElement(element);
        idObjectManager.updateNoAcl(element.getDataSet());
    }
    List<DataSet> dataSets = idObjectManager.getAllNoAcl(DataSet.class);
    for (DataSet dataSet : dataSets) {
        boolean update = false;
        Iterator<DataElementOperand> operands = dataSet.getCompulsoryDataElementOperands().iterator();
        while (operands.hasNext()) {
            DataElementOperand operand = operands.next();
            if (operand.getDataElement().equals(dataElement)) {
                operands.remove();
                update = true;
            }
        }
        if (update) {
            idObjectManager.updateNoAcl(dataSet);
        }
    }
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand)

Example 69 with DataElementOperand

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

the class DefaultCompleteDataSetRegistrationService method getMissingCompulsoryFields.

@Override
@Transactional(readOnly = true)
public List<DataElementOperand> getMissingCompulsoryFields(DataSet dataSet, Period period, OrganisationUnit organisationUnit, CategoryOptionCombo attributeOptionCombo) {
    List<DataElementOperand> missingDataElementOperands = new ArrayList<>();
    if (!dataSet.getCompulsoryDataElementOperands().isEmpty()) {
        DataExportParams params = new DataExportParams();
        params.setDataElementOperands(dataSet.getCompulsoryDataElementOperands());
        params.setPeriods(Sets.newHashSet(period));
        params.setAttributeOptionCombos(Sets.newHashSet(attributeOptionCombo));
        params.setOrganisationUnits(Sets.newHashSet(organisationUnit));
        List<DeflatedDataValue> deflatedDataValues = dataValueService.getDeflatedDataValues(params);
        MapMapMap<Long, Long, Long, Boolean> dataPresent = new MapMapMap<>();
        for (DeflatedDataValue dv : deflatedDataValues) {
            dataPresent.putEntry(dv.getSourceId(), dv.getDataElementId(), dv.getCategoryOptionComboId(), true);
        }
        User currentUser = currentUserService.getCurrentUser();
        for (DataElementOperand deo : dataSet.getCompulsoryDataElementOperands()) {
            List<String> errors = accessManager.canWrite(currentUser, deo);
            if (!errors.isEmpty()) {
                continue;
            }
            MapMap<Long, Long, Boolean> ouDataPresent = dataPresent.get(organisationUnit.getId());
            if (ouDataPresent != null) {
                Map<Long, Boolean> deDataPresent = ouDataPresent.get(deo.getDataElement().getId());
                if (deDataPresent != null && (deo.getCategoryOptionCombo() == null || deDataPresent.get(deo.getCategoryOptionCombo().getId()) != null)) {
                    continue;
                }
            }
            missingDataElementOperands.add(deo);
        }
    }
    return missingDataElementOperands;
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) User(org.hisp.dhis.user.User) ArrayList(java.util.ArrayList) MapMapMap(org.hisp.dhis.common.MapMapMap) DeflatedDataValue(org.hisp.dhis.datavalue.DeflatedDataValue) DataExportParams(org.hisp.dhis.datavalue.DataExportParams) Transactional(org.springframework.transaction.annotation.Transactional)

Example 70 with DataElementOperand

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

the class DefaultPreheatService method collectReferences.

@SuppressWarnings("unchecked")
private Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences(Map<Class<?>, List<?>> objects) {
    Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> map = new HashMap<>();
    map.put(PreheatIdentifier.UID, new HashMap<>());
    map.put(PreheatIdentifier.CODE, new HashMap<>());
    Map<Class<? extends IdentifiableObject>, Set<String>> uidMap = map.get(PreheatIdentifier.UID);
    Map<Class<? extends IdentifiableObject>, Set<String>> codeMap = map.get(PreheatIdentifier.CODE);
    if (objects.isEmpty()) {
        return map;
    }
    // Clone objects list, we don't want to modify it
    Map<Class<?>, List<?>> targets = new HashMap<>(objects);
    collectScanTargets(targets);
    for (Class<?> klass : targets.keySet()) {
        Schema schema = schemaService.getDynamicSchema(klass);
        List<Property> referenceProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType())).collect(Collectors.toList());
        for (Object object : targets.get(klass)) {
            if (schema.isIdentifiableObject()) {
                IdentifiableObject identifiableObject = (IdentifiableObject) object;
                identifiableObject.getAttributeValues().forEach(av -> addIdentifiers(map, av.getAttribute()));
                identifiableObject.getSharing().getUserGroups().values().forEach(uga -> addIdentifiers(map, uga.toDtoObject().getUserGroup()));
                identifiableObject.getSharing().getUsers().values().forEach(ua -> addIdentifiers(map, ua.toDtoObject().getUser()));
                if (identifiableObject.getCreatedBy() != null) {
                    addIdentifiers(map, identifiableObject.getCreatedBy());
                }
                addIdentifiers(map, identifiableObject);
            }
            referenceProperties.forEach(p -> {
                if (!p.isCollection()) {
                    Class<? extends IdentifiableObject> itemKlass = (Class<? extends IdentifiableObject>) p.getKlass();
                    if (!uidMap.containsKey(itemKlass))
                        uidMap.put(itemKlass, new HashSet<>());
                    if (!codeMap.containsKey(itemKlass))
                        codeMap.put(itemKlass, new HashSet<>());
                    Object reference = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
                    if (reference != null) {
                        IdentifiableObject identifiableObject = (IdentifiableObject) reference;
                        addIdentifiers(map, identifiableObject);
                    }
                } else {
                    Collection<IdentifiableObject> reference = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
                    reference.forEach(identifiableObject -> addIdentifiers(map, identifiableObject));
                    if (DataElementOperand.class.isAssignableFrom(p.getItemKlass())) {
                        CollectionUtils.nullSafeForEach(reference, identifiableObject -> {
                            DataElementOperand dataElementOperand = (DataElementOperand) identifiableObject;
                            addIdentifiers(map, dataElementOperand.getDataElement());
                            addIdentifiers(map, dataElementOperand.getCategoryOptionCombo());
                        });
                    }
                }
            });
            collectAnalyticalObjectReferences(map, object);
        }
    }
    cleanEmptyEntries(uidMap);
    cleanEmptyEntries(codeMap);
    return map;
}
Also used : CategoryDimension(org.hisp.dhis.category.CategoryDimension) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) BaseAnalyticalObject(org.hisp.dhis.common.BaseAnalyticalObject) Restrictions(org.hisp.dhis.query.Restrictions) PeriodService(org.hisp.dhis.period.PeriodService) MergeService(org.hisp.dhis.schema.MergeService) TrackedEntityAttributeDimension(org.hisp.dhis.trackedentity.TrackedEntityAttributeDimension) StringUtils(org.apache.commons.lang3.StringUtils) MergeParams(org.hisp.dhis.schema.MergeParams) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) Map(java.util.Map) Period(org.hisp.dhis.period.Period) Query(org.hisp.dhis.query.Query) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) UserGroup(org.hisp.dhis.user.UserGroup) Collection(java.util.Collection) Set(java.util.Set) SchemaService(org.hisp.dhis.schema.SchemaService) Collectors(java.util.stream.Collectors) QueryService(org.hisp.dhis.query.QueryService) Property(org.hisp.dhis.schema.Property) DataDimensionItem(org.hisp.dhis.common.DataDimensionItem) Sets(com.google.common.collect.Sets) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) AttributeService(org.hisp.dhis.attribute.AttributeService) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) Schema(org.hisp.dhis.schema.Schema) SharingUtils(org.hisp.dhis.util.SharingUtils) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) PropertyType(org.hisp.dhis.schema.PropertyType) DataSetElement(org.hisp.dhis.dataset.DataSetElement) HashMap(java.util.HashMap) Attribute(org.hisp.dhis.attribute.Attribute) Scope(org.springframework.context.annotation.Scope) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) Service(org.springframework.stereotype.Service) User(org.hisp.dhis.user.User) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) TrackedEntityProgramIndicatorDimension(org.hisp.dhis.trackedentity.TrackedEntityProgramIndicatorDimension) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ScopedProxyMode(org.springframework.context.annotation.ScopedProxyMode) Timer(org.hisp.dhis.commons.timer.Timer) CollectionUtils(org.hisp.dhis.commons.collection.CollectionUtils) CurrentUserService(org.hisp.dhis.user.CurrentUserService) PeriodStore(org.hisp.dhis.period.PeriodStore) TrackedEntityDataElementDimension(org.hisp.dhis.trackedentity.TrackedEntityDataElementDimension) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Transactional(org.springframework.transaction.annotation.Transactional) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) List(java.util.List) ArrayList(java.util.ArrayList) BaseAnalyticalObject(org.hisp.dhis.common.BaseAnalyticalObject) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Map(java.util.Map) HashMap(java.util.HashMap) Property(org.hisp.dhis.schema.Property) HashSet(java.util.HashSet)

Aggregations

DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)75 DataElement (org.hisp.dhis.dataelement.DataElement)36 Test (org.junit.jupiter.api.Test)30 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)25 Period (org.hisp.dhis.period.Period)24 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)23 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)19 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)17 Matchers.emptyOrNullString (org.hamcrest.Matchers.emptyOrNullString)14 PeriodType.getPeriodFromIsoString (org.hisp.dhis.period.PeriodType.getPeriodFromIsoString)14 ProgramIndicator (org.hisp.dhis.program.ProgramIndicator)13 Indicator (org.hisp.dhis.indicator.Indicator)11 HashMap (java.util.HashMap)10 DhisSpringTest (org.hisp.dhis.DhisSpringTest)10 IndicatorType (org.hisp.dhis.indicator.IndicatorType)10 DimensionalItemId (org.hisp.dhis.common.DimensionalItemId)9 DataSet (org.hisp.dhis.dataset.DataSet)9 ArrayList (java.util.ArrayList)8 DataValue (org.hisp.dhis.datavalue.DataValue)8 List (java.util.List)7