Search in sources :

Example 6 with Schema

use of org.hisp.dhis.schema.Schema 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 7 with Schema

use of org.hisp.dhis.schema.Schema 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 8 with Schema

use of org.hisp.dhis.schema.Schema in project dhis2-core by dhis2.

the class QueryServiceTest method sortNameAsc.

@Test
public void sortNameAsc() {
    Schema schema = schemaService.getDynamicSchema(DataElement.class);
    Query query = Query.from(schema);
    query.addOrder(new Order(schema.getProperty("name"), Direction.ASCENDING));
    List<? extends IdentifiableObject> objects = queryService.query(query);
    assertEquals(6, objects.size());
    assertEquals("deabcdefghA", objects.get(0).getUid());
    assertEquals("deabcdefghB", objects.get(1).getUid());
    assertEquals("deabcdefghC", objects.get(2).getUid());
    assertEquals("deabcdefghD", objects.get(3).getUid());
    assertEquals("deabcdefghE", objects.get(4).getUid());
    assertEquals("deabcdefghF", objects.get(5).getUid());
}
Also used : Schema(org.hisp.dhis.schema.Schema) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 9 with Schema

use of org.hisp.dhis.schema.Schema in project dhis2-core by dhis2.

the class QueryServiceTest method sortCreatedDesc.

@Test
public void sortCreatedDesc() {
    Schema schema = schemaService.getDynamicSchema(DataElement.class);
    Query query = Query.from(schema);
    query.addOrder(new Order(schema.getProperty("created"), Direction.DESCENDING));
    List<? extends IdentifiableObject> objects = queryService.query(query);
    assertEquals(6, objects.size());
    assertEquals("deabcdefghF", objects.get(0).getUid());
    assertEquals("deabcdefghE", objects.get(1).getUid());
    assertEquals("deabcdefghD", objects.get(2).getUid());
    assertEquals("deabcdefghC", objects.get(3).getUid());
    assertEquals("deabcdefghB", objects.get(4).getUid());
    assertEquals("deabcdefghA", objects.get(5).getUid());
}
Also used : Schema(org.hisp.dhis.schema.Schema) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 10 with Schema

use of org.hisp.dhis.schema.Schema in project dhis2-core by dhis2.

the class CriteriaQueryEngineTest method sortCreatedAsc.

@Test
public void sortCreatedAsc() {
    Schema schema = schemaService.getDynamicSchema(DataElement.class);
    Query query = Query.from(schema);
    query.addOrder(new Order(schema.getProperty("created"), Direction.ASCENDING));
    List<? extends IdentifiableObject> objects = queryEngine.query(query);
    assertEquals(6, objects.size());
    assertEquals("deabcdefghA", objects.get(0).getUid());
    assertEquals("deabcdefghB", objects.get(1).getUid());
    assertEquals("deabcdefghC", objects.get(2).getUid());
    assertEquals("deabcdefghD", objects.get(3).getUid());
    assertEquals("deabcdefghE", objects.get(4).getUid());
    assertEquals("deabcdefghF", objects.get(5).getUid());
}
Also used : Schema(org.hisp.dhis.schema.Schema) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

Schema (org.hisp.dhis.schema.Schema)149 Authority (org.hisp.dhis.security.Authority)65 Property (org.hisp.dhis.schema.Property)29 ArrayList (java.util.ArrayList)20 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)20 Test (org.junit.Test)16 Collection (java.util.Collection)14 List (java.util.List)13 HashMap (java.util.HashMap)12 DhisSpringTest (org.hisp.dhis.DhisSpringTest)12 EmbeddedObject (org.hisp.dhis.common.EmbeddedObject)12 Map (java.util.Map)10 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)10 User (org.hisp.dhis.user.User)10 AnalyticalObject (org.hisp.dhis.common.AnalyticalObject)9 BaseAnalyticalObject (org.hisp.dhis.common.BaseAnalyticalObject)9 UserCredentials (org.hisp.dhis.user.UserCredentials)9 HashSet (java.util.HashSet)8 Set (java.util.Set)8 Log (org.apache.commons.logging.Log)8