use of org.hisp.dhis.period.YearlyPeriodType in project dhis2-core by dhis2.
the class AnalyticsUtilsTest method testIsPeriodOverApprovalThreshold.
@Test
public void testIsPeriodOverApprovalThreshold() {
java.util.Calendar calendar = java.util.Calendar.getInstance();
int currentYear = calendar.get(Calendar.YEAR);
YearlyPeriodType pt = new YearlyPeriodType();
calendar.set(currentYear, 1, 1);
Period thisYear = pt.createPeriod(Date.from(calendar.toInstant()));
calendar.set(currentYear - 1, 1, 1);
Period oneYearAgo = pt.createPeriod(Date.from(calendar.toInstant()));
calendar.set(currentYear - 2, 1, 1);
Period twoYearAgo = pt.createPeriod(Date.from(calendar.toInstant()));
calendar.set(currentYear - 3, 1, 1);
Period threeYearAgo = pt.createPeriod(Date.from(calendar.toInstant()));
// maxYears = 0 should always return false
assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(thisYear, 0));
assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(oneYearAgo, 0));
assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(twoYearAgo, 0));
assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(threeYearAgo, 0));
// maxYears = 1 should only return true for years other than thisYear
assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(thisYear, 1));
assertTrue(AnalyticsUtils.periodIsOutsideApprovalMaxYears(oneYearAgo, 1));
assertTrue(AnalyticsUtils.periodIsOutsideApprovalMaxYears(twoYearAgo, 1));
assertTrue(AnalyticsUtils.periodIsOutsideApprovalMaxYears(threeYearAgo, 1));
// maxYears = 4 should only return false for all three years defined
assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(thisYear, 5));
assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(oneYearAgo, 5));
assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(twoYearAgo, 5));
assertTrue(!AnalyticsUtils.periodIsOutsideApprovalMaxYears(threeYearAgo, 5));
}
use of org.hisp.dhis.period.YearlyPeriodType in project dhis2-core by dhis2.
the class AdxPeriod method parse.
public static Period parse(String periodString) throws AdxException {
String[] tokens = periodString.split("/");
if (tokens.length != 2) {
throw new AdxException(periodString + " not in valid <date>/<duration> format");
}
try {
Period period = null;
PeriodType periodType = null;
Date startDate = DateUtils.getMediumDate(tokens[0]);
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
Duration duration = Duration.valueOf(tokens[1]);
switch(duration) {
case P1D:
periodType = new DailyPeriodType();
break;
case P7D:
periodType = new WeeklyPeriodType();
break;
case P1M:
periodType = new MonthlyPeriodType();
break;
case P2M:
periodType = new BiMonthlyPeriodType();
break;
case P1Q:
periodType = new QuarterlyPeriodType();
break;
case P6M:
switch(cal.get(Calendar.MONTH)) {
case 0:
periodType = new SixMonthlyPeriodType();
break;
case 6:
periodType = new SixMonthlyAprilPeriodType();
break;
default:
throw new AdxException(periodString + "is invalid sixmonthly type");
}
case P1Y:
switch(cal.get(Calendar.MONTH)) {
case 0:
periodType = new YearlyPeriodType();
break;
case 3:
periodType = new FinancialAprilPeriodType();
break;
case 6:
periodType = new FinancialJulyPeriodType();
break;
case 9:
periodType = new FinancialOctoberPeriodType();
break;
default:
throw new AdxException(periodString + "is invalid yearly type");
}
}
if (periodType != null) {
period = periodType.createPeriod(startDate);
} else {
throw new AdxException("Failed to create period type from " + duration);
}
return period;
} catch (IllegalArgumentException ex) {
throw new AdxException(tokens[1] + " is not a supported duration type");
}
}
use of org.hisp.dhis.period.YearlyPeriodType in project dhis2-core by dhis2.
the class PeriodUtil method getPeriod.
public static Period getPeriod(String periodName, PeriodType periodType) throws InvalidIdentifierReferenceException {
if (periodType instanceof DailyPeriodType) {
return periodType.createPeriod(DateUtils.getMediumDate(periodName));
}
if (periodType instanceof WeeklyPeriodType) {
return periodType.createPeriod(DateUtils.getMediumDate(periodName));
}
if (periodType instanceof MonthlyPeriodType) {
int dashIndex = periodName.indexOf('-');
if (dashIndex < 0) {
return null;
}
int month = Integer.parseInt(periodName.substring(0, dashIndex));
int year = Integer.parseInt(periodName.substring(dashIndex + 1, periodName.length()));
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
return periodType.createPeriod(cal.getTime());
}
if (periodType instanceof YearlyPeriodType) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.parseInt(periodName));
return periodType.createPeriod(cal.getTime());
}
if (periodType instanceof QuarterlyPeriodType) {
Calendar cal = Calendar.getInstance();
int month = 0;
if (periodName.substring(0, periodName.indexOf(" ")).equals("Jan")) {
month = 1;
} else if (periodName.substring(0, periodName.indexOf(" ")).equals("Apr")) {
month = 4;
} else if (periodName.substring(0, periodName.indexOf(" ")).equals("Jul")) {
month = 6;
} else if (periodName.substring(0, periodName.indexOf(" ")).equals("Oct")) {
month = 10;
}
int year = Integer.parseInt(periodName.substring(periodName.lastIndexOf(" ") + 1));
cal.set(Calendar.MONTH, month);
cal.set(Calendar.YEAR, year);
if (month != 0) {
return periodType.createPeriod(cal.getTime());
}
}
throw new InvalidIdentifierReferenceException("Couldn't make a period of type " + periodType.getName() + " and name " + periodName);
}
use of org.hisp.dhis.period.YearlyPeriodType in project dhis2-core by dhis2.
the class FormUtilsImpl method getPeriodsForDataSet.
@Override
public List<Period> getPeriodsForDataSet(Integer dataSetId, int first, int max) {
Validate.notNull(dataSetId);
DataSet dataSet = dataSetService.getDataSet(dataSetId);
CalendarPeriodType periodType;
if (dataSet.getPeriodType().getName().equalsIgnoreCase("Yearly")) {
periodType = new YearlyPeriodType();
} else {
periodType = (CalendarPeriodType) dataSet.getPeriodType();
}
if (dataSet.getOpenFuturePeriods() > 0) {
List<Period> periods = periodType.generatePeriods(new Date());
Collections.reverse(periods);
return periods;
} else {
List<Period> periods = periodType.generateLast5Years(new Date());
FilterUtils.filter(periods, new PastAndCurrentPeriodFilter());
Collections.reverse(periods);
if (periods.size() > (first + max)) {
periods = periods.subList(first, max);
}
return periods;
}
}
use of org.hisp.dhis.period.YearlyPeriodType in project dhis2-core by dhis2.
the class J2MEDataValueSMSListener method getPeriod.
public Period getPeriod(String periodName, PeriodType periodType) throws IllegalArgumentException {
if (periodType instanceof DailyPeriodType) {
return periodType.createPeriod(DateUtils.getMediumDate(periodName));
}
if (periodType instanceof WeeklyPeriodType) {
return periodType.createPeriod(DateUtils.getMediumDate(periodName));
}
if (periodType instanceof MonthlyPeriodType) {
int dashIndex = periodName.indexOf('-');
if (dashIndex < 0) {
return null;
}
int month = Integer.parseInt(periodName.substring(0, dashIndex));
int year = Integer.parseInt(periodName.substring(dashIndex + 1, periodName.length()));
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
return periodType.createPeriod(cal.getTime());
}
if (periodType instanceof YearlyPeriodType) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.parseInt(periodName));
return periodType.createPeriod(cal.getTime());
}
if (periodType instanceof QuarterlyPeriodType) {
Calendar cal = Calendar.getInstance();
int month = 0;
if (periodName.substring(0, periodName.indexOf(" ")).equals("Jan")) {
month = 1;
} else if (periodName.substring(0, periodName.indexOf(" ")).equals("Apr")) {
month = 4;
} else if (periodName.substring(0, periodName.indexOf(" ")).equals("Jul")) {
month = 6;
} else if (periodName.substring(0, periodName.indexOf(" ")).equals("Oct")) {
month = 10;
}
int year = Integer.parseInt(periodName.substring(periodName.lastIndexOf(" ") + 1));
cal.set(Calendar.MONTH, month);
cal.set(Calendar.YEAR, year);
if (month != 0) {
return periodType.createPeriod(cal.getTime());
}
}
throw new IllegalArgumentException("Couldn't make a period of type " + periodType.getName() + " and name " + periodName);
}
Aggregations