Search in sources :

Example 1 with BaseAnalyticalObject

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

the class FavoriteDataItemUpgrader method upgradeFavorites.

private void upgradeFavorites(Class<? extends BaseAnalyticalObject> favoriteClass, String favoriteTablename, Class<? extends IdentifiableObject> objectClass, String objectTablename) {
    String linkTablename = favoriteTablename + "_" + objectTablename + "s";
    String selectSql = "select " + favoriteTablename + "id, " + objectTablename + "id from " + linkTablename + " " + "order by " + favoriteTablename + "id, sort_order";
    SqlRowSet rs = jdbcTemplate.queryForRowSet(selectSql);
    while (rs.next()) {
        int rtId = rs.getInt(1);
        int obId = rs.getInt(2);
        BaseAnalyticalObject favorite = idObjectManager.get(favoriteClass, rtId);
        DimensionalItemObject object = (DimensionalItemObject) idObjectManager.get(objectClass, obId);
        DataDimensionItem item = DataDimensionItem.create(object);
        favorite.getDataDimensionItems().add(item);
        idObjectManager.update(favorite);
        log.debug("Upgraded " + favoriteTablename + " " + favorite.getUid() + " for " + objectTablename + " " + object.getUid());
    }
    String dropSql = "drop table " + linkTablename;
    jdbcTemplate.update(dropSql);
    log.info("Update done, dropped table " + linkTablename);
}
Also used : SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseAnalyticalObject(org.hisp.dhis.common.BaseAnalyticalObject) DataDimensionItem(org.hisp.dhis.common.DataDimensionItem)

Example 2 with BaseAnalyticalObject

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

the class AnalyticalObjectObjectBundleHook method preUpdate.

@Override
public void preUpdate(IdentifiableObject object, IdentifiableObject persistedObject, ObjectBundle bundle) {
    if (!AnalyticalObject.class.isInstance(object))
        return;
    BaseAnalyticalObject analyticalObject = (BaseAnalyticalObject) object;
    Schema schema = schemaService.getDynamicSchema(analyticalObject.getClass());
    Session session = sessionFactory.getCurrentSession();
    handleDataDimensionItems(session, schema, analyticalObject, bundle);
    handleCategoryDimensions(session, schema, analyticalObject, bundle);
    handleDataElementDimensions(session, schema, analyticalObject, bundle);
    handleAttributeDimensions(session, schema, analyticalObject, bundle);
    handleProgramIndicatorDimensions(session, schema, analyticalObject, bundle);
}
Also used : BaseAnalyticalObject(org.hisp.dhis.common.BaseAnalyticalObject) Schema(org.hisp.dhis.schema.Schema) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) BaseAnalyticalObject(org.hisp.dhis.common.BaseAnalyticalObject) Session(org.hibernate.Session)

Example 3 with BaseAnalyticalObject

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

the class GeoToolsMapGenerationService method generateMapImageForUser.

@Override
public BufferedImage generateMapImageForUser(Map map, Date date, OrganisationUnit unit, Integer width, Integer height, User user) {
    Assert.isTrue(map != null, "Map cannot be null");
    if (width == null && height == null) {
        width = MapUtils.DEFAULT_MAP_WIDTH;
    }
    InternalMap internalMap = new InternalMap();
    List<MapView> mapViews = new ArrayList<>(map.getMapViews());
    Collections.reverse(mapViews);
    for (MapView mapView : mapViews) {
        InternalMapLayer mapLayer = getSingleInternalMapLayer(mapView, user, date);
        if (mapLayer != null) {
            internalMap.getLayers().add(mapLayer);
        }
    }
    if (internalMap.getLayers().isEmpty()) {
        return null;
    }
    InternalMapLayer dataLayer = internalMap.getFirstDataLayer();
    BufferedImage mapImage = MapUtils.render(internalMap, width, height);
    if (dataLayer == null) {
        mapViews.forEach(BaseAnalyticalObject::clearTransientState);
        return mapImage;
    } else {
        LegendSet legendSet = new LegendSet(dataLayer);
        BufferedImage legendImage = legendSet.render(i18nManager.getI18nFormat());
        BufferedImage titleImage = MapUtils.renderTitle(map.getName(), getImageWidth(legendImage, mapImage));
        mapViews.forEach(BaseAnalyticalObject::clearTransientState);
        return combineLegendAndMapImages(titleImage, legendImage, mapImage);
    }
}
Also used : BaseAnalyticalObject(org.hisp.dhis.common.BaseAnalyticalObject) MapView(org.hisp.dhis.mapping.MapView) BufferedImage(java.awt.image.BufferedImage)

Example 4 with BaseAnalyticalObject

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

the class AnalyticalObjectObjectBundleHook method preCreate.

@Override
public void preCreate(IdentifiableObject object, ObjectBundle bundle) {
    if (!AnalyticalObject.class.isInstance(object))
        return;
    BaseAnalyticalObject analyticalObject = (BaseAnalyticalObject) object;
    Schema schema = schemaService.getDynamicSchema(analyticalObject.getClass());
    Session session = sessionFactory.getCurrentSession();
    handleDataDimensionItems(session, schema, analyticalObject, bundle);
    handleCategoryDimensions(session, schema, analyticalObject, bundle);
    handleDataElementDimensions(session, schema, analyticalObject, bundle);
    handleAttributeDimensions(session, schema, analyticalObject, bundle);
    handleProgramIndicatorDimensions(session, schema, analyticalObject, bundle);
}
Also used : BaseAnalyticalObject(org.hisp.dhis.common.BaseAnalyticalObject) Schema(org.hisp.dhis.schema.Schema) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) BaseAnalyticalObject(org.hisp.dhis.common.BaseAnalyticalObject) Session(org.hibernate.Session)

Example 5 with BaseAnalyticalObject

use of org.hisp.dhis.common.BaseAnalyticalObject 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)

Aggregations

BaseAnalyticalObject (org.hisp.dhis.common.BaseAnalyticalObject)5 AnalyticalObject (org.hisp.dhis.common.AnalyticalObject)3 Schema (org.hisp.dhis.schema.Schema)3 Session (org.hibernate.Session)2 DataDimensionItem (org.hisp.dhis.common.DataDimensionItem)2 Lists (com.google.common.collect.Lists)1 BufferedImage (java.awt.image.BufferedImage)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 Attribute (org.hisp.dhis.attribute.Attribute)1 AttributeService (org.hisp.dhis.attribute.AttributeService)1