dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #03019
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 1015: Slight refactor of I18nService.
------------------------------------------------------------
revno: 1015
committer: Lars Helge Oeverland larshelge@xxxxxxxxx
branch nick: trunk
timestamp: Tue 2009-11-10 02:44:26 +0100
message:
Slight refactor of I18nService.
modified:
dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/i18n/DefaultI18nService.java
dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/i18n/I18nService.java
dhis-2/dhis-i18n/dhis-i18n-db/src/test/java/org/hisp/dhis/i18n/I18nServiceTest.java
dhis-2/dhis-i18n/dhis-i18n-db/src/test/java/org/hisp/dhis/i18n/I18nServiceTranslationTest.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-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/i18n/DefaultI18nService.java'
--- dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/i18n/DefaultI18nService.java 2009-11-10 00:02:57 +0000
+++ dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/i18n/DefaultI18nService.java 2009-11-10 01:44:26 +0000
@@ -31,6 +31,7 @@
import static org.hisp.dhis.system.util.ReflectionUtils.getId;
import static org.hisp.dhis.system.util.ReflectionUtils.getProperty;
import static org.hisp.dhis.system.util.ReflectionUtils.setProperty;
+import static org.hisp.dhis.system.util.ReflectionUtils.isCollection;
import java.util.ArrayList;
import java.util.Collection;
@@ -86,122 +87,100 @@
// Internationalise
// -------------------------------------------------------------------------
+ public void internationalise( Object object )
+ {
+ if ( isCollection( object ) )
+ {
+ internationaliseCollection( (Collection<?>)object );
+ }
+
+ internationalise( object, localeManager.getCurrentLocale() );
+ }
+
private void internationalise( Object object, Locale locale )
{
- for ( I18nObject i18nObject : objects )
+ I18nObject i18nObject = isI18nObject( object );
+
+ if ( i18nObject != null && locale != null )
{
- if ( i18nObject.match( object ) )
- {
- Collection<Translation> translations = translationService.getTranslations( getClassName( object ),
- getId( object ), locale );
-
- Map<String, String> translationsCurrentLocale = convertTranslations( translations );
-
- Collection<Translation> translationsFallback = null; // Dont initiate unless needed
- Map<String, String> translationsFallbackLocale = null;
-
- List<String> propertyNames = i18nObject.getPropertyNames();
-
- for ( String property : propertyNames )
- {
- String value = translationsCurrentLocale.get( property );
-
- if ( value != null && !value.equals( "" ) )
+ Collection<Translation> translations = translationService.getTranslations( getClassName( object ), getId( object ), locale );
+
+ Map<String, String> translationsCurrentLocale = convertTranslations( translations );
+
+ Collection<Translation> translationsFallback = null; // Not initialized unless needed
+ Map<String, String> translationsFallbackLocale = null; // Not initialized unless needed
+
+ List<String> propertyNames = i18nObject.getPropertyNames();
+
+ for ( String property : propertyNames )
+ {
+ String value = translationsCurrentLocale.get( property );
+
+ if ( value != null && !value.isEmpty() )
+ {
+ setProperty( object, property, value );
+ }
+ else
+ {
+ if ( translationsFallback == null )
+ {
+ translationsFallback = translationService.getTranslations( getClassName( object ),
+ getId( object ), localeManager.getFallbackLocale() );
+
+ translationsFallbackLocale = convertTranslations( translationsFallback );
+ }
+
+ value = translationsFallbackLocale.get( property );
+
+ if ( value != null && !value.isEmpty() )
+ {
+ setProperty( object, property, value );
+ }
+ }
+ }
+ }
+ }
+
+ private void internationaliseCollection( Collection<?> intObjects )
+ {
+ I18nObject i18nObject = isI18nObject( intObjects.iterator().next() );
+
+ Locale locale = localeManager.getCurrentLocale();
+
+ if ( i18nObject != null && locale != null && intObjects != null )
+ {
+ Collection<Translation> allTranslations = translationService.getTranslations( i18nObject.getClassName(), locale );
+
+ Collection<Translation> fallbackTranslations = null; // Not initialized unless needed
+ Map<String, String> fallbackTranslationsMap = null; // Not initialized unless needed
+
+ for ( Object object : intObjects )
+ {
+ Map<String, String> translations = getTranslationsForObject( allTranslations, getId( object ) );
+
+ for ( Map.Entry<String,String> translation : translations.entrySet() )
+ {
+ String property = translation.getKey();
+ String value = translation.getValue();
+
+ if ( value != null && !value.isEmpty() )
{
setProperty( object, property, value );
}
else
{
- if ( translationsFallback == null )
- {
- translationsFallback = translationService.getTranslations( getClassName( object ),
- getId( object ), localeManager.getFallbackLocale() );
-
- translationsFallbackLocale = convertTranslations( translationsFallback );
- }
-
- value = translationsFallbackLocale.get( property );
-
- if ( value != null && !value.equals( "" ) )
- {
- setProperty( object, property, value );
- }
- }
- }
- }
- }
- }
-
- public void internationalise( Object object )
- {
- if ( !isI18nObject( object ) | object == null )
- {
- return;
- }
-
- Locale locale = null;
-
- locale = localeManager.getCurrentLocale();
-
- if ( locale == null )
- {
- log.error( "Unable to get current locale" );
- }
- else
- {
- internationalise( object, locale );
- }
- }
-
- public void internationaliseCollection( Collection<?> intObjects )
- {
- Locale locale = localeManager.getCurrentLocale();
-
- if ( locale == null || intObjects == null || intObjects.isEmpty() )
- {
- return;
- }
-
- for ( I18nObject i18nObject : objects )
- {
- if ( i18nObject.match( intObjects.iterator().next() ) )
- {
- Collection<Translation> allTranslations = translationService.getTranslations( i18nObject
- .getClassName(), locale );
-
- Collection<Translation> fallbackTranslations = null; // Don't initiate unless needed
- Map<String, String> fallbackTranslationsMap = null;
-
- for ( Object object : intObjects )
- {
- Map<String, String> translations = getTranslationsForObject( allTranslations, getId( object ) );
-
- for ( Map.Entry<String,String> translation : translations.entrySet() )
- {
- String property = translation.getKey();
- String value = translation.getValue();
-
- if ( value != null && !value.equals( "" ) )
- {
- setProperty( object, property, value );
- }
- else
- {
- if ( fallbackTranslations == null )
- {
- fallbackTranslations = translationService.getTranslations( i18nObject.getClassName(),
- locale );
-
- fallbackTranslationsMap = getTranslationsForObject( fallbackTranslations,
- getId( object ) );
- }
-
- value = fallbackTranslationsMap.get( property );
-
- if ( value != null && !value.equals( "" ) )
- {
- setProperty( object, property, value );
- }
+ if ( fallbackTranslations == null )
+ {
+ fallbackTranslations = translationService.getTranslations( i18nObject.getClassName(), locale );
+
+ fallbackTranslationsMap = getTranslationsForObject( fallbackTranslations, getId( object ) );
+ }
+
+ value = fallbackTranslationsMap.get( property );
+
+ if ( value != null && !value.isEmpty() )
+ {
+ setProperty( object, property, value );
}
}
}
@@ -215,92 +194,46 @@
public void addObject( Object object )
{
- if ( !isI18nObject( object ) )
- {
- return;
- }
+ I18nObject i18nObject = isI18nObject( object );
Locale locale = localeManager.getCurrentLocale();
- if ( locale == null )
- {
- log.warn( "Failed to get current locale while adding object" );
-
- return;
- }
-
- for ( I18nObject i18nObject : objects )
- {
- if ( i18nObject.match( object ) )
+ if ( i18nObject != null && locale != null )
+ {
+ String className = getClassName( object );
+ int id = getId( object );
+
+ Map<String, String> translations = new Hashtable<String, String>();
+
+ for ( String property : i18nObject.getPropertyNames() )
{
- String className = getClassName( object );
- int id = getId( object );
-
- Map<String, String> translations = new Hashtable<String, String>();
-
- for ( String property : i18nObject.getPropertyNames() )
- {
- String value = getProperty( object, property );
-
- if ( value != null && !value.equals( "" ) )
- {
- translations.put( property, value );
- }
- }
-
- if ( !translations.isEmpty() )
- {
- updateTranslation( className, id, locale, translations );
+ String value = getProperty( object, property );
+
+ if ( value != null && !value.isEmpty() )
+ {
+ translations.put( property, value );
}
}
+
+ updateTranslation( className, id, locale, translations );
}
}
public void verify( Object object )
{
- if ( !isI18nObject( object ) | object == null )
- {
- return;
- }
-
- Locale locale = localeManager.getCurrentLocale();
-
- /**
- * Save translations
- */
-
- for ( I18nObject i18nObject : objects )
- {
- if ( i18nObject.match( object ) )
+ if ( isI18nObject( object ) != null )
+ {
+ addObject( object );
+
+ // -----------------------------------------------------------------
+ // Set properties from fallback locale
+ // -----------------------------------------------------------------
+
+ if ( !localeManager.getCurrentLocale().equals( localeManager.getFallbackLocale() ) )
{
- String className = getClassName( object );
- int id = getId( object );
-
- Map<String, String> translations = new Hashtable<String, String>();
-
- for ( String property : i18nObject.getPropertyNames() )
- {
- String value = getProperty( object, property );
-
- if ( value != null && !value.equals( "" ) )
- {
- translations.put( property, value );
- }
- }
-
- updateTranslation( className, id, locale, translations );
+ internationalise( object, localeManager.getFallbackLocale() );
}
}
-
- /**
- * Set properties to properties from the fallback locale
- */
-
- if ( !locale.equals( localeManager.getFallbackLocale() ) )
- {
- internationalise( object, localeManager.getFallbackLocale() );
- }
-
}
public void removeObject( Object object )
@@ -313,12 +246,10 @@
public void setToFallback( Object object )
{
- if ( !isI18nObject( object ) | object == null )
+ if ( isI18nObject( object ) != null )
{
- return;
+ internationalise( object, localeManager.getFallbackLocale() );
}
-
- internationalise( object, localeManager.getFallbackLocale() );
}
// -------------------------------------------------------------------------
@@ -494,21 +425,27 @@
}
/**
- * Test if an object is enabled for i18n.
+ * Test if an object is not null and enabled for i18n. Returns the I18nObject
+ * if so. Returns null if not so.
*
- * @param object Object to check.
- * @return true if the object is enabled for i18n.
+ * @param object the object to test.
+ * @return the I18nObject or null.
*/
- private boolean isI18nObject( Object object )
+ private I18nObject isI18nObject( Object object )
{
- for ( I18nObject i18nObject : objects )
+ if ( object != null )
{
- if ( i18nObject.match( object ) )
+ for ( I18nObject i18nObject : objects )
{
- return true;
+ if ( i18nObject.match( object ) )
+ {
+ return i18nObject;
+ }
}
+
+ log.warn( "Object not enabled for i18n: " + object );
}
-
- return false;
+
+ return null;
}
}
=== modified file 'dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/i18n/I18nService.java'
--- dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/i18n/I18nService.java 2009-11-09 23:22:20 +0000
+++ dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/i18n/I18nService.java 2009-11-10 01:44:26 +0000
@@ -45,15 +45,13 @@
public void internationalise( Object object );
- public void internationaliseCollection( Collection<?> objects );
-
// -------------------------------------------------------------------------
// Object
// -------------------------------------------------------------------------
public void addObject( Object object );
- public void verify(Object object);
+ public void verify( Object object );
public void removeObject( Object object );
=== modified file 'dhis-2/dhis-i18n/dhis-i18n-db/src/test/java/org/hisp/dhis/i18n/I18nServiceTest.java'
--- dhis-2/dhis-i18n/dhis-i18n-db/src/test/java/org/hisp/dhis/i18n/I18nServiceTest.java 2009-11-10 00:02:57 +0000
+++ dhis-2/dhis-i18n/dhis-i18n-db/src/test/java/org/hisp/dhis/i18n/I18nServiceTest.java 2009-11-10 01:44:26 +0000
@@ -28,7 +28,7 @@
*/
import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.fail;
+import static junit.framework.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Collection;
@@ -143,7 +143,7 @@
translations1.put( "comment", "Just another orgunit" );
i18nService.updateTranslation( className1, id3, Locale.UK, translations1 );
- i18nService.internationaliseCollection( orgunits );
+ i18nService.internationalise( orgunits );
for ( int i = 0; i < orgunits.size(); i++ )
{
@@ -265,11 +265,10 @@
Collection<Locale> availableLocales = i18nService.getAvailableLocales();
- if ( !availableLocales.contains( Locale.UK ) | !availableLocales.contains( Locale.FRANCE )
- | !availableLocales.contains( Locale.US ) )
- {
- fail( "Failed to get available locales" );
- }
+ assertEquals( 3, availableLocales.size() );
+ assertTrue( availableLocales.contains( Locale.UK ) );
+ assertTrue( availableLocales.contains( Locale.FRANCE ) );
+ assertTrue( availableLocales.contains( Locale.US ) );
}
@Test
=== modified file 'dhis-2/dhis-i18n/dhis-i18n-db/src/test/java/org/hisp/dhis/i18n/I18nServiceTranslationTest.java'
--- dhis-2/dhis-i18n/dhis-i18n-db/src/test/java/org/hisp/dhis/i18n/I18nServiceTranslationTest.java 2009-11-10 00:02:57 +0000
+++ dhis-2/dhis-i18n/dhis-i18n-db/src/test/java/org/hisp/dhis/i18n/I18nServiceTranslationTest.java 2009-11-10 01:44:26 +0000
@@ -1,7 +1,37 @@
package org.hisp.dhis.i18n;
+/*
+ * Copyright (c) 2004-2005, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of the <ORGANIZATION> nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@@ -9,10 +39,15 @@
import org.hisp.dhis.DhisSpringTest;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.i18n.locale.LocaleManager;
+import org.hisp.dhis.translation.Translation;
import org.hisp.dhis.translation.TranslationService;
import org.junit.Before;
import org.junit.Test;
+/**
+ * @author Lars Helge Overland
+ * @version $Id$
+ */
public class I18nServiceTranslationTest
extends DhisSpringTest
{
@@ -24,11 +59,15 @@
private DataElement dataElementA;
- private int dataElementIdA;
+ private int dataElementIdA = 10;
+
+ private String dataElementClassNameA = DataElement.class.getSimpleName();
private Map<String, String> translationsA;
+ private Map<String, String> translationsB;
- private Locale localeA = Locale.UK;
+ private Locale localeA = Locale.FRANCE;
+ private Locale localeB = Locale.GERMANY;
// -------------------------------------------------------------------------
// Fixture
@@ -49,13 +88,92 @@
dataElementA.setId( dataElementIdA );
translationsA = new HashMap<String, String>();
- translationsA.put( "name", "DataElementUkA" );
- translationsA.put( "shortName", "ShortNameUkA" );
- translationsA.put( "code", "CodeUkA" );
- }
-
- // -------------------------------------------------------------------------
- // Tests
+ translationsA.put( "name", "DataElementFrA" );
+ translationsA.put( "shortName", "ShortNameFrA" );
+ translationsA.put( "description", "DescriptionFrA" );
+
+ translationsB = new HashMap<String, String>();
+ translationsB.put( "name", "DataElementGeA" );
+ translationsB.put( "shortName", "ShortNameGeA" );
+ translationsB.put( "description", "DescriptionGeA" );
+ }
+
+ // -------------------------------------------------------------------------
+ // Internationalise
+ // -------------------------------------------------------------------------
+
+ @Test
+ public void internationalise()
+ {
+ i18nService.addObject( dataElementA );
+
+ i18nService.internationalise( dataElementA );
+
+ assertEquals( "DataElementA", dataElementA.getName() );
+ assertEquals( "ShortNameA", dataElementA.getShortName() );
+ assertEquals( "DescriptionA", dataElementA.getDescription() );
+
+ i18nService.updateTranslation( dataElementClassNameA, dataElementIdA, localeB, translationsB );
+
+ localeManager.setCurrentLocale( localeB );
+
+ i18nService.internationalise( dataElementA );
+
+ assertEquals( "DataElementGeA", dataElementA.getName() );
+ assertEquals( "ShortNameGeA", dataElementA.getShortName() );
+ assertEquals( "DescriptionGeA", dataElementA.getDescription() );
+ }
+
+ @Test
+ public void internationaliseCollection()
+ {
+ Collection<DataElement> dataElements = new ArrayList<DataElement>();
+ dataElements.add( dataElementA );
+ dataElements.add( dataElementA );
+ dataElements.add( dataElementA );
+
+ i18nService.internationalise( dataElements );
+
+ for ( DataElement dataElement : dataElements )
+ {
+ assertEquals( "DataElementA", dataElement.getName() );
+ assertEquals( "ShortNameA", dataElement.getShortName() );
+ assertEquals( "DescriptionA", dataElement.getDescription() );
+ }
+
+ i18nService.updateTranslation( dataElementClassNameA, dataElementIdA, localeB, translationsB );
+
+ localeManager.setCurrentLocale( localeB );
+
+ i18nService.internationalise( dataElements );
+
+ for ( DataElement dataElement : dataElements )
+ {
+ assertEquals( "DataElementGeA", dataElement.getName() );
+ assertEquals( "ShortNameGeA", dataElement.getShortName() );
+ assertEquals( "DescriptionGeA", dataElement.getDescription() );
+ }
+ }
+
+ // -------------------------------------------------------------------------
+ // Object
+ // -------------------------------------------------------------------------
+
+ @Test
+ public void addObject()
+ {
+ i18nService.addObject( dataElementA );
+
+ Collection<Translation> translations = translationService.getTranslations( dataElementClassNameA, dataElementIdA, localeA );
+
+ assertNotNull( translations );
+ assertEquals( 3, translations.size() );
+
+ i18nService.addObject( null );
+ }
+
+ // -------------------------------------------------------------------------
+ // Translation
// -------------------------------------------------------------------------
@Test
@@ -63,22 +181,35 @@
{
// No existing translations exist
- i18nService.updateTranslation( DataElement.class.getSimpleName(), dataElementIdA, localeA, translationsA );
+ i18nService.updateTranslation( dataElementClassNameA, dataElementIdA, localeA, translationsA );
- assertEquals( "DataElementUkA", translationService.getTranslation( DataElement.class.getSimpleName(), dataElementIdA, localeA, "name" ).getValue() );
- assertEquals( "ShortNameUkA", translationService.getTranslation( DataElement.class.getSimpleName(), dataElementIdA, localeA, "shortName" ).getValue() );
- assertEquals( "CodeUkA", translationService.getTranslation( DataElement.class.getSimpleName(), dataElementIdA, localeA, "code" ).getValue() );
+ assertEquals( "DataElementFrA", translationService.getTranslation( dataElementClassNameA, dataElementIdA, localeA, "name" ).getValue() );
+ assertEquals( "ShortNameFrA", translationService.getTranslation( dataElementClassNameA, dataElementIdA, localeA, "shortName" ).getValue() );
+ assertEquals( "DescriptionFrA", translationService.getTranslation( dataElementClassNameA, dataElementIdA, localeA, "description" ).getValue() );
// There are existing translations
translationsA.put( "name", "DataElementUpdatedA" );
translationsA.put( "shortName", "ShortNameUpdatedA" );
- translationsA.put( "code", "CodeUpdatedA" );
-
- i18nService.updateTranslation( DataElement.class.getSimpleName(), dataElementIdA, localeA, translationsA );
-
- assertEquals( "DataElementUpdatedA", translationService.getTranslation( DataElement.class.getSimpleName(), dataElementIdA, localeA, "name" ).getValue() );
- assertEquals( "ShortNameUpdatedA", translationService.getTranslation( DataElement.class.getSimpleName(), dataElementIdA, localeA, "shortName" ).getValue() );
- assertEquals( "CodeUpdatedA", translationService.getTranslation( DataElement.class.getSimpleName(), dataElementIdA, localeA, "code" ).getValue() );
+ translationsA.put( "description", "DescriptionUpdatedA" );
+
+ i18nService.updateTranslation( dataElementClassNameA, dataElementIdA, localeA, translationsA );
+
+ assertEquals( "DataElementUpdatedA", translationService.getTranslation( dataElementClassNameA, dataElementIdA, localeA, "name" ).getValue() );
+ assertEquals( "ShortNameUpdatedA", translationService.getTranslation( dataElementClassNameA, dataElementIdA, localeA, "shortName" ).getValue() );
+ assertEquals( "DescriptionUpdatedA", translationService.getTranslation( dataElementClassNameA, dataElementIdA, localeA, "description" ).getValue() );
+ }
+
+ @Test
+ public void getTranslations()
+ {
+ i18nService.updateTranslation( dataElementClassNameA, dataElementIdA, localeA, translationsA );
+
+ Map<String, String> translations = i18nService.getTranslations( dataElementClassNameA, dataElementIdA, localeA );
+
+ assertEquals( 3, translations.keySet().size() );
+ assertEquals( "DataElementFrA", translations.get( "name" ) );
+ assertEquals( "ShortNameFrA", translations.get( "shortName" ) );
+ assertEquals( "DescriptionFrA", translations.get( "description" ) );
}
}