Search in sources :

Example 6 with AnalyticalObject

use of org.hisp.dhis.common.AnalyticalObject 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;
    }
    Map<Class<?>, List<?>> targets = new HashMap<>();
    // clone objects list, we don't want to modify it
    targets.putAll(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.getUserGroupAccesses().forEach(uga -> addIdentifiers(map, uga.getUserGroup()));
                identifiableObject.getUserAccesses().forEach(ua -> addIdentifiers(map, ua.getUser()));
                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());
                        });
                    }
                }
            });
            if (AnalyticalObject.class.isInstance(object)) {
                BaseAnalyticalObject analyticalObject = (BaseAnalyticalObject) object;
                List<DataDimensionItem> dataDimensionItems = analyticalObject.getDataDimensionItems();
                List<CategoryDimension> categoryDimensions = analyticalObject.getCategoryDimensions();
                List<TrackedEntityDataElementDimension> trackedEntityDataElementDimensions = analyticalObject.getDataElementDimensions();
                List<TrackedEntityAttributeDimension> attributeDimensions = analyticalObject.getAttributeDimensions();
                List<TrackedEntityProgramIndicatorDimension> programIndicatorDimensions = analyticalObject.getProgramIndicatorDimensions();
                CollectionUtils.nullSafeForEach(dataDimensionItems, dataDimensionItem -> {
                    addIdentifiers(map, dataDimensionItem.getDimensionalItemObject());
                    if (dataDimensionItem.getDataElementOperand() != null) {
                        addIdentifiers(map, dataDimensionItem.getDataElementOperand().getDataElement());
                        addIdentifiers(map, dataDimensionItem.getDataElementOperand().getCategoryOptionCombo());
                    }
                    if (dataDimensionItem.getReportingRate() != null) {
                        addIdentifiers(map, dataDimensionItem.getReportingRate().getDataSet());
                    }
                });
                CollectionUtils.nullSafeForEach(categoryDimensions, categoryDimension -> {
                    addIdentifiers(map, categoryDimension.getDimension());
                    categoryDimension.getItems().forEach(item -> addIdentifiers(map, item));
                });
                CollectionUtils.nullSafeForEach(trackedEntityDataElementDimensions, trackedEntityDataElementDimension -> {
                    addIdentifiers(map, trackedEntityDataElementDimension.getDataElement());
                    addIdentifiers(map, trackedEntityDataElementDimension.getLegendSet());
                });
                CollectionUtils.nullSafeForEach(attributeDimensions, trackedEntityAttributeDimension -> {
                    addIdentifiers(map, trackedEntityAttributeDimension.getAttribute());
                    addIdentifiers(map, trackedEntityAttributeDimension.getLegendSet());
                });
                CollectionUtils.nullSafeForEach(programIndicatorDimensions, programIndicatorDimension -> {
                    addIdentifiers(map, programIndicatorDimension.getProgramIndicator());
                    addIdentifiers(map, programIndicatorDimension.getLegendSet());
                });
            }
        }
    }
    cleanEmptyEntries(uidMap);
    cleanEmptyEntries(codeMap);
    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) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) CategoryDimension(org.hisp.dhis.dataelement.CategoryDimension) TrackedEntityDataElementDimension(org.hisp.dhis.trackedentity.TrackedEntityDataElementDimension) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) BaseAnalyticalObject(org.hisp.dhis.common.BaseAnalyticalObject) List(java.util.List) ArrayList(java.util.ArrayList) DataDimensionItem(org.hisp.dhis.common.DataDimensionItem) Property(org.hisp.dhis.schema.Property) HashSet(java.util.HashSet) TrackedEntityAttributeDimension(org.hisp.dhis.trackedentity.TrackedEntityAttributeDimension) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) TrackedEntityProgramIndicatorDimension(org.hisp.dhis.trackedentity.TrackedEntityProgramIndicatorDimension) 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)

Example 7 with AnalyticalObject

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

the class AnalyticalObjectEmbeddedDimensionUpgrader method upgradeGrupSetDimensions.

@SuppressWarnings("unchecked")
private void upgradeGrupSetDimensions(String favorite, String dimension, String item, Class<? extends AnalyticalObject> favoriteClazz, Class<? extends DimensionalObject> dimensionClass, Class<? extends DimensionalItemObject> itemClass, BiConsumer<BaseDimensionalEmbeddedObject, AnalyticalObject> consumer) {
    String groupSetSqlPattern = "select distinct d.{favorite}id, gsm.{dimension}id " + "from {favorite}_{item}s d " + "inner join {dimension}members gsm on d.{item}id=gsm.{item}id";
    String groupSetSql = TextUtils.replace(groupSetSqlPattern, "{favorite}", favorite, "{dimension}", dimension, "{item}", item);
    log.debug(String.format("Group set SQL: %s", groupSetSql));
    String groupSqlPattern = "select d.{item}id " + "from {favorite}_{item}s d " + "inner join {dimension}members gsm on d.{item}id=gsm.{item}id " + "where d.{favorite}id={favoriteId} " + "and gsm.{dimension}id={dimensionId} " + "order by d.sort_order";
    SqlRowSet groupSetRs = jdbcTemplate.queryForRowSet(groupSetSql);
    while (groupSetRs.next()) {
        int favoriteId = groupSetRs.getInt(1);
        int dimensionId = groupSetRs.getInt(2);
        AnalyticalObject analyticalObject = idObjectManager.get(favoriteClazz, favoriteId);
        DimensionalObject groupSet = idObjectManager.get(dimensionClass, dimensionId);
        Assert.notNull(analyticalObject, String.format("Analytical object not found: %s, class: %s", favoriteId, favoriteClazz));
        Assert.notNull(groupSet, String.format("Group set not found: %s, class: %s", dimensionId, dimensionClass));
        String groupSql = TextUtils.replace(groupSqlPattern, "{favorite}", favorite, "{dimension}", dimension, "{item}", item, "{favoriteId}", String.valueOf(favoriteId), "{dimensionId}", String.valueOf(dimensionId));
        log.debug(String.format("Group SQL: %s", groupSql));
        SqlRowSet groupRs = jdbcTemplate.queryForRowSet(groupSql);
        List<Integer> groupIds = new ArrayList<>();
        while (groupRs.next()) {
            groupIds.add(groupRs.getInt(1));
        }
        List<DimensionalItemObject> groups = (List<DimensionalItemObject>) idObjectManager.getById(itemClass, groupIds);
        Assert.notNull(groups, "Groups cannot be null");
        BaseDimensionalEmbeddedObject embeddedDimension = new BaseDimensionalEmbeddedObject(groupSet, groups);
        consumer.accept(embeddedDimension, analyticalObject);
        idObjectManager.update(analyticalObject);
        log.info(String.format("Added %s group set dimension: %s with groups: %d for favorite: %s", favorite, groupSet.getUid(), groups.size(), analyticalObject.getUid()));
    }
    String dropSql = TextUtils.replace("drop table {favorite}_{item}s", "{favorite}", favorite, "{item}", item);
    jdbcTemplate.update(dropSql);
    log.info(String.format("Dropped table, update done for favorite: %s and dimension: %s", favorite, dimension));
}
Also used : SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

Aggregations

AnalyticalObject (org.hisp.dhis.common.AnalyticalObject)7 ArrayList (java.util.ArrayList)3 List (java.util.List)3 BaseAnalyticalObject (org.hisp.dhis.common.BaseAnalyticalObject)3 Schema (org.hisp.dhis.schema.Schema)3 Log (org.apache.commons.logging.Log)2 LogFactory (org.apache.commons.logging.LogFactory)2 Session (org.hibernate.Session)2 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)2 DimensionalObject (org.hisp.dhis.common.DimensionalObject)2 IdentifiableObjectManager (org.hisp.dhis.common.IdentifiableObjectManager)2 Period (org.hisp.dhis.period.Period)2 ReportTable (org.hisp.dhis.reporttable.ReportTable)2 Lists (com.google.common.collect.Lists)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 BiConsumer (java.util.function.BiConsumer)1