use of org.hisp.dhis.period.WeeklyPeriodType in project dhis2-core by dhis2.
the class FacilityReportingServiceImpl method getMobileDataSetsForUnit.
// -------------------------------------------------------------------------
// Service methods
// -------------------------------------------------------------------------
@Override
public List<DataSet> getMobileDataSetsForUnit(OrganisationUnit unit, String localeString) {
List<DataSet> datasets = new ArrayList<>();
Locale locale = LocaleUtil.getLocale(localeString);
if (DEBUG)
log.debug("Getting data sets for unit " + unit.getName());
for (org.hisp.dhis.dataset.DataSet dataSet : dataSetService.getDataSetsForMobile(unit)) {
PeriodType periodType = dataSet.getPeriodType();
if (periodType instanceof DailyPeriodType || periodType instanceof WeeklyPeriodType || periodType instanceof MonthlyPeriodType || periodType instanceof YearlyPeriodType || periodType instanceof QuarterlyPeriodType) {
if (DEBUG)
log.debug("Found data set " + dataSet.getName());
datasets.add(getDataSetForLocale(dataSet.getId(), locale));
} else {
log.warn("Dataset '" + dataSet.getName() + "' set to be reported from mobile, but not of a supported period type: " + periodType.getName());
}
}
return datasets;
}
use of org.hisp.dhis.period.WeeklyPeriodType in project dhis2-core by dhis2.
the class I18nFormat method formatPeriod.
/**
* Formats a period. Returns null if value is null. Returns INVALID_DATE if
* formatting string is invalid.
*
* @param period the value to format.
*/
public String formatPeriod(Period period) {
if (period == null) {
return null;
}
PeriodType periodType = period.getPeriodType();
String typeName = periodType.getName();
if (// Use ISO dates
periodType instanceof WeeklyAbstractPeriodType) // due to
// potential week
// confusion
{
DateTime dateTime = new DateTime(period.getStartDate());
LocalDate date = Instant.ofEpochMilli(period.getStartDate().getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
WeekFields weekFields = WeekFields.of(PeriodType.MAP_WEEK_TYPE.get(periodType.getName()), 4);
String year = String.valueOf(date.get(weekFields.weekBasedYear()));
String week = String.valueOf(date.get(weekFields.weekOfWeekBasedYear()));
if (periodType instanceof WeeklyPeriodType) {
return String.format("W%s %s", week, year);
}
year += dateTime.dayOfWeek().getAsShortText() + " " + year;
return String.format("W%s %s", week, year);
} else if (periodType instanceof BiWeeklyAbstractPeriodType) {
int year;
int week;
Calendar calendar = PeriodType.getCalendar();
BiWeeklyAbstractPeriodType biWeeklyAbstractPeriodType = (BiWeeklyAbstractPeriodType) periodType;
DateTimeUnit dateTimeUnit = DateTimeUnit.fromJdkDate(period.getStartDate());
if (calendar.isIso8601()) {
LocalDate date = LocalDate.of(dateTimeUnit.getYear(), dateTimeUnit.getMonth(), dateTimeUnit.getDay());
WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 4);
year = date.get(weekFields.weekBasedYear());
week = (date.get(weekFields.weekOfWeekBasedYear()) / 2) + 1;
} else {
DateTimeUnit date = biWeeklyAbstractPeriodType.adjustToStartOfBiWeek(dateTimeUnit, calendar);
week = calendar.week(date);
if (week == 1 && date.getMonth() == calendar.monthsInYear()) {
date.setYear(date.getYear() + 1);
}
year = date.getYear();
}
return String.format("BiW%s %s", week, year);
}
String keyStartDate = "format." + typeName + ".startDate";
String keyEndDate = "format." + typeName + ".endDate";
String startPattern = resourceBundle.getString(keyStartDate);
String endPattern = resourceBundle.getString(keyEndDate);
boolean dayPattern = startPattern.contains("dd") || endPattern.contains("dd");
Date periodStartDate = period.getStartDate();
Date periodEndDate = period.getEndDate();
DateTimeUnit start = PeriodType.getCalendar().fromIso(periodStartDate);
DateTimeUnit end = PeriodType.getCalendar().fromIso(periodEndDate);
String startDate;
String endDate;
if (!dayPattern) {
// Set day to first of month to not overflow when converting to JDK
// date
start.setDay(1);
end.setDay(1);
startDate = commonFormatting(new DateTimeUnit(start, true).toJdkDate(), startPattern);
endDate = commonFormatting(new DateTimeUnit(end, true).toJdkDate(), endPattern);
} else {
startDate = PeriodType.getCalendar().formattedDate(startPattern, start);
endDate = PeriodType.getCalendar().formattedDate(endPattern, end);
}
try {
return Character.toUpperCase(startDate.charAt(0)) + startDate.substring(1) + endDate;
} catch (IllegalArgumentException ex) {
return INVALID_DATE;
}
}
use of org.hisp.dhis.period.WeeklyPeriodType in project dhis2-core by dhis2.
the class DataApprovalWorkflowTest method testGetWorkflowSqlCoEnddateExtension.
/**
* Note that as part of the following test, we are checking to be sure that
* the longest extension to the category option end date is returned from
* data sets that are successively added to the workflow. The total
* extension length (period length times number of periods) is always
* increasing as we add data sets to the workflow. The most recently-added
* data set always has the longest total extension so far.
*/
@Test
void testGetWorkflowSqlCoEnddateExtension() {
DataSet noPeriodsAfterCoEndDate = createDataSet("noPeriodsAfterCoEndDate", new MonthlyPeriodType(), 0);
DataSet dailyDs = createDataSet("dailyDs", new DailyPeriodType(), 3);
DataSet weeklyDs = createDataSet("weeklyDs", new WeeklyPeriodType(), 3);
DataSet weeklyWedDs = createDataSet("weeklyWedDs", new WeeklyWednesdayPeriodType(), 4);
DataSet weeklyThuDs = createDataSet("weeklyThuDs", new WeeklyThursdayPeriodType(), 5);
DataSet weeklySatDs = createDataSet("weeklySatDs", new WeeklySaturdayPeriodType(), 6);
DataSet weeklySunDs = createDataSet("weeklySunDs", new WeeklySundayPeriodType(), 7);
DataSet biWeeklyDs = createDataSet("biWeeklyDs", new BiWeeklyPeriodType(), 4);
DataSet monthlyDs = createDataSet("monthlyDs", new MonthlyPeriodType(), 3);
DataSet biMonthlyDs = createDataSet("biMonthlyDs", new BiMonthlyPeriodType(), 3);
DataSet querterlyDs = createDataSet("querterlyDs", new QuarterlyPeriodType(), 3);
DataSet sixMonthlyDs = createDataSet("sixMonthlyDs", new SixMonthlyPeriodType(), 3);
DataSet sixMonthlyAprDs = createDataSet("sixMonthlyAprDs", new SixMonthlyAprilPeriodType(), 4);
DataSet sixMonthlyNovDs = createDataSet("sixMonthlyNovDs", new SixMonthlyNovemberPeriodType(), 5);
DataSet yearlyDs = createDataSet("yearlyDs", new YearlyPeriodType(), 3);
DataSet financialAprDs = createDataSet("financialAprDs", new FinancialAprilPeriodType(), 4);
DataSet financialJulDs = createDataSet("financialJulDs", new FinancialJulyPeriodType(), 5);
DataSet financialOctDs = createDataSet("financialOctDs", new FinancialOctoberPeriodType(), 6);
DataSet financialNovDs = createDataSet("financialNovDs", new FinancialNovemberPeriodType(), 7);
DataSet twoYearlyDs = createDataSet("twoYearlyDs", new TwoYearlyPeriodType(), 4);
DataApprovalWorkflow workflow = new DataApprovalWorkflow("test workflow", new DailyPeriodType(), newHashSet());
assertEquals("", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(noPeriodsAfterCoEndDate);
assertEquals("", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(dailyDs);
assertEquals(" + 3 * INTERVAL '+ 1 day'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(weeklyDs);
assertEquals(" + 3 * INTERVAL '+ 1 week'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(weeklyWedDs);
assertEquals(" + 4 * INTERVAL '+ 1 week'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(weeklyThuDs);
assertEquals(" + 5 * INTERVAL '+ 1 week'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(weeklySatDs);
assertEquals(" + 6 * INTERVAL '+ 1 week'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(weeklySunDs);
assertEquals(" + 7 * INTERVAL '+ 1 week'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(biWeeklyDs);
assertEquals(" + 4 * INTERVAL '+ 2 weeks'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(monthlyDs);
assertEquals(" + 3 * INTERVAL '+ 1 month'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(biMonthlyDs);
assertEquals(" + 3 * INTERVAL '+ 2 months'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(querterlyDs);
assertEquals(" + 3 * INTERVAL '+ 3 months'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(sixMonthlyDs);
assertEquals(" + 3 * INTERVAL '+ 6 months'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(sixMonthlyAprDs);
assertEquals(" + 4 * INTERVAL '+ 6 months'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(sixMonthlyNovDs);
assertEquals(" + 5 * INTERVAL '+ 6 months'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(yearlyDs);
assertEquals(" + 3 * INTERVAL '+ 1 year'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(financialAprDs);
assertEquals(" + 4 * INTERVAL '+ 1 year'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(financialJulDs);
assertEquals(" + 5 * INTERVAL '+ 1 year'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(financialOctDs);
assertEquals(" + 6 * INTERVAL '+ 1 year'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(financialNovDs);
assertEquals(" + 7 * INTERVAL '+ 1 year'", workflow.getSqlCoEndDateExtension());
workflow.getDataSets().add(twoYearlyDs);
assertEquals(" + 4 * INTERVAL '+ 2 years'", workflow.getSqlCoEndDateExtension());
}
use of org.hisp.dhis.period.WeeklyPeriodType in project dhis2-core by dhis2.
the class IdentifiableObjectUtilsTest method testGetPeriodByPeriodType.
@Test
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("201708"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017W35"), 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("2017ThuW10"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201702"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SatW7"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201703"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SatW10"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201702"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SunW7"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201703"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SunW9"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201702"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SunW7"), monthly, calendar));
assertEquals(PeriodType.getPeriodFromIsoString("201703"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SunW9"), 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));
assertNull(PeriodType.getPeriodFromIsoString("u3847847"));
}
use of org.hisp.dhis.period.WeeklyPeriodType in project dhis2-core by dhis2.
the class ValidationRuleServiceTest method setUpTest.
// -------------------------------------------------------------------------
// Fixture
// -------------------------------------------------------------------------
@Override
public void setUpTest() throws Exception {
periodTypeWeekly = new WeeklyPeriodType();
periodTypeMonthly = new MonthlyPeriodType();
periodTypeYearly = new YearlyPeriodType();
dataElementA = createDataElement('A');
dataElementB = createDataElement('B');
dataElementC = createDataElement('C');
dataElementD = createDataElement('D');
dataElementE = createDataElement('E');
dataElementService.addDataElement(dataElementA);
dataElementService.addDataElement(dataElementB);
dataElementService.addDataElement(dataElementC);
dataElementService.addDataElement(dataElementD);
dataElementService.addDataElement(dataElementE);
optionCombo = categoryService.getDefaultCategoryOptionCombo();
String suffix = SEPARATOR + optionCombo.getUid();
optionCombos = new HashSet<>();
optionCombos.add(optionCombo);
expressionA = new Expression("#{" + dataElementA.getUid() + suffix + "} + #{" + dataElementB.getUid() + suffix + "}", "expressionA");
expressionB = new Expression("#{" + dataElementC.getUid() + suffix + "} - #{" + dataElementD.getUid() + suffix + "}", "expressionB");
expressionC = new Expression("#{" + dataElementC.getUid() + suffix + "} - #{" + dataElementD.getUid() + suffix + "}", "expressionC");
expressionD = new Expression("#{" + dataElementB.getUid() + suffix + "} * 2", "expressionD");
dataSetWeekly = createDataSet('W', periodTypeWeekly);
dataSetMonthly = createDataSet('M', periodTypeMonthly);
dataSetYearly = createDataSet('Y', periodTypeYearly);
sourceA = createOrganisationUnit('A');
sourceB = createOrganisationUnit('B');
sourceC = createOrganisationUnit('C', sourceB);
sourceD = createOrganisationUnit('D', sourceB);
sourceE = createOrganisationUnit('E', sourceD);
sourceF = createOrganisationUnit('F', sourceD);
sourceG = createOrganisationUnit('G');
sourcesA.add(sourceA);
sourcesA.add(sourceB);
allSources.add(sourceA);
allSources.add(sourceB);
allSources.add(sourceC);
allSources.add(sourceD);
allSources.add(sourceE);
allSources.add(sourceF);
allSources.add(sourceG);
dataSetMonthly.addOrganisationUnit(sourceA);
dataSetMonthly.addOrganisationUnit(sourceB);
dataSetMonthly.addOrganisationUnit(sourceC);
dataSetMonthly.addOrganisationUnit(sourceD);
dataSetMonthly.addOrganisationUnit(sourceE);
dataSetMonthly.addOrganisationUnit(sourceF);
dataSetWeekly.addOrganisationUnit(sourceB);
dataSetWeekly.addOrganisationUnit(sourceC);
dataSetWeekly.addOrganisationUnit(sourceD);
dataSetWeekly.addOrganisationUnit(sourceE);
dataSetWeekly.addOrganisationUnit(sourceF);
dataSetWeekly.addOrganisationUnit(sourceG);
dataSetYearly.addOrganisationUnit(sourceB);
dataSetYearly.addOrganisationUnit(sourceC);
dataSetYearly.addOrganisationUnit(sourceD);
dataSetYearly.addOrganisationUnit(sourceE);
dataSetYearly.addOrganisationUnit(sourceF);
organisationUnitService.addOrganisationUnit(sourceA);
organisationUnitService.addOrganisationUnit(sourceB);
organisationUnitService.addOrganisationUnit(sourceC);
organisationUnitService.addOrganisationUnit(sourceD);
organisationUnitService.addOrganisationUnit(sourceE);
organisationUnitService.addOrganisationUnit(sourceF);
organisationUnitService.addOrganisationUnit(sourceG);
dataSetMonthly.addDataSetElement(dataElementA);
dataSetMonthly.addDataSetElement(dataElementB);
dataSetMonthly.addDataSetElement(dataElementC);
dataSetMonthly.addDataSetElement(dataElementD);
dataSetWeekly.addDataSetElement(dataElementE);
dataSetYearly.addDataSetElement(dataElementE);
dataSetService.addDataSet(dataSetWeekly);
dataSetService.addDataSet(dataSetMonthly);
dataSetService.addDataSet(dataSetYearly);
dataElementService.updateDataElement(dataElementA);
dataElementService.updateDataElement(dataElementB);
dataElementService.updateDataElement(dataElementC);
dataElementService.updateDataElement(dataElementD);
dataElementService.updateDataElement(dataElementE);
// deA
validationRuleA = createValidationRule("A", equal_to, expressionA, expressionB, periodTypeMonthly, true);
// +
// deB
// =
// deC
// -
// deD
validationRuleB = createValidationRule(// deC
"B", // deC
greater_than, // deC
expressionC, // deC
expressionD, // 2
periodTypeMonthly);
}
Aggregations