Search in sources :

Example 1 with AnalyticsFinancialYearStartKey

use of org.hisp.dhis.analytics.AnalyticsFinancialYearStartKey in project dhis2-core by dhis2.

the class DefaultDataQueryService method getDimension.

// TODO Optimize so that org unit levels + boundary are used in query
// instead of fetching all org units one by one.
@Override
public DimensionalObject getDimension(String dimension, List<String> items, Date relativePeriodDate, List<OrganisationUnit> userOrgUnits, I18nFormat format, boolean allowNull, boolean allowAllPeriodItems, IdScheme inputIdScheme) {
    final boolean allItems = items.isEmpty();
    User user = currentUserService.getCurrentUser();
    if (DATA_X_DIM_ID.equals(dimension)) {
        List<DimensionalItemObject> dataDimensionItems = new ArrayList<>();
        DimensionItemKeywords dimensionalKeywords = new DimensionItemKeywords();
        for (String uid : items) {
            if (// DATA ELEMENT GROUP
            uid.startsWith(KEY_DE_GROUP)) {
                String groupUid = DimensionalObjectUtils.getUidFromGroupParam(uid);
                DataElementGroup group = idObjectManager.getObject(DataElementGroup.class, inputIdScheme, groupUid);
                if (group != null) {
                    dataDimensionItems.addAll(group.getMembers());
                    dimensionalKeywords.addKeyword(group);
                }
            } else if (// INDICATOR GROUP
            uid.startsWith(KEY_IN_GROUP)) {
                String groupUid = DimensionalObjectUtils.getUidFromGroupParam(uid);
                IndicatorGroup group = idObjectManager.getObject(IndicatorGroup.class, inputIdScheme, groupUid);
                if (group != null) {
                    dataDimensionItems.addAll(group.getMembers());
                    dimensionalKeywords.addKeyword(group);
                }
            } else {
                DimensionalItemObject dimItemObject = dimensionService.getDataDimensionalItemObject(inputIdScheme, uid);
                if (dimItemObject != null) {
                    dataDimensionItems.add(dimItemObject);
                }
            }
        }
        if (dataDimensionItems.isEmpty()) {
            throwIllegalQueryEx(ErrorCode.E7124, DimensionalObject.DATA_X_DIM_ID);
        }
        return new BaseDimensionalObject(dimension, DimensionType.DATA_X, null, DISPLAY_NAME_DATA_X, dataDimensionItems, dimensionalKeywords);
    } else if (CATEGORYOPTIONCOMBO_DIM_ID.equals(dimension)) {
        return new BaseDimensionalObject(dimension, DimensionType.CATEGORY_OPTION_COMBO, null, DISPLAY_NAME_CATEGORYOPTIONCOMBO, getCategoryOptionComboList(items, inputIdScheme));
    } else if (ATTRIBUTEOPTIONCOMBO_DIM_ID.equals(dimension)) {
        return new BaseDimensionalObject(dimension, DimensionType.ATTRIBUTE_OPTION_COMBO, null, DISPLAY_NAME_ATTRIBUTEOPTIONCOMBO, getCategoryOptionComboList(items, inputIdScheme));
    } else if (PERIOD_DIM_ID.equals(dimension)) {
        Calendar calendar = PeriodType.getCalendar();
        I18n i18n = i18nManager.getI18n();
        List<Period> periods = new ArrayList<>();
        DimensionItemKeywords dimensionalKeywords = new DimensionItemKeywords();
        AnalyticsFinancialYearStartKey financialYearStart = systemSettingManager.getSystemSetting(SettingKey.ANALYTICS_FINANCIAL_YEAR_START, AnalyticsFinancialYearStartKey.class);
        boolean containsRelativePeriods = false;
        for (String isoPeriod : items) {
            // Contains isoPeriod and timeField
            IsoPeriodHolder isoPeriodHolder = IsoPeriodHolder.of(isoPeriod);
            if (RelativePeriodEnum.contains(isoPeriodHolder.getIsoPeriod())) {
                containsRelativePeriods = true;
                RelativePeriodEnum relativePeriod = RelativePeriodEnum.valueOf(isoPeriodHolder.getIsoPeriod());
                dimensionalKeywords.addKeyword(isoPeriodHolder.getIsoPeriod(), i18n.getString(isoPeriodHolder.getIsoPeriod()));
                List<Period> relativePeriods = RelativePeriods.getRelativePeriodsFromEnum(relativePeriod, relativePeriodDate, format, true, financialYearStart);
                // If custom time filter is specified, set it in periods
                if (isoPeriodHolder.hasDateField()) {
                    relativePeriods.forEach(period -> period.setDateField(isoPeriodHolder.getDateField()));
                }
                periods.addAll(relativePeriods);
            } else {
                Period period = PeriodType.getPeriodFromIsoString(isoPeriodHolder.getIsoPeriod());
                if (period != null) {
                    if (isoPeriodHolder.hasDateField()) {
                        period.setDescription(isoPeriodHolder.getIsoPeriod());
                        period.setDateField(isoPeriodHolder.getDateField());
                    }
                    dimensionalKeywords.addKeyword(isoPeriodHolder.getIsoPeriod(), format != null ? i18n.getString(format.formatPeriod(period)) : isoPeriodHolder.getIsoPeriod());
                    periods.add(period);
                } else {
                    tryParseDateRange(isoPeriodHolder).ifPresent(periods::add);
                }
            }
        }
        // Remove duplicates
        periods = periods.stream().distinct().collect(Collectors.toList());
        if (containsRelativePeriods) {
            periods.sort(new AscendingPeriodComparator());
        }
        for (Period period : periods) {
            String name = format != null ? format.formatPeriod(period) : null;
            if (!period.getPeriodType().getName().contains(WeeklyPeriodType.NAME)) {
                period.setShortName(name);
            }
            period.setName(name);
            if (!calendar.isIso8601()) {
                period.setUid(getLocalPeriodIdentifier(period, calendar));
            }
        }
        return new BaseDimensionalObject(dimension, DimensionType.PERIOD, null, DISPLAY_NAME_PERIOD, asList(periods), dimensionalKeywords);
    } else if (ORGUNIT_DIM_ID.equals(dimension)) {
        List<DimensionalItemObject> ous = new ArrayList<>();
        List<Integer> levels = new ArrayList<>();
        List<OrganisationUnitGroup> groups = new ArrayList<>();
        for (String ou : items) {
            if (KEY_USER_ORGUNIT.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
                ous.addAll(userOrgUnits);
            } else if (KEY_USER_ORGUNIT_CHILDREN.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
                ous.addAll(OrganisationUnit.getSortedChildren(userOrgUnits));
            } else if (KEY_USER_ORGUNIT_GRANDCHILDREN.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
                ous.addAll(OrganisationUnit.getSortedGrandChildren(userOrgUnits));
            } else if (ou != null && ou.startsWith(KEY_LEVEL)) {
                String level = DimensionalObjectUtils.getValueFromKeywordParam(ou);
                Integer orgUnitLevel = organisationUnitService.getOrganisationUnitLevelByLevelOrUid(level);
                if (orgUnitLevel != null) {
                    levels.add(orgUnitLevel);
                }
            } else if (ou != null && ou.startsWith(KEY_ORGUNIT_GROUP)) {
                String uid = DimensionalObjectUtils.getUidFromGroupParam(ou);
                OrganisationUnitGroup group = idObjectManager.getObject(OrganisationUnitGroup.class, inputIdScheme, uid);
                if (group != null) {
                    groups.add(group);
                }
            } else if (!inputIdScheme.is(IdentifiableProperty.UID) || CodeGenerator.isValidUid(ou)) {
                OrganisationUnit unit = idObjectManager.getObject(OrganisationUnit.class, inputIdScheme, ou);
                if (unit != null) {
                    ous.add(unit);
                }
            }
        }
        // Remove duplicates
        ous = ous.stream().distinct().collect(Collectors.toList());
        List<DimensionalItemObject> orgUnits = new ArrayList<>();
        List<OrganisationUnit> ousList = asTypedList(ous);
        DimensionItemKeywords dimensionalKeywords = new DimensionItemKeywords();
        if (!levels.isEmpty()) {
            orgUnits.addAll(sort(organisationUnitService.getOrganisationUnitsAtLevels(levels, ousList)));
            dimensionalKeywords.addKeywords(levels.stream().map(level -> organisationUnitService.getOrganisationUnitLevelByLevel(level)).filter(Objects::nonNull).collect(Collectors.toList()));
        }
        if (!groups.isEmpty()) {
            orgUnits.addAll(sort(organisationUnitService.getOrganisationUnits(groups, ousList)));
            dimensionalKeywords.addKeywords(groups.stream().map(group -> new BaseNameableObject(group.getUid(), group.getCode(), group.getName())).collect(Collectors.toList()));
        }
        if (levels.isEmpty() && groups.isEmpty()) {
            orgUnits.addAll(ous);
        }
        if (!dimensionalKeywords.isEmpty()) {
            dimensionalKeywords.addKeywords(ousList);
        }
        if (orgUnits.isEmpty()) {
            throwIllegalQueryEx(ErrorCode.E7124, DimensionalObject.ORGUNIT_DIM_ID);
        }
        // Remove duplicates
        orgUnits = orgUnits.stream().distinct().collect(Collectors.toList());
        return new BaseDimensionalObject(dimension, DimensionType.ORGANISATION_UNIT, null, DISPLAY_NAME_ORGUNIT, orgUnits, dimensionalKeywords);
    } else if (ORGUNIT_GROUP_DIM_ID.equals(dimension)) {
        List<DimensionalItemObject> ougs = new ArrayList<>();
        for (String uid : items) {
            OrganisationUnitGroup organisationUnitGroup = idObjectManager.getObject(OrganisationUnitGroup.class, inputIdScheme, uid);
            if (organisationUnitGroup != null) {
                ougs.add(organisationUnitGroup);
            }
        }
        return new BaseDimensionalObject(dimension, DimensionType.ORGANISATION_UNIT_GROUP, null, DISPLAY_NAME_ORGUNIT_GROUP, ougs);
    } else if (LONGITUDE_DIM_ID.contains(dimension)) {
        return new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LONGITUDE, new ArrayList<>());
    } else if (LATITUDE_DIM_ID.contains(dimension)) {
        return new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LATITUDE, new ArrayList<>());
    } else {
        DimensionalObject dimObject = idObjectManager.get(DataQueryParams.DYNAMIC_DIM_CLASSES, inputIdScheme, dimension);
        if (dimObject != null && dimObject.isDataDimension()) {
            Class<?> dimClass = HibernateProxyUtils.getRealClass(dimObject);
            Class<? extends DimensionalItemObject> itemClass = DimensionalObject.DIMENSION_CLASS_ITEM_CLASS_MAP.get(dimClass);
            List<DimensionalItemObject> dimItems = !allItems ? asList(idObjectManager.getOrdered(itemClass, inputIdScheme, items)) : getCanReadItems(user, dimObject);
            return new BaseDimensionalObject(dimObject.getDimension(), dimObject.getDimensionType(), null, dimObject.getName(), dimItems, allItems);
        }
    }
    if (allowNull) {
        return null;
    }
    throw new IllegalQueryException(new ErrorMessage(ErrorCode.E7125, dimension));
}
Also used : KEY_USER_ORGUNIT_CHILDREN(org.hisp.dhis.organisationunit.OrganisationUnit.KEY_USER_ORGUNIT_CHILDREN) KEY_IN_GROUP(org.hisp.dhis.analytics.DataQueryParams.KEY_IN_GROUP) DISPLAY_NAME_LONGITUDE(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_LONGITUDE) DataQueryParams.getMeasureCriteriaFromParam(org.hisp.dhis.analytics.DataQueryParams.getMeasureCriteriaFromParam) Date(java.util.Date) DimensionService(org.hisp.dhis.common.DimensionService) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) AnalyticsFinancialYearStartKey(org.hisp.dhis.analytics.AnalyticsFinancialYearStartKey) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage) AnalyticsSecurityManager(org.hisp.dhis.analytics.AnalyticsSecurityManager) Calendar(org.hisp.dhis.calendar.Calendar) I18nFormat(org.hisp.dhis.i18n.I18nFormat) IdentifiableProperty(org.hisp.dhis.common.IdentifiableProperty) ISO_FORMAT(org.hisp.dhis.period.DailyPeriodType.ISO_FORMAT) KEY_USER_ORGUNIT(org.hisp.dhis.organisationunit.OrganisationUnit.KEY_USER_ORGUNIT) DimensionItemKeywords(org.hisp.dhis.common.DimensionItemKeywords) IndicatorGroup(org.hisp.dhis.indicator.IndicatorGroup) AscendingPeriodComparator(org.hisp.dhis.period.comparator.AscendingPeriodComparator) Period(org.hisp.dhis.period.Period) KEY_USER_ORGUNIT_GRANDCHILDREN(org.hisp.dhis.organisationunit.OrganisationUnit.KEY_USER_ORGUNIT_GRANDCHILDREN) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) PERIOD_DIM_ID(org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID) Collection(java.util.Collection) LONGITUDE_DIM_ID(org.hisp.dhis.common.DimensionalObject.LONGITUDE_DIM_ID) FORMAT_DATE(org.hisp.dhis.i18n.I18nFormat.FORMAT_DATE) DISPLAY_NAME_PERIOD(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_PERIOD) Set(java.util.Set) AnalyticsUtils.throwIllegalQueryEx(org.hisp.dhis.analytics.util.AnalyticsUtils.throwIllegalQueryEx) DimensionType(org.hisp.dhis.common.DimensionType) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) Objects(java.util.Objects) RelativePeriodEnum(org.hisp.dhis.period.RelativePeriodEnum) List(java.util.List) Stream(java.util.stream.Stream) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) LocalDate(java.time.LocalDate) AclService(org.hisp.dhis.security.acl.AclService) DISPLAY_NAME_LATITUDE(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_LATITUDE) ATTRIBUTEOPTIONCOMBO_DIM_ID(org.hisp.dhis.common.DimensionalObject.ATTRIBUTEOPTIONCOMBO_DIM_ID) Optional(java.util.Optional) RelativePeriods(org.hisp.dhis.period.RelativePeriods) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) DISPLAY_NAME_DATA_X(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_DATA_X) ListUtils.sort(org.hisp.dhis.commons.collection.ListUtils.sort) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) ORGUNIT_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID) ORGUNIT_GROUP_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_GROUP_DIM_ID) DataQueryService(org.hisp.dhis.analytics.DataQueryService) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DimensionalObjectUtils.asList(org.hisp.dhis.common.DimensionalObjectUtils.asList) DISPLAY_NAME_ORGUNIT_GROUP(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_ORGUNIT_GROUP) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) I18n(org.hisp.dhis.i18n.I18n) ArrayList(java.util.ArrayList) LATITUDE_DIM_ID(org.hisp.dhis.common.DimensionalObject.LATITUDE_DIM_ID) BaseNameableObject(org.hisp.dhis.common.BaseNameableObject) KEY_LEVEL(org.hisp.dhis.organisationunit.OrganisationUnit.KEY_LEVEL) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) Service(org.springframework.stereotype.Service) AnalyticsAggregationType(org.hisp.dhis.analytics.AnalyticsAggregationType) DATA_X_DIM_ID(org.hisp.dhis.common.DimensionalObject.DATA_X_DIM_ID) PERIOD_FREE_RANGE_SEPARATOR(org.hisp.dhis.common.DimensionalObject.PERIOD_FREE_RANGE_SEPARATOR) DimensionalObjectUtils.getDimensionalItemIds(org.hisp.dhis.common.DimensionalObjectUtils.getDimensionalItemIds) DataQueryRequest(org.hisp.dhis.common.DataQueryRequest) User(org.hisp.dhis.user.User) ErrorCode(org.hisp.dhis.feedback.ErrorCode) I18nManager(org.hisp.dhis.i18n.I18nManager) WeeklyPeriodType(org.hisp.dhis.period.WeeklyPeriodType) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) DimensionalObjectUtils(org.hisp.dhis.common.DimensionalObjectUtils) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) DISPLAY_NAME_CATEGORYOPTIONCOMBO(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_CATEGORYOPTIONCOMBO) IdentifiableObjectUtils.getLocalPeriodIdentifier(org.hisp.dhis.common.IdentifiableObjectUtils.getLocalPeriodIdentifier) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) DISPLAY_NAME_ORGUNIT(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_ORGUNIT) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) OutputFormat(org.hisp.dhis.analytics.OutputFormat) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ObjectUtils(org.hisp.dhis.util.ObjectUtils) DailyPeriodType(org.hisp.dhis.period.DailyPeriodType) CurrentUserService(org.hisp.dhis.user.CurrentUserService) DateTimeFormatter(java.time.format.DateTimeFormatter) PeriodType(org.hisp.dhis.period.PeriodType) CodeGenerator(org.hisp.dhis.common.CodeGenerator) KEY_ORGUNIT_GROUP(org.hisp.dhis.organisationunit.OrganisationUnit.KEY_ORGUNIT_GROUP) SettingKey(org.hisp.dhis.setting.SettingKey) DISPLAY_NAME_ATTRIBUTEOPTIONCOMBO(org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_ATTRIBUTEOPTIONCOMBO) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) IdScheme(org.hisp.dhis.common.IdScheme) KEY_DE_GROUP(org.hisp.dhis.analytics.DataQueryParams.KEY_DE_GROUP) CATEGORYOPTIONCOMBO_DIM_ID(org.hisp.dhis.common.DimensionalObject.CATEGORYOPTIONCOMBO_DIM_ID) Assert(org.springframework.util.Assert) User(org.hisp.dhis.user.User) IndicatorGroup(org.hisp.dhis.indicator.IndicatorGroup) AnalyticsFinancialYearStartKey(org.hisp.dhis.analytics.AnalyticsFinancialYearStartKey) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionItemKeywords(org.hisp.dhis.common.DimensionItemKeywords) ArrayList(java.util.ArrayList) DimensionalObject(org.hisp.dhis.common.DimensionalObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) RelativePeriodEnum(org.hisp.dhis.period.RelativePeriodEnum) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) List(java.util.List) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) DimensionalObjectUtils.asList(org.hisp.dhis.common.DimensionalObjectUtils.asList) ArrayList(java.util.ArrayList) I18n(org.hisp.dhis.i18n.I18n) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Calendar(org.hisp.dhis.calendar.Calendar) Period(org.hisp.dhis.period.Period) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) AscendingPeriodComparator(org.hisp.dhis.period.comparator.AscendingPeriodComparator) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) Objects(java.util.Objects) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage) BaseNameableObject(org.hisp.dhis.common.BaseNameableObject)

Example 2 with AnalyticsFinancialYearStartKey

use of org.hisp.dhis.analytics.AnalyticsFinancialYearStartKey in project dhis2-core by dhis2.

the class GeoToolsMapGenerationService method getSingleInternalMapLayer.

private InternalMapLayer getSingleInternalMapLayer(MapView mapView, User user, Date date) {
    if (mapView == null) {
        return null;
    }
    List<OrganisationUnit> atLevels = new ArrayList<>();
    List<OrganisationUnit> inGroups = new ArrayList<>();
    if (mapView.hasOrganisationUnitLevels()) {
        atLevels.addAll(organisationUnitService.getOrganisationUnitsAtLevels(mapView.getOrganisationUnitLevels(), mapView.getOrganisationUnits()));
    }
    if (mapView.hasItemOrganisationUnitGroups()) {
        inGroups.addAll(organisationUnitService.getOrganisationUnits(mapView.getItemOrganisationUnitGroups(), mapView.getOrganisationUnits()));
    }
    mapView.init(user, date, null, atLevels, inGroups, null);
    List<OrganisationUnit> organisationUnits = mapView.getAllOrganisationUnits();
    FilterUtils.filter(organisationUnits, new OrganisationUnitWithCoordinatesFilter());
    java.util.Map<String, OrganisationUnit> uidOuMap = new HashMap<>();
    for (OrganisationUnit ou : organisationUnits) {
        uidOuMap.put(ou.getUid(), ou);
    }
    String name = mapView.getName();
    Period period = null;
    if (// TODO integrate with
    !mapView.getPeriods().isEmpty()) // BaseAnalyticalObject
    {
        period = mapView.getPeriods().get(0);
    } else if (mapView.getRelatives() != null) {
        AnalyticsFinancialYearStartKey financialYearStart = systemSettingManager.getSystemSetting(SettingKey.ANALYTICS_FINANCIAL_YEAR_START, AnalyticsFinancialYearStartKey.class);
        period = mapView.getRelatives().getRelativePeriods(date, null, false, financialYearStart).get(0);
    }
    Integer radiusLow = mapView.getRadiusLow() != null ? mapView.getRadiusLow() : DEFAULT_RADIUS_LOW;
    Integer radiusHigh = mapView.getRadiusHigh() != null ? mapView.getRadiusHigh() : DEFAULT_RADIUS_HIGH;
    // Get the low and high colors, typically in hexadecimal form, e.g.
    // #ff3200
    Color colorLow = MapUtils.createColorFromString(StringUtils.trimToNull(mapView.getColorLow()) != null ? mapView.getColorLow() : DEFAULT_COLOR_LOW);
    Color colorHigh = MapUtils.createColorFromString(StringUtils.trimToNull(mapView.getColorHigh()) != null ? mapView.getColorHigh() : DEFAULT_COLOR_HIGH);
    float opacity = mapView.getOpacity() != null ? mapView.getOpacity().floatValue() : DEFAULT_OPACITY;
    boolean hasLegendSet = mapView.hasLegendSet();
    // Create and setup an internal layer
    InternalMapLayer mapLayer = new InternalMapLayer();
    mapLayer.setName(name);
    mapLayer.setPeriod(period);
    mapLayer.setMethod(mapView.getMethod());
    mapLayer.setLayer(mapView.getLayer());
    mapLayer.setRadiusLow(radiusLow);
    mapLayer.setRadiusHigh(radiusHigh);
    mapLayer.setColorLow(colorLow);
    mapLayer.setColorHigh(colorHigh);
    mapLayer.setOpacity(opacity);
    mapLayer.setClasses(mapView.getClasses());
    if (// Boundary (and facility) layer
    !mapView.isDataLayer()) {
        for (OrganisationUnit unit : organisationUnits) {
            mapLayer.addBoundaryMapObject(unit);
        }
    } else // Thematic layer
    {
        Collection<MapValue> mapValues = getAggregatedMapValues(mapView);
        if (mapValues.isEmpty()) {
            return null;
        }
        for (MapValue mapValue : mapValues) {
            OrganisationUnit orgUnit = uidOuMap.get(mapValue.getOu());
            if (orgUnit != null) {
                mapLayer.addDataMapObject(mapValue.getValue(), orgUnit);
            }
        }
        if (!mapLayer.hasMapObjects()) {
            return null;
        }
        if (hasLegendSet) {
            mapLayer.setIntervalSetFromLegendSet(mapView.getLegendSet());
            mapLayer.distributeAndUpdateMapObjectsInIntervalSet();
        } else {
            mapLayer.setAutomaticIntervalSet(mapLayer.getClasses());
            mapLayer.distributeAndUpdateMapObjectsInIntervalSet();
        }
        // Update the radius of each map object in this map layer according
        // to
        // its map object's highest and lowest values
        mapLayer.applyInterpolatedRadii();
    }
    return mapLayer;
}
Also used : OrganisationUnitWithCoordinatesFilter(org.hisp.dhis.system.filter.OrganisationUnitWithCoordinatesFilter) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) AnalyticsFinancialYearStartKey(org.hisp.dhis.analytics.AnalyticsFinancialYearStartKey) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period)

Example 3 with AnalyticsFinancialYearStartKey

use of org.hisp.dhis.analytics.AnalyticsFinancialYearStartKey in project dhis2-core by dhis2.

the class DefaultReportService method renderReport.

// -------------------------------------------------------------------------
// ReportService implementation
// -------------------------------------------------------------------------
@Override
@Transactional(readOnly = true)
public JasperPrint renderReport(OutputStream out, String reportUid, Period period, String organisationUnitUid, String type) {
    I18nFormat format = i18nManager.getI18nFormat();
    Report report = getReport(reportUid);
    Map<String, Object> params = new HashMap<>();
    params.putAll(constantService.getConstantParameterMap());
    Date reportDate = new Date();
    if (period != null) {
        params.put(PARAM_PERIOD_NAME, format.formatPeriod(period));
        reportDate = period.getStartDate();
    }
    OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit(organisationUnitUid);
    if (orgUnit != null) {
        int level = orgUnit.getLevel();
        params.put(PARAM_ORGANISATIONUNIT_COLUMN_NAME, orgUnit.getName());
        params.put(PARAM_ORGANISATIONUNIT_LEVEL, level);
        params.put(PARAM_ORGANISATIONUNIT_LEVEL_COLUMN, ORGUNIT_LEVEL_COLUMN_PREFIX + level);
        params.put(PARAM_ORGANISATIONUNIT_UID_LEVEL_COLUMN, ORGUNIT_UID_LEVEL_COLUMN_PREFIX + level);
    }
    JasperPrint print;
    log.debug(String.format("Get report for report date: '%s', org unit: '%s'", DateUtils.getMediumDateString(reportDate), organisationUnitUid));
    try {
        JasperReport jasperReport = JasperCompileManager.compileReport(IOUtils.toInputStream(report.getDesignContent(), StandardCharsets.UTF_8));
        if (// Use JR data source
        report.hasVisualization()) {
            Visualization visualization = report.getVisualization();
            Grid grid = visualizationService.getVisualizationGrid(visualization.getUid(), reportDate, organisationUnitUid);
            print = JasperFillManager.fillReport(jasperReport, params, grid);
        } else // Use JDBC data source
        {
            if (report.hasRelativePeriods()) {
                AnalyticsFinancialYearStartKey financialYearStart = systemSettingManager.getSystemSetting(SettingKey.ANALYTICS_FINANCIAL_YEAR_START, AnalyticsFinancialYearStartKey.class);
                List<Period> relativePeriods = report.getRelatives().getRelativePeriods(reportDate, null, false, financialYearStart);
                String periodString = getCommaDelimitedString(getIdentifiers(periodService.reloadPeriods(relativePeriods)));
                String isoPeriodString = getCommaDelimitedString(IdentifiableObjectUtils.getUids(relativePeriods));
                params.put(PARAM_RELATIVE_PERIODS, periodString);
                params.put(PARAM_RELATIVE_ISO_PERIODS, isoPeriodString);
            }
            if (report.hasReportParams() && report.getReportParams().isOrganisationUnit() && orgUnit != null) {
                params.put(PARAM_ORG_UNITS, String.valueOf(orgUnit.getId()));
                params.put(PARAM_ORG_UNITS_UID, String.valueOf(orgUnit.getUid()));
            }
            Connection connection = DataSourceUtils.getConnection(dataSource);
            try {
                print = JasperFillManager.fillReport(jasperReport, params, connection);
            } finally {
                DataSourceUtils.releaseConnection(connection, dataSource);
            }
        }
        if (print != null) {
            JRExportUtils.export(type, out, print);
        }
    } catch (Exception ex) {
        throw new RuntimeException("Failed to render report", ex);
    }
    return print;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Visualization(org.hisp.dhis.visualization.Visualization) AnalyticsFinancialYearStartKey(org.hisp.dhis.analytics.AnalyticsFinancialYearStartKey) JasperReport(net.sf.jasperreports.engine.JasperReport) Report(org.hisp.dhis.report.Report) HashMap(java.util.HashMap) JasperPrint(net.sf.jasperreports.engine.JasperPrint) Grid(org.hisp.dhis.common.Grid) Connection(java.sql.Connection) Period(org.hisp.dhis.period.Period) I18nFormat(org.hisp.dhis.i18n.I18nFormat) TextUtils.getCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString) JasperReport(net.sf.jasperreports.engine.JasperReport) Date(java.util.Date) JasperPrint(net.sf.jasperreports.engine.JasperPrint) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with AnalyticsFinancialYearStartKey

use of org.hisp.dhis.analytics.AnalyticsFinancialYearStartKey in project dhis2-core by dhis2.

the class DefaultReportService method renderHtmlReport.

@Override
@Transactional(readOnly = true)
public void renderHtmlReport(Writer writer, String uid, Date date, String ou) {
    Report report = getReport(uid);
    OrganisationUnit organisationUnit = null;
    List<OrganisationUnit> organisationUnitHierarchy = new ArrayList<>();
    List<OrganisationUnit> organisationUnitChildren = new ArrayList<>();
    List<String> periods = new ArrayList<>();
    I18nFormat format = i18nManager.getI18nFormat();
    if (ou != null) {
        organisationUnit = organisationUnitService.getOrganisationUnit(ou);
        if (organisationUnit != null) {
            organisationUnitHierarchy.add(organisationUnit);
            OrganisationUnit parent = organisationUnit;
            while (parent.getParent() != null) {
                parent = parent.getParent();
                organisationUnitHierarchy.add(parent);
            }
            organisationUnitChildren.addAll(organisationUnit.getChildren());
        }
    }
    Calendar calendar = PeriodType.getCalendar();
    if (report != null && report.hasRelativePeriods()) {
        AnalyticsFinancialYearStartKey financialYearStart = systemSettingManager.getSystemSetting(SettingKey.ANALYTICS_FINANCIAL_YEAR_START, AnalyticsFinancialYearStartKey.class);
        if (calendar.isIso8601()) {
            for (Period period : report.getRelatives().getRelativePeriods(date, format, true, financialYearStart)) {
                periods.add(period.getIsoDate());
            }
        } else {
            periods = IdentifiableObjectUtils.getLocalPeriodIdentifiers(report.getRelatives().getRelativePeriods(date, format, true, financialYearStart), calendar);
        }
    }
    String dateString = DateUtils.getMediumDateString(date);
    if (date != null && !calendar.isIso8601()) {
        dateString = calendar.formattedDate(calendar.fromIso(date));
    }
    final VelocityContext context = new VelocityContext();
    context.put("report", report);
    context.put("organisationUnit", organisationUnit);
    context.put("organisationUnitHierarchy", organisationUnitHierarchy);
    context.put("organisationUnitChildren", organisationUnitChildren);
    context.put("date", dateString);
    context.put("periods", periods);
    context.put("format", format);
    context.put("encoder", ENCODER);
    new VelocityManager().getEngine().getTemplate("html-report.vm").merge(context, writer);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) AnalyticsFinancialYearStartKey(org.hisp.dhis.analytics.AnalyticsFinancialYearStartKey) JasperReport(net.sf.jasperreports.engine.JasperReport) Report(org.hisp.dhis.report.Report) VelocityContext(org.apache.velocity.VelocityContext) VelocityManager(org.hisp.dhis.system.velocity.VelocityManager) Calendar(org.hisp.dhis.calendar.Calendar) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) TextUtils.getCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString) I18nFormat(org.hisp.dhis.i18n.I18nFormat) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

AnalyticsFinancialYearStartKey (org.hisp.dhis.analytics.AnalyticsFinancialYearStartKey)4 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)4 Period (org.hisp.dhis.period.Period)4 ArrayList (java.util.ArrayList)3 I18nFormat (org.hisp.dhis.i18n.I18nFormat)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 JasperReport (net.sf.jasperreports.engine.JasperReport)2 Calendar (org.hisp.dhis.calendar.Calendar)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Connection (java.sql.Connection)1 LocalDate (java.time.LocalDate)1 ZoneId (java.time.ZoneId)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 Collection (java.util.Collection)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1