use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.
the class YearlyPeriodType method getRewindedDate.
@Override
public Date getRewindedDate(Date date, Integer rewindedPeriods) {
Calendar calendar = getCalendar();
date = date != null ? date : new Date();
rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
DateTimeUnit dateTimeUnit = createLocalDateUnitInstance(date);
dateTimeUnit = calendar.minusYears(dateTimeUnit, rewindedPeriods);
return calendar.toIso(dateTimeUnit).toJdkDate();
}
use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.
the class YearlyPeriodType method generateLast5Years.
/**
* Generates the last 5 years where the last one is the year which the given
* date is inside.
*/
@Override
public List<Period> generateLast5Years(Date date) {
Calendar calendar = getCalendar();
DateTimeUnit dateTimeUnit = createLocalDateUnitInstance(date);
dateTimeUnit = calendar.minusYears(dateTimeUnit, 4);
dateTimeUnit.setDay(1);
dateTimeUnit.setMonth(1);
List<Period> periods = Lists.newArrayList();
for (int i = 0; i < 5; ++i) {
periods.add(createPeriod(dateTimeUnit, calendar));
dateTimeUnit = calendar.plusYears(dateTimeUnit, 1);
}
return periods;
}
use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.
the class AnalyticsUtils method getDimensionItemNameMap.
/**
* Returns a mapping between identifiers and names for the given query.
*
* @param params the data query parameters.
* @return a mapping between identifiers and names.
*/
public static Map<String, String> getDimensionItemNameMap(DataQueryParams params) {
List<DimensionalObject> dimensions = params.getDimensionsAndFilters();
Map<String, String> map = new HashMap<>();
Calendar calendar = PeriodType.getCalendar();
for (DimensionalObject dimension : dimensions) {
for (DimensionalItemObject item : dimension.getItems()) {
if (DimensionType.PERIOD.equals(dimension.getDimensionType()) && !calendar.isIso8601()) {
Period period = (Period) item;
DateTimeUnit dateTimeUnit = calendar.fromIso(period.getStartDate());
map.put(period.getPeriodType().getIsoDate(dateTimeUnit), period.getDisplayName());
} else {
map.put(item.getDimensionItem(), item.getDisplayProperty(params.getDisplayProperty()));
}
if (DimensionType.ORGANISATION_UNIT.equals(dimension.getDimensionType()) && params.isHierarchyMeta()) {
OrganisationUnit unit = (OrganisationUnit) item;
map.putAll(NameableObjectUtils.getUidDisplayPropertyMap(unit.getAncestors(), params.getDisplayProperty()));
}
}
map.put(dimension.getDimension(), dimension.getDisplayProperty(params.getDisplayProperty()));
}
return map;
}
use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.
the class PeriodResourceTable method getPopulateTempTableContent.
@Override
public Optional<List<Object[]>> getPopulateTempTableContent() {
Calendar calendar = PeriodType.getCalendar();
List<Object[]> batchArgs = new ArrayList<>();
Set<String> uniqueIsoDates = new HashSet<>();
for (Period period : objects) {
if (period != null && period.isValid()) {
final PeriodType rowType = period.getPeriodType();
final String isoDate = period.getIsoDate();
if (!uniqueIsoDates.add(isoDate)) {
log.warn("Duplicate ISO date for period, ignoring: " + period + ", ISO date: " + isoDate);
continue;
}
List<Object> values = new ArrayList<>();
values.add(period.getId());
values.add(isoDate);
values.add(period.getDaysInPeriod());
values.add(period.getStartDate());
values.add(period.getEndDate());
for (PeriodType periodType : PeriodType.PERIOD_TYPES) {
if (rowType.getFrequencyOrder() < periodType.getFrequencyOrder() || rowType.equals(periodType)) {
Period targetPeriod = IdentifiableObjectUtils.getPeriodByPeriodType(period, periodType, calendar);
values.add(IdentifiableObjectUtils.getLocalPeriodIdentifier(targetPeriod, calendar));
} else {
values.add(null);
}
}
batchArgs.add(values.toArray());
}
}
return Optional.of(batchArgs);
}
use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.
the class DefaultEventAnalyticsService method addMetadata.
// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
/**
* Adds meta data values to the given grid based on the given data query
* parameters.
*
* @param params the data query parameters.
* @param grid the grid.
*/
private void addMetadata(EventQueryParams params, Grid grid) {
if (!params.isSkipMeta()) {
Calendar calendar = PeriodType.getCalendar();
List<String> periodUids = calendar.isIso8601() ? getDimensionalItemIds(params.getDimensionOrFilterItems(PERIOD_DIM_ID)) : getLocalPeriodIdentifiers(params.getDimensionOrFilterItems(PERIOD_DIM_ID), calendar);
Map<String, Object> metaData = new HashMap<>();
Map<String, String> uidNameMap = AnalyticsUtils.getUidNameMap(params);
if (params.getApiVersion().ge(DhisApiVersion.V26)) {
metaData.put(AnalyticsMetaDataKey.ITEMS.getKey(), uidNameMap.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> new MetadataItem(e.getValue()))));
} else {
metaData.put(AnalyticsMetaDataKey.NAMES.getKey(), uidNameMap);
}
Map<String, Object> dimensionItems = new HashMap<>();
dimensionItems.put(PERIOD_DIM_ID, periodUids);
for (DimensionalObject dim : params.getDimensionsAndFilters()) {
if (!metaData.keySet().contains(dim.getDimension())) {
dimensionItems.put(dim.getDimension(), getDimensionalItemIds(dim.getItems()));
}
}
for (QueryItem item : params.getItems()) {
if (item.hasOptionSet()) {
dimensionItems.put(item.getItemId(), item.getQueryFilterItems());
} else if (item.hasLegendSet()) {
dimensionItems.put(item.getItemId(), item.getLegendSetFilterItemsOrAll());
} else {
dimensionItems.put(item.getItemId(), Lists.newArrayList());
}
}
for (QueryItem item : params.getItemFilters()) {
if (item.hasOptionSet()) {
dimensionItems.put(item.getItemId(), item.getQueryFilterItems());
} else if (item.hasLegendSet()) {
dimensionItems.put(item.getItemId(), item.getLegendSetFilterItemsOrAll());
} else {
dimensionItems.put(item.getItemId(), Lists.newArrayList(item.getFiltersAsString()));
}
}
if (params.getApiVersion().ge(DhisApiVersion.V26)) {
metaData.put(AnalyticsMetaDataKey.DIMENSIONS.getKey(), dimensionItems);
} else {
metaData.putAll(dimensionItems);
}
User user = securityManager.getCurrentUser(params);
List<OrganisationUnit> organisationUnits = asTypedList(params.getDimensionOrFilterItems(ORGUNIT_DIM_ID));
Collection<OrganisationUnit> roots = user != null ? user.getOrganisationUnits() : null;
if (params.isHierarchyMeta()) {
metaData.put(AnalyticsMetaDataKey.ORG_UNIT_HIERARCHY.getKey(), getParentGraphMap(organisationUnits, roots));
}
if (params.isShowHierarchy()) {
metaData.put(AnalyticsMetaDataKey.ORG_UNIT_NAME_HIERARCHY.getKey(), getParentNameGraphMap(organisationUnits, roots, true));
}
grid.setMetaData(metaData);
}
}
Aggregations