← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 919: Implemented a class as DefaultTranslationUserSettingSupport, with purposive used to get the whole...

 

------------------------------------------------------------
revno: 919
committer: hieu <hieu.hispvietnam@xxxxxxxxx>
branch nick: trunk
timestamp: Thu 2009-10-29 10:23:24 +0700
message:
  Implemented a class as DefaultTranslationUserSettingSupport, with purposive used to get the whole translations of the corresponding clazz, ie.OrganisationUnit, DataElement, etc... which belongs to user interface setting.
  Applied for OrganisationUnits list.
added:
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/i18n/action/DefaultTranslationUserSettingSupport.java
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/i18n/action/TranslationUserSettingSupport.java
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-web/dhis-web-commons/src/main/java/org/hisp/dhis/i18n/action/I18nAction.java
  dhis-2/dhis-web/dhis-web-commons/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/java/org/hisp/dhis/oum/action/organisationunit/GetOrganisationUnitListAction.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/resources/META-INF/dhis/beans.xml


--
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-10-23 05:41:15 +0000
+++ dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/i18n/DefaultI18nService.java	2009-10-29 03:23:24 +0000
@@ -51,6 +51,8 @@
     implements I18nService
 {
     private static final Log log = LogFactory.getLog( DefaultI18nService.class );
+    
+    private static final String UNDERSCORE = "_";
 
     // -------------------------------------------------------------------------
     // Dependencies
@@ -523,7 +525,7 @@
         {
             if ( translation.getProperty() != null && translation.getValue() != null )
             {
-                translationMap.put( translation.getId() + "_" + translation.getProperty(), translation.getValue() );
+                translationMap.put( translation.getId() + UNDERSCORE + translation.getProperty(), translation.getValue() );
             }
         }
 

=== 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-10-23 05:41:15 +0000
+++ dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/i18n/I18nService.java	2009-10-29 03:23:24 +0000
@@ -65,7 +65,7 @@
 
     public void removeObject( Object object );
 
-    // /////////////////////////////////
+    // -------------------------------------------------------------------------
     public Map<String, String> getTranslations( String className, Locale locale );
 
     public Map<String, String> getTranslations( String className, String propertyName, Locale locate );

=== added file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/i18n/action/DefaultTranslationUserSettingSupport.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/i18n/action/DefaultTranslationUserSettingSupport.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/i18n/action/DefaultTranslationUserSettingSupport.java	2009-10-29 03:23:24 +0000
@@ -0,0 +1,188 @@
+package org.hisp.dhis.i18n.action;
+
+/*
+ * Copyright (c) 2004-2007, 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 HISP project 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 java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import org.hisp.dhis.i18n.I18nService;
+import org.hisp.dhis.i18n.locale.LocaleManager;
+
+/**
+ * @author Dang Duy Hieu
+ * @version $Id$
+ * @since 2009-10-28
+ */
+
+public class DefaultTranslationUserSettingSupport
+    implements TranslationUserSettingSupport
+{
+
+    private static final String UNDERSCORE = "_";
+
+    private static final String GET_ID_METHOD = "getId";
+
+    private static final String TRANSLATION_PROPERTY_NAME = "name";
+
+    // -------------------------------------------------------------------------
+    // Dependencies
+    // -------------------------------------------------------------------------
+
+    private I18nService i18nService;
+
+    public void setI18nService( I18nService service )
+    {
+        i18nService = service;
+    }
+
+    private LocaleManager localeManager;
+
+    public void setLocaleManager( LocaleManager localeManager )
+    {
+        this.localeManager = localeManager;
+    }
+
+    // -------------------------------------------------------------------------
+    // Local variables
+    // -------------------------------------------------------------------------
+
+    private Class<?> clazz;
+
+    private List<Object> objectList;
+
+    // -------------------------------------------------------------------------
+    // Constructors
+    // -------------------------------------------------------------------------
+
+    public DefaultTranslationUserSettingSupport()
+    {
+        super();
+    }
+
+    // -------------------------------------------------------------------------
+    // Implementing methods
+    // -------------------------------------------------------------------------
+
+    public Locale getCurrentLocale()
+    {
+        return localeManager.getCurrentLocale();
+    }
+
+    public Locale getCurrentRefLocale()
+    {
+        return localeManager.getFallbackLocale();
+    }
+
+    @SuppressWarnings( "static-access" )
+    public List<String> getPropertyNames()
+    {
+        return i18nService.getPropertyNames( this.clazz.getSimpleName() );
+    }
+
+    public void setClazz( Class<?> clazz )
+    {
+        this.clazz = clazz;
+    }
+
+    public void setObjectList( List<Object> objectList )
+    {
+        this.objectList = new ArrayList<Object>( objectList );
+    }
+
+    @SuppressWarnings( "static-access" )
+    public Map<String, String> initTranslations( Map<String, String> translationsClazz )
+    {
+        translationsClazz = i18nService.getTranslations( this.clazz.getSimpleName(), this.TRANSLATION_PROPERTY_NAME,
+            getCurrentLocale() );
+
+        /**
+         * Fill in empty strings for null values
+         */
+
+        for ( Object o : objectList )
+        {
+            System.out.println( "\no.getId = " + getId( o ) );
+
+            for ( String property : getPropertyNames() )
+            {
+                if ( translationsClazz.get( getId( o ) + this.UNDERSCORE + property ) == null )
+                {
+                    translationsClazz.put( getId( o ) + this.UNDERSCORE + property, "" );
+                }
+            }
+        }
+        return translationsClazz;
+    }
+
+    // -------------------------------------------------------------------------
+    // Supporting methods
+    // -------------------------------------------------------------------------
+
+    /**
+     * Calls the method getId for this object, throws exception if this fails.
+     * 
+     * @param object object to call method on, needs to have the public method
+     *        getId():int
+     * @return The id
+     */
+    @SuppressWarnings( "static-access" )
+    private String getId( Object object )
+    {
+        int result = -1;
+
+        Class<?> c = object.getClass();
+
+        Method method;
+
+        try
+        {
+            method = c.getMethod( this.GET_ID_METHOD );
+
+            result = (Integer) method.invoke( object );
+        }
+        catch ( NoSuchMethodException e )
+        {
+            System.out.println( e );
+        }
+        catch ( IllegalAccessException e )
+        {
+            System.out.println( e );
+        }
+        catch ( InvocationTargetException e )
+        {
+            System.out.println( e );
+        }
+
+        return String.valueOf( result );
+    }
+
+}

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/i18n/action/I18nAction.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/i18n/action/I18nAction.java	2009-10-23 05:41:15 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/i18n/action/I18nAction.java	2009-10-29 03:23:24 +0000
@@ -44,6 +44,8 @@
 public class I18nAction
     implements Action
 {
+	private static final String UNDERSCORE = "_";
+
     private String className;
 
     private String objectId;
@@ -155,7 +157,8 @@
     public String execute()
         throws Exception
     {
-        propertyLabels = i18nService.getPropertyNamesLabel( className );
+
+		propertyLabels = i18nService.getPropertyNamesLabel( className );
 
         translations = i18nService.getTranslations( className, Integer.parseInt( objectId ), getCurrentLocale() );
 
@@ -168,13 +171,13 @@
 
         for ( String property : getPropertyNames() )
         {
-            if ( translations.get( String.valueOf( objectId ) + "_" + property ) == null )
+            if ( translations.get( String.valueOf( objectId ) + UNDERSCORE + property ) == null )
             {
-                translations.put( String.valueOf( objectId ) + "_" + property, "" );
+                translations.put( String.valueOf( objectId ) + UNDERSCORE + property, "" );
             }
-            if ( referenceTranslations.get( String.valueOf( objectId ) + "_" + property ) == null )
+            if ( referenceTranslations.get( String.valueOf( objectId ) + UNDERSCORE + property ) == null )
             {
-                referenceTranslations.put( String.valueOf( objectId ) + "_" + property, "" );
+                referenceTranslations.put( String.valueOf( objectId ) + UNDERSCORE + property, "" );
             }
         }
 

=== added file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/i18n/action/TranslationUserSettingSupport.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/i18n/action/TranslationUserSettingSupport.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/i18n/action/TranslationUserSettingSupport.java	2009-10-29 03:23:24 +0000
@@ -0,0 +1,53 @@
+package org.hisp.dhis.i18n.action;
+
+/*
+ * Copyright (c) 2004-2007, 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 HISP project 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 java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+/**
+ * @author Dang Duy Hieu
+ * @version $Id$
+ * @since 2009-10-28
+ */
+
+public interface TranslationUserSettingSupport
+{
+    Locale getCurrentLocale();
+
+    Locale getCurrentRefLocale();
+
+    List<String> getPropertyNames();
+
+    void setClazz( Class<?> clazz );
+
+    void setObjectList( List<Object> objectList );
+
+    Map<String, String> initTranslations( Map<String, String> translationsClazz );
+}

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-commons/src/main/resources/META-INF/dhis/beans.xml	2009-10-18 22:44:41 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/resources/META-INF/dhis/beans.xml	2009-10-29 03:23:24 +0000
@@ -647,4 +647,14 @@
       ref="org.hisp.dhis.options.SystemSettingManager"/>
   </bean>
   
+	<!-- TRANSLATION SUPPORTING -->
+  
+	<bean id="translationUserSettingSupport"
+		class="org.hisp.dhis.i18n.action.DefaultTranslationUserSettingSupport">
+		<property name="i18nService"
+			ref="org.hisp.dhis.i18n.I18nService"/>
+		<property name="localeManager"
+			ref="org.hisp.dhis.i18n.locale.LocaleManager"/>
+	</bean>
+  
 </beans>

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/java/org/hisp/dhis/oum/action/organisationunit/GetOrganisationUnitListAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/java/org/hisp/dhis/oum/action/organisationunit/GetOrganisationUnitListAction.java	2009-10-23 05:41:15 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/java/org/hisp/dhis/oum/action/organisationunit/GetOrganisationUnitListAction.java	2009-10-29 03:23:24 +0000
@@ -33,11 +33,9 @@
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
-import java.util.Locale;
 import java.util.Map;
 
-import org.hisp.dhis.i18n.I18nService;
-import org.hisp.dhis.i18n.locale.LocaleManager;
+import org.hisp.dhis.i18n.action.TranslationUserSettingSupport;
 import org.hisp.dhis.options.displayproperty.DisplayPropertyHandler;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager;
@@ -63,8 +61,6 @@
      */
     private static final long serialVersionUID = 1L;
 
-    private static String TRANSLATION_PROPERTY_NAME = "name";
-
     private OrganisationUnitSelectionManager selectionManager;
 
     public void setSelectionManager( OrganisationUnitSelectionManager selectionManager )
@@ -72,18 +68,11 @@
         this.selectionManager = selectionManager;
     }
 
-    private LocaleManager localeManager;
-
-    public void setLocaleManager( LocaleManager localeManager )
-    {
-        this.localeManager = localeManager;
-    }
-
-    private I18nService i18nService;
-
-    public void setI18nService( I18nService service )
-    {
-        i18nService = service;
+    private TranslationUserSettingSupport translationUserSettingSupport;
+
+    public void setTranslationUserSettingSupport( TranslationUserSettingSupport translationUserSettingSupport )
+    {
+        this.translationUserSettingSupport = translationUserSettingSupport;
     }
 
     // -------------------------------------------------------------------------
@@ -119,11 +108,6 @@
         return organisationUnits;
     }
 
-    private List<String> getPropertyNames()
-    {
-        return i18nService.getPropertyNames( OrganisationUnit.class.getSimpleName() );
-    }
-
     private Map<String, String> translationsOrgUnit = new HashMap<String, String>();
 
     public Map<String, String> getTranslationsOrgUnit()
@@ -131,11 +115,11 @@
         return translationsOrgUnit;
     }
 
-    private Map<String, String> refTranslationOrgUnit = new HashMap<String, String>();
+    private Map<String, String> refTranslationsOrgUnit = new HashMap<String, String>();
 
-    public Map<String, String> getRefTranslationOrgUnit()
+    public Map<String, String> getRefTranslationsOrgUnit()
     {
-        return refTranslationOrgUnit;
+        return refTranslationsOrgUnit;
     }
 
     // -------------------------------------------------------------------------
@@ -164,45 +148,19 @@
 
         displayPropertyHandler.handle( organisationUnits );
 
-        translationsOrgUnit = i18nService.getTranslations( OrganisationUnit.class.getSimpleName(),
-            TRANSLATION_PROPERTY_NAME, getCurrentLocale() );
-        refTranslationOrgUnit = i18nService.getTranslations( OrganisationUnit.class.getSimpleName(),
-            TRANSLATION_PROPERTY_NAME, getCurrentRefLocale() );
-
-        /**
-         * Fill in empty strings for null values
-         */
-
-        for ( OrganisationUnit o : organisationUnits )
-        {
-            for ( String property : getPropertyNames() )
-            {
-                if ( translationsOrgUnit.get( String.valueOf( o.getId() ) + "_" + property ) == null )
-                {
-                    translationsOrgUnit.put( String.valueOf( o.getId() ) + "_" + property, "" );
-                }
-                if ( refTranslationOrgUnit.get( String.valueOf( o.getId() ) + "_" + property ) == null )
-                {
-                    refTranslationOrgUnit.put( String.valueOf( o.getId() ) + "_" + property, "" );
-                }
-            }
-        }
+        // ---------------------------------------------------------------------
+        // Translation for Organization Unit
+        // ---------------------------------------------------------------------
+
+        this.translationUserSettingSupport.setClazz( OrganisationUnit.class );
+        
+        this.translationUserSettingSupport.setObjectList( new ArrayList<Object>( organisationUnits ) );
+        
+        translationsOrgUnit = this.translationUserSettingSupport.initTranslations( translationsOrgUnit );
+        
+        refTranslationsOrgUnit = this.translationUserSettingSupport.initTranslations( refTranslationsOrgUnit );
+
 
         return SUCCESS;
     }
-
-    // -------------------------------------------------------------------------
-    // Supporting method
-    // -------------------------------------------------------------------------
-
-    private Locale getCurrentLocale()
-    {
-        return localeManager.getCurrentLocale();
-    }
-
-    private Locale getCurrentRefLocale()
-    {
-        return localeManager.getFallbackLocale();
-    }
-
 }

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/resources/META-INF/dhis/beans.xml	2009-10-26 15:39:29 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/resources/META-INF/dhis/beans.xml	2009-10-29 03:23:24 +0000
@@ -16,11 +16,8 @@
 		<property name="selectionManager">
 			<ref bean="org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager" />
 		</property>
-		<property name="i18nService">
-			<ref bean="org.hisp.dhis.i18n.I18nService" />
-		</property>
-		<property name="localeManager">
-			<ref bean="org.hisp.dhis.i18n.locale.LocaleManager" />
+		<property name="translationUserSettingSupport">
+			<ref bean="translationUserSettingSupport" />
 		</property>
 	</bean>