Search in sources :

Example 11 with IdentifiableObject

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

the class DefaultPreheatService method handleUniqueProperties.

private Map<String, Map<Object, String>> handleUniqueProperties(Schema schema, List<IdentifiableObject> objects) {
    List<Property> uniqueProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && p.isUnique() && p.isSimple()).collect(Collectors.toList());
    Map<String, Map<Object, String>> map = new HashMap<>();
    for (IdentifiableObject object : objects) {
        uniqueProperties.forEach(property -> {
            if (!map.containsKey(property.getName()))
                map.put(property.getName(), new HashMap<>());
            Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
            if (value != null)
                map.get(property.getName()).put(value, object.getUid());
        });
    }
    return map;
}
Also used : 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) Autowired(org.springframework.beans.factory.annotation.Autowired) TrackedEntityAttributeDimension(org.hisp.dhis.trackedentity.TrackedEntityAttributeDimension) StringUtils(org.apache.commons.lang3.StringUtils) MergeParams(org.hisp.dhis.schema.MergeParams) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) CategoryDimension(org.hisp.dhis.dataelement.CategoryDimension) UserCredentials(org.hisp.dhis.user.UserCredentials) 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) QueryService(org.hisp.dhis.query.QueryService) Property(org.hisp.dhis.schema.Property) Collectors(java.util.stream.Collectors) DataDimensionItem(org.hisp.dhis.common.DataDimensionItem) List(java.util.List) AttributeService(org.hisp.dhis.attribute.AttributeService) Schema(org.hisp.dhis.schema.Schema) LogFactory(org.apache.commons.logging.LogFactory) 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) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) 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) TrackedEntityProgramIndicatorDimension(org.hisp.dhis.trackedentity.TrackedEntityProgramIndicatorDimension) 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) Log(org.apache.commons.logging.Log) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Transactional(org.springframework.transaction.annotation.Transactional) HashMap(java.util.HashMap) 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) Property(org.hisp.dhis.schema.Property) Map(java.util.Map) HashMap(java.util.HashMap) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 12 with IdentifiableObject

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

the class DefaultPreheatService method collectObjectReferences.

@SuppressWarnings("unchecked")
private Map<Class<?>, Map<String, Map<String, Object>>> collectObjectReferences(Map<Class<?>, List<?>> objects) {
    Map<Class<?>, Map<String, Map<String, Object>>> map = new HashMap<>();
    if (objects.isEmpty()) {
        return map;
    }
    Map<Class<?>, List<?>> targets = new HashMap<>();
    // clone objects list, we don't want to modify it
    targets.putAll(objects);
    collectScanTargets(targets);
    for (Class<?> objectClass : targets.keySet()) {
        Schema schema = schemaService.getDynamicSchema(objectClass);
        if (!schema.isIdentifiableObject()) {
            continue;
        }
        List<Property> properties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType())).collect(Collectors.toList());
        List<IdentifiableObject> identifiableObjects = (List<IdentifiableObject>) targets.get(objectClass);
        Map<String, Map<String, Object>> refMap = new HashMap<>();
        map.put(objectClass, refMap);
        for (IdentifiableObject object : identifiableObjects) {
            refMap.put(object.getUid(), new HashMap<>());
            properties.forEach(p -> {
                if (!p.isCollection()) {
                    IdentifiableObject reference = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
                    if (reference != null) {
                        try {
                            IdentifiableObject identifiableObject = (IdentifiableObject) p.getKlass().newInstance();
                            mergeService.merge(new MergeParams<>(reference, identifiableObject));
                            refMap.get(object.getUid()).put(p.getName(), identifiableObject);
                        } catch (InstantiationException | IllegalAccessException ignored) {
                        }
                    }
                } else {
                    Collection<IdentifiableObject> refObjects = ReflectionUtils.newCollectionInstance(p.getKlass());
                    Collection<IdentifiableObject> references = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
                    if (references != null) {
                        for (IdentifiableObject reference : references) {
                            try {
                                IdentifiableObject identifiableObject = (IdentifiableObject) p.getItemKlass().newInstance();
                                mergeService.merge(new MergeParams<>(reference, identifiableObject));
                                refObjects.add(identifiableObject);
                            } catch (InstantiationException | IllegalAccessException ignored) {
                            }
                        }
                    }
                    refMap.get(object.getUid()).put(p.getCollectionName(), refObjects);
                }
            });
        }
    }
    return map;
}
Also used : 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) Autowired(org.springframework.beans.factory.annotation.Autowired) TrackedEntityAttributeDimension(org.hisp.dhis.trackedentity.TrackedEntityAttributeDimension) StringUtils(org.apache.commons.lang3.StringUtils) MergeParams(org.hisp.dhis.schema.MergeParams) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) CategoryDimension(org.hisp.dhis.dataelement.CategoryDimension) UserCredentials(org.hisp.dhis.user.UserCredentials) 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) QueryService(org.hisp.dhis.query.QueryService) Property(org.hisp.dhis.schema.Property) Collectors(java.util.stream.Collectors) DataDimensionItem(org.hisp.dhis.common.DataDimensionItem) List(java.util.List) AttributeService(org.hisp.dhis.attribute.AttributeService) Schema(org.hisp.dhis.schema.Schema) LogFactory(org.apache.commons.logging.LogFactory) 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) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) 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) TrackedEntityProgramIndicatorDimension(org.hisp.dhis.trackedentity.TrackedEntityProgramIndicatorDimension) 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) Log(org.apache.commons.logging.Log) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Transactional(org.springframework.transaction.annotation.Transactional) HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) 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) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) Property(org.hisp.dhis.schema.Property)

Example 13 with IdentifiableObject

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

the class DefaultPreheatService method handleAttributes.

private void handleAttributes(Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objects, Preheat preheat) {
    for (Class<? extends IdentifiableObject> klass : objects.keySet()) {
        List<Attribute> mandatoryAttributes = attributeService.getMandatoryAttributes(klass);
        if (!mandatoryAttributes.isEmpty()) {
            preheat.getMandatoryAttributes().put(klass, new HashSet<>());
        }
        mandatoryAttributes.forEach(attribute -> preheat.getMandatoryAttributes().get(klass).add(attribute.getUid()));
        List<Attribute> uniqueAttributes = attributeService.getUniqueAttributes(klass);
        if (!uniqueAttributes.isEmpty()) {
            preheat.getUniqueAttributes().put(klass, new HashSet<>());
        }
        uniqueAttributes.forEach(attribute -> preheat.getUniqueAttributes().get(klass).add(attribute.getUid()));
        List<? extends IdentifiableObject> uniqueAttributeValues = manager.getAllByAttributes(klass, uniqueAttributes);
        handleUniqueAttributeValues(klass, uniqueAttributeValues, preheat);
    }
    if (objects.containsKey(Attribute.class)) {
        List<IdentifiableObject> attributes = objects.get(Attribute.class);
        for (IdentifiableObject identifiableObject : attributes) {
            Attribute attribute = (Attribute) identifiableObject;
            if (attribute.isMandatory()) {
                attribute.getSupportedClasses().forEach(klass -> {
                    if (!preheat.getMandatoryAttributes().containsKey(klass))
                        preheat.getMandatoryAttributes().put(klass, new HashSet<>());
                    preheat.getMandatoryAttributes().get(klass).add(attribute.getUid());
                });
            }
            if (attribute.isUnique()) {
                attribute.getSupportedClasses().forEach(klass -> {
                    if (!preheat.getUniqueAttributes().containsKey(klass))
                        preheat.getUniqueAttributes().put(klass, new HashSet<>());
                    preheat.getUniqueAttributes().get(klass).add(attribute.getUid());
                });
            }
        }
    }
}
Also used : Attribute(org.hisp.dhis.attribute.Attribute) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) HashSet(java.util.HashSet)

Example 14 with IdentifiableObject

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

the class QueryServiceTest method resultTransformerTest.

@Test
@SuppressWarnings("rawtypes")
public void resultTransformerTest() {
    Query query = Query.from(schemaService.getDynamicSchema(DataElement.class));
    List<? extends IdentifiableObject> objects = queryService.query(query, result1 -> new ArrayList());
    assertEquals(0, objects.size());
    objects = queryService.query(query, result1 -> result1);
    assertEquals(6, objects.size());
}
Also used : IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Year(org.jfree.data.time.Year) ValueType(org.hisp.dhis.common.ValueType) Collection(java.util.Collection) Autowired(org.springframework.beans.factory.annotation.Autowired) SchemaService(org.hisp.dhis.schema.SchemaService) Test(org.junit.Test) ArrayList(java.util.ArrayList) DataElement(org.hisp.dhis.dataelement.DataElement) List(java.util.List) Lists(com.google.common.collect.Lists) MatchMode(org.hisp.dhis.query.operators.MatchMode) DhisSpringTest(org.hisp.dhis.DhisSpringTest) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) Schema(org.hisp.dhis.schema.Schema) Assert(org.junit.Assert) Before(org.junit.Before) DataElement(org.hisp.dhis.dataelement.DataElement) ArrayList(java.util.ArrayList) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 15 with IdentifiableObject

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

the class ObjectBundleServiceFavoritesTest method testDeleteLegendSet.

@Test
public void testDeleteLegendSet() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/favorites/legends.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE_AND_UPDATE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
    assertTrue(validate.getErrorReports().isEmpty());
    objectBundleService.commit(bundle);
    List<LegendSet> legendSets = manager.getAll(LegendSet.class);
    assertEquals(1, legendSets.size());
    LegendSet legendSet = legendSets.get(0);
    assertEquals("fqs276KXCXi", legendSet.getUid());
    assertEquals("ANC Coverage", legendSet.getName());
    assertEquals(7, legendSet.getLegends().size());
    manager.delete(legendSet);
    legendSets = manager.getAll(LegendSet.class);
    assertTrue(legendSets.isEmpty());
}
Also used : ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) List(java.util.List) LegendSet(org.hisp.dhis.legend.LegendSet) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)124 List (java.util.List)76 Test (org.junit.Test)67 DhisSpringTest (org.hisp.dhis.DhisSpringTest)64 ClassPathResource (org.springframework.core.io.ClassPathResource)54 DataElement (org.hisp.dhis.dataelement.DataElement)44 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)39 User (org.hisp.dhis.user.User)37 DataSet (org.hisp.dhis.dataset.DataSet)24 ArrayList (java.util.ArrayList)22 ObjectReport (org.hisp.dhis.feedback.ObjectReport)22 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)22 Schema (org.hisp.dhis.schema.Schema)19 HashMap (java.util.HashMap)18 TypeReport (org.hisp.dhis.feedback.TypeReport)18 Set (java.util.Set)15 ErrorReport (org.hisp.dhis.feedback.ErrorReport)15 PreheatErrorReport (org.hisp.dhis.preheat.PreheatErrorReport)15 Map (java.util.Map)14 Property (org.hisp.dhis.schema.Property)13