← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11078: Implemented dynamic dimensionality for server side chart generation

 

------------------------------------------------------------
revno: 11078
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-05-29 15:05:35 +0200
message:
  Implemented dynamic dimensionality for server side chart generation
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/chart/Chart.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/chart/ChartService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/RelativePeriods.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/chart/Chart.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/chart/Chart.java	2013-05-24 10:54:03 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/chart/Chart.java	2013-05-29 13:05:35 +0000
@@ -27,18 +27,15 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.hisp.dhis.common.DimensionalObject.DATA_X_DIM_ID;
-import static org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID;
-import static org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID;
-
 import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
+import java.util.Date;
 import java.util.List;
 
 import org.hisp.dhis.common.BaseAnalyticalObject;
+import org.hisp.dhis.common.DimensionalObject;
 import org.hisp.dhis.common.DxfNamespaces;
 import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.common.IdentifiableObjectUtils;
 import org.hisp.dhis.common.NameableObject;
 import org.hisp.dhis.common.view.DetailedView;
 import org.hisp.dhis.common.view.DimensionalView;
@@ -46,7 +43,7 @@
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.period.Period;
-import org.hisp.dhis.period.comparator.AscendingPeriodEndDateComparator;
+import org.hisp.dhis.user.User;
 
 import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.fasterxml.jackson.annotation.JsonProperty;
@@ -64,8 +61,6 @@
 {
     private static final long serialVersionUID = 2570074075484545534L;
     
-    private static final Comparator<Period> PERIOD_COMPARATOR = new AscendingPeriodEndDateComparator();
-
     public static final String SIZE_NORMAL = "normal";
     public static final String SIZE_WIDE = "wide";
     public static final String SIZE_TALL = "tall";
@@ -110,8 +105,6 @@
 
     private boolean showData;
 
-    private boolean rewindRelativePeriods;
-
     // -------------------------------------------------------------------------
     // Transient properties
     // -------------------------------------------------------------------------
@@ -119,8 +112,8 @@
     private transient I18nFormat format;
 
     private transient List<Period> relativePeriods = new ArrayList<Period>();
-
-    private transient List<OrganisationUnit> relativeOrganisationUnits = new ArrayList<OrganisationUnit>();
+    
+    private transient User user;
 
     // -------------------------------------------------------------------------
     // Constructors
@@ -136,24 +129,55 @@
     }
 
     // -------------------------------------------------------------------------
+    // Init
+    // -------------------------------------------------------------------------
+
+    public void init( User user, Date date, OrganisationUnit organisationUnit, I18nFormat format )
+    {
+        this.user = user;
+        this.relativePeriodDate = date;
+        this.relativeOrganisationUnit = organisationUnit;
+        this.format = format;        
+    }
+    
+    // -------------------------------------------------------------------------
     // Logic
     // -------------------------------------------------------------------------
 
     public List<NameableObject> series()
     {
-        return dimensionToList( series );
+        DimensionalObject object = getDimensionalObject( series, relativePeriodDate, user, true, format );
+        
+        return object != null ? object.getItems() : null;
     }
 
     public List<NameableObject> category()
     {
-        return dimensionToList( category );
-    }
-
-    public NameableObject filter()
-    {
-        List<NameableObject> list = dimensionToList( filterDimensions.get( 0 ) ); //TODO
-
-        return list != null && !list.isEmpty() ? list.iterator().next() : null;
+        DimensionalObject object = getDimensionalObject( category, relativePeriodDate, user, true, format );
+        
+        return object != null ? object.getItems() : null;
+    }
+
+    public List<NameableObject> filters()
+    {
+        List<NameableObject> filterItems = new ArrayList<NameableObject>();
+        
+        for ( String filter : filterDimensions )
+        {
+            DimensionalObject object = getDimensionalObject( filter, relativePeriodDate, user, true, format );
+            
+            if ( object != null )
+            {
+                filterItems.addAll( object.getItems() );
+            }
+        }
+        
+        return filterItems;
+    }
+    
+    public String generateTitle()
+    {
+        return IdentifiableObjectUtils.join( filters() );
     }
 
     @Override
@@ -167,22 +191,12 @@
             filters.addAll( getDimensionalObjectList( filter ) );
         }
     }
-        
-    public String generateTitle()
-    {
-        if ( PERIOD_DIM_ID.equals( filterDimensions.get( 0 ) ) )
-        {
-            return format.formatPeriod( getAllPeriods().get( 0 ) );
-        }
-
-        return filter().getName();
-    }
 
     public List<OrganisationUnit> getAllOrganisationUnits()
     {
-        if ( relativeOrganisationUnits != null && !relativeOrganisationUnits.isEmpty() )
+        if ( transientOrganisationUnits != null && !transientOrganisationUnits.isEmpty() )
         {
-            return relativeOrganisationUnits;
+            return transientOrganisationUnits;
         }
         else
         {
@@ -213,40 +227,6 @@
         return list;
     }
 
-    private List<NameableObject> dimensionToList( String dimension )
-    {
-        List<NameableObject> list = new ArrayList<NameableObject>();
-
-        if ( DATA_X_DIM_ID.equals( dimension ) )
-        {
-            list.addAll( dataElements );
-            list.addAll( indicators );
-            list.addAll( dataSets );
-        }
-        else if ( PERIOD_DIM_ID.equals( dimension ) )
-        {
-            List<Period> periods = getAllPeriods();
-            namePeriods( periods, format );
-            Collections.sort( periods, PERIOD_COMPARATOR );
-            list.addAll( periods );
-        }
-        else if ( ORGUNIT_DIM_ID.equals( dimension ) )
-        {
-            list.addAll( getAllOrganisationUnits() );
-        }
-
-        return list;
-    }
-
-    private void namePeriods( List<Period> periods, I18nFormat format )
-    {
-        for ( Period period : periods )
-        {
-            period.setName( format.formatPeriod( period ) );
-            period.setShortName( format.formatPeriod( period ) );
-        }
-    }
-
     /**
      * Sets all dimensions for this chart.
      *
@@ -541,18 +521,6 @@
         this.relativePeriods = relativePeriods;
     }
 
-    @JsonIgnore
-    public List<OrganisationUnit> getRelativeOrganisationUnits()
-    {
-        return relativeOrganisationUnits;
-    }
-
-    @JsonIgnore
-    public void setRelativeOrganisationUnits( List<OrganisationUnit> relativeOrganisationUnits )
-    {
-        this.relativeOrganisationUnits = relativeOrganisationUnits;
-    }
-
     // -------------------------------------------------------------------------
     // Merge with
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/chart/ChartService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/chart/ChartService.java	2013-05-21 11:39:31 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/chart/ChartService.java	2013-05-29 13:05:35 +0000
@@ -59,14 +59,14 @@
      * 
      * @param chart the chart to use as basis for the JFreeChart generation.
      * @param date the date to use as basis for relative periods, can be null.
-     * @param unit the org unit to use as basis for relative units, will
+     * @param organisationUnit the org unit to use as basis for relative units, will
      *        override the current user org unit if set, can be null.
      * @param format the i18n format.
      * @return a JFreeChart object.
      */
-    JFreeChart getJFreeChart( Chart chart, Date date, OrganisationUnit unit, I18nFormat format );
+    JFreeChart getJFreeChart( Chart chart, Date date, OrganisationUnit organisationUnit, I18nFormat format );
     
-    JFreeChart getJFreePeriodChart( Indicator indicator, OrganisationUnit unit, boolean title, I18nFormat format );
+    JFreeChart getJFreePeriodChart( Indicator indicator, OrganisationUnit organisationUnit, boolean title, I18nFormat format );
 
     JFreeChart getJFreeOrganisationUnitChart( Indicator indicator, OrganisationUnit parent, boolean title, I18nFormat format );
 

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java	2013-05-27 16:33:14 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java	2013-05-29 13:05:35 +0000
@@ -126,6 +126,8 @@
 
     protected boolean userOrganisationUnitChildren;
 
+    protected boolean rewindRelativePeriods;
+
     // -------------------------------------------------------------------------
     // Analytical properties
     // -------------------------------------------------------------------------
@@ -147,7 +149,9 @@
     protected transient List<DataElementCategoryOptionCombo> transientCategoryOptionCombos = new ArrayList<DataElementCategoryOptionCombo>();
         
     protected transient Date relativePeriodDate;
-    
+
+    protected transient OrganisationUnit relativeOrganisationUnit;
+        
     // -------------------------------------------------------------------------
     // Logic
     // -------------------------------------------------------------------------
@@ -213,11 +217,20 @@
         }
         else if ( PERIOD_DIM_ID.equals( dimension ) )
         {
+            setPeriodNames( periods, dynamicNames, format );
+            
             items.addAll( periods );
             
             if ( hasRelativePeriods() )
             {
-                items.addAll( relatives.getRelativePeriods( date, format, dynamicNames ) ); //TODO dyn names if period is on rows
+                if ( rewindRelativePeriods )
+                {
+                    items.addAll( relatives.getRewindedRelativePeriods( 1, date, format, dynamicNames ) );
+                }
+                else
+                {
+                    items.addAll( relatives.getRelativePeriods( date, format, dynamicNames ) );
+                }
             }
             
             type = DimensionType.PERIOD;
@@ -234,7 +247,7 @@
             
             if ( userOrganisationUnitChildren && user != null && user.hasOrganisationUnit() )
             {
-                items.addAll( user.getOrganisationUnit().getChildren() );
+                items.addAll( user.getOrganisationUnit().getSortedChildren() );
             }
             
             type = DimensionType.ORGANISATIONUNIT;
@@ -429,6 +442,14 @@
         
         return categoryDims;
     }
+
+    private void setPeriodNames( List<Period> periods, boolean dynamicNames, I18nFormat format )
+    {
+        for ( Period period : periods )
+        {
+            RelativePeriods.setName( period, null, dynamicNames, format );
+        }
+    }
     
     /**
      * Splits the keys of the given map on the dimension identifier separator, 
@@ -701,16 +722,41 @@
         this.userOrganisationUnitChildren = userOrganisationUnitChildren;
     }
 
+    @JsonProperty
+    @JsonView( {DetailedView.class, ExportView.class} )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0)
+    public boolean isRewindRelativePeriods()
+    {
+        return rewindRelativePeriods;
+    }
+
+    public void setRewindRelativePeriods( boolean rewindRelativePeriods )
+    {
+        this.rewindRelativePeriods = rewindRelativePeriods;
+    }
+
     // -------------------------------------------------------------------------
     // Transient properties
     // -------------------------------------------------------------------------
 
     @JsonIgnore
+    public List<OrganisationUnit> getTransientOrganisationUnits()
+    {
+        return transientOrganisationUnits;
+    }
+
+    @JsonIgnore
     public Date getRelativePeriodDate()
     {
         return relativePeriodDate;
     }
 
+    @JsonIgnore
+    public OrganisationUnit getRelativeOrganisationUnit()
+    {
+        return relativeOrganisationUnit;
+    }
+
     // -------------------------------------------------------------------------
     // Web domain properties
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/RelativePeriods.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/RelativePeriods.java	2013-05-24 12:13:28 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/RelativePeriods.java	2013-05-29 13:05:35 +0000
@@ -703,6 +703,21 @@
     }
 
     /**
+     * Returns a date.
+     *
+     * @param months the number of months to subtract from the current date.
+     * @param date the date representing now, ignored if null.
+     * @return a date.
+     */
+    private static Date subtractMonths( int months, Date date )
+    {
+        Calendar cal = PeriodType.createCalendarInstance( date );
+        cal.add( Calendar.MONTH, (months * -1) );
+
+        return cal.getTime();
+    }
+
+    /**
      * Sets the name and short name of the given Period. The name will be
      * formatted to the real period name if the given dynamicNames argument is
      * true. The short name will be formatted in any case.
@@ -713,7 +728,7 @@
      * @param format       the I18nFormat.
      * @return a period.
      */
-    private Period setName( Period period, String periodName, boolean dynamicNames, I18nFormat format )
+    public static Period setName( Period period, String periodName, boolean dynamicNames, I18nFormat format )
     {
         period.setName( dynamicNames && format != null ? format.formatPeriod( period ) : periodName );
         period.setShortName( format != null ? format.formatPeriod( period ) : null );
@@ -723,26 +738,11 @@
     /**
      * Returns a date.
      *
-     * @param months the number of months to subtract from the current date.
-     * @param date the date representing now, ignored if null.
-     * @return a date.
-     */
-    private Date subtractMonths( int months, Date date )
-    {
-        Calendar cal = PeriodType.createCalendarInstance( date );
-        cal.add( Calendar.MONTH, (months * -1) );
-
-        return cal.getTime();
-    }
-
-    /**
-     * Returns a date.
-     *
      * @param months the number of weeks to subtract from the current date.
      * @param date the date representing now, ignored if null.
      * @return a date.
      */
-    public Date subtractWeeks( int weeks, Date date )
+    public static Date subtractWeeks( int weeks, Date date )
     {
         Calendar cal = PeriodType.createCalendarInstance( date );
         cal.add( Calendar.DAY_OF_YEAR, (weeks * -7) );

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java	2013-05-28 17:05:43 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java	2013-05-29 13:05:35 +0000
@@ -212,11 +212,6 @@
     private transient String reportingPeriodName;
 
     /**
-     * The parent organisation unit.
-     */
-    private transient OrganisationUnit parentOrganisationUnit;
-    
-    /**
      * The title of the report table grid.
      */
     private transient String title;
@@ -314,14 +309,14 @@
         if ( organisationUnit != null && hasReportParams() && reportParams.isParamParentOrganisationUnit() )
         {
             organisationUnit.setCurrentParent( true );
-            this.parentOrganisationUnit = organisationUnit;
+            this.relativeOrganisationUnit = organisationUnit;
             addTransientOrganisationUnits( organisationUnit.getChildren() );
             addTransientOrganisationUnit( organisationUnit );
         }
 
         if ( organisationUnit != null && hasReportParams() && reportParams.isParamOrganisationUnit() )
         {
-            this.parentOrganisationUnit = organisationUnit;
+            this.relativeOrganisationUnit = organisationUnit;
             addTransientOrganisationUnit( organisationUnit );
         }
 
@@ -495,7 +490,7 @@
      */
     public String getParentOrganisationUnitName()
     {
-        return parentOrganisationUnit != null ? parentOrganisationUnit.getName() : EMPTY;
+        return relativeOrganisationUnit != null ? relativeOrganisationUnit.getName() : EMPTY;
     }
 
     public boolean isDoIndicators()
@@ -965,18 +960,6 @@
     }
 
     @JsonIgnore
-    public OrganisationUnit getParentOrganisationUnit()
-    {
-        return parentOrganisationUnit;
-    }
-
-    @JsonIgnore
-    public void setParentOrganisationUnit( OrganisationUnit parentOrganisationUnit )
-    {
-        this.parentOrganisationUnit = parentOrganisationUnit;
-    }
-
-    @JsonIgnore
     public String getTitle()
     {
         return title;

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.java	2013-05-23 16:57:03 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.java	2013-05-29 13:05:35 +0000
@@ -41,7 +41,6 @@
 import java.awt.Color;
 import java.awt.Font;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
@@ -189,43 +188,21 @@
         return getJFreeChart( chart, null, null, format );
     }
 
-    public JFreeChart getJFreeChart( Chart chart, Date date, OrganisationUnit unit, I18nFormat format )
+    public JFreeChart getJFreeChart( Chart chart, Date date, OrganisationUnit organisationUnit, I18nFormat format )
     {
-        if ( chart.hasRelativePeriods() )
-        {
-            List<Period> periods = chart.isRewindRelativePeriods() ?
-                chart.getRelatives().getRewindedRelativePeriods( 1, date, format, true ) :
-                chart.getRelatives().getRelativePeriods( date, format, true );
-
-            chart.setRelativePeriods( periodService.reloadPeriods( periods ) );
-        }
-
-        User currentUser = currentUserService.getCurrentUser();
-
-        if ( currentUser != null && chart.hasUserOrgUnit() && 
-            ( currentUser.getOrganisationUnit() != null || unit != null ) )
-        {
-            if ( unit == null )
-            {
-                unit = currentUser.getOrganisationUnit();
-            }
-            
-            if ( chart.isUserOrganisationUnit() )
-            {
-                chart.getRelativeOrganisationUnits().add( unit );
-            }
-            else if ( chart.isUserOrganisationUnitChildren() )
-            {
-                chart.getRelativeOrganisationUnits().addAll( unit.hasChild() ? unit.getSortedChildren() : Arrays.asList( unit ) );
-            }
-        }
-
-        chart.setFormat( format );
-
-        return getJFreeChart( chart, !chart.isHideSubtitle(), format );
+        User user = currentUserService.getCurrentUser();
+
+        if ( organisationUnit == null && user != null )
+        {
+            organisationUnit = user.getOrganisationUnit();
+        }
+        
+        chart.init( user, date, organisationUnit, format );
+
+        return getJFreeChart( chart, !chart.isHideSubtitle() );
     }
 
-    public JFreeChart getJFreePeriodChart( Indicator indicator, OrganisationUnit unit, boolean title, I18nFormat format )
+    public JFreeChart getJFreePeriodChart( Indicator indicator, OrganisationUnit unit, boolean title, I18nFormat format ) //TODO remove?
     {
         List<Period> periods = periodService.reloadPeriods(
             new RelativePeriods().setLast12Months( true ).getRelativePeriods( format, true ) );
@@ -245,11 +222,11 @@
         chart.getOrganisationUnits().add( unit );
         chart.setFormat( format );
 
-        return getJFreeChart( chart, title, format );
+        return getJFreeChart( chart, title );
     }
 
     public JFreeChart getJFreeOrganisationUnitChart( Indicator indicator, OrganisationUnit parent, boolean title,
-        I18nFormat format )
+        I18nFormat format ) //TODO remove?
     {
         List<Period> periods = periodService.reloadPeriods(
             new RelativePeriods().setThisYear( true ).getRelativePeriods( format, true ) );
@@ -269,7 +246,7 @@
         chart.setOrganisationUnits( parent.getSortedChildren() );
         chart.setFormat( format );
 
-        return getJFreeChart( chart, title, format );
+        return getJFreeChart( chart, title );
     }
 
     public JFreeChart getJFreeChart( List<Indicator> indicators, List<DataElement> dataElements,
@@ -290,7 +267,7 @@
         chart.setFormat( format );
         chart.setName( chart.generateTitle() );
 
-        return getJFreeChart( chart, false, format );
+        return getJFreeChart( chart, false );
     }
 
     public JFreeChart getJFreeChart( String name, PlotOrientation orientation, CategoryLabelPositions labelPositions,
@@ -527,7 +504,7 @@
     /**
      * Returns a JFreeChart of type defined in the chart argument.
      */
-    private JFreeChart getJFreeChart( Chart chart, boolean subTitle, I18nFormat format )
+    private JFreeChart getJFreeChart( Chart chart, boolean subTitle )
     {
         final BarRenderer barRenderer = getBarRenderer();
         final LineAndShapeRenderer lineRenderer = getLineRenderer();
@@ -538,7 +515,7 @@
 
         CategoryPlot plot = null;
 
-        CategoryDataset[] dataSets = getCategoryDataSet( chart, format );
+        CategoryDataset[] dataSets = getCategoryDataSet( chart );
 
         if ( chart.isType( TYPE_LINE ) )
         {
@@ -718,10 +695,10 @@
         return multiplePieChart;
     }
 
-    private CategoryDataset[] getCategoryDataSet( Chart chart, I18nFormat format )
+    private CategoryDataset[] getCategoryDataSet( Chart chart )
     {
-        Map<String, Double> valueMap = analyticsService.getAggregatedDataValueMapping( chart, format );
-
+        Map<String, Double> valueMap = analyticsService.getAggregatedDataValueMapping( chart, chart.getFormat() );
+        
         DefaultCategoryDataset regularDataSet = new DefaultCategoryDataset();
         DefaultCategoryDataset regressionDataSet = new DefaultCategoryDataset();