← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21382: Centralized code through DimensionService.getDataDimensionalItemObject

 

------------------------------------------------------------
revno: 21382
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-12-08 23:49:42 +0100
message:
  Centralized code through DimensionService.getDataDimensionalItemObject
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DataDimensionItem.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultDataQueryService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dimension/DefaultDimensionService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dimension/DimensionServiceTest.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/common/DataDimensionItem.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DataDimensionItem.java	2015-11-20 15:20:20 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DataDimensionItem.java	2015-12-08 22:49:42 +0000
@@ -57,8 +57,8 @@
 public class DataDimensionItem
 {    
     public static final Set<Class<? extends IdentifiableObject>> DATA_DIMENSION_CLASSES = ImmutableSet.<Class<? extends IdentifiableObject>>builder().
-        add( Indicator.class ).add( DataElement.class ).add( DataElementOperand.class ).
-        add( DataSet.class ).add( ProgramIndicator.class ).add( ProgramDataElement.class ).add( ProgramTrackedEntityAttribute.class ).build();
+        add( Indicator.class ).add( DataElement.class ).add( DataElementOperand.class ).add( DataSet.class ).
+        add( ProgramIndicator.class ).add( ProgramDataElement.class ).add( ProgramTrackedEntityAttribute.class ).build();
     
     public static final Map<DataDimensionItemType, Class<? extends NameableObject>> DATA_DIMENSION_TYPE_CLASS_MAP = ImmutableMap.<DataDimensionItemType, Class<? extends NameableObject>>builder().
         put( DataDimensionItemType.INDICATOR, Indicator.class ).put( DataDimensionItemType.AGGREGATE_DATA_ELEMENT, DataElement.class ).

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionService.java	2015-11-26 18:17:05 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionService.java	2015-12-08 22:49:42 +0000
@@ -54,4 +54,12 @@
     void mergeAnalyticalObject( BaseAnalyticalObject object );
     
     void mergeEventAnalyticalObject( EventAnalyticalObject object );
+
+    /**
+     * Gets a dimension item object which are among the data dimension item objects.
+     * 
+     * @param dimensionItem the dimension item identifier.
+     * @return a dimensional item object.
+     */
+    DimensionalItemObject getDataDimensionalItemObject( String dimensionItem );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java	2015-11-22 11:20:29 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java	2015-12-08 22:49:42 +0000
@@ -58,7 +58,8 @@
     public static final String COMPOSITE_DIM_OBJECT_ESCAPED_SEP = "\\.";
     public static final String COMPOSITE_DIM_OBJECT_PLAIN_SEP = ".";
     public static final String TITLE_ITEM_SEP = ", ";
-    public static final String NULL_REPLACEMENT = "[n/a]";
+    
+    private static final String NULL_REPLACEMENT = "[n/a]";
 
     private static final Pattern INT_PATTERN = Pattern.compile( "^(0|-?[1-9]\\d*)$" );
     private static final Pattern COMPOSITE_DIM_OBJECT_PATTERN = Pattern.compile( "([a-zA-Z]\\w{10})\\.([a-zA-Z]\\w{10})" );

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultDataQueryService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultDataQueryService.java	2015-11-22 17:08:47 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultDataQueryService.java	2015-12-08 22:49:42 +0000
@@ -48,13 +48,11 @@
 import static org.hisp.dhis.common.DimensionalObjectUtils.asTypedList;
 import static org.hisp.dhis.common.DimensionalObjectUtils.getDimensionalItemIds;
 import static org.hisp.dhis.commons.collection.ListUtils.sort;
-import static org.hisp.dhis.commons.util.TextUtils.splitSafe;
 import static org.hisp.dhis.organisationunit.OrganisationUnit.KEY_LEVEL;
 import static org.hisp.dhis.organisationunit.OrganisationUnit.KEY_ORGUNIT_GROUP;
 import static org.hisp.dhis.organisationunit.OrganisationUnit.KEY_USER_ORGUNIT;
 import static org.hisp.dhis.organisationunit.OrganisationUnit.KEY_USER_ORGUNIT_CHILDREN;
 import static org.hisp.dhis.organisationunit.OrganisationUnit.KEY_USER_ORGUNIT_GRANDCHILDREN;
-import static org.hisp.dhis.common.DimensionalObjectUtils.COMPOSITE_DIM_OBJECT_ESCAPED_SEP;
 
 import java.util.ArrayList;
 import java.util.Collections;
@@ -71,7 +69,7 @@
 import org.hisp.dhis.common.AnalyticalObject;
 import org.hisp.dhis.common.BaseDimensionalObject;
 import org.hisp.dhis.common.CodeGenerator;
-import org.hisp.dhis.common.DataDimensionItem;
+import org.hisp.dhis.common.DimensionService;
 import org.hisp.dhis.common.DimensionType;
 import org.hisp.dhis.common.DimensionalItemObject;
 import org.hisp.dhis.common.DimensionalObject;
@@ -83,8 +81,6 @@
 import org.hisp.dhis.common.comparator.IdentifiableObjectNameComparator;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementGroup;
-import org.hisp.dhis.dataelement.DataElementOperand;
-import org.hisp.dhis.dataelement.DataElementOperandService;
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
@@ -94,11 +90,7 @@
 import org.hisp.dhis.period.RelativePeriodEnum;
 import org.hisp.dhis.period.RelativePeriods;
 import org.hisp.dhis.period.comparator.AscendingPeriodEndDateComparator;
-import org.hisp.dhis.program.ProgramDataElement;
-import org.hisp.dhis.program.ProgramService;
-import org.hisp.dhis.program.ProgramTrackedEntityAttribute;
 import org.hisp.dhis.system.util.ReflectionUtils;
-import org.hisp.dhis.trackedentity.TrackedEntityAttributeService;
 import org.hisp.dhis.user.CurrentUserService;
 import org.hisp.dhis.user.User;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -116,13 +108,7 @@
     private OrganisationUnitService organisationUnitService;
 
     @Autowired
-    private DataElementOperandService operandService;
-
-    @Autowired
-    private ProgramService programService;
-    
-    @Autowired
-    private TrackedEntityAttributeService attributeService;
+    private DimensionService dimensionService;
     
     @Autowired
     private CurrentUserService currentUserService;
@@ -260,35 +246,13 @@
                         dataDimensionItems.addAll( group.getMembers() );
                     }
                 }
-                else if ( DimensionalObjectUtils.isCompositeDimensionalObject( uid ) )
-                {
-                    String id0 = splitSafe( uid, COMPOSITE_DIM_OBJECT_ESCAPED_SEP, 0 );
-                    String id1 = splitSafe( uid, COMPOSITE_DIM_OBJECT_ESCAPED_SEP, 1 );
-                    
-                    DataElementOperand operand = null;
-                    ProgramDataElement programDataElement = null;                    
-                    ProgramTrackedEntityAttribute programAttribute = null;
-                    
-                    if ( ( operand = operandService.getDataElementOperand( id0, id1 ) ) != null )
-                    {
-                        dataDimensionItems.add( operand );
-                    }                    
-                    else if ( ( programDataElement = programService.getProgramDataElement( id0, id1 ) ) != null )
-                    {
-                        dataDimensionItems.add( programDataElement );
-                    }
-                    else if ( ( programAttribute = attributeService.getProgramTrackedEntityAttribute( id0, id1 ) ) != null )
-                    {
-                        dataDimensionItems.add( programAttribute );
-                    }
-                }
-                else if ( CodeGenerator.isValidCode( uid ) )
-                {
-                    DimensionalItemObject item = idObjectManager.get( DataDimensionItem.DATA_DIMENSION_CLASSES, uid );
-                    
-                    if ( item != null )
-                    {
-                        dataDimensionItems.add( item );
+                else
+                {
+                    DimensionalItemObject dimItemObject = dimensionService.getDataDimensionalItemObject( uid );
+                    
+                    if ( dimItemObject != null )
+                    {
+                        dataDimensionItems.add( dimItemObject );
                     }
                 }
             }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dimension/DefaultDimensionService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dimension/DefaultDimensionService.java	2015-12-08 22:12:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dimension/DefaultDimensionService.java	2015-12-08 22:49:42 +0000
@@ -56,6 +56,7 @@
 
 import org.hisp.dhis.common.BaseAnalyticalObject;
 import org.hisp.dhis.common.BaseDimensionalObject;
+import org.hisp.dhis.common.CodeGenerator;
 import org.hisp.dhis.common.DataDimensionItem;
 import org.hisp.dhis.common.DimensionService;
 import org.hisp.dhis.common.DimensionType;
@@ -346,6 +347,44 @@
         return copy;
     }
 
+    @Override
+    public DimensionalItemObject getDataDimensionalItemObject( String dimensionItem )
+    {
+        if ( DimensionalObjectUtils.isCompositeDimensionalObject( dimensionItem ) )
+        {
+            String id0 = splitSafe( dimensionItem, COMPOSITE_DIM_OBJECT_ESCAPED_SEP, 0 );
+            String id1 = splitSafe( dimensionItem, COMPOSITE_DIM_OBJECT_ESCAPED_SEP, 1 );
+
+            DataElementOperand operand = null;
+            ProgramDataElement programDataElement = null;                    
+            ProgramTrackedEntityAttribute programAttribute = null;
+            
+            if ( ( operand = operandService.getOrAddDataElementOperand( id0, id1 ) ) != null )
+            {
+                return operand;
+            }
+            else if ( ( programDataElement = programService.getOrAddProgramDataElement( id0, id1 ) ) != null )
+            {
+                return programDataElement;
+            }
+            else if ( ( programAttribute = attributeService.getOrAddProgramTrackedEntityAttribute( id0, id1 ) ) != null )
+            {
+                return programAttribute;
+            }
+        }
+        else if ( CodeGenerator.isValidCode( dimensionItem ) )
+        {
+            DimensionalItemObject itemObject = identifiableObjectManager.get( DataDimensionItem.DATA_DIMENSION_CLASSES, dimensionItem );
+            
+            if ( itemObject != null )
+            {
+                return itemObject;
+            }
+        }
+        
+        return null;
+    }
+
     //--------------------------------------------------------------------------
     // Supportive methods
     //--------------------------------------------------------------------------
@@ -385,36 +424,11 @@
                 {
                     for ( String uid : uids )
                     {
-                        if ( DimensionalObjectUtils.isCompositeDimensionalObject( uid ) )
-                        {
-                            String id0 = splitSafe( uid, COMPOSITE_DIM_OBJECT_ESCAPED_SEP, 0 );
-                            String id1 = splitSafe( uid, COMPOSITE_DIM_OBJECT_ESCAPED_SEP, 1 );
-
-                            DataElementOperand operand = null;
-                            ProgramDataElement programDataElement = null;                    
-                            ProgramTrackedEntityAttribute programAttribute = null;
-                            
-                            if ( ( operand = operandService.getOrAddDataElementOperand( id0, id1 ) ) != null )
-                            {
-                                object.getDataDimensionItems().add( DataDimensionItem.create( operand ) );
-                            }
-                            else if ( ( programDataElement = programService.getOrAddProgramDataElement( id0, id1 ) ) != null )
-                            {
-                                object.getDataDimensionItems().add( DataDimensionItem.create( programDataElement ) );
-                            }
-                            else if ( ( programAttribute = attributeService.getOrAddProgramTrackedEntityAttribute( id0, id1 ) ) != null )
-                            {
-                                object.getDataDimensionItems().add( DataDimensionItem.create( programAttribute ) );
-                            }
-                        }
-                        else
-                        {
-                            DimensionalItemObject dataObject = identifiableObjectManager.get( DataDimensionItem.DATA_DIMENSION_CLASSES, uid );
-                            
-                            if ( dataObject != null )
-                            {
-                                object.getDataDimensionItems().add( DataDimensionItem.create( dataObject ) );
-                            }
+                        DimensionalItemObject dimItemObject = getDataDimensionalItemObject( uid );
+                        
+                        if ( dimItemObject != null )
+                        {
+                            object.getDataDimensionItems().add( DataDimensionItem.create( dimItemObject ) );
                         }
                     }
                 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dimension/DimensionServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dimension/DimensionServiceTest.java	2015-12-08 22:12:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dimension/DimensionServiceTest.java	2015-12-08 22:49:42 +0000
@@ -33,6 +33,7 @@
 import static org.hisp.dhis.period.RelativePeriodEnum.LAST_12_MONTHS;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNotNull;
 
 import org.hisp.dhis.DhisSpringTest;
 import org.hisp.dhis.common.BaseDimensionalItemObject;
@@ -41,6 +42,8 @@
 import org.hisp.dhis.common.DimensionType;
 import org.hisp.dhis.common.DimensionalItemObject;
 import org.hisp.dhis.common.DimensionalObject;
+import org.hisp.dhis.common.DimensionalObjectUtils;
+import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -49,7 +52,9 @@
 import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.period.Period;
+import org.hisp.dhis.program.Program;
 import org.hisp.dhis.reporttable.ReportTable;
+import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
@@ -64,6 +69,10 @@
     private DataElement deA;
     private DataElement deB;
     
+    private Program prA;
+    
+    private TrackedEntityAttribute atA;
+    
     private Period peA;
     private Period peB;
     
@@ -94,6 +103,9 @@
     private OrganisationUnitGroupService organisationUnitGroupService;
     
     @Autowired
+    private IdentifiableObjectManager idObjectManager;
+    
+    @Autowired
     private DimensionService dimensionService;
     
     @Override
@@ -105,6 +117,14 @@
         dataElementService.addDataElement( deA );
         dataElementService.addDataElement( deB );
         
+        prA = createProgram( 'A' );
+        
+        idObjectManager.save( prA );
+        
+        atA = createTrackedEntityAttribute( 'A' );
+        
+        idObjectManager.save( atA );
+        
         peA = createPeriod( "201201" );
         peB = createPeriod( "201202" );
         peLast12Months = new BaseDimensionalItemObject( LAST_12_MONTHS.toString() );
@@ -238,5 +258,19 @@
         assertEquals( 2, reportTable.getDataDimensionItems().size() );
         assertEquals( 2, reportTable.getPeriods().size() );
         assertEquals( 3, reportTable.getOrganisationUnitGroups().size() );
-    }    
+    }
+    
+    @Test
+    public void testGetDimensionalItemObject()
+    {
+        String idA = deA.getUid();
+        String idB = prA.getUid() + DimensionalObjectUtils.COMPOSITE_DIM_OBJECT_PLAIN_SEP + deA.getUid();
+        String idC = prA.getUid() + DimensionalObjectUtils.COMPOSITE_DIM_OBJECT_PLAIN_SEP + atA.getUid();
+        
+        assertNotNull( dimensionService.getDataDimensionalItemObject( idA ) );
+        assertNotNull( dimensionService.getDataDimensionalItemObject( idB ) );
+        assertNotNull( dimensionService.getDataDimensionalItemObject( idC ) );
+        
+        assertEquals( deA, dimensionService.getDataDimensionalItemObject( idA ) );
+    }
 }