← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2918: Reverted my own commit r 2917. Now I finally understand how the i18n db solution works and it is ...

 

------------------------------------------------------------
revno: 2918
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2011-02-25 01:31:09 +0100
message:
  Reverted my own commit r 2917. Now I finally understand how the i18n db solution works and it is not reassuring. The translated properties are put on the persisted objects through reflection. This update will be persisted through hibernate dirty-checking when selecting a locale and loading the objects into a view. The reason why the original values are coming back when switching back to the default locale is that all objects have a fallback locale translation. We will need to fix this.
added:
  dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/I18nUpgrader.java
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/DefaultI18nService.java
  dhis-2/dhis-services/dhis-service-i18n/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-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	2011-02-24 23:46:43 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/DefaultI18nService.java	2011-02-25 00:31:09 +0000
@@ -115,6 +115,9 @@
 
             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 )
@@ -125,6 +128,23 @@
                 {
                     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 );
+                    }
+                }
             }
         }
     }
@@ -148,6 +168,9 @@
             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 ) );
@@ -160,6 +183,23 @@
                     {
                         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.isEmpty() )
+                        {
+                            setProperty( object, property, value );
+                        }
+                    }
                 }
             }
         }

=== added file 'dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/I18nUpgrader.java'
--- dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/I18nUpgrader.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/I18nUpgrader.java	2011-02-25 00:31:09 +0000
@@ -0,0 +1,397 @@
+package org.hisp.dhis.i18n;
+
+/*
+ * Copyright (c) 2004-2010, 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.Collection;
+import java.util.Locale;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.datadictionary.DataDictionary;
+import org.hisp.dhis.datadictionary.DataDictionaryService;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategory;
+import org.hisp.dhis.dataelement.DataElementCategoryCombo;
+import org.hisp.dhis.dataelement.DataElementCategoryOption;
+import org.hisp.dhis.dataelement.DataElementCategoryService;
+import org.hisp.dhis.dataelement.DataElementGroup;
+import org.hisp.dhis.dataelement.DataElementService;
+import org.hisp.dhis.dataset.DataSet;
+import org.hisp.dhis.dataset.DataSetService;
+import org.hisp.dhis.i18n.locale.LocaleManager;
+import org.hisp.dhis.indicator.Indicator;
+import org.hisp.dhis.indicator.IndicatorGroup;
+import org.hisp.dhis.indicator.IndicatorService;
+import org.hisp.dhis.indicator.IndicatorType;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
+import org.hisp.dhis.organisationunit.OrganisationUnitGroupService;
+import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.system.startup.AbstractStartupRoutine;
+import org.hisp.dhis.system.util.SystemUtils;
+import org.hisp.dhis.translation.Translation;
+import org.hisp.dhis.translation.TranslationService;
+
+/**
+ * The purpose of the I18nUpgrader is to give all objects in the database a default
+ * translation at startup time. Objects might be added with other means than the 
+ * DHIS 2 API and will so need to be given a default translation.
+ * 
+ * @author Oyvind Brucker
+ */
+public class I18nUpgrader
+    extends AbstractStartupRoutine
+{
+    private static Log log = LogFactory.getLog( I18nUpgrader.class );
+
+    String ID = I18nUpgrader.class.getName();
+
+    // -------------------------------------------------------------------------
+    // Dependencies
+    // -------------------------------------------------------------------------
+
+    private DataElementService dataElementService;
+
+    public void setDataElementService( DataElementService dataElementService )
+    {
+        this.dataElementService = dataElementService;
+    }
+
+    private DataSetService dataSetService;
+    
+    public void setDataSetService( DataSetService dataSetService )
+    {
+        this.dataSetService = dataSetService;
+    }
+
+    private OrganisationUnitGroupService organisationUnitGroupService;
+
+    public void setOrganisationUnitGroupService( OrganisationUnitGroupService organisationUnitGroupService )
+    {
+        this.organisationUnitGroupService = organisationUnitGroupService;
+    }
+
+    private OrganisationUnitService organisationUnitService;
+
+    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
+    {
+        this.organisationUnitService = organisationUnitService;
+    }
+
+    private IndicatorService indicatorService;
+
+    public void setIndicatorService( IndicatorService indicatorService )
+    {
+        this.indicatorService = indicatorService;
+    }
+    
+    private DataDictionaryService dataDictionaryService;
+
+    public void setDataDictionaryService( DataDictionaryService dataDictionaryService )
+    {
+        this.dataDictionaryService = dataDictionaryService;
+    }    
+    
+    private DataElementCategoryService categoryService;
+    
+    public void setCategoryService( DataElementCategoryService categoryService )
+    {
+        this.categoryService = categoryService;
+    }
+
+    private LocaleManager localeManager;
+
+    public void setLocaleManager( LocaleManager localeManager )
+    {
+        this.localeManager = localeManager;
+    }
+
+    private TranslationService translationService;
+
+    public void setTranslationService( TranslationService translationService )
+    {
+        this.translationService = translationService;
+    }    
+
+    private I18nService i18nService;
+
+    public void setI18nService( I18nService i18nService )
+    {
+        this.i18nService = i18nService;
+    }
+
+    // -------------------------------------------------------------------------
+    // Execute
+    // -------------------------------------------------------------------------
+    
+    public void execute()
+    {
+        // ---------------------------------------------------------------------
+        // Check permission to run
+        // ---------------------------------------------------------------------
+
+        if ( !SystemUtils.isRunningForUse() )
+        {
+            return;
+        }
+
+        // ---------------------------------------------------------------------
+        // Check if it should run - if so make the i18n service aware of all objects
+        // ---------------------------------------------------------------------
+
+        Collection<Translation> translations = translationService.getAllTranslations();
+
+        if ( translations == null || translations.isEmpty() )
+        {
+            Locale orgLocale = localeManager.getCurrentLocale();
+
+            localeManager.setCurrentLocale( localeManager.getFallbackLocale() );
+
+            log.info( "I18n Upgrader running using locale " + localeManager.getFallbackLocale().getDisplayName() );
+
+            // ---------------------------------------------------------------------
+            // DataElement
+            // ---------------------------------------------------------------------
+
+            Collection<DataElement> dataElements = dataElementService.getAllDataElements();
+
+            if ( dataElements != null && !dataElements.isEmpty() )
+            {
+                log.info( "I18n Upgrader: DataElement (" + dataElements.size() + ")" );
+
+                for ( DataElement dataElement : dataElements )
+                {
+                    i18nService.addObject( dataElement );
+                }
+            }
+
+            // ---------------------------------------------------------------------
+            // DataElementGroup
+            // ---------------------------------------------------------------------
+
+            Collection<DataElementGroup> dataElementGroups = dataElementService.getAllDataElementGroups();
+
+            if ( dataElementGroups != null && !dataElementGroups.isEmpty() )
+            {
+                log.info( "I18n Upgrader: dataElementGroup (" + dataElementGroups.size() + ")" );
+
+                for ( DataElementGroup dataElementGroup : dataElementGroups )
+                {
+                    i18nService.addObject( dataElementGroup );
+                }
+            }
+
+            // ---------------------------------------------------------------------
+            // Indicator
+            // ---------------------------------------------------------------------
+
+            Collection<Indicator> indicators = indicatorService.getAllIndicators();
+
+            if ( indicators != null && !indicators.isEmpty() )
+            {
+                log.info( "I18n Upgrader: Indicator (" + indicators.size() + ")" );
+
+                for ( Indicator indicator : indicators )
+                {
+                    i18nService.addObject( indicator );
+                }
+            }
+
+            // ---------------------------------------------------------------------
+            // IndicatorType
+            // ---------------------------------------------------------------------
+
+            Collection<IndicatorType> indicatorTypes = indicatorService.getAllIndicatorTypes();
+
+            if ( indicatorTypes != null && !indicatorTypes.isEmpty() )
+            {
+                log.info( "I18n Upgrader: indicatorType (" + indicatorTypes.size() + ")" );
+
+                for ( IndicatorType indicatorType : indicatorTypes )
+                {
+                    i18nService.addObject( indicatorType );
+                }
+            }
+
+            // ---------------------------------------------------------------------
+            // IndicatorGroup
+            // ---------------------------------------------------------------------
+
+            Collection<IndicatorGroup> indicatorGroups = indicatorService.getAllIndicatorGroups();
+
+            if ( indicatorGroups != null && !indicatorGroups.isEmpty() )
+            {
+                log.info( "I18n Upgrader: indicatorGroup (" + indicatorGroups.size() + ")" );
+
+                for ( IndicatorGroup indicatorGroup : indicatorGroups )
+                {
+                    i18nService.addObject( indicatorGroup );
+                }
+            }
+
+            // ---------------------------------------------------------------------
+            // OrganisationUnit
+            // ---------------------------------------------------------------------
+
+            Collection<OrganisationUnit> orgunits = organisationUnitService.getAllOrganisationUnits();
+
+            if ( orgunits != null && !orgunits.isEmpty() )
+            {
+                log.info( "I18n Upgrader: OrganisationUnit (" + orgunits.size() + ")" );
+
+                for ( OrganisationUnit orgunit : orgunits )
+                {
+                    i18nService.addObject( orgunit );
+                }
+            }
+
+            // ---------------------------------------------------------------------
+            // OrganisationUnitGroup
+            // ---------------------------------------------------------------------
+
+            Collection<OrganisationUnitGroup> orgunitGroups = organisationUnitGroupService
+                .getAllOrganisationUnitGroups();
+
+            if ( orgunitGroups != null && !orgunitGroups.isEmpty() )
+            {
+                log.info( "I18n Upgrader: OrganisationUnitGroup (" + orgunitGroups.size() + ")" );
+
+                for ( OrganisationUnitGroup orgunitGroup : orgunitGroups )
+                {
+                    i18nService.addObject( orgunitGroup );
+                }
+            }
+
+            // ---------------------------------------------------------------------
+            // OrganisationUnitGroupSet
+            // ---------------------------------------------------------------------
+
+            Collection<OrganisationUnitGroupSet> orgunitGroupSets = organisationUnitGroupService
+                .getAllOrganisationUnitGroupSets();
+
+            if ( orgunitGroupSets != null && !orgunitGroupSets.isEmpty() )
+            {
+                log.info( "I18n Upgrader: OrganisationUnitGroupSet (" + orgunitGroupSets.size() + ")" );
+
+                for ( OrganisationUnitGroupSet orgunitGroupSet : orgunitGroupSets )
+                {
+                    i18nService.addObject( orgunitGroupSet );
+                }
+            }
+
+            // ---------------------------------------------------------------------
+            // DataSet
+            // ---------------------------------------------------------------------
+
+            Collection<DataSet> dataSets = dataSetService.getAllDataSets();
+
+            if ( dataSets != null && !dataSets.isEmpty() )
+            {
+                log.info( "I18n Upgrader: DataSet (" + dataSets.size() + ")" );
+
+                for ( DataSet dataSet : dataSets )
+                {
+                    i18nService.addObject( dataSet );
+                }
+            }
+
+            // ---------------------------------------------------------------------
+            // DataDictionary
+            // ---------------------------------------------------------------------
+
+            Collection<DataDictionary> dataDictionaries = dataDictionaryService.getAllDataDictionaries();
+            
+            if ( dataDictionaries != null && !dataDictionaries.isEmpty() )
+            {
+                log.info( "I81n Upgrader: DataDictionary (" + dataDictionaries.size() + ")" );
+                
+                for ( DataDictionary dataDictionary : dataDictionaries )
+                {
+                    i18nService.addObject( dataDictionary );
+                }
+            }
+
+            localeManager.setCurrentLocale( orgLocale );
+            
+            // ---------------------------------------------------------------------
+            // DataElementCategory
+            // ---------------------------------------------------------------------
+
+            Collection<DataElementCategory> categories = categoryService.getAllDataElementCategories();
+            
+            if ( categories != null && !categories.isEmpty() )
+            {
+                log.info( "I81n Upgrader: DataElementCategory (" + categories.size() + ")" );
+                
+                for ( DataElementCategory category : categories )
+                {
+                    i18nService.addObject( category );
+                }
+            }
+
+            localeManager.setCurrentLocale( orgLocale );
+            
+            // ---------------------------------------------------------------------
+            // DataElementCategoryOption
+            // ---------------------------------------------------------------------            
+            
+            Collection<DataElementCategoryOption> categoryOptions = categoryService.getAllDataElementCategoryOptions();
+            
+            if ( categoryOptions != null && !categoryOptions.isEmpty() )
+            {
+                log.info( "I81n Upgrader: DataElementCategoryOption (" + categoryOptions.size() + ")" );                
+                
+                for ( DataElementCategoryOption categoryOption : categoryOptions )
+                {
+                    i18nService.addObject( categoryOption );                    
+                }
+            }
+
+            localeManager.setCurrentLocale( orgLocale );
+            
+            // ---------------------------------------------------------------------
+            // DataElementCategoryCombo
+            // ---------------------------------------------------------------------
+
+            Collection<DataElementCategoryCombo> categoryCombos = categoryService.getAllDataElementCategoryCombos();
+            
+            if ( categoryCombos != null && !categoryCombos.isEmpty() )
+            {
+                log.info( "I81n Upgrader: DataElementCategoryCombo (" + categoryCombos.size() + ")" );
+                
+                for ( DataElementCategoryCombo categoryCombo : categoryCombos )
+                {
+                    i18nService.addObject( categoryCombo );
+                }
+            }
+
+            localeManager.setCurrentLocale( orgLocale );
+        }
+    }
+}

=== modified file 'dhis-2/dhis-services/dhis-service-i18n/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-i18n/src/main/resources/META-INF/dhis/beans.xml	2011-02-24 23:46:43 +0000
+++ dhis-2/dhis-services/dhis-service-i18n/src/main/resources/META-INF/dhis/beans.xml	2011-02-25 00:31:09 +0000
@@ -37,4 +37,53 @@
     </property>
   </bean>
   
+  <!-- Upgrader -->
+  
+  <bean id="org.hisp.dhis.i18n.I18nUpgrader" class="org.hisp.dhis.i18n.I18nUpgrader">
+    <property name="i18nService">
+      <ref bean="org.hisp.dhis.i18n.I18nService"/>
+    </property>
+    <property name="translationService">
+      <ref bean="org.hisp.dhis.translation.TranslationService"/>
+    </property>
+    <property name="dataElementService">
+      <ref bean="org.hisp.dhis.dataelement.DataElementService"/>
+    </property>
+    <property name="organisationUnitService">
+      <ref bean="org.hisp.dhis.organisationunit.OrganisationUnitService"/>
+    </property>
+    <property name="organisationUnitGroupService">
+      <ref bean="org.hisp.dhis.organisationunit.OrganisationUnitGroupService"/>
+    </property>
+    <property name="dataSetService">
+      <ref bean="org.hisp.dhis.dataset.DataSetService"/>
+    </property>
+    <property name="indicatorService">
+      <ref bean="org.hisp.dhis.indicator.IndicatorService"/>
+    </property>
+    <property name="dataDictionaryService">
+      <ref bean="org.hisp.dhis.datadictionary.DataDictionaryService"/>
+    </property>  
+    <property name="categoryService">
+      <ref bean="org.hisp.dhis.dataelement.DataElementCategoryService"/>
+    </property>  
+    <property name="localeManager">
+      <ref bean="org.hisp.dhis.i18n.locale.LocaleManagerDb"/>
+    </property>
+	<property name="name" value="I18nUpgrader"/>
+    <property name="runlevel" value="8"/>
+    <property name="skipInTests" value="true"/>
+  </bean>
+  
+  <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
+    <property name="targetObject"
+      ref="org.hisp.dhis.system.startup.StartupRoutineExecutor"/>
+    <property name="targetMethod" value="addStartupRoutine"/>
+    <property name="arguments">
+      <list>
+        <ref local="org.hisp.dhis.i18n.I18nUpgrader"/>
+      </list>
+    </property>
+  </bean>
+  
 </beans>