← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18120: Category combo validation, WIP

 

------------------------------------------------------------
revno: 18120
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2015-01-24 12:12:45 +0100
message:
  Category combo validation, WIP
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementCategoryService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DefaultDataElementCategoryService.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-datadictionary/src/main/java/org/hisp/dhis/dd/action/categorycombo/ValidateDataElementCategoryComboAction.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/dataelement/DataElementCategoryService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementCategoryService.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementCategoryService.java	2015-01-24 11:12:45 +0000
@@ -342,6 +342,22 @@
      * @return a collection of DataElementCategoryCombos.
      */
     Collection<DataElementCategoryCombo> getAttributeCategoryCombos();
+    
+    /**
+     * Validates the category combo. Possible return values are:
+     * 
+     * <ul>
+     * <li>category_combo_is_null</li>
+     * <li>category_combo_must_have_at_least_one_category</li>
+     * <li>category_combo_cannot_have_duplicate_categories</li>
+     * <li>categories_must_have_at_least_one_category_option</li>
+     * <li>categories_cannot_share_category_options</li>
+     * </ul>
+     * 
+     * @param categoryCombo the category combo to validate.
+     * @return null if valid, non-empty string if invalid.
+     */
+    String validateCategoryCombo( DataElementCategoryCombo categoryCombo );
 
     // -------------------------------------------------------------------------
     // CategoryOptionCombo

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DefaultDataElementCategoryService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DefaultDataElementCategoryService.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DefaultDataElementCategoryService.java	2015-01-24 11:12:45 +0000
@@ -47,6 +47,8 @@
 import org.hisp.dhis.system.util.FilterUtils;
 import org.springframework.transaction.annotation.Transactional;
 
+import com.google.common.collect.Sets;
+
 /**
  * @author Abyot Asalefew
  */
@@ -488,6 +490,42 @@
             categoryComboStore.getCategoryCombosByDimensionType( DataElementCategoryCombo.DIMENSION_TYPE_ATTTRIBUTE ) );
     }
 
+    @Override
+    public String validateCategoryCombo( DataElementCategoryCombo categoryCombo )
+    {
+        if ( categoryCombo == null )
+        {
+            return "category_combo_is_null";
+        }
+        
+        if ( categoryCombo.getCategories() == null || categoryCombo.getCategories().isEmpty() )
+        {
+            return "category_combo_must_have_at_least_one_category";
+        }
+        
+        if ( Sets.newHashSet( categoryCombo.getCategories() ).size() < categoryCombo.getCategories().size() )
+        {
+            return "category_combo_cannot_have_duplicate_categories";
+        }
+        
+        Set<DataElementCategoryOption> categoryOptions = new HashSet<DataElementCategoryOption>();
+        
+        for ( DataElementCategory category: categoryCombo.getCategories() )
+        {
+            if ( category == null || category.getCategoryOptions().isEmpty() )
+            {
+                return "categories_must_have_at_least_one_category_option";
+            }
+            
+            if ( !Sets.intersection( categoryOptions, Sets.newHashSet( category.getCategoryOptions() ) ).isEmpty() )
+            {
+                return "categories_cannot_share_category_options";
+            }
+        }
+        
+        return null;
+    }
+    
     // -------------------------------------------------------------------------
     // CategoryOptionCombo
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-datadictionary/src/main/java/org/hisp/dhis/dd/action/categorycombo/ValidateDataElementCategoryComboAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-datadictionary/src/main/java/org/hisp/dhis/dd/action/categorycombo/ValidateDataElementCategoryComboAction.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-datadictionary/src/main/java/org/hisp/dhis/dd/action/categorycombo/ValidateDataElementCategoryComboAction.java	2015-01-24 11:12:45 +0000
@@ -130,6 +130,8 @@
             }
         }
 
+        //TODO validateCategoryCombo
+        
         return SUCCESS;
     }
 }