use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.
the class DatePeriodResourceTable method getPopulateTempTableContent.
@Override
public Optional<List<Object[]>> getPopulateTempTableContent() {
List<PeriodType> periodTypes = PeriodType.getAvailablePeriodTypes();
List<Object[]> batchArgs = new ArrayList<>();
//TODO
Date startDate = new Cal(1975, 1, 1, true).time();
Date endDate = new Cal(2025, 1, 1, true).time();
List<Period> days = new UniqueArrayList<>(new DailyPeriodType().generatePeriods(startDate, endDate));
Calendar calendar = PeriodType.getCalendar();
for (Period day : days) {
List<Object> values = new ArrayList<>();
values.add(day.getStartDate());
for (PeriodType periodType : periodTypes) {
values.add(periodType.createPeriod(day.getStartDate(), calendar).getIsoDate());
}
batchArgs.add(values.toArray());
}
return Optional.of(batchArgs);
}
use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.
the class IdentifiableObjectUtilsTest method testGetPeriodByPeriodType.
@Test
public void testGetPeriodByPeriodType() {
Calendar calendar = Iso8601Calendar.getInstance();
WeeklyPeriodType weekly = new WeeklyPeriodType();
MonthlyPeriodType monthly = new MonthlyPeriodType();
QuarterlyPeriodType quarterly = new QuarterlyPeriodType();
YearlyPeriodType yearly = new YearlyPeriodType();
assertEquals(PeriodType.getPeriodFromIsoString("2017W10"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("20170308"), weekly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("2017W9"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("20170301"), weekly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201702"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017W8"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201703"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017W9"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201705"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017W21"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201706"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017W22"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201702"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017WedW8"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201703"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017WedW9"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201702"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017ThuW8"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201703"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017ThuW9"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201702"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SatW7"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201703"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SatW8"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201702"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SunW7"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201703"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SunW8"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201702"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SunW7"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201703"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SunW8"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("2017Q1"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("201703"), quarterly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("2017Q2"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("201704"), quarterly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("2016"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2016Q4"), yearly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("2017"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017Q1"), yearly, calendar));
}
use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.
the class GetReportParamsAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() {
if (mode == null || uid == null) {
return SUCCESS;
}
RelativePeriods relatives = null;
if (MODE_REPORT_TABLE.equals(mode)) {
ReportTable reportTable = reportTableService.getReportTable(uid);
if (reportTable != null) {
reportParams = reportTable.getReportParams();
relatives = reportTable.getRelatives();
}
} else if (MODE_REPORT.equals(mode)) {
Report report = reportService.getReport(uid);
if (report != null && report.isTypeReportTable()) {
reportParams = report.getReportTable().getReportParams();
relatives = report.getReportTable().getRelatives();
} else if (report != null && (report.isTypeJdbc() || report.isTypeHtml())) {
reportParams = report.getReportParams();
relatives = report.getRelatives();
}
if (type == null && report != null) {
// Set type based on report
type = report.getType();
}
}
if (reportParams != null && reportParams.isParamReportingMonth() && relatives != null) {
CalendarPeriodType periodType = (CalendarPeriodType) relatives.getPeriodType();
List<Period> periods = periodType.generateLast5Years(new Date());
Collections.reverse(periods);
FilterUtils.filter(periods, new PastAndCurrentPeriodFilter());
Calendar calendar = PeriodType.getCalendar();
for (Period period_ : periods) {
BaseIdentifiableObject period = new BaseIdentifiableObject();
if (calendar.isIso8601()) {
period.setUid(period_.getIsoDate());
period.setDisplayName(format.formatPeriod(period_));
} else {
DateTimeUnit dateTimeUnit = calendar.fromIso(period_.getStartDate());
period.setUid(period_.getPeriodType().getIsoDate(dateTimeUnit));
period.setDisplayName(format.formatPeriod(period_));
}
this.periods.add(period);
}
}
return SUCCESS;
}
use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.
the class DefaultAnalyticsService method addMetaData.
/**
* Adds meta data values to the given grid based on the given data query
* parameters.
*
* @param params the {@link DataQueryParams}.
* @param grid the grid.
*/
private void addMetaData(DataQueryParams params, Grid grid) {
if (!params.isSkipMeta()) {
Map<String, Object> metaData = new HashMap<>();
Map<String, Object> internalMetaData = new HashMap<>();
// -----------------------------------------------------------------
// Items / names element
// -----------------------------------------------------------------
Map<String, String> cocNameMap = AnalyticsUtils.getCocNameMap(params);
if (params.getApiVersion().ge(DhisApiVersion.V26)) {
metaData.put(AnalyticsMetaDataKey.ITEMS.getKey(), AnalyticsUtils.getDimensionMetadataItemMap(params));
} else {
Map<String, String> uidNameMap = AnalyticsUtils.getDimensionItemNameMap(params);
uidNameMap.putAll(cocNameMap);
uidNameMap.put(DATA_X_DIM_ID, DISPLAY_NAME_DATA_X);
metaData.put(AnalyticsMetaDataKey.NAMES.getKey(), uidNameMap);
}
// -----------------------------------------------------------------
// Item order elements
// -----------------------------------------------------------------
Map<String, Object> dimensionItems = new HashMap<>();
Calendar calendar = PeriodType.getCalendar();
List<String> periodUids = calendar.isIso8601() ? getDimensionalItemIds(params.getDimensionOrFilterItems(PERIOD_DIM_ID)) : getLocalPeriodIdentifiers(params.getDimensionOrFilterItems(PERIOD_DIM_ID), calendar);
dimensionItems.put(PERIOD_DIM_ID, periodUids);
dimensionItems.put(CATEGORYOPTIONCOMBO_DIM_ID, cocNameMap.keySet());
for (DimensionalObject dim : params.getDimensionsAndFilters()) {
if (!dimensionItems.keySet().contains(dim.getDimension())) {
dimensionItems.put(dim.getDimension(), getDimensionalItemIds(dim.getItems()));
}
}
if (params.getApiVersion().ge(DhisApiVersion.V26)) {
metaData.put(AnalyticsMetaDataKey.DIMENSIONS.getKey(), dimensionItems);
} else {
metaData.putAll(dimensionItems);
}
// -----------------------------------------------------------------
// Organisation unit hierarchy
// -----------------------------------------------------------------
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()) {
Map<Object, List<?>> ancestorMap = organisationUnits.stream().collect(Collectors.toMap(OrganisationUnit::getUid, ou -> ou.getAncestorNames(roots, true)));
internalMetaData.put(AnalyticsMetaDataKey.ORG_UNIT_ANCESTORS.getKey(), ancestorMap);
metaData.put(AnalyticsMetaDataKey.ORG_UNIT_NAME_HIERARCHY.getKey(), getParentNameGraphMap(organisationUnits, roots, true));
}
grid.setMetaData(ImmutableMap.copyOf(metaData));
grid.setInternalMetaData(ImmutableMap.copyOf(internalMetaData));
}
}
use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.
the class JdbcEventAnalyticsTableManager method getTables.
@Override
@Transactional
public List<AnalyticsTable> getTables(Date earliest) {
log.info(String.format("Get tables using earliest: %s, spatial support: %b", earliest, databaseInfo.isSpatialSupport()));
List<AnalyticsTable> tables = new UniqueArrayList<>();
Calendar calendar = PeriodType.getCalendar();
String baseName = getTableName();
List<Program> programs = idObjectManager.getAllNoAcl(Program.class);
for (Program program : programs) {
List<Integer> dataYears = getDataYears(program, earliest);
Collections.sort(dataYears);
for (Integer year : dataYears) {
Period period = PartitionUtils.getPeriod(calendar, year);
AnalyticsTable table = new AnalyticsTable(baseName, null, period, program);
List<AnalyticsTableColumn> dimensionColumns = getDimensionColumns(table);
table.setDimensionColumns(dimensionColumns);
tables.add(table);
}
}
return tables;
}
Aggregations