use of org.hisp.dhis.period.QuarterlyPeriodType 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.QuarterlyPeriodType 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));
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;
switch(periodName.substring(0, periodName.indexOf(" "))) {
case "Jan":
month = 1;
break;
case "Apr":
month = 4;
break;
case "Jul":
month = 6;
break;
case "Oct":
month = 10;
break;
}
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);
}
use of org.hisp.dhis.period.QuarterlyPeriodType 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;
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:
switch(cal.get(Calendar.DAY_OF_WEEK)) {
case MONDAY:
periodType = new WeeklyPeriodType();
break;
case WEDNESDAY:
periodType = new WeeklyWednesdayPeriodType();
break;
case THURSDAY:
periodType = new WeeklyThursdayPeriodType();
break;
case SATURDAY:
periodType = new WeeklySaturdayPeriodType();
break;
case SUNDAY:
periodType = new WeeklySundayPeriodType();
break;
default:
throw new AdxException(periodString + " is invalid weekly type");
}
break;
case P14D:
periodType = new BiWeeklyPeriodType();
break;
case P1M:
periodType = new MonthlyPeriodType();
break;
case P2M:
periodType = new BiMonthlyPeriodType();
break;
case P3M:
periodType = new QuarterlyPeriodType();
break;
case P6M:
switch(cal.get(Calendar.MONTH)) {
case JANUARY:
case JULY:
periodType = new SixMonthlyPeriodType();
break;
case APRIL:
case OCTOBER:
periodType = new SixMonthlyAprilPeriodType();
break;
case NOVEMBER:
case MAY:
periodType = new SixMonthlyNovemberPeriodType();
break;
default:
throw new AdxException(periodString + " is invalid sixmonthly type");
}
break;
case P1Y:
switch(cal.get(Calendar.MONTH)) {
case JANUARY:
periodType = new YearlyPeriodType();
break;
case APRIL:
periodType = new FinancialAprilPeriodType();
break;
case JULY:
periodType = new FinancialJulyPeriodType();
break;
case OCTOBER:
periodType = new FinancialOctoberPeriodType();
break;
case NOVEMBER:
periodType = new FinancialNovemberPeriodType();
break;
default:
throw new AdxException(periodString + " is invalid yearly type");
}
break;
}
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.QuarterlyPeriodType in project dhis2-core by dhis2.
the class PersianCalendarTest method testGenerateQuarterlyPeriods.
@Test
void testGenerateQuarterlyPeriods() {
Date startDate = new Cal(2017, 3, 21, true).time();
Date endDate = new Cal(2017, 6, 21, true).time();
List<Period> monthly = new QuarterlyPeriodType().generatePeriods(calendar, startDate, endDate);
assertEquals(1, monthly.size());
}
use of org.hisp.dhis.period.QuarterlyPeriodType in project dhis2-core by dhis2.
the class DataSetFrequencyComparatorTest method testA.
@Test
void testA() {
DataSet dsA = new DataSet("DataSetA", new QuarterlyPeriodType());
DataSet dsB = new DataSet("DataSetB", new YearlyPeriodType());
DataSet dsC = new DataSet("DataSetC", new MonthlyPeriodType());
DataSet dsD = new DataSet("DataSetD", new QuarterlyPeriodType());
List<DataSet> list = Lists.newArrayList(dsA, dsC, dsB, dsD);
Collections.sort(list, DataSetFrequencyComparator.INSTANCE);
assertEquals(dsC, list.get(0));
assertEquals(dsA, list.get(1));
assertEquals(dsD, list.get(2));
assertEquals(dsB, list.get(3));
}
Aggregations