=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/PartitionUtils.java' --- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/PartitionUtils.java 2014-06-27 08:38:40 +0000 +++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/PartitionUtils.java 2014-08-07 16:50:09 +0000 @@ -28,88 +28,90 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import java.util.ArrayList; -import java.util.Collection; -import java.util.Date; -import java.util.List; -import java.util.Set; - import org.apache.commons.lang.StringUtils; import org.hisp.dhis.analytics.Partitions; +import org.hisp.dhis.calendar.DateUnit; import org.hisp.dhis.common.ListMap; import org.hisp.dhis.common.NameableObject; import org.hisp.dhis.period.Cal; import org.hisp.dhis.period.Period; +import org.hisp.dhis.period.PeriodType; import org.hisp.dhis.period.YearlyPeriodType; import org.hisp.dhis.system.util.UniqueArrayList; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.List; +import java.util.Set; + /** * @author Lars Helge Overland */ public class PartitionUtils { private static final YearlyPeriodType PERIODTYPE = new YearlyPeriodType(); - + private static final String SEP = "_"; public static List getPeriods( Date earliest, Date latest ) { List periods = new ArrayList(); - + Period period = PERIODTYPE.createPeriod( earliest ); - + while ( period != null && period.getStartDate().before( latest ) ) { - periods.add( period ); + periods.add( period ); period = PERIODTYPE.getNextPeriod( period ); } - + return periods; } //TODO optimize by including required filter periods only - + public static Partitions getPartitions( Period period, String tablePrefix, String tableSuffix, Set validPartitions ) { tablePrefix = StringUtils.trimToEmpty( tablePrefix ); tableSuffix = StringUtils.trimToEmpty( tableSuffix ); Partitions partitions = new Partitions(); - - int startYear = year( period.getStartDate() ); - int endYear = year( period.getEndDate() ); - + + int startYear = PeriodType.getCalendar().fromIso( DateUnit.fromJdkDate( period.getStartDate() ) ).getYear(); + int endYear = PeriodType.getCalendar().fromIso( DateUnit.fromJdkDate( period.getEndDate() ) ).getYear(); + while ( startYear <= endYear ) { - String name = tablePrefix + SEP + startYear + tableSuffix; + String name = tablePrefix + SEP + startYear + tableSuffix; partitions.add( name.toLowerCase() ); startYear++; } return partitions.prunePartitions( validPartitions ); } - + public static Partitions getPartitions( List periods, String tablePrefix, String tableSuffix, Set validPartitions ) { UniqueArrayList partitions = new UniqueArrayList(); - + for ( NameableObject period : periods ) { partitions.addAll( getPartitions( (Period) period, tablePrefix, tableSuffix, null ).getPartitions() ); } - + return new Partitions( new ArrayList( partitions ) ).prunePartitions( validPartitions ); } - + public static ListMap getPartitionPeriodMap( List periods, String tablePrefix, String tableSuffix, Set validPartitions ) { ListMap map = new ListMap(); - + for ( NameableObject period : periods ) { map.putValue( getPartitions( (Period) period, tablePrefix, tableSuffix, null ).prunePartitions( validPartitions ), period ); } - + return map; } @@ -119,17 +121,17 @@ public static ListMap getPeriodTypePeriodMap( Collection periods ) { ListMap map = new ListMap(); - + for ( NameableObject period : periods ) { String periodTypeName = ((Period) period).getPeriodType().getName(); - + map.putValue( periodTypeName, period ); } - + return map; } - + /** * Returns the year of the given date. */ @@ -137,7 +139,7 @@ { return new Cal( date ).getYear(); } - + /** * Returns the max date within the year of the given date. */