← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 1781: Reorganized DataBrowserService - Move some of supporting methods from DateUtils class and SearchA...

 

------------------------------------------------------------
revno: 1781
committer: hieu <hieu.hispvietnam@xxxxxxxxx>
branch nick: trunk
timestamp: Wed 2010-04-14 16:23:51 +0700
message:
  Reorganized DataBrowserService - Move some of supporting methods from DateUtils class and SearchAction class into it.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/databrowser/DataBrowserService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/databrowser/DataBrowserTable.java
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/databrowser/DefaultDataBrowserService.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/DateUtils.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/java/org/hisp/dhis/dataadmin/action/databrowser/SearchAction.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/databrowser/DataBrowserService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/databrowser/DataBrowserService.java	2009-08-24 13:55:25 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/databrowser/DataBrowserService.java	2010-04-14 09:23:51 +0000
@@ -27,6 +27,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.period.PeriodType;
 
 /**
@@ -47,7 +48,8 @@
      * @param startDate the start date
      * @param endDate the end date
      * @param periodType the period type
-     * @return DataBrowserTable the DataBrowserTable with structure for presentation
+     * @return DataBrowserTable the DataBrowserTable with structure for
+     *         presentation
      */
     DataBrowserTable getDataSetsInPeriod( String startDate, String endDate, PeriodType periodType );
 
@@ -58,30 +60,33 @@
      * @param startDate the start date
      * @param endDate the end date
      * @param periodType the period type
-     * @return DataBrowserTable the DataBrowserTable with structure for presentation
+     * @return DataBrowserTable the DataBrowserTable with structure for
+     *         presentation
      */
     DataBrowserTable getDataElementGroupsInPeriod( String startDate, String endDate, PeriodType periodType );
-  
+
     /**
-     * Method that retrieves - all OrganisationUnitGroups with DataElement quantity - in
-     * a given period and type (OrgUnitGroup | Count)
+     * Method that retrieves - all OrganisationUnitGroups with DataElement
+     * quantity - in a given period and type (OrgUnitGroup | Count)
      * 
      * @param startDate the start date
      * @param endDate the end date
      * @param periodType the period type
-     * @return DataBrowserTable the DataBrowserTable with structure for presentation
+     * @return DataBrowserTable the DataBrowserTable with structure for
+     *         presentation
      */
     DataBrowserTable getOrgUnitGroupsInPeriod( String startDate, String endDate, PeriodType periodType );
 
     /**
-     * Method that retrieves - all OrganisationUnits with DataElement quantity - in a
-     * given period - that is child of a given OrganisationUnit parent.
+     * Method that retrieves - all OrganisationUnits with DataElement quantity -
+     * in a given period - that is child of a given OrganisationUnit parent.
      * 
      * @param orgUnitParent the OrganisationUnit parent
      * @param startDate the start date
      * @param endDate the end date
      * @param periodType the period type
-     * @return DataBrowserTable the DataBrowserTable with structure for presentation
+     * @return DataBrowserTable the DataBrowserTable with structure for
+     *         presentation
      */
     DataBrowserTable getOrgUnitsInPeriod( Integer orgUnitParent, String startDate, String endDate, PeriodType periodType );
 
@@ -93,48 +98,80 @@
      * @param startDate the start date
      * @param endDate the end date
      * @param periodType the period type
-     * @return DataBrowserTable the DataBrowserTable with structure for presentation
+     * @return DataBrowserTable the DataBrowserTable with structure for
+     *         presentation
      */
     DataBrowserTable getCountDataElementsForDataSetInPeriod( Integer dataSetId, String startDate, String endDate,
         PeriodType periodType );
 
     /**
      * Method that retrieves - all the DataElements count - in a given period -
-     * for a given DataElementGroup and returns a DataBrowserTable with the data.
+     * for a given DataElementGroup and returns a DataBrowserTable with the
+     * data.
      * 
      * @param dataElementGroupId the DataElementGroup id
      * @param startDate the start date
      * @param endDate the end date
      * @param periodType the period type
-     * @return DataBrowserTable the DataBrowserTable with structure for presentation
+     * @return DataBrowserTable the DataBrowserTable with structure for
+     *         presentation
      */
     DataBrowserTable getCountDataElementsForDataElementGroupInPeriod( Integer dataElementGroupId, String startDate,
         String endDate, PeriodType periodType );
 
     /**
      * Method retrieves - all the DataElementGroups count - in a given period -
-     * for a given OrganisationUnitGroup and returns a DataBrowserTable with the data.
+     * for a given OrganisationUnitGroup and returns a DataBrowserTable with the
+     * data.
      * 
      * @param orgUnitGroupId the OrganisationUnitGroup id
      * @param startDate the start date
      * @param endDate the end date
      * @param periodType the period type
-     * @return DataBrowserTable the DataBrowserTable with structure for presentation
-     */  
+     * @return DataBrowserTable the DataBrowserTable with structure for
+     *         presentation
+     */
     DataBrowserTable getCountDataElementGroupsForOrgUnitGroupInPeriod( Integer orgUnitGroupId, String startDate,
         String endDate, PeriodType periodType );
 
     /**
-     * Method that retrieves - all the DataElements count - in a given period - 
-     * for a given OrganisationUnit and returns a DataBrowserTable with the data.
+     * Method that retrieves - all the DataElements count - in a given period -
+     * for a given OrganisationUnit and returns a DataBrowserTable with the
+     * data.
      * 
      * @param orgUnitId the OrganisationUnit id
      * @param startDate the start date
      * @param endDate the end date
      * @param periodType the period type
-     * @return DataBrowserTable the DataBrowserTable with structure for presentation
-     */
-    DataBrowserTable getCountDataElementsForOrgUnitInPeriod( Integer orgUnitId, String startDate,
-        String endDate, PeriodType periodType );    
+     * @return DataBrowserTable the DataBrowserTable with structure for
+     *         presentation
+     */
+    DataBrowserTable getCountDataElementsForOrgUnitInPeriod( Integer orgUnitId, String startDate, String endDate,
+        PeriodType periodType );
+
+    /**
+     * This method converts a string from the date format "yyyy-MM-dd" to "MMMM
+     * yyyy". Default is Monthly period type.
+     * 
+     * @param date is the string to be converted.
+     * @param periodService service of period
+     * @param format is i18n format object
+     * @return converted string if the date is valid, else the original string
+     *         is returned
+     */
+    String convertDate( PeriodType periodType, String dateString, I18nFormat format );
+
+    /**
+     * This method returns the string of name of periods in the list which
+     * between fromDate and toDate input params
+     * 
+     * @param periodType is the type of period
+     * @param fromDate the beginning date
+     * @param toDate the end date
+     * @param format is i18n format object
+     * @return The name of periods in the list which between fromDate and toDate
+     *         input params will be returned.
+     */
+    String getFromToDateFormat( PeriodType periodType, String fromDate, String toDate, I18nFormat format );
 
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/databrowser/DataBrowserTable.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/databrowser/DataBrowserTable.java	2010-04-13 06:13:35 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/databrowser/DataBrowserTable.java	2010-04-14 09:23:51 +0000
@@ -100,7 +100,8 @@
      * 
      * Adds one Column to the table structure.
      * 
-     * ResultSet contents: index 1: Id index 2: Name index 3: counts_of_aggregated_values
+     * ResultSet contents: index 1: Id index 2: Name index 3:
+     * counts_of_aggregated_values
      * 
      * The ResultSet can also contain index 4: PeriodId AND index 5: ColumnName
      * if it does, this functions will add data to rows divided in new columns
@@ -126,9 +127,13 @@
             if ( data.getColumnCount() == 5 )
             {
                 if ( data.getColumnLabel( 5 ).equalsIgnoreCase( "ColumnHeader" ) )
+                {
                     hasColumnName = true;
+                }
                 if ( data.getColumnLabel( 4 ).equalsIgnoreCase( "PeriodId" ) )
+                {
                     hasPeriodIds = true;
+                }
             }
         }
         catch ( SQLException e1 )
@@ -147,6 +152,7 @@
 
             Integer prid = 0;
             boolean makeEmptyCol = false;
+
             while ( resultSet.next() )
             {
                 if ( resultSet.isFirst() )
@@ -164,13 +170,15 @@
                 if ( hasPeriodIds )
                 {
                     int tmp = resultSet.getInt( 4 );
+
                     if ( prid == 0 )
+                    {
                         prid = tmp;
+                    }
                     if ( tmp != prid )
                     {
                         prid = tmp;
                         makeEmptyCol = true;
-
                     }
 
                     if ( makeEmptyCol )
@@ -202,7 +210,9 @@
         }
 
         if ( countRows == 0 )
+        {
             this.addZeroColumn();
+        }
         return countRows;
     }
 
@@ -344,7 +354,7 @@
     {
         queryCount++;
     }
-    
+
     /**
      * This overridden toString method writes out the DataBrowserTable as a
      * table to screen. Very handy in case of debugging and testing.
@@ -353,14 +363,20 @@
     public String toString()
     {
         String ret = "\n\n";
+
         for ( MetaValue col : this.getColumns() )
         {
             ret += "|" + col.getName();
         }
+
         ret += "|\n";
         int i = ret.length();
+
         for ( int o = 0; o < i; o++ )
+        {
             ret += "-";
+        }
+
         ret += "\n";
         Iterator<MetaValue> it = this.getRows().iterator();
 
@@ -368,14 +384,16 @@
         {
             MetaValue rowMeta = it.next();
             ret += "|" + rowMeta.getName();
+
             for ( Integer rowItem : col )
             {
                 ret += "|" + rowItem;
             }
             ret += "|\n";
         }
+
         ret += "\n\n";
         return ret;
     }
-    
+
 }

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/databrowser/DefaultDataBrowserService.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/databrowser/DefaultDataBrowserService.java	2010-04-13 06:13:35 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/databrowser/DefaultDataBrowserService.java	2010-04-14 09:23:51 +0000
@@ -31,13 +31,17 @@
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 
+import org.hisp.dhis.i18n.I18nFormat;
+import org.hisp.dhis.period.CalendarPeriodType;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodService;
 import org.hisp.dhis.period.PeriodType;
+import org.hisp.dhis.period.comparator.AscendingPeriodComparator;
 import org.hisp.dhis.system.util.DateUtils;
 
 /**
@@ -51,6 +55,10 @@
 
     private static final String ENDDATE = "3000-01-01";
 
+    private static final String SPACE = " ";
+
+    private static final String DASH = " - ";
+
     // -------------------------------------------------------------------------
     // Dependencies
     // -------------------------------------------------------------------------
@@ -212,6 +220,54 @@
         return table;
     }
 
+    public String convertDate( PeriodType periodType, String dateString, I18nFormat format )
+    {
+        if ( !DateUtils.dateIsValid( dateString ) )
+        {
+            return dateString;
+        }
+
+        SimpleDateFormat dateFormat = new SimpleDateFormat( DateUtils.DEFAULT_DATE_FORMAT );
+
+        try
+        {
+            Date date = dateFormat.parse( dateString );
+            CalendarPeriodType calendarPeriodType = (CalendarPeriodType) periodType;
+
+            return format.formatPeriod( calendarPeriodType.createPeriod( date ) );
+        }
+        catch ( ParseException pe )
+        {
+            throw new RuntimeException( "Date string could not be parsed: " + dateString );
+        }
+    }
+
+    public String getFromToDateFormat( PeriodType periodType, String fromDate, String toDate, I18nFormat format )
+    {
+        String stringFormatDate = "";
+        List<Period> periods = new ArrayList<Period>( this.getPeriodsList( periodType, fromDate, toDate ) );
+
+        for ( Period period : periods )
+        {
+            String sTemp = format.formatPeriod( period );
+
+            if ( stringFormatDate.isEmpty() )
+            {
+                stringFormatDate = SPACE + sTemp;
+            }
+            else if ( !stringFormatDate.contains( sTemp ) )
+            {
+                stringFormatDate += DASH + sTemp;
+            }
+        }
+
+        return stringFormatDate;
+    }
+
+    // -------------------------------------------------------------------------
+    // Supportive methods
+    // -------------------------------------------------------------------------
+
     /**
      * Helper-method that finds all PeriodIds between a given period. Uses
      * functionality already in the DHIS. Returns a list with all id's that was
@@ -257,4 +313,47 @@
 
         return betweenPeriodIds;
     }
+
+    /**
+     * This is a helper method for checking if the fromDate is later than the
+     * toDate. This is necessary in case a user sends the dates with HTTP GET.
+     * 
+     * @param fromDate
+     * @param toDate
+     * @return List of Periods
+     */
+    private List<Period> getPeriodsList( PeriodType periodType, String fromDate, String toDate )
+    {
+        String formatString = DateUtils.DEFAULT_DATE_FORMAT;
+        SimpleDateFormat sdf = new SimpleDateFormat( formatString );
+
+        Date date1 = new Date();
+        Date date2 = new Date();
+
+        try
+        {
+            date1 = sdf.parse( fromDate );
+            date2 = sdf.parse( toDate );
+
+            List<Period> periods = new ArrayList<Period>( periodService.getPeriodsBetweenDates( periodType, date1,
+                date2 ) );
+
+            if ( periods.isEmpty() )
+            {
+                CalendarPeriodType calendarPeriodType = (CalendarPeriodType) periodType;
+
+                periods.add( calendarPeriodType.createPeriod( date1 ) );
+                periods.add( calendarPeriodType.createPeriod( date2 ) );
+            }
+
+            Collections.sort( periods, new AscendingPeriodComparator() );
+
+            return periods;
+        }
+        catch ( ParseException e )
+        {
+            return null; // The user hasn't specified any dates
+        }
+    }
+
 }

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/DateUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/DateUtils.java	2010-04-13 07:12:30 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/DateUtils.java	2010-04-14 09:23:51 +0000
@@ -33,11 +33,6 @@
 import java.util.Date;
 
 import org.apache.commons.validator.DateValidator;
-import org.hisp.dhis.i18n.I18nFormat;
-import org.hisp.dhis.period.CalendarPeriodType;
-import org.hisp.dhis.period.MonthlyPeriodType;
-import org.hisp.dhis.period.PeriodService;
-import org.hisp.dhis.period.PeriodType;
 import org.joda.time.DateTime;
 import org.joda.time.Days;
 
@@ -356,38 +351,6 @@
     }
 
     /**
-     * This method converts a string from the date format "yyyy-MM-dd" to "MMMM
-     * yyyy". Default is Monthly period type.
-     * 
-     * @param date is the string to be converted.
-     * @param periodService service of period
-     * @param format is i18n format object
-     * @return converted string if the date is valid, else the original string
-     *         is returned
-     */
-    public static String convertDate( PeriodService periodService, String dateString, I18nFormat format )
-    {
-        if ( !dateIsValid( dateString ) )
-        {
-            return dateString;
-        }
-
-        SimpleDateFormat dateFormat = new SimpleDateFormat( DEFAULT_DATE_FORMAT );
-
-        try
-        {
-            Date date = dateFormat.parse( dateString );
-            CalendarPeriodType calendarPeriodType = (CalendarPeriodType) getMonthlyPeriodType( periodService );
-
-            return format.formatPeriod( calendarPeriodType.createPeriod( date ) );
-        }
-        catch ( ParseException pe )
-        {
-            throw new RuntimeException( "Date string could not be parsed: " + dateString );
-        }
-    }
-
-    /**
      * This method adds days to a date
      * 
      * @param date the date.
@@ -439,14 +402,4 @@
         }
     }
 
-    /**
-     * This method returns monthly period type is default
-     * 
-     * @param periodService service of period persistent object
-     */
-    public static final PeriodType getMonthlyPeriodType( PeriodService periodService )
-    {
-        return periodService.getPeriodTypeByName( MonthlyPeriodType.NAME );
-    }
-
 }

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/java/org/hisp/dhis/dataadmin/action/databrowser/SearchAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/java/org/hisp/dhis/dataadmin/action/databrowser/SearchAction.java	2010-04-13 06:00:13 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/java/org/hisp/dhis/dataadmin/action/databrowser/SearchAction.java	2010-04-14 09:23:51 +0000
@@ -27,11 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
@@ -48,11 +45,9 @@
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.organisationunit.comparator.OrganisationUnitNameComparator;
 import org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager;
-import org.hisp.dhis.period.CalendarPeriodType;
-import org.hisp.dhis.period.Period;
+import org.hisp.dhis.period.MonthlyPeriodType;
 import org.hisp.dhis.period.PeriodService;
 import org.hisp.dhis.period.PeriodType;
-import org.hisp.dhis.period.comparator.AscendingPeriodComparator;
 import org.hisp.dhis.system.util.DateUtils;
 import org.hisp.dhis.util.SessionUtils;
 
@@ -81,10 +76,6 @@
 
     private static final String KEY_DATABROWSERTABLE = "dataBrowserTableResults";
 
-    private static final String SPACE = " ";
-
-    private static final String DASH = " - ";
-
     // -------------------------------------------------------------------------
     // Dependencies
     // -------------------------------------------------------------------------
@@ -509,7 +500,7 @@
 
         // Get format standard for periods which appropriate with from date, to
         // date and period type
-        fromToDate = getFromToDateFormat( periodType, fromDate, toDate );
+        fromToDate = dataBrowserService.getFromToDateFormat( periodType, fromDate, toDate, format );
 
         if ( fromToDate == null )
         {
@@ -543,75 +534,11 @@
     private void convertColumnNames( DataBrowserTable dataBrowserTable )
     {
         allColumnsConverted = dataBrowserTable.getColumns();
+        PeriodType monthlyPeriodType = periodService.getPeriodTypeByName( MonthlyPeriodType.NAME );
 
         for ( MetaValue col : allColumnsConverted )
         {
-            col.setName( DateUtils.convertDate( periodService, col.getName(), format ) );
-        }
-    }
-
-    /**
-     * This is a helper method for checking if the fromDate is later than the
-     * toDate. This is necessary in case a user sends the dates with HTTP GET.
-     * 
-     * @param fromDate
-     * @param toDate
-     * @return List of Periods
-     */
-    private List<Period> getPeriodsList( PeriodType periodType, String fromDate, String toDate )
-    {
-        String formatString = DateUtils.DEFAULT_DATE_FORMAT;
-        SimpleDateFormat sdf = new SimpleDateFormat( formatString );
-
-        Date date1 = new Date();
-        Date date2 = new Date();
-
-        try
-        {
-            date1 = sdf.parse( fromDate );
-            date2 = sdf.parse( toDate );
-
-            List<Period> periods = new ArrayList<Period>( periodService.getPeriodsBetweenDates( periodType, date1,
-                date2 ) );
-
-            if ( periods.isEmpty() )
-            {
-                CalendarPeriodType calendarPeriodType = (CalendarPeriodType) periodType;
-
-                periods.add( calendarPeriodType.createPeriod( date1 ) );
-                periods.add( calendarPeriodType.createPeriod( date2 ) );
-            }
-
-            Collections.sort( periods, new AscendingPeriodComparator() );
-
-            return periods;
-        }
-        catch ( ParseException e )
-        {
-            return null; // The user hasn't specified any dates
-        }
-    }
-
-    @SuppressWarnings( "unchecked" )
-    private String getFromToDateFormat( PeriodType periodType, String fromDate, String toDate )
-    {
-        String stringFormatDate = "";
-        List<Period> periods = new ArrayList( this.getPeriodsList( periodType, fromDate, toDate ) );
-
-        for ( Period period : periods )
-        {
-            String sTemp = format.formatPeriod( period );
-
-            if ( stringFormatDate.isEmpty() )
-            {
-                stringFormatDate = SPACE + sTemp;
-            }
-            else if ( !stringFormatDate.contains( sTemp ) )
-            {
-                stringFormatDate += DASH + sTemp;
-            }
-        }
-
-        return stringFormatDate;
+            col.setName( dataBrowserService.convertDate( monthlyPeriodType, col.getName(), format ) );
+        }
     }
 }