/** * Returns an instance of a DateUnit. * * @param date date of calendar in local calendar * @return an instance of a Calendar without any time of day. */ public static DateTimeUnit createLocalDateUnitInstance( Date date ) { return createLocalDateUnitInstance( date, getCalendar() ); }
/** * Returns an iso8601 formatted string representation of the dataUnit * * @param dateTimeUnit Period * @return the period as string */ public String getIsoDate( DateTimeUnit dateTimeUnit ) { return getIsoDate( dateTimeUnit, getCalendar() ); }
/** * This method checks whether the String inDate is a valid date following * the format "yyyy-MM-dd". * * @param dateString the string to be checked. * @return true/false depending on whether the string is a date according to the format "yyyy-MM-dd". */ public static boolean dateIsValid( String dateString ) { return dateIsValid( PeriodType.getCalendar(), dateString ); }
public Period toIsoPeriod( DateTimeUnit start, DateTimeUnit end ) { return toIsoPeriod( start, end, getCalendar() ); }
/** * 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. * @return a Period which is the next of the given Period. */ public final Period getNextPeriod( Period period ) { return getNextPeriod( period, getCalendar() ); }
/** * 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 final Period getPreviousPeriod( Period period ) { return getPreviousPeriod( period, getCalendar() ); }
/** * Returns the previous period determined by the given number of periods. * * @param period the Period to base the previous Period on. * @param periods the number of periods into the past. * @return the previous period. */ public Period getPreviousPeriod( Period period, int periods ) { Period previousPeriod = period; if ( periods > 0 ) { org.hisp.dhis.calendar.Calendar calendar = getCalendar(); for ( int i = 0; i < periods; i++ ) { previousPeriod = getPreviousPeriod( previousPeriod, calendar ); } } return previousPeriod; }
/** * Returns the next period determined by the given number of periods. * * @param period the Period to base the next Period on. * @param periods the number of periods into the future. * @return the next period. */ public Period getNextPeriod( Period period, int periods ) { Period nextPeriod = period; if ( periods > 0 ) { org.hisp.dhis.calendar.Calendar calendar = getCalendar(); for ( int i = 0; i < periods; i++ ) { nextPeriod = getNextPeriod( nextPeriod, calendar ); } } return nextPeriod; }
private String getCacheKey( Date date ) { return getCalendar().name() + getName() + date.getTime(); }
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(); }
public Period createPeriod( Calendar cal ) { org.hisp.dhis.calendar.Calendar calendar = getCalendar(); return createPeriod( calendar.fromIso( DateTimeUnit.fromJdkCalendar( cal ) ), calendar ); }
/** * Returns the years which the given period spans. * * @param period the period. * @return a set of years. */ private static Set<Integer> getYears( Period period ) { Set<Integer> years = new HashSet<>(); int startYear = PeriodType.getCalendar().fromIso( period.getStartDate() ).getYear(); int endYear = PeriodType.getCalendar().fromIso( period.getEndDate() ).getYear(); while ( startYear <= endYear ) { years.add( startYear ); startYear++; } return years; }
/** * Returns an instance of a Calendar without any time of day, with the * current date. * * @return an instance of a Calendar without any time of day. */ public static Calendar createCalendarInstance() { org.hisp.dhis.calendar.Calendar cal = getCalendar(); return cal.toIso( cal.today() ).toJdkCalendar(); }
@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 ); }
/** * Offsets the input date with the provided number of periods within the current period type. * If the offset number is positive, the date is offset into later periods. When the offset is * negative, the date is offset into earlier periods. * @param date for where to start the offset. * @param period how many periods to go back(if negative) or forward(if positive). A value of 0 will * result in the original date to be returned. * @return a new date object that has been offset from the original date passed into the function. */ public Date getDateWithOffset( Date date, int offset ) { org.hisp.dhis.calendar.Calendar calendar = getCalendar(); DateTimeUnit dateTimeUnit = createLocalDateUnitInstance( date, calendar ); return getDateWithOffset( dateTimeUnit, offset, calendar ).toJdkDate(); }
@Override @Transactional public List<AnalyticsTable> getAnalyticsTables( Date earliest ) { log.info( String.format( "Get tables using earliest: %s, spatial support: %b", earliest, databaseInfo.isSpatialSupport() ) ); List<AnalyticsTable> tables = new ArrayList<>(); Calendar calendar = PeriodType.getCalendar(); String baseName = getTableName(); List<Program> programs = idObjectManager.getAllNoAcl( Program.class ); for ( Program program : programs ) { List<Integer> dataYears = getDataYears( program, earliest ); Collections.sort( dataYears ); AnalyticsTable table = new AnalyticsTable( baseName, getDimensionColumns( program ), Lists.newArrayList(), program ); for ( Integer year : dataYears ) { table.addPartitionTable( year, PartitionUtils.getStartDate( calendar, year ), PartitionUtils.getEndDate( calendar, year ) ); } if ( table.hasPartitionTables() ) { tables.add( table ); } } return tables; }
private void write( ExportParams params, final CompleteDataSetRegistrations items ) { String query = createQuery( params ); final Calendar calendar = PeriodType.getCalendar(); items.open(); jdbcTemplate.query( query, rs -> { CompleteDataSetRegistration cdsr = items.getCompleteDataSetRegistrationInstance(); cdsr.open(); cdsr.setPeriod( toIsoDate( rs.getString( P_PERIOD_TYPE ), rs.getDate( P_PERIOD_START ), calendar ) ); cdsr.setDataSet( rs.getString( P_DATA_SET ) ); cdsr.setOrganisationUnit( rs.getString( P_ORG_UNIT ) ); cdsr.setAttributeOptionCombo( rs.getString( P_ATTR_OPT_COMBO ) ); cdsr.setDate( removeTime( rs.getString( P_DATE ) ) ); cdsr.setStoredBy( rs.getString( P_STORED_BY ) ); cdsr.close(); } ); items.close(); }
/** * Generates a list of {@link AnalyticsTable} based on a list of years with data. * * @param dataYears the list of years with data. * @param dimensionColumns the list of dimension {@link AnalyticsTableColumn}. * @param valueColumns the list of value {@link AnalyticsTableColumn}. */ protected AnalyticsTable getAnalyticsTable( List<Integer> dataYears, List<AnalyticsTableColumn> dimensionColumns, List<AnalyticsTableColumn> valueColumns ) { Calendar calendar = PeriodType.getCalendar(); Collections.sort( dataYears ); String baseName = getAnalyticsTableType().getTableName(); AnalyticsTable table = new AnalyticsTable( baseName, dimensionColumns, valueColumns ); for ( Integer year : dataYears ) { table.addPartitionTable( year, PartitionUtils.getStartDate( calendar, year ), PartitionUtils.getEndDate( calendar, year ) ); } return table; }
/** * Returns the start date of the year which occurred * {@code lastYears} ago. * * @param lastYears the number of years ago to base the * returned date. * @return the start date of the given year. */ public static Date getStartDate( Integer lastYears ) { Date earliest = null; if ( lastYears != null ) { Calendar calendar = PeriodType.getCalendar(); DateTimeUnit dateTimeUnit = calendar.today(); dateTimeUnit = calendar.minusYears( dateTimeUnit, lastYears - 1 ); dateTimeUnit.setMonth( 1 ); dateTimeUnit.setDay( 1 ); earliest = dateTimeUnit.toJdkDate(); } return earliest; }
/** * @param dateInterval DateInterval to create period from * @return the period. */ public Period createPeriod( DateInterval dateInterval ) { if ( dateInterval == null || dateInterval.getFrom() == null || dateInterval.getTo() == null ) { return null; } org.hisp.dhis.calendar.Calendar cal = getCalendar(); final DateTimeUnit from = cal.toIso( dateInterval.getFrom() ); final DateTimeUnit to = cal.toIso( dateInterval.getTo() ); return new Period( this, from.toJdkDate(), to.toJdkDate(), getIsoDate( from ) ); }