← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21501: I18nService, translations of collections. Handling translations of objects of multiple classes in...

 

------------------------------------------------------------
revno: 21501
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-12-21 15:19:03 +0100
message:
  I18nService, translations of collections. Handling translations of objects of multiple classes inside a collection.
modified:
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/DefaultI18nService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/i18n/I18nServiceTest.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-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2015-12-17 14:29:34 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2015-12-21 14:19:03 +0000
@@ -54,7 +54,6 @@
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.Future;
@@ -922,10 +921,10 @@
 
         for ( DimensionalObject dimension : dimensions )
         {
-            i18nItems( dimension );
-
             List<DimensionalItemObject> items = new ArrayList<>( dimension.getItems() );
             
+            i18nService.internationalise( items );
+
             for ( DimensionalItemObject object : items )
             {
                 if ( DimensionType.PERIOD.equals( dimension.getDimensionType() ) && !calendar.isIso8601() )
@@ -991,29 +990,6 @@
 
         return metaData;
     }
-
-    /**
-     * Translate the items of the given dimensional object.
-     * 
-     * @param dimension the dimensional object.
-     * @param items the dimensional items.
-     */
-    private void i18nItems( DimensionalObject dimension )
-    {
-        Locale locale = i18nService.getCurrentLocale();
-        
-        if ( DimensionalObject.DATA_X_DIM_ID.equals( dimension.getDimension() ) )
-        {
-            for ( DimensionalItemObject item : dimension.getItems() )
-            {
-                i18nService.internationalise( item, locale );
-            }
-        }
-        else
-        {
-            i18nService.internationalise( dimension.getItems(), locale );            
-        }
-    }
     
     /**
      * Gets the number of available cores. Uses explicit number from system

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/DefaultI18nService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/DefaultI18nService.java	2015-10-21 09:23:15 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/DefaultI18nService.java	2015-12-21 14:19:03 +0000
@@ -35,6 +35,8 @@
 import org.hisp.dhis.translation.TranslationService;
 import org.hisp.dhis.user.UserSettingService;
 
+import com.google.common.collect.Multimaps;
+
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -88,11 +90,12 @@
     }
 
     @Override
+    @SuppressWarnings("unchecked")
     public void internationalise( Object object, Locale locale )
     {
         if ( isCollection( object ) )
         {
-            internationaliseCollection( (Collection<?>) object, locale );
+            internationaliseCollection( (Collection<Object>) object, locale );
         }
         else
         {
@@ -125,30 +128,35 @@
         }
     }
 
-    private void internationaliseCollection( Collection<?> objects, Locale locale )
+    private void internationaliseCollection( Collection<Object> objects, Locale locale )
     {
         if ( locale == null || objects == null || objects.size() == 0 )
         {
             return;
         }
 
-        Object peek = objects.iterator().next();
-
-        List<String> properties = getObjectPropertyNames( peek );
-
-        Collection<Translation> translations = translationService.getTranslations( getClassName( peek ), locale );
-
-        for ( Object object : objects )
+        Map<String, List<Object>> classNameObjectMap = Multimaps.asMap( Multimaps.index( objects, o -> getClassName( o ) ) );
+        
+        for ( String className : classNameObjectMap.keySet() )
         {
-            Map<String, String> translationMap = getTranslationsForObject( translations, getProperty( object, "uid" ) );
-
-            for ( String property : properties )
+            List<?> list = classNameObjectMap.get( className );
+                
+            List<String> properties = getObjectPropertyNames( list.iterator().next() );
+    
+            Collection<Translation> translations = translationService.getTranslations( className, locale );
+    
+            for ( Object object : list )
             {
-                String value = translationMap.get( property );
-
-                if ( value != null && !value.isEmpty() )
+                Map<String, String> translationMap = getTranslationsForObject( translations, getProperty( object, "uid" ) );
+    
+                for ( String property : properties )
                 {
-                    setProperty( object, "display", property, value );
+                    String value = translationMap.get( property );
+    
+                    if ( value != null && !value.isEmpty() )
+                    {
+                        setProperty( object, "display", property, value );
+                    }
                 }
             }
         }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/i18n/I18nServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/i18n/I18nServiceTest.java	2015-09-13 17:45:53 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/i18n/I18nServiceTest.java	2015-12-21 14:19:03 +0000
@@ -33,6 +33,7 @@
 import static org.junit.Assert.assertTrue;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -40,8 +41,16 @@
 import java.util.Map;
 
 import org.hisp.dhis.DhisSpringTest;
+import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementService;
+import org.hisp.dhis.dataset.DataSet;
+import org.hisp.dhis.dataset.DataSetService;
+import org.hisp.dhis.indicator.Indicator;
+import org.hisp.dhis.indicator.IndicatorService;
+import org.hisp.dhis.indicator.IndicatorType;
+import org.hisp.dhis.period.MonthlyPeriodType;
+import org.hisp.dhis.period.PeriodType;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
@@ -56,7 +65,24 @@
     
     @Autowired
     private DataElementService dataElementService;
+    
+    @Autowired
+    private IndicatorService indicatorService;
+    
+    @Autowired
+    private DataSetService dataSetService;
+    
+    private IndicatorType itA;
+    
+    private PeriodType ptA = new MonthlyPeriodType();
 
+    @Override
+    public void setUpTest()
+    {
+        itA = createIndicatorType( 'A' );
+        indicatorService.addIndicatorType( itA );
+    }
+    
     // -------------------------------------------------------------------------
     // Tests
     // -------------------------------------------------------------------------
@@ -147,8 +173,8 @@
         translationsC.put( "shortName", "frenchShortNameC" );
         translationsC.put( "description", "frenchDescriptionC" );        
 
-        i18nService.updateTranslation( className, locale, translationsA,dataElementA.getUid() );
-        i18nService.updateTranslation( className, locale, translationsB,dataElementB.getUid() );
+        i18nService.updateTranslation( className, locale, translationsA, dataElementA.getUid() );
+        i18nService.updateTranslation( className, locale, translationsB, dataElementB.getUid() );
         i18nService.updateTranslation( className, locale, translationsC, dataElementC.getUid());
         
         i18nService.internationalise( elements, locale );
@@ -159,6 +185,74 @@
         assertEquals( "frenchNameB", elementIter.next().getDisplayName() );
         assertEquals( "frenchNameC", elementIter.next().getDisplayName() );
     }
+
+    @Test
+    public void testInternationaliseCollectionMultiObjectTypes()
+    {
+        Locale locale = Locale.FRANCE;
+        
+        DataElement dataElementA = createDataElement( 'A' );
+        dataElementService.addDataElement( dataElementA );
+
+        DataElement dataElementB = createDataElement( 'B' );
+        dataElementService.addDataElement( dataElementB );
+
+        Indicator indicatorA = createIndicator( 'A', itA );
+        indicatorService.addIndicator( indicatorA );
+
+        Indicator indicatorB = createIndicator( 'B', itA );
+        indicatorService.addIndicator( indicatorB );
+        
+        DataSet dataSetA = createDataSet( 'A', ptA );
+        dataSetService.addDataSet( dataSetA );
+
+        DataSet dataSetB = createDataSet( 'B', ptA );
+        dataSetService.addDataSet( dataSetB );
+                
+        List<? extends IdentifiableObject> elements = Arrays.asList( dataElementA, dataElementB, indicatorA, indicatorB, dataSetA, dataSetB );
+        
+        Map<String, String> translationsA = new HashMap<>();
+        translationsA.put( "name", "frenchNameDeA" );
+        translationsA.put( "shortName", "frenchShortNameDeA" );
+        
+        Map<String, String> translationsB = new HashMap<>();
+        translationsB.put( "name", "frenchNameDeB" );
+        translationsB.put( "shortName", "frenchShortNameDeB" );
+        
+        Map<String, String> translationsC = new HashMap<>();
+        translationsC.put( "name", "frenchNameInA" );
+        translationsC.put( "shortName", "frenchShortNameInA" );  
+
+        Map<String, String> translationsD = new HashMap<>();
+        translationsD.put( "name", "frenchNameInB" );
+        translationsD.put( "shortName", "frenchShortNameInB" );  
+
+        Map<String, String> translationsE = new HashMap<>();
+        translationsE.put( "name", "frenchNameDsA" );
+        translationsE.put( "shortName", "frenchShortNameDsA" );  
+
+        Map<String, String> translationsF = new HashMap<>();
+        translationsF.put( "name", "frenchNameDsB" );
+        translationsF.put( "shortName", "frenchShortNameDsB" );  
+
+        i18nService.updateTranslation( DataElement.class.getSimpleName(), locale, translationsA, dataElementA.getUid() );
+        i18nService.updateTranslation( DataElement.class.getSimpleName(), locale, translationsB, dataElementB.getUid() );
+        i18nService.updateTranslation( Indicator.class.getSimpleName(), locale, translationsC, indicatorA.getUid() );
+        i18nService.updateTranslation( Indicator.class.getSimpleName(), locale, translationsD, indicatorB.getUid() );
+        i18nService.updateTranslation( DataSet.class.getSimpleName(), locale, translationsE, dataSetA.getUid() );
+        i18nService.updateTranslation( DataSet.class.getSimpleName(), locale, translationsF, dataSetB.getUid() );
+        
+        i18nService.internationalise( elements, locale );
+        
+        Iterator<? extends IdentifiableObject> iter = elements.iterator();
+        
+        assertEquals( "frenchNameDeA", iter.next().getDisplayName() );
+        assertEquals( "frenchNameDeB", iter.next().getDisplayName() );
+        assertEquals( "frenchNameInA", iter.next().getDisplayName() );
+        assertEquals( "frenchNameInB", iter.next().getDisplayName() );
+        assertEquals( "frenchNameDsA", iter.next().getDisplayName() );
+        assertEquals( "frenchNameDsB", iter.next().getDisplayName() );
+    }
     
     @Test
     public void testGetObjectPropertyValues()