use of org.hisp.dhis.period.PeriodType 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.PeriodType in project dhis2-core by dhis2.
the class TranslationServiceTest method testPredictorTranslations.
@Test
void testPredictorTranslations() {
DataElement dataElementX = createDataElement('X', ValueType.NUMBER, AggregationType.NONE);
DataElement dataElementA = createDataElement('A');
DataElement dataElementB = createDataElement('B');
manager.save(dataElementA);
manager.save(dataElementB);
manager.save(dataElementX);
OrganisationUnitLevel orgUnitLevel1 = new OrganisationUnitLevel(1, "Level1");
manager.save(orgUnitLevel1);
CategoryOptionCombo defaultCombo = categoryService.getDefaultCategoryOptionCombo();
PeriodType periodTypeMonthly = PeriodType.getPeriodTypeByName("Monthly");
Expression expressionA = new Expression("AVG(#{" + dataElementA.getUid() + "})+1.5*STDDEV(#{" + dataElementA.getUid() + "})", "descriptionA");
expressionA.setTranslations(Sets.newHashSet(new Translation(locale.getLanguage(), "DESCRIPTION", "translated descriptionA")));
Expression expressionB = new Expression("AVG(#{" + dataElementB.getUid() + "." + defaultCombo.getUid() + "})", "descriptionB");
expressionB.setTranslations(Sets.newHashSet(new Translation(locale.getLanguage(), "DESCRIPTION", "translated descriptionB")));
Predictor predictor = createPredictor(dataElementX, defaultCombo, "A", expressionA, expressionB, periodTypeMonthly, orgUnitLevel1, 6, 1, 0);
manager.save(predictor);
manager.updateTranslations(predictor, Sets.newHashSet(new Translation(locale.getLanguage(), "NAME", "translated Predictor Name")));
predictor = manager.get(Predictor.class, predictor.getUid());
assertEquals("translated Predictor Name", predictor.getDisplayName());
assertEquals("translated descriptionA", predictor.getGenerator().getDisplayDescription());
assertEquals("translated descriptionB", predictor.getSampleSkipTest().getDisplayDescription());
}
use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.
the class JdbcCompleteDataSetRegistrationExchangeStore method writeCompleteness.
// --------------------------------------------------------------------------
// Supportive methods
// --------------------------------------------------------------------------
private void writeCompleteness(String sql, CompleteDataSetRegistrations completeDataSetRegistrations) {
final Calendar calendar = PeriodType.getCalendar();
completeDataSetRegistrations.open();
jdbcTemplate.query(sql, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
CompleteDataSetRegistration completeDataSetRegistration = completeDataSetRegistrations.getCompleteDataSetRegistrationInstance();
PeriodType pt = PeriodType.getPeriodTypeByName(rs.getString("ptname"));
completeDataSetRegistration.open();
completeDataSetRegistration.setDataSet(rs.getString("dsid"));
completeDataSetRegistration.setPeriod(pt.createPeriod(rs.getDate("pestart"), calendar).getIsoDate());
completeDataSetRegistration.setOrganisationUnit(rs.getString("ouid"));
completeDataSetRegistration.setAttributeOptionCombo(rs.getString("aocid"));
completeDataSetRegistration.setDate(removeTime(rs.getString("date")));
completeDataSetRegistration.setStoredBy(rs.getString("storedby"));
completeDataSetRegistration.setLastUpdatedBy(rs.getString("lastupdatedby"));
completeDataSetRegistration.setLastUpdated(removeTime(rs.getString("lastupdated")));
completeDataSetRegistration.setCompleted(rs.getBoolean("iscompleted"));
completeDataSetRegistration.close();
}
});
completeDataSetRegistrations.close();
}
use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.
the class ExpirationDaysCheck method checkEventOrPsiExpirationDate.
private ImportSummary checkEventOrPsiExpirationDate(Program program, ImmutableEvent event, ProgramStageInstance programStageInstance) {
PeriodType periodType = program.getExpiryPeriodType();
if (periodType != null && program.getExpiryDays() > 0) {
if (programStageInstance != null) {
Date today = new Date();
if (programStageInstance.getExecutionDate() == null) {
return error("Event needs to have event date", event.getEvent());
}
Period period = periodType.createPeriod(programStageInstance.getExecutionDate());
if (today.after(DateUtils.getDateAfterAddition(period.getEndDate(), program.getExpiryDays()))) {
return error("The program's expiry date has passed. It is not possible to make changes to this event", event.getEvent());
}
} else {
String referenceDate = event.getEventDate() != null ? event.getEventDate() : event.getDueDate();
if (referenceDate == null) {
return error("Event needs to have at least one (event or schedule) date", event.getEvent());
}
Period period = periodType.createPeriod(new Date());
if (DateUtils.parseDate(referenceDate).before(period.getStartDate())) {
return error("The event's date belongs to an expired period. It is not possible to create such event", event.getEvent());
}
}
}
return success();
}
use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.
the class PeriodTypeObjectBundleHook method preCreate.
@Override
public void preCreate(IdentifiableObject object, ObjectBundle bundle) {
Schema schema = schemaService.getDynamicSchema(HibernateProxyUtils.getRealClass(object));
for (Property property : schema.getPropertyMap().values()) {
if (PeriodType.class.isAssignableFrom(property.getKlass())) {
PeriodType periodType = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
if (periodType != null) {
periodType = bundle.getPreheat().getPeriodTypeMap().get(periodType.getName());
periodType = periodService.reloadPeriodType(periodType);
ReflectionUtils.invokeMethod(object, property.getSetterMethod(), periodType);
}
}
}
}
Aggregations