dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #03015
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 1011: Minor fix.
------------------------------------------------------------
revno: 1011
committer: Lars Helge Oeverland larshelge@xxxxxxxxx
branch nick: trunk
timestamp: Mon 2009-11-09 23:14:51 +0100
message:
Minor fix.
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/main/resources/META-INF/dhis/beans.xml
dhis-2/dhis-i18n/dhis-i18n-db/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-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-09 17:07:37 +0000
+++ dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/i18n/DefaultI18nService.java 2009-11-09 22:14:51 +0000
@@ -83,7 +83,7 @@
}
// -------------------------------------------------------------------------
- // I18nService implementation
+ // Internationalise
// -------------------------------------------------------------------------
private void internationalise( Object object, Locale locale )
@@ -157,84 +157,51 @@
{
Locale locale = localeManager.getCurrentLocale();
- if ( locale == null || intObjects == null )
+ if ( locale == null || intObjects == null || intObjects.isEmpty() )
{
return;
}
-
- /**
- * Check if all objects are of the same type, in as good
- * as all cases this will be true so we use the optimized query for that.
- */
- boolean oneType = true;
- Class<?> type = null;
-
- for ( Object object : intObjects )
- {
- if ( type == null )
- {
- type = object.getClass();
- }
- else
- {
- if ( !type.equals( object.getClass() ) )
- {
- oneType = false;
- }
- }
- }
-
- if ( !oneType )
- {
- for ( Object object : intObjects )
- {
- internationalise( object );
- }
- }
- else if ( !intObjects.isEmpty() )
- {
- 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 )
+ 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() )
{
- Map<String, String> translations = getTranslationsForObject( allTranslations, getId( object ) );
-
- for ( Map.Entry<String,String> translation : translations.entrySet() )
- {
- String property = translation.getKey();
- String value = translation.getValue();
+ 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 );
}
- 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 );
- }
- }
}
}
}
@@ -242,66 +209,48 @@
}
}
- public List<String> getPropertyNames( String className )
- {
- for ( I18nObject i18nObject : objects )
- {
- if ( i18nObject.getClassName().equals( className ) )
- {
- return i18nObject.getPropertyNames();
- }
- }
-
- return null;
- }
-
- public Map<String, String> getPropertyNamesLabel( String className )
- {
- for ( I18nObject i18nObject : objects )
- {
- if ( i18nObject.getClassName().equals( className ) )
- {
- Map<String, String> propertyNamesLabel = new Hashtable<String, String>();
+ // -------------------------------------------------------------------------
+ // Internationalise
+ // -------------------------------------------------------------------------
+
+ public void addObject( Object object )
+ {
+ if ( !isI18nObject( object ) )
+ {
+ return;
+ }
+
+ 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 ) )
+ {
+ String className = getClassName( object );
+ int id = getId( object );
+
+ Map<String, String> translations = new Hashtable<String, String>();
for ( String property : i18nObject.getPropertyNames() )
{
- propertyNamesLabel.put( property, convertPropertyToKey( property ) );
- }
-
- return propertyNamesLabel;
- }
- }
-
- return null;
- }
-
- public Map<String, String> getTranslations( String className, int id, Locale locale )
- {
- Collection<Translation> translationsCol = translationService.getTranslations( className, id, locale );
-
- return convertTranslations( translationsCol );
- }
-
- public void updateTranslation( String className, int id, Locale locale, Map<String, String> translations )
- {
- for ( Map.Entry<String,String> translationEntry : translations.entrySet() )
- {
- String key = translationEntry.getKey();
- String value = translationEntry.getValue();
-
- if ( value != null && value.trim().length() > 0 )
- {
- Translation translation = translationService.getTranslation( className, id, locale, key );
-
- if ( translation != null )
- {
- translation.setValue( value );
- translationService.updateTranslation( translation );
- }
- else
- {
- translation = new Translation( className, id, locale.toString(), key, value );
- translationService.addTranslation( translation );
+ String value = getProperty( object, property );
+
+ if ( value != null && !value.equals( "" ) )
+ {
+ translations.put( property, value );
+ }
+ }
+
+ if ( !translations.isEmpty() )
+ {
+ updateTranslation( className, id, locale, translations );
}
}
}
@@ -362,48 +311,27 @@
}
}
- public void addObject( Object object )
- {
- if ( !isI18nObject( object ) )
- {
- return;
- }
-
- 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 ) )
- {
- 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 );
- }
- }
- }
- }
+ public void removeObject( Object object )
+ {
+ if ( object != null )
+ {
+ translationService.deleteTranslations( getClassName( object ), getId( object ) );
+ }
+ }
+
+ public void setToFallback( Object object )
+ {
+ if ( !isI18nObject( object ) | object == null )
+ {
+ return;
+ }
+
+ internationalise( object, localeManager.getFallbackLocale() );
+ }
+
+ // -------------------------------------------------------------------------
+ // Translation
+ // -------------------------------------------------------------------------
public void addTranslation( Object object, String property, String value, Locale locale )
{
@@ -422,23 +350,78 @@
updateTranslation( className, id, locale, translations );
}
- public void removeObject( Object object )
- {
- if ( object != null )
- {
- translationService.deleteTranslations( getClassName( object ), getId( object ) );
- }
- }
-
- public void setToFallback( Object object )
- {
- if ( !isI18nObject( object ) | object == null )
- {
- return;
- }
-
- internationalise( object, localeManager.getFallbackLocale() );
- }
+ public void updateTranslation( String className, int id, Locale locale, Map<String, String> translations )
+ {
+ for ( Map.Entry<String,String> translationEntry : translations.entrySet() )
+ {
+ String key = translationEntry.getKey();
+ String value = translationEntry.getValue();
+
+ if ( value != null && value.trim().length() > 0 )
+ {
+ Translation translation = translationService.getTranslation( className, id, locale, key );
+
+ if ( translation != null )
+ {
+ translation.setValue( value );
+ translationService.updateTranslation( translation );
+ }
+ else
+ {
+ translation = new Translation( className, id, locale.toString(), key, value );
+ translationService.addTranslation( translation );
+ }
+ }
+ }
+ }
+
+ public Map<String, String> getTranslations( String className, int id, Locale locale )
+ {
+ Collection<Translation> translationsCol = translationService.getTranslations( className, id, locale );
+
+ return convertTranslations( translationsCol );
+ }
+
+ // -------------------------------------------------------------------------
+ // Property
+ // -------------------------------------------------------------------------
+
+ public List<String> getPropertyNames( String className )
+ {
+ for ( I18nObject i18nObject : objects )
+ {
+ if ( i18nObject.getClassName().equals( className ) )
+ {
+ return i18nObject.getPropertyNames();
+ }
+ }
+
+ return null;
+ }
+
+ public Map<String, String> getPropertyNamesLabel( String className )
+ {
+ for ( I18nObject i18nObject : objects )
+ {
+ if ( i18nObject.getClassName().equals( className ) )
+ {
+ Map<String, String> propertyNamesLabel = new Hashtable<String, String>();
+
+ for ( String property : i18nObject.getPropertyNames() )
+ {
+ propertyNamesLabel.put( property, convertPropertyToKey( property ) );
+ }
+
+ return propertyNamesLabel;
+ }
+ }
+
+ return null;
+ }
+
+ // -------------------------------------------------------------------------
+ // Locale
+ // -------------------------------------------------------------------------
public Collection<Locale> getAvailableLocales()
{
@@ -461,7 +444,7 @@
}
// -------------------------------------------------------------------------
- // Support methods
+ // Supportive methods
// -------------------------------------------------------------------------
/**
=== 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-08 20:18:11 +0000
+++ dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/i18n/I18nService.java 2009-11-09 22:14:51 +0000
@@ -39,29 +39,49 @@
{
String ID = I18nService.class.getName();
+ // -------------------------------------------------------------------------
+ // Internationalise
+ // -------------------------------------------------------------------------
+
public void internationalise( Object object );
public void internationaliseCollection( Collection<?> objects );
- public Map<String, String> getTranslations( String className, int id, Locale locale );
-
- public List<String> getPropertyNames( String className );
-
- public void updateTranslation( String className, int id, Locale thisLocale, Map<String, String> translations );
-
- public Collection<Locale> getAvailableLocales();
-
- public Map<String, String> getPropertyNamesLabel( String className );
+ // -------------------------------------------------------------------------
+ // Object
+ // -------------------------------------------------------------------------
public void addObject( Object object );
- public void addTranslation( Object object, String property, String value, Locale locale );
-
- public void setToFallback( Object object );
-
public void verify(Object object);
public void verifyCollection( Collection<?> collection );
public void removeObject( Object object );
+
+ public void setToFallback( Object object );
+
+ // -------------------------------------------------------------------------
+ // Translation
+ // -------------------------------------------------------------------------
+
+ public void addTranslation( Object object, String property, String value, Locale locale );
+
+ public void updateTranslation( String className, int id, Locale thisLocale, Map<String, String> translations );
+
+ public Map<String, String> getTranslations( String className, int id, Locale locale );
+
+ // -------------------------------------------------------------------------
+ // Property
+ // -------------------------------------------------------------------------
+
+ public List<String> getPropertyNames( String className );
+
+ public Map<String, String> getPropertyNamesLabel( String className );
+
+ // -------------------------------------------------------------------------
+ // Locale
+ // -------------------------------------------------------------------------
+
+ public Collection<Locale> getAvailableLocales();
}
=== modified file 'dhis-2/dhis-i18n/dhis-i18n-db/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-i18n/dhis-i18n-db/src/main/resources/META-INF/dhis/beans.xml 2009-11-09 18:17:36 +0000
+++ dhis-2/dhis-i18n/dhis-i18n-db/src/main/resources/META-INF/dhis/beans.xml 2009-11-09 22:14:51 +0000
@@ -240,5 +240,11 @@
ref="org.hisp.dhis.i18n.I18nService"/>
</bean>
-->
-
+ <!--
+ <bean id="i18nTranslationInterceptor"
+ class="org.hisp.dhis.i18n.interceptor.I18nObjectInterceptor">
+ <property name="i18nService"
+ ref="org.hisp.dhis.i18n.I18nService"/>
+ </bean>
+ -->
</beans>
=== 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-08 21:54:59 +0000
+++ dhis-2/dhis-i18n/dhis-i18n-db/src/test/java/org/hisp/dhis/i18n/I18nServiceTest.java 2009-11-09 22:14:51 +0000
@@ -27,6 +27,9 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.fail;
+
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
@@ -39,11 +42,8 @@
import org.hisp.dhis.i18n.locale.LocaleManager;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.junit.Before;
-import org.junit.Ignore;
import org.junit.Test;
-import static junit.framework.Assert.*;
-
/**
* @author Oyvind Brucker
*/