← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16325: Added a maintenance function for validating and potentially updating the data elements in express...

 

------------------------------------------------------------
revno: 16325
committer: Tran Chau<tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-08-05 23:23:58 +0700
message:
  Added a maintenance function for validating and potentially updating the data elements in expression and option combos in expression properties of all expressions.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/java/org/hisp/dhis/dataadmin/action/maintenance/PerformMaintenanceAction.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/javascript/maintenance.js
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/maintenanceForm.vm


--
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/expression/ExpressionService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java	2014-07-27 19:59:20 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java	2014-08-05 16:23:58 +0000
@@ -363,4 +363,11 @@
      * @param indicators the collection of Indicators.
      */
     Set<DataElementOperand> getOperandsInIndicators( Collection<Indicator> indicators );
+
+    /**
+     * the data element and option combo of expression formula, which doesn't
+     * match with any data elements and option combos existed in database, will
+     * be created and saved into database
+     */
+    void updateDataElementsInExpression();
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java	2014-07-27 19:59:20 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java	2014-08-05 16:23:58 +0000
@@ -36,6 +36,7 @@
 import static org.hisp.dhis.system.util.MathUtils.calculateExpression;
 import static org.hisp.dhis.system.util.MathUtils.isEqual;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -52,9 +53,12 @@
 import org.hisp.dhis.constant.Constant;
 import org.hisp.dhis.constant.ConstantService;
 import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategory;
 import org.hisp.dhis.dataelement.DataElementCategoryCombo;
+import org.hisp.dhis.dataelement.DataElementCategoryOption;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementCategoryService;
+import org.hisp.dhis.dataelement.DataElementDomain;
 import org.hisp.dhis.dataelement.DataElementOperand;
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.indicator.Indicator;
@@ -931,6 +935,93 @@
         return operands;
     }
 
+    public void updateDataElementsInExpression()
+    {
+        Collection<Expression> expressionList = expressionStore.getAll();
+        
+        for( Expression expression : expressionList )
+        {
+            final Matcher matcher = OPERAND_PATTERN.matcher( expression.getExpression() );
+
+            int i = 0;
+            String description = expression.getDescription();
+            while ( matcher.find() )
+            {
+                String optionComboUid = matcher.group( 2 );
+                DataElementCategoryOptionCombo optionCombo = categoryService.
+                    getDataElementCategoryOptionCombo( optionComboUid );
+                DataElementCategoryCombo categoryCombo = null;
+                if( optionCombo != null)
+                {
+                    categoryCombo = optionCombo.getCategoryCombo();
+                }
+                else
+                { 
+                    // Add Option
+                    
+                    DataElementCategoryOption option = new DataElementCategoryOption( description );
+                    option.setCode( optionComboUid );
+                    categoryService.addDataElementCategoryOption( option );
+                    
+                    // Add category
+                    
+                    DataElementCategory category = new DataElementCategory();
+                    category.setName( description );
+                    category.setDataDimension( false );
+                    category.setDataDimensionType( DataElementCategoryCombo.DIMENSION_TYPE_DISAGGREGATION );
+
+                    List<DataElementCategoryOption> categoryOptions = new ArrayList<DataElementCategoryOption>();
+                    categoryOptions.add( option );
+                    category.setCategoryOptions( categoryOptions );
+                    categoryService.addDataElementCategory( category );
+                    
+                    
+                    // Add CategoryCombo
+                    
+                    categoryCombo = new DataElementCategoryCombo();
+                    categoryCombo.setName( description );
+                    categoryCombo.setDimensionType( DataElementCategoryCombo.DIMENSION_TYPE_DISAGGREGATION );
+                    categoryCombo.setSkipTotal( false );
+
+                    List<DataElementCategory> categories = new ArrayList<DataElementCategory>();
+                    categories.add( category );
+                    categoryCombo.setCategories( categories );
+                    int categoryComboId = categoryService.addDataElementCategoryCombo( categoryCombo );
+                    categoryCombo.setId( categoryComboId );
+                    
+                    // Generate OptionCombo
+                    categoryService.generateOptionCombos( categoryCombo );
+                    
+                    optionCombo = categoryCombo.getOptionCombos().iterator().next();
+                    optionCombo.setUid( optionComboUid );
+                    categoryService.updateDataElementCategoryOptionCombo( optionCombo );
+                }
+              
+                String dataElementUid = matcher.group( 1 ) ;  
+                DataElement dataElement = dataElementService.getDataElement( dataElementUid );
+                 
+                if ( dataElement == null )
+                {
+                    dataElement = new DataElement();
+                    dataElement.setUid( dataElementUid );
+                    dataElement.setName( description );
+                    int length = ( expression.getDescription().length() < 50 ) ?  expression.getDescription().length() : 49;
+                    dataElement.setShortName( expression.getDescription().substring( 0, length ) );
+                    dataElement.setType( DataElement.VALUE_TYPE_INT );
+                    dataElement.setNumberType( DataElement.VALUE_TYPE_NUMBER );
+                    dataElement.setDomainType( DataElementDomain.AGGREGATE );
+                    dataElement.setAggregationOperator( DataElement.AGGREGATION_OPERATOR_SUM );
+                    dataElement.setCategoryCombo( categoryCombo );
+                    
+                    dataElementService.addDataElement( dataElement );
+                }
+                
+                i++;
+                description += "" + i;
+            }
+        }
+    }
+    
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/java/org/hisp/dhis/dataadmin/action/maintenance/PerformMaintenanceAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/java/org/hisp/dhis/dataadmin/action/maintenance/PerformMaintenanceAction.java	2014-04-16 15:03:22 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/java/org/hisp/dhis/dataadmin/action/maintenance/PerformMaintenanceAction.java	2014-08-05 16:23:58 +0000
@@ -37,8 +37,10 @@
 import org.hisp.dhis.completeness.DataSetCompletenessService;
 import org.hisp.dhis.dataelement.DataElementCategoryService;
 import org.hisp.dhis.datamart.DataMartManager;
+import org.hisp.dhis.expression.ExpressionService;
 import org.hisp.dhis.maintenance.MaintenanceService;
 import org.hisp.dhis.user.CurrentUserService;
+import org.springframework.beans.factory.annotation.Autowired;
 
 import com.opensymphony.xwork2.Action;
 
@@ -50,30 +52,30 @@
     implements Action
 {
     private static final Log log = LogFactory.getLog( PerformMaintenanceAction.class );
-    
+
     // -------------------------------------------------------------------------
     // Dependencies
     // -------------------------------------------------------------------------
 
-    @Resource(name="org.hisp.dhis.analytics.AnalyticsTableService")
+    @Resource( name = "org.hisp.dhis.analytics.AnalyticsTableService" )
     private AnalyticsTableService analyticsTableService;
 
-    @Resource(name="org.hisp.dhis.analytics.CompletenessTableService")
+    @Resource( name = "org.hisp.dhis.analytics.CompletenessTableService" )
     private AnalyticsTableService completenessTableService;
-    
-    @Resource(name="org.hisp.dhis.analytics.CompletenessTargetTableService")
+
+    @Resource( name = "org.hisp.dhis.analytics.CompletenessTargetTableService" )
     private AnalyticsTableService completenessTargetTableService;
-    
-    @Resource(name="org.hisp.dhis.analytics.EventAnalyticsTableService")
+
+    @Resource( name = "org.hisp.dhis.analytics.EventAnalyticsTableService" )
     private AnalyticsTableService eventAnalyticsTableService;
-    
+
     private MaintenanceService maintenanceService;
 
     public void setMaintenanceService( MaintenanceService maintenanceService )
     {
         this.maintenanceService = maintenanceService;
     }
-    
+
     private DataSetCompletenessService completenessService;
 
     public void setCompletenessService( DataSetCompletenessService completenessService )
@@ -82,12 +84,12 @@
     }
 
     private AggregatedDataValueService aggregatedDataValueService;
-    
+
     public void setAggregatedDataValueService( AggregatedDataValueService aggregatedDataValueService )
     {
         this.aggregatedDataValueService = aggregatedDataValueService;
     }
-        
+
     private DataMartManager dataMartManager;
 
     public void setDataMartManager( DataMartManager dataMartManager )
@@ -101,20 +103,23 @@
     {
         this.currentUserService = currentUserService;
     }
-    
+
     private DataElementCategoryService categoryService;
 
     public void setCategoryService( DataElementCategoryService categoryService )
     {
         this.categoryService = categoryService;
     }
+    
+    @Autowired
+    private ExpressionService expressionService;
 
     // -------------------------------------------------------------------------
     // Input
     // -------------------------------------------------------------------------
-    
+
     private boolean clearAnalytics;
-    
+
     public void setClearAnalytics( boolean clearAnalytics )
     {
         this.clearAnalytics = clearAnalytics;
@@ -128,7 +133,7 @@
     }
 
     public boolean dataMartIndex;
-    
+
     public void setDataMartIndex( boolean dataMartIndex )
     {
         this.dataMartIndex = dataMartIndex;
@@ -140,21 +145,21 @@
     {
         this.zeroValues = zeroValues;
     }
-    
+
     private boolean dataSetCompleteness;
 
     public void setDataSetCompleteness( boolean dataSetCompleteness )
     {
         this.dataSetCompleteness = dataSetCompleteness;
     }
-    
+
     private boolean prunePeriods;
 
     public void setPrunePeriods( boolean prunePeriods )
     {
         this.prunePeriods = prunePeriods;
     }
-    
+
     private boolean updateCategoryOptionCombos;
 
     public void setUpdateCategoryOptionCombos( boolean updateCategoryOptionCombos )
@@ -162,15 +167,23 @@
         this.updateCategoryOptionCombos = updateCategoryOptionCombos;
     }
 
+    private boolean updateExpression;
+
+    public void setUpdateExpression( boolean updateExpression )
+    {
+        this.updateExpression = updateExpression;
+    }
+    
+
     // -------------------------------------------------------------------------
     // Action implementation
     // -------------------------------------------------------------------------
-    
-    public String execute() 
+
+    public String execute()
         throws Exception
     {
         String username = currentUserService.getCurrentUsername();
-        
+
         if ( clearAnalytics )
         {
             analyticsTableService.dropTables();
@@ -178,61 +191,68 @@
             completenessTargetTableService.dropTables();
             eventAnalyticsTableService.dropTables();
         }
-        
+
         if ( clearDataMart )
         {
             aggregatedDataValueService.dropDataMart();
             aggregatedDataValueService.createDataMart();
-            
+
             log.info( "'" + username + "': Cleared data mart" );
         }
-        
+
         if ( dataMartIndex )
         {
             dataMartManager.dropDataValueIndex();
             dataMartManager.dropIndicatorValueIndex();
             dataMartManager.dropOrgUnitDataValueIndex();
             dataMartManager.dropOrgUnitIndicatorValueIndex();
-            
+
             dataMartManager.createDataValueIndex();
             dataMartManager.createIndicatorValueIndex();
             dataMartManager.createOrgUnitDataValueIndex();
             dataMartManager.createOrgUnitIndicatorValueIndex();
-            
+
             completenessService.dropIndex();
             completenessService.createIndex();
-            
+
             log.info( "'" + username + "': Rebuilt data mart indexes" );
         }
-        
+
         if ( zeroValues )
         {
             maintenanceService.deleteZeroDataValues();
-            
+
             log.info( "Cleared zero values" );
         }
-        
+
         if ( dataSetCompleteness )
         {
             completenessService.deleteDataSetCompleteness();
-            
+
             log.info( "'" + username + "': Cleared data completeness" );
         }
-        
+
         if ( prunePeriods )
         {
             maintenanceService.prunePeriods();
-            
+
             log.info( "'" + username + "': Pruned periods" );
         }
-        
+
         if ( updateCategoryOptionCombos )
         {
             categoryService.updateAllOptionCombos();
-            
+
             log.info( "'" + username + "': Updated category option combos" );
         }
-        
+
+        if ( updateExpression )
+        {
+            expressionService.updateDataElementsInExpression();
+
+            log.info( "'" + username + "': Updated data elements and category option combos in expression" );
+        }
+
         return SUCCESS;
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/javascript/maintenance.js'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/javascript/maintenance.js	2013-12-07 17:58:52 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/javascript/maintenance.js	2014-08-05 16:23:58 +0000
@@ -8,8 +8,9 @@
     var dataSetCompleteness = document.getElementById( "dataSetCompleteness" ).checked;
     var prunePeriods = document.getElementById( "prunePeriods" ).checked;
     var updateCategoryOptionCombos = document.getElementById( "updateCategoryOptionCombos" ).checked;
+    var updateExpression = document.getElementById( "updateExpression" ).checked;
     
-    if ( clearAnalytics || clearDataMart || dataMartIndex || zeroValues || dataSetCompleteness || prunePeriods || updateCategoryOptionCombos )
+    if ( clearAnalytics || clearDataMart || dataMartIndex || zeroValues || dataSetCompleteness || prunePeriods || updateCategoryOptionCombos || updateExpression )
     {
         setWaitMessage( i18n_performing_maintenance );
         
@@ -19,7 +20,8 @@
             "&zeroValues=" + zeroValues +
             "&dataSetCompleteness=" + dataSetCompleteness +
             "&prunePeriods=" + prunePeriods +
-            "&updateCategoryOptionCombos=" + updateCategoryOptionCombos;
+            "&updateCategoryOptionCombos=" + updateCategoryOptionCombos +
+            "&updateExpression=" + updateExpression;
         
 		$.ajax({
 			   type: "POST",

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/maintenanceForm.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/maintenanceForm.vm	2013-12-07 17:58:52 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/maintenanceForm.vm	2014-08-05 16:23:58 +0000
@@ -42,6 +42,11 @@
 </p>
 
 <p>
+    <input type="checkbox" id="updateExpression"/>
+    <label for="updateExpression">$i18n.getString( "update_the_data_elements_and_option_combos_in_expression" )</label>
+</p>
+
+<p>
 	<input type="button" value="$i18n.getString( 'perform_maintenance' )" onclick="performMaintenance()" style="width:170px"/>
 </p>