← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19158: Fixed bug - Exception thrown when to execute Schedule automated aggregation function; improve thi...

 

------------------------------------------------------------
revno: 19158
committer: Tran Chau<tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2015-05-15 11:04:17 +0700
message:
  Fixed bug - Exception thrown when to execute Schedule automated aggregation function; improve this function.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/caseaggregation/CaseAggregateSchedule.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/caseaggregation/CaseAggregationConditionService.java
  dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/DefaultCaseAggregationConditionService.java
  dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/hibernate/HibernateCaseAggregationConditionStore.java
  dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/CaseAggregateConditionTask.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/java/org/hisp/dhis/trackedentity/action/schedule/ScheduleCaseAggregateConditionAction.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/resources/META-INF/dhis/beans.xml


--
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/caseaggregation/CaseAggregateSchedule.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/caseaggregation/CaseAggregateSchedule.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/caseaggregation/CaseAggregateSchedule.java	2015-05-15 04:04:17 +0000
@@ -33,9 +33,9 @@
  */
 public class CaseAggregateSchedule
 {
-    private Integer dataSetId;
+    private Integer caseAggregateId;
 
-    private String dataSetName;
+    private String caseAggregateName;
 
     private String periodTypeName;
 
@@ -43,31 +43,31 @@
     {        
     }
 
-    public CaseAggregateSchedule( Integer dataSetId, String dataSetName, String periodTypeName )
+    public CaseAggregateSchedule( Integer caseAggregateId, String caseAggregateName, String periodTypeName )
     {
-        this.dataSetId = dataSetId;
-        this.dataSetName = dataSetName;
+        this.caseAggregateId = caseAggregateId;
+        this.caseAggregateName = caseAggregateName;
         this.periodTypeName = periodTypeName;
     }
 
-    public Integer getDataSetId()
-    {
-        return dataSetId;
-    }
-
-    public void setDataSetId( Integer dataSetId )
-    {
-        this.dataSetId = dataSetId;
-    }
-
-    public String getDataSetName()
-    {
-        return dataSetName;
-    }
-
-    public void setDataSetName( String dataSetName )
-    {
-        this.dataSetName = dataSetName;
+    public Integer getCaseAggregateId()
+    {
+        return caseAggregateId;
+    }
+
+    public void setCaseAggregateId( Integer caseAggregateId )
+    {
+        this.caseAggregateId = caseAggregateId;
+    }
+
+    public String getCaseAggregateName()
+    {
+        return caseAggregateName;
+    }
+
+    public void setCaseAggregateName( String caseAggregateName )
+    {
+        this.caseAggregateName = caseAggregateName;
     }
 
     public String getPeriodTypeName()

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/caseaggregation/CaseAggregationConditionService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/caseaggregation/CaseAggregationConditionService.java	2015-02-09 22:18:20 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/caseaggregation/CaseAggregationConditionService.java	2015-05-15 04:04:17 +0000
@@ -40,6 +40,7 @@
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.program.Program;
+import org.hisp.dhis.scheduling.TaskId;
 import org.hisp.dhis.trackedentity.TrackedEntityInstance;
 import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
 
@@ -193,7 +194,7 @@
      *        of each dataset. There are four options, include last month, last
      *        3 month, last 6 month and last 12 month
      */
-    void aggregate( List<CaseAggregateSchedule> caseAggregateSchedules, String taskStrategy );
+    void aggregate( List<CaseAggregateSchedule> caseAggregateSchedules, String taskStrategy, TaskId taskId );
     
     /**
      * Insert value aggregated from a {@link CaseAggregationCondition}

=== modified file 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/DefaultCaseAggregationConditionService.java'
--- dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/DefaultCaseAggregationConditionService.java	2015-05-01 16:42:48 +0000
+++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/DefaultCaseAggregationConditionService.java	2015-05-15 04:04:17 +0000
@@ -65,6 +65,8 @@
 import org.hisp.dhis.program.ProgramService;
 import org.hisp.dhis.program.ProgramStage;
 import org.hisp.dhis.program.ProgramStageService;
+import org.hisp.dhis.scheduling.TaskId;
+import org.hisp.dhis.system.notification.Notifier;
 import org.hisp.dhis.system.util.ConcurrentUtils;
 import org.hisp.dhis.system.util.DateUtils;
 import org.hisp.dhis.system.util.SystemUtils;
@@ -105,7 +107,10 @@
     
     @Autowired
     private DataElementCategoryService categoryService;
-
+    
+    @Autowired
+    private Notifier notifier;
+    
     // -------------------------------------------------------------------------
     // Getters && Setters
     // -------------------------------------------------------------------------
@@ -423,15 +428,23 @@
     }
     
     @Override
-    public void aggregate( List<CaseAggregateSchedule> caseAggregateSchedules, String taskStrategy )
-    {       
-        ConcurrentLinkedQueue<CaseAggregateSchedule> datasetQ = new ConcurrentLinkedQueue<>(
+    public void aggregate( List<CaseAggregateSchedule> caseAggregateSchedules, String taskStrategy, TaskId taskId )
+    {   
+        // Generate the periods
+        
+        for( CaseAggregateSchedule  schedule : caseAggregateSchedules ){
+            List<Period> periods = new ArrayList<>(aggregationConditionStore.getPeriods( schedule.getPeriodTypeName(),
+                taskStrategy ));
+            periodService.reloadPeriods( periods );
+        }
+        
+        ConcurrentLinkedQueue<CaseAggregateSchedule> queryBuilderQ = new ConcurrentLinkedQueue<>(
             caseAggregateSchedules );
         List<Future<?>> futures = new ArrayList<>();
 
         for ( int i = 0; i < getProcessNo(); i++ )
         { 
-            Future<?> future = aggregateValueManager( datasetQ, taskStrategy );
+            Future<?> future = aggregateValueManager( queryBuilderQ, taskStrategy, taskId );
             if ( future != null )
             {
                 futures.add( future );
@@ -555,21 +568,25 @@
     
     @Async
     private Future<?> aggregateValueManager( ConcurrentLinkedQueue<CaseAggregateSchedule> caseAggregateSchedule,
-        String taskStrategy )
+        String taskStrategy, TaskId taskId )
     {
         int attributeOptioncomboId = categoryService.getDefaultDataElementCategoryOptionCombo().getId();
         
         taskLoop: while ( true )
         {
-            CaseAggregateSchedule dataSet = caseAggregateSchedule.poll();
-            if ( dataSet == null )
+            CaseAggregateSchedule condition = caseAggregateSchedule.poll();
+            if ( condition == null )
             {
                 break taskLoop;
             }
 
-            Collection<Period> periods = aggregationConditionStore.getPeriods( dataSet.getPeriodTypeName(),
+            Collection<Period> periods = aggregationConditionStore.getPeriods( condition.getPeriodTypeName(),
                 taskStrategy );
-            aggregationConditionStore.runAggregate( null, dataSet, periods, attributeOptioncomboId );
+            if( taskId != null )
+            {
+                notifier.notify( taskId, "Importing data for " + condition.getCaseAggregateName() );
+            }
+            aggregationConditionStore.runAggregate( null, condition, periods, attributeOptioncomboId );
         }
 
         return null;

=== modified file 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/hibernate/HibernateCaseAggregationConditionStore.java'
--- dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/hibernate/HibernateCaseAggregationConditionStore.java	2015-05-01 16:42:48 +0000
+++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/hibernate/HibernateCaseAggregationConditionStore.java	2015-05-15 04:04:17 +0000
@@ -72,6 +72,7 @@
 import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
+import org.hisp.dhis.dataelement.DataElementCategoryService;
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.datavalue.DataValue;
 import org.hisp.dhis.datavalue.DataValueService;
@@ -145,6 +146,9 @@
     
     @Autowired
     private OrganisationUnitService orgunitService;
+
+    @Autowired
+    private DataElementCategoryService categoryService;
     
     // -------------------------------------------------------------------------
     // Implementation Methods
@@ -299,18 +303,18 @@
                 
                 DataValue dataValue = dataValueService.getDataValue( dataElement, period, source, optionCombo );
                 
-                if ( dataValue == null && value != 0 )
+                if ( dataValue == null )
                 {
                     dataValue = new DataValue( dataElement, period, source, optionCombo, attributeOptionCombo );
                     dataValue.setValue( value + "" );
                     dataValue.setStoredBy( row.getString("storedby") );
                     dataValueService.addDataValue( dataValue );
                 }
-                else if ( dataValue != null && value == 0 )
+                else if ( dataValue != null && value == 0 && !dataElement.isZeroIsSignificant() )
                 {
                     dataValueService.deleteDataValue( dataValue );
                 }
-                else if ( dataValue != null && value != 0 )
+                else if ( dataValue != null )
                 {
                     dataValue.setValue( value + "" );
                     dataValueService.updateDataValue( dataValue );
@@ -327,9 +331,8 @@
     public void insertAggregateValue( String sql, int dataElementId, int optionComboId, Collection<Integer> orgunitIds,
         Period period )
     {
-        try
+         try
         {
-            period = periodService.reloadPeriod( period );
             final String deleteDataValueSql = "delete from datavalue where dataelementid=" + dataElementId
                 + " and categoryoptioncomboid=" + optionComboId + " and sourceid in ("
                 + TextUtils.getCommaDelimitedString( orgunitIds ) + ") " + "and periodid = " + period.getId();
@@ -343,7 +346,7 @@
             ex.printStackTrace();
         }
     }
-
+    
     @Override
     public String parseExpressionToSql( boolean isInsert, CaseAggregationCondition aggregationCondition,
         int attributeOptionComboId, Collection<Integer> orgunitIds )
@@ -447,7 +450,7 @@
     }
 
     @Override
-    public void runAggregate( Collection<Integer> orgunitIds, CaseAggregateSchedule dataSet, Collection<Period> periods, int attributeOptioncomboId )
+    public void runAggregate( Collection<Integer> orgunitIds, CaseAggregateSchedule condition, Collection<Period> periods, int attributeOptioncomboId )
     {
         Collection<Integer> _orgunitIds = getServiceOrgunit();
 
@@ -461,19 +464,15 @@
             orgunitIds.retainAll( _orgunitIds );
         }
         
-        if ( orgunitIds.size() > 0 )
+        if (  !orgunitIds.isEmpty()  )
         {
             String sql = "select caseaggregationconditionid, aggregationdataelementid, optioncomboid, "
-                + "cagg.aggregationexpression as caseexpression, cagg.operator as caseoperator, cagg.desum as desumid "
-                + "from caseaggregationcondition cagg inner join datasetmembers dm "
-                + "on cagg.aggregationdataelementid=dm.dataelementid inner join dataset ds "
-                + "on ds.datasetid = dm.datasetid inner join periodtype pt "
-                + "on pt.periodtypeid=ds.periodtypeid inner join dataelement de "
-                + "on de.dataelementid=dm.dataelementid where ds.datasetid = " + dataSet.getDataSetId();
-    
+                + "aggregationexpression as caseexpression, operator as caseoperator, desum as desumid "
+                + "from caseaggregationcondition where caseaggregationconditionid = " + condition.getCaseAggregateId();
+            
             SqlRowSet rs = jdbcTemplate.queryForRowSet( sql );
     
-             while ( rs.next() )
+            if ( rs.next() )
             {
                  String caseExpression = rs.getString( "caseexpression" );
                  int dataElementId = rs.getInt( "aggregationdataelementid" );
@@ -485,11 +484,7 @@
                 for ( Period period : periods )
                 {
                     String insertSql = replacePeriodSql( insertParamsSql, period );
-                    
-                    if ( !orgunitIds.isEmpty() )
-                    { 
-                        insertAggregateValue( insertSql, dataElementId, optionComboId, _orgunitIds, period );
-                    }
+                    insertAggregateValue( insertSql, dataElementId, optionComboId, _orgunitIds, period );
                 }
     
             }

=== modified file 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/CaseAggregateConditionTask.java'
--- dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/CaseAggregateConditionTask.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/CaseAggregateConditionTask.java	2015-05-15 04:04:17 +0000
@@ -41,6 +41,8 @@
 import org.hisp.dhis.system.notification.Notifier;
 import org.hisp.dhis.system.util.Clock;
 import org.hisp.dhis.system.util.SystemUtils;
+import org.hisp.dhis.user.CurrentUserService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.support.rowset.SqlRowSet;
 
@@ -79,14 +81,11 @@
     {
         this.notifier = notifier;
     }
-
-    private TaskId taskId;
-
-    public void setTaskId( TaskId taskId )
-    {
-        this.taskId = taskId;
-    }
-
+    
+    @Autowired
+    private CurrentUserService currentUserService;
+
+   
     // -------------------------------------------------------------------------
     // Runnable implementation
     // -------------------------------------------------------------------------
@@ -94,7 +93,11 @@
     @Override
     public void run()
     {
-        final int cpuCores = SystemUtils.getCpuCores();
+        final int cpuCores = SystemUtils.getCpuCores();        
+
+        TaskId taskId = new TaskId( TaskCategory.AGGREGATE_QUERY_BUILDER, currentUserService.getCurrentUser() );
+        notifier.clear( taskId );
+        
         Clock clock = new Clock().startClock().logTime(
             "Aggregate process started, number of CPU cores: " + cpuCores + ", " + SystemUtils.getMemoryString() );
         notifier.clear( taskId ).notify( taskId, "Aggregate process started" );
@@ -102,24 +105,22 @@
         String taskStrategy = (String) systemSettingManager.getSystemSetting(
             KEY_SCHEDULE_AGGREGATE_QUERY_BUILDER_TASK_STRATEGY, DEFAULT_SCHEDULE_AGGREGATE_QUERY_BUILDER_TASK_STRATEGY );
 
-        // Get datasets which are used in case-aggregate-query-builder formula
-        
-        String datasetSQL = "select distinct( dm.datasetid ) as datasetid, pt.name as periodtypename, ds.name as datasetname";
-        datasetSQL += "      from caseaggregationcondition cagg inner join datasetmembers dm ";
-        datasetSQL += "            on cagg.aggregationdataelementid=dm.dataelementid inner join dataset ds ";
-        datasetSQL += "            on ds.datasetid = dm.datasetid inner join periodtype pt ";
-        datasetSQL += "            on pt.periodtypeid=ds.periodtypeid ";
+        String sql = "select cagg.caseaggregationconditionid as caseaggregationconditionid, cagg.name as caseaggregationconditionname, pt.name as periodtypename ";
+        sql += "from caseaggregationcondition cagg inner join datasetmembers dm  ";
+        sql += " on cagg.aggregationdataelementid=dm.dataelementid inner join dataset ds  ";
+        sql += " on ds.datasetid = dm.datasetid inner join periodtype pt  ";
+        sql += "  on pt.periodtypeid=ds.periodtypeid";
 
-        SqlRowSet rsDataset = jdbcTemplate.queryForRowSet( datasetSQL );
+        SqlRowSet rsCondition = jdbcTemplate.queryForRowSet( sql );
         List<CaseAggregateSchedule> caseAggregateSchedule = new ArrayList<>();
-        while ( rsDataset.next() )
+        while ( rsCondition.next() )
         {
-            CaseAggregateSchedule dataSet = new CaseAggregateSchedule( rsDataset.getInt( "datasetid" ),
-                rsDataset.getString( "datasetname" ), rsDataset.getString( "periodtypename" ) );
-            caseAggregateSchedule.add( dataSet );
+            CaseAggregateSchedule condition = new CaseAggregateSchedule( rsCondition.getInt( "caseaggregationconditionid" ),
+                rsCondition.getString( "caseaggregationconditionname"), rsCondition.getString( "periodtypename" ) );
+          caseAggregateSchedule.add( condition );
         }
-
-        aggregationConditionService.aggregate( caseAggregateSchedule, taskStrategy );
+        
+        aggregationConditionService.aggregate( caseAggregateSchedule, taskStrategy, taskId );
 
         clock.logTime( "Improrted aggregate data completed " );
 

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/java/org/hisp/dhis/trackedentity/action/schedule/ScheduleCaseAggregateConditionAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/java/org/hisp/dhis/trackedentity/action/schedule/ScheduleCaseAggregateConditionAction.java	2015-02-26 09:57:07 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/java/org/hisp/dhis/trackedentity/action/schedule/ScheduleCaseAggregateConditionAction.java	2015-05-15 04:04:17 +0000
@@ -34,11 +34,7 @@
 import java.util.Map;
 
 import org.hisp.dhis.scheduling.CaseAggregateConditionSchedulingManager;
-import org.hisp.dhis.scheduling.CaseAggregateConditionTask;
-import org.hisp.dhis.scheduling.TaskCategory;
-import org.hisp.dhis.scheduling.TaskId;
 import org.hisp.dhis.setting.SystemSettingManager;
-import org.hisp.dhis.system.notification.Notifier;
 import org.hisp.dhis.system.scheduling.Scheduler;
 import org.hisp.dhis.user.CurrentUserService;
 
@@ -63,13 +59,6 @@
         this.schedulingManager = schedulingManager;
     }
 
-    private CurrentUserService currentUserService;
-
-    public void setCurrentUserService( CurrentUserService currentUserService )
-    {
-        this.currentUserService = currentUserService;
-    }
-
     private SystemSettingManager systemSettingManager;
 
     public void setSystemSettingManager( SystemSettingManager systemSettingManager )
@@ -113,32 +102,14 @@
         this.taskStrategy = taskStrategy;
     }
 
-    private CaseAggregateConditionTask aggregateConditionTask;
-
-    public void setAggregateConditionTask( CaseAggregateConditionTask aggregateConditionTask )
-    {
-        this.aggregateConditionTask = aggregateConditionTask;
-    }
-
-    private Notifier notifier;
-
-    public void setNotifier( Notifier notifier )
-    {
-        this.notifier = notifier;
-    }
-
     // -------------------------------------------------------------------------
     // Action implementation
     // -------------------------------------------------------------------------
-
+ 
     @Override
     public String execute()
         throws Exception
     {
-        TaskId taskId = new TaskId( TaskCategory.AGGREGATE_QUERY_BUILDER, currentUserService.getCurrentUser() );
-        notifier.clear( taskId );
-        aggregateConditionTask.setTaskId( taskId );
-        
         if ( execute )
         {
             schedulingManager.executeTasks();

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/resources/META-INF/dhis/beans.xml	2015-04-11 13:18:25 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/resources/META-INF/dhis/beans.xml	2015-05-15 04:04:17 +0000
@@ -955,11 +955,6 @@
     <property name="systemSettingManager">
       <ref bean="org.hisp.dhis.setting.SystemSettingManager" />
     </property>
-    <property name="currentUserService">
-      <ref bean="org.hisp.dhis.user.CurrentUserService" />
-    </property>
-    <property name="notifier" ref="notifier" />
-    <property name="aggregateConditionTask" ref="aggregateQueryBuilder" />
   </bean>
 
   <!-- Program Indicator -->