/** * Creates a valid Period based on the current date. E.g. if today is * January 5. 2007, a monthly PeriodType should return January 2007. * * @return a valid Period based on the current date. */ public Period createPeriod() { return createPeriod( createCalendarInstance() ); }
/** * Generates a period based on the given iso8601 formatted string. * * @param isoDate the iso8601 string. * @return the period. */ public Period createPeriod( String isoDate ) { return createPeriod( dateUnitFormat.parse( isoDate ) ); }
/** * Return the name of the reporting period. * * @param format the i18n format. * @return the name of the reporting period. */ public String getReportingPeriodName( I18nFormat format ) { Period period = getPeriodType().createPeriod( new Date() ); return format.formatPeriod( period ); }
/** * Returns the period at the same time of year going back a number of years. * * @param period the Period to base the previous Period on. * @param yearCount how many years to go back. * @return the past year period. */ public Period getPreviousYearsPeriod( Period period, int yearCount ) { Calendar calendar = PeriodType.createCalendarInstance( period.getStartDate() ); calendar.set( Calendar.YEAR, calendar.get( Calendar.YEAR ) - yearCount ); return createPeriod( calendar ); }
/** * Returns a period based on the given date string in ISO format. Returns * null if the date string cannot be parsed to a period. * * @param isoPeriod the date string in ISO format. * @return a period. */ public static Period getPeriodFromIsoString( String isoPeriod ) { if ( isoPeriod != null ) { PeriodType periodType = getPeriodTypeFromIsoString( isoPeriod ); try { return periodType != null ? periodType.createPeriod( isoPeriod ) : null; } catch ( Exception ex ) { // Do nothing and return null } } return null; }
private static String toIsoDate( String periodName, Date start, final Calendar calendar ) { return PeriodType.getPeriodTypeByName( periodName ).createPeriod( start, calendar ).getIsoDate(); }
private Period getPeriod( SMSCommand command, Date date ) { Period period = null; period = command.getDataset().getPeriodType().createPeriod(); PeriodType periodType = period.getPeriodType(); if ( command.isCurrentPeriodUsedForReporting() ) { period = periodType.createPeriod( new Date() ); } else { period = periodType.getPreviousPeriod( period ); } if ( date != null ) { period = periodType.createPeriod( date ); } return period; }
public Period createPeriod( Calendar cal ) { org.hisp.dhis.calendar.Calendar calendar = getCalendar(); return createPeriod( calendar.fromIso( DateTimeUnit.fromJdkCalendar( cal ) ), calendar ); }
/** * Returns the {@link Period} of the argument period type which corresponds to the argument period. * The frequency order of the given period type must greater than or equal to the period type * of the given period (represent "longer" periods). Weeks are converted to "longer" periods by * determining which period contains at least 4 days of the week. * <p> * As an example, providing * {@code Quarter 1, 2017} and {@code Yearly} as arguments will return the yearly * period {@code 2017}. * * @param period the period. * @param periodType the period type of the period to return. * @param calendar the calendar to use when calculating the period. * @return a period. */ public static Period getPeriodByPeriodType( Period period, PeriodType periodType, Calendar calendar ) { Assert.isTrue( periodType.getFrequencyOrder() >= period.getPeriodType().getFrequencyOrder(), "Frequency order of period type must be greater than or equal to period" ); Date date = period.getStartDate(); if ( WeeklyAbstractPeriodType.class.isAssignableFrom( period.getPeriodType().getClass() ) ) { date = new DateTime( date.getTime() ).plusDays( 3 ).toDate(); } return periodType.createPeriod( date, calendar ); }
/** * Creates a valid Period based on the given date. E.g. the given date is * February 10. 2007, a monthly PeriodType should return February 2007. * * @param date the date which is contained by the created period. * @return the valid Period based on the given date */ public Period createPeriod( final Date date ) { return PERIOD_CACHE.get( getCacheKey( date ), s -> createPeriod( createCalendarInstance( date ) ) ); }
/** * Returns a Period which is the previous of the given Period. Only valid * Periods are returned. If the given Period is of different PeriodType than * the executing PeriodType, or the given Period is invalid, the returned * Period might overlap the given Period. * * @param period the Period to base the previous Period on. * @return a Period which is the previous of the given Period. */ public Period getPreviousPeriod( Period period, org.hisp.dhis.calendar.Calendar calendar ) { DateTimeUnit dateWithOffset = getDateWithOffset( createLocalDateUnitInstance( period.getStartDate(), calendar ), -1, calendar ); return createPeriod( dateWithOffset, calendar ); }
/** * Return the name of the reporting period. * * @param date the start date of the reporting period. * @param format the i18n format. * @return the name of the reporting period. */ public String getReportingPeriodName( Date date, I18nFormat format ) { if ( date == null ) { return getReportingPeriodName( format ); } Period period = getPeriodType().createPeriod( date ); return format.formatPeriod( period ); }
/** * Returns a Period which is the next of the given Period. Only valid * Periods are returned. If the given Period is of different PeriodType than * the executing PeriodType, or the given Period is invalid, the returned * Period might overlap the given Period. * * @param period the Period to base the next Period on. * @param calendar the Calendar to use. * @return a Period which is the next of the given Period. */ public Period getNextPeriod( Period period, org.hisp.dhis.calendar.Calendar calendar ) { DateTimeUnit dateWithOffset = getDateWithOffset( createLocalDateUnitInstance( period.getStartDate(), calendar ), 1, calendar ); return createPeriod( dateWithOffset, calendar ); }
@Override public Optional<List<Object[]>> getPopulateTempTableContent() { List<PeriodType> periodTypes = PeriodType.getAvailablePeriodTypes(); List<Object[]> batchArgs = new ArrayList<>(); Date startDate = new Cal( 1975, 1, 1, true ).time(); //TODO Date endDate = new Cal( 2025, 1, 1, true ).time(); List<Period> dailyPeriods = new DailyPeriodType().generatePeriods( startDate, endDate ); List<Date> days = new UniqueArrayList<>( dailyPeriods.stream().map( Period::getStartDate ).collect( Collectors.toList() ) ); Calendar calendar = PeriodType.getCalendar(); for ( Date day : days ) { List<Object> values = new ArrayList<>(); final int year = PeriodType.getCalendar().fromIso( day ).getYear(); values.add( day ); values.add( year ); for ( PeriodType periodType : periodTypes ) { values.add( periodType.createPeriod( day, calendar ).getIsoDate() ); } batchArgs.add( values.toArray() ); } return Optional.of( batchArgs ); }
@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(); } } );
/** * Creates a valid Period based on the given date. E.g. the given date is * February 10. 2007, a monthly PeriodType should return February 2007. This * method is intended for use in situations where a huge number of of periods * will be generated and its desirable to re-use the calendar. * * @param date the date which is contained by the created period. * @param calendar the calendar implementation to use. * @return the valid Period based on the given date */ public Period createPeriod( final Date date, final org.hisp.dhis.calendar.Calendar calendar ) { return PERIOD_CACHE.get( getCacheKey( calendar, date ), p -> createPeriod( calendar.fromIso( DateTimeUnit.fromJdkDate( date ) ), calendar ) ); }
@Override public void processRow( ResultSet rs ) throws SQLException { DataValue dataValue = dataValueSet.getDataValueInstance(); PeriodType pt = PeriodType.getPeriodTypeByName( rs.getString( "ptname" ) ); boolean deleted = rs.getBoolean( "deleted" ); dataValue.setDataElement( rs.getString( "deid" ) ); dataValue.setPeriod( pt.createPeriod( rs.getDate( "pestart" ), calendar ).getIsoDate() ); dataValue.setOrgUnit( rs.getString( "ouid" ) ); dataValue.setCategoryOptionCombo( rs.getString( "cocid" ) ); dataValue.setAttributeOptionCombo( rs.getString( "aocid" ) ); dataValue.setValue( rs.getString( "value" ) ); dataValue.setStoredBy( rs.getString( "storedby" ) ); dataValue.setCreated( getLongGmtDateString( rs.getTimestamp( "created" ) ) ); dataValue.setLastUpdated( getLongGmtDateString( rs.getTimestamp( "lastupdated" ) ) ); dataValue.setComment( rs.getString( "comment" ) ); dataValue.setFollowup( rs.getBoolean( "followup" ) ); if ( deleted ) { dataValue.setDeleted( deleted ); } dataValue.close(); } } );
/** * Creates a mapping between the data periods, based on the data period type * for this query, and the aggregation periods for this query. */ public ListMap<DimensionalItemObject, DimensionalItemObject> getDataPeriodAggregationPeriodMap() { ListMap<DimensionalItemObject, DimensionalItemObject> map = new ListMap<>(); if ( dataPeriodType != null ) { for ( DimensionalItemObject aggregatePeriod : getDimensionOrFilterItems( PERIOD_DIM_ID ) ) { Period dataPeriod = dataPeriodType.createPeriod( ((Period) aggregatePeriod).getStartDate() ); map.putValue( dataPeriod, aggregatePeriod ); } } return map; }
/** * Returns the latest period which is open for data input. Returns null if * data element is not associated with any data sets. * * @return the latest period which is open for data input. */ public Period getLatestOpenFuturePeriod() { int periods = getOpenFuturePeriods(); PeriodType periodType = getPeriodType(); if ( periodType != null ) { Period period = periodType.createPeriod(); // Rewind one as 0 open periods implies current period is locked period = periodType.getPreviousPeriod( period ); return periodType.getNextPeriod( period, periods ); } return null; }
private CompleteDataSetRegistration createRespectiveRegistrationObject( DataSet dataSet, OrganisationUnit ou ) { Period period = dataSet.getPeriodType().createPeriod(); CompleteDataSetRegistration registration = new CompleteDataSetRegistration(); registration.setDataSet( dataSet ); registration.setPeriod( periodService.getPeriod( period.getStartDate(), period.getEndDate(), period.getPeriodType() ) ); registration.setPeriodName( getPeriodString( registration.getPeriod() ) ); registration.setAttributeOptionCombo( categoryService.getDefaultCategoryOptionCombo() ); registration.setSource( ou ); return registration; }