Search in sources :

Example 81 with PeriodType

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");
    }
}
Also used : SixMonthlyNovemberPeriodType(org.hisp.dhis.period.SixMonthlyNovemberPeriodType) WeeklyWednesdayPeriodType(org.hisp.dhis.period.WeeklyWednesdayPeriodType) FinancialNovemberPeriodType(org.hisp.dhis.period.FinancialNovemberPeriodType) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType) BiMonthlyPeriodType(org.hisp.dhis.period.BiMonthlyPeriodType) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) WeeklyThursdayPeriodType(org.hisp.dhis.period.WeeklyThursdayPeriodType) QuarterlyPeriodType(org.hisp.dhis.period.QuarterlyPeriodType) FinancialAprilPeriodType(org.hisp.dhis.period.FinancialAprilPeriodType) WeeklyPeriodType(org.hisp.dhis.period.WeeklyPeriodType) FinancialJulyPeriodType(org.hisp.dhis.period.FinancialJulyPeriodType) WeeklySundayPeriodType(org.hisp.dhis.period.WeeklySundayPeriodType) FinancialOctoberPeriodType(org.hisp.dhis.period.FinancialOctoberPeriodType) WeeklySaturdayPeriodType(org.hisp.dhis.period.WeeklySaturdayPeriodType) SixMonthlyAprilPeriodType(org.hisp.dhis.period.SixMonthlyAprilPeriodType) DailyPeriodType(org.hisp.dhis.period.DailyPeriodType) PeriodType(org.hisp.dhis.period.PeriodType) BiWeeklyPeriodType(org.hisp.dhis.period.BiWeeklyPeriodType) SixMonthlyPeriodType(org.hisp.dhis.period.SixMonthlyPeriodType) BiMonthlyPeriodType(org.hisp.dhis.period.BiMonthlyPeriodType) WeeklySaturdayPeriodType(org.hisp.dhis.period.WeeklySaturdayPeriodType) FinancialJulyPeriodType(org.hisp.dhis.period.FinancialJulyPeriodType) SixMonthlyAprilPeriodType(org.hisp.dhis.period.SixMonthlyAprilPeriodType) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType) FinancialAprilPeriodType(org.hisp.dhis.period.FinancialAprilPeriodType) WeeklyThursdayPeriodType(org.hisp.dhis.period.WeeklyThursdayPeriodType) DailyPeriodType(org.hisp.dhis.period.DailyPeriodType) SixMonthlyNovemberPeriodType(org.hisp.dhis.period.SixMonthlyNovemberPeriodType) QuarterlyPeriodType(org.hisp.dhis.period.QuarterlyPeriodType) Calendar(java.util.Calendar) Period(org.hisp.dhis.period.Period) WeeklySundayPeriodType(org.hisp.dhis.period.WeeklySundayPeriodType) Date(java.util.Date) WeeklyPeriodType(org.hisp.dhis.period.WeeklyPeriodType) BiWeeklyPeriodType(org.hisp.dhis.period.BiWeeklyPeriodType) FinancialNovemberPeriodType(org.hisp.dhis.period.FinancialNovemberPeriodType) WeeklyWednesdayPeriodType(org.hisp.dhis.period.WeeklyWednesdayPeriodType) BiMonthlyPeriodType(org.hisp.dhis.period.BiMonthlyPeriodType) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) SixMonthlyPeriodType(org.hisp.dhis.period.SixMonthlyPeriodType) FinancialOctoberPeriodType(org.hisp.dhis.period.FinancialOctoberPeriodType) BiWeeklyPeriodType(org.hisp.dhis.period.BiWeeklyPeriodType) SixMonthlyPeriodType(org.hisp.dhis.period.SixMonthlyPeriodType)

Example 82 with PeriodType

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());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) PeriodType(org.hisp.dhis.period.PeriodType) Expression(org.hisp.dhis.expression.Expression) Predictor(org.hisp.dhis.predictor.Predictor) OrganisationUnitLevel(org.hisp.dhis.organisationunit.OrganisationUnitLevel) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 83 with PeriodType

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();
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) SQLException(java.sql.SQLException) Calendar(org.hisp.dhis.calendar.Calendar) ResultSet(java.sql.ResultSet) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler)

Example 84 with PeriodType

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();
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) Period(org.hisp.dhis.period.Period) Date(java.util.Date)

Example 85 with PeriodType

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);
            }
        }
    }
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) Schema(org.hisp.dhis.schema.Schema) Property(org.hisp.dhis.schema.Property)

Aggregations

PeriodType (org.hisp.dhis.period.PeriodType)104 Period (org.hisp.dhis.period.Period)27 MonthlyPeriodType (org.hisp.dhis.period.MonthlyPeriodType)24 DataElement (org.hisp.dhis.dataelement.DataElement)17 DataSet (org.hisp.dhis.dataset.DataSet)17 ArrayList (java.util.ArrayList)15 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)13 Test (org.junit.jupiter.api.Test)12 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)11 Date (java.util.Date)10 DailyPeriodType (org.hisp.dhis.period.DailyPeriodType)8 ProgramStage (org.hisp.dhis.program.ProgramStage)8 HashMap (java.util.HashMap)7 UniqueArrayList (org.hisp.dhis.commons.collection.UniqueArrayList)7 Expression (org.hisp.dhis.expression.Expression)7 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)7 OrganisationUnitGroupSet (org.hisp.dhis.organisationunit.OrganisationUnitGroupSet)6 Program (org.hisp.dhis.program.Program)6 Property (org.hisp.dhis.schema.Property)6 Schema (org.hisp.dhis.schema.Schema)6