=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/translation/TranslationService.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/translation/TranslationService.java 2009-11-08 20:18:11 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/translation/TranslationService.java 2009-11-09 18:17:36 +0000 @@ -37,22 +37,77 @@ public interface TranslationService { String ID = TranslationService.class.getName(); - + + /** + * Adds a Translation. + * + * @param translation the Translation. + */ + void addTranslation( Translation translation ); + + /** + * Updates a Translation. + * + * @param translation the Translation. + */ + void updateTranslation( Translation translation ); + + /** + * Retrieves a Translation. + * + * @param className the class name. + * @param id the id. + * @param locale the locale. + * @param property the property. + * @return a Translation. + */ Translation getTranslation( String className, int id, Locale locale, String property ); + /** + * Retrieves a Collection of Translations. + * + * @param className the class name. + * @param id the id. + * @param locale the locale. + * @return a Collection of Translations. + */ Collection getTranslations( String className, int id, Locale locale ); + /** + * Retrieves a Collection of Translations. + * + * @param className the class name. + * @param locale the locale. + * @return a Collection of Translations. + */ Collection getTranslations( String className, Locale locale ); + /** + * Retrieves a Collection of all Translations. + * + * @return a Collection of all Translations. + */ Collection getAllTranslations(); - void addTranslation( Translation translation ); - - void updateTranslation( Translation translation ); - + /** + * Deletes a Translation. + * + * @param translation the Translation. + */ void deleteTranslation( Translation translation ); + /** + * Deletes Translations. + * + * @param className the class name. + * @param id the id. + */ + void deleteTranslations( String className, int id ); + + /** + * Retrieves all available Locales. + * + * @return a Collection of all available Locales. + */ Collection getAvailableLocales(); - - void deleteTranslations( String className, int id ); } === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/translation/TranslationStore.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/translation/TranslationStore.java 2009-11-08 20:18:11 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/translation/TranslationStore.java 2009-11-09 18:17:36 +0000 @@ -37,21 +37,77 @@ { String ID = TranslationStore.class.getName(); + /** + * Adds a Translation. + * + * @param translation the Translation. + */ + void addTranslation( Translation translation ); + + /** + * Updates a Translation. + * + * @param translation the Translation. + */ + void updateTranslation( Translation translation ); + + /** + * Retrieves a Translation. + * + * @param className the class name. + * @param id the id. + * @param locale the locale. + * @param property the property. + * @return a Translation. + */ Translation getTranslation( String className, int id, Locale locale, String property ); + /** + * Retrieves a Collection of Translations. + * + * @param className the class name. + * @param id the id. + * @param locale the locale. + * @return a Collection of Translations. + */ Collection getTranslations( String className, int id, Locale locale ); + /** + * Retrieves a Collection of Translations. + * + * @param className the class name. + * @param locale the locale. + * @return a Collection of Translations. + */ Collection getTranslations( String className, Locale locale ); + /** + * Retrieves a Collection of all Translations. + * + * @return a Collection of all Translations. + */ Collection getAllTranslations(); - void addTranslation( Translation translation ); - - void updateTranslation( Translation translation ); - + /** + * Deletes a Translation. + * + * @param translation the Translation. + */ void deleteTranslation( Translation translation ); + /** + * Deletes Translations. + * + * @param className the class name. + * @param id the id. + */ + void deleteTranslations( String className, int id ); + + /** + * Retrieves all available Locales. + * + * @return a Collection of all available Locales. + */ Collection getAvailableLocales(); - void deleteTranslations( String className, int id ); } \ No newline at end of file === modified file 'dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/translation/hibernate/HibernateTranslationStore.java' --- dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/translation/hibernate/HibernateTranslationStore.java 2009-11-08 20:18:11 +0000 +++ dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/translation/hibernate/HibernateTranslationStore.java 2009-11-09 18:17:36 +0000 @@ -37,6 +37,7 @@ import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Restrictions; +import org.hisp.dhis.i18n.util.LocaleUtils; import org.hisp.dhis.translation.Translation; import org.hisp.dhis.translation.TranslationStore; @@ -165,47 +166,11 @@ for ( Object object : objlist ) { - Locale locale = getLocale( object.toString() ); + Locale locale = LocaleUtils.getLocale( object.toString() ); locales.add( locale ); } return locales; } - - // ------------------------------------------------------------------------- - // Supportive methods - // ------------------------------------------------------------------------- - - /** - * Creates a Locale object based on the input String - * - * @param localestr String to parse - * @return A locale object or null if not valid - */ - private Locale getLocale( String localestr ) - { - String[] parts = localestr.split( "_" ); - - Locale thisLocale; - - if ( parts.length == 1 ) - { - thisLocale = new Locale( parts[0] ); - } - else if ( parts.length == 2 ) - { - thisLocale = new Locale( parts[0], parts[1] ); - } - else if ( parts.length == 3 ) - { - thisLocale = new Locale( parts[0], parts[1], parts[2] ); - } - else - { - return null; - } - - return thisLocale; - } } === modified file 'dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/translation/impl/DefaultTranslationService.java' --- dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/translation/impl/DefaultTranslationService.java 2009-11-08 20:18:11 +0000 +++ dhis-2/dhis-i18n/dhis-i18n-db/src/main/java/org/hisp/dhis/translation/impl/DefaultTranslationService.java 2009-11-09 18:17:36 +0000 @@ -43,13 +43,31 @@ public class DefaultTranslationService implements TranslationService { + // ------------------------------------------------------------------------- + // Dependencies + // ------------------------------------------------------------------------- + private TranslationStore translationStore; public void setTranslationStore( TranslationStore translationStore ) { this.translationStore = translationStore; } - + + // ------------------------------------------------------------------------- + // Translation + // ------------------------------------------------------------------------- + + public void addTranslation( Translation translation ) + { + translationStore.addTranslation( translation ); + } + + public void updateTranslation( Translation translation ) + { + translationStore.updateTranslation( translation ); + } + public Translation getTranslation( String className, int id, Locale locale, String property ) { return translationStore.getTranslation( className, id, locale, property ); @@ -70,28 +88,18 @@ return translationStore.getAllTranslations(); } - public void addTranslation( Translation translation ) - { - translationStore.addTranslation( translation ); - } - - public void updateTranslation( Translation translation ) - { - translationStore.updateTranslation( translation ); - } - public void deleteTranslation( Translation translation ) { translationStore.deleteTranslation( translation ); } + public void deleteTranslations( String className, int id ) + { + translationStore.deleteTranslations( className, id ); + } + public Collection getAvailableLocales() { return translationStore.getAvailableLocales(); } - - public void deleteTranslations( String className, int id ) - { - translationStore.deleteTranslations( className, id ); - } } === 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-04 06:39:59 +0000 +++ dhis-2/dhis-i18n/dhis-i18n-db/src/main/resources/META-INF/dhis/beans.xml 2009-11-09 18:17:36 +0000 @@ -9,16 +9,16 @@ - + - + locales = translationService.getAvailableLocales(); + + assertEquals( 3, locales.size() ); + } +} === modified file 'dhis-2/dhis-i18n/dhis-i18n-db/src/test/java/org/hisp/dhis/translation/TranslationStoreTest.java' --- dhis-2/dhis-i18n/dhis-i18n-db/src/test/java/org/hisp/dhis/translation/TranslationStoreTest.java 2009-06-18 12:45:22 +0000 +++ dhis-2/dhis-i18n/dhis-i18n-db/src/test/java/org/hisp/dhis/translation/TranslationStoreTest.java 2009-11-09 18:17:36 +0000 @@ -27,8 +27,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNull; +import static junit.framework.Assert.*; import java.util.Collection; import java.util.Locale; @@ -70,83 +69,112 @@ private String className1 = OrganisationUnit.class.getName(); private String className2 = DataElement.class.getName(); - private Translation translation1a = new Translation( className1, id1, locale1, "name", "orgunitss" ); - private Translation translation1b = new Translation( className1, id1, locale1, "shortName", "orgs" ); - private Translation translation2a = new Translation( className1, id1, locale2, "name", "orgunitzz" ); - private Translation translation2b = new Translation( className2, id1, locale2, "name", "dataelement1" ); - private Translation translation2c = new Translation( className2, id2, locale2, "name", "dataelement2" ); - private Translation translation3 = new Translation( className1, id1, locale3, "name", "orgunit" ); + private Translation translation1a = new Translation( className1, id1, locale1, "name", "cheers" ); + private Translation translation1b = new Translation( className1, id1, locale1, "shortName", "goodbye" ); + private Translation translation2a = new Translation( className1, id1, locale2, "name", "hello" ); + private Translation translation2b = new Translation( className2, id1, locale2, "name", "hey" ); + private Translation translation2c = new Translation( className2, id2, locale3, "name", "bonjour" ); // ------------------------------------------------------------------------- // Tests // ------------------------------------------------------------------------- @Test - public void testAddGetUpdateDelete() - throws Exception - { - // Add - translationStore.addTranslation( translation1a ); - translationStore.addTranslation( translation1b ); - translationStore.addTranslation( translation2a ); - translationStore.addTranslation( translation2b ); - translationStore.addTranslation( translation2c ); - translationStore.addTranslation( translation3 ); - - // Get - Translation savedTranslation1a = translationStore.getTranslation( className1, id1, Locale.UK, "name" ); - Translation savedTranslation1b = translationStore.getTranslation( className1, id1, Locale.UK, "shortName" ); - Translation savedTranslation2b = translationStore.getTranslation( className2, id1, Locale.US, "name" ); - Translation savedTranslation2c = translationStore.getTranslation( className2, id2, Locale.US, "name" ); - - assertEquals( "orgunitss", savedTranslation1a.getValue() ); - assertEquals( "orgs", savedTranslation1b.getValue() ); - assertEquals( "dataelement1", savedTranslation2b.getValue() ); - assertEquals( "dataelement2", savedTranslation2c.getValue() ); - - Collection col = translationStore.getTranslations( className1, id1, Locale.UK ); - - assertEquals( "Unexpected amount of translations received", 2, col.size() ); - - // Update - translation1a.setValue( "org-unitssz" ); - translation2c.setValue( "dataelement-2" ); - + public void testAddGet() + { + translationStore.addTranslation( translation1a ); + translationStore.addTranslation( translation1b ); + + assertEquals( translation1a, translationStore.getTranslation( className1, id1, Locale.UK, "name" ) ); + assertEquals( translation1b, translationStore.getTranslation( className1, id1, Locale.UK, "shortName" ) ); + } + + @Test + public void delete() + { + translationStore.addTranslation( translation1a ); + translationStore.addTranslation( translation1b ); + + assertNotNull( translationStore.getTranslation( className1, id1, Locale.UK, "name" ) ); + assertNotNull( translationStore.getTranslation( className1, id1, Locale.UK, "shortName" ) ); + + translationStore.deleteTranslation( translation1a ); + + assertNull( translationStore.getTranslation( className1, id1, Locale.UK, "name" ) ); + assertNotNull( translationStore.getTranslation( className1, id1, Locale.UK, "shortName" ) ); + + translationStore.deleteTranslation( translation1b ); + + assertNull( translationStore.getTranslation( className1, id1, Locale.UK, "name" ) ); + assertNull( translationStore.getTranslation( className1, id1, Locale.UK, "shortName" ) ); + } + + @Test + public void testUpdateTranslation() + { + translationStore.addTranslation( translation1a ); + + assertEquals( translation1a, translationStore.getTranslation( className1, id1, Locale.UK, "name" ) ); + + translation1a.setValue( "regards" ); + translationStore.updateTranslation( translation1a ); - translationStore.updateTranslation( translation2c ); - - Translation savedTranslationAfterUpdate1 = - translationStore.getTranslation( className1, id1, Locale.UK, "name" ); - Translation savedTranslationAfterUpdate2 = - translationStore.getTranslation( className2, id2, Locale.US, "name" ); - - assertEquals( "org-unitssz", savedTranslationAfterUpdate1.getValue() ); - assertEquals( "dataelement-2", savedTranslationAfterUpdate2.getValue() ); - - // Delete - translationStore.deleteTranslation( translation3 ); - - Translation deletedTranslation = translationStore.getTranslation(className1, id1, Locale.FRANCE ,"name" ); - - assertNull(deletedTranslation); - - translationStore.deleteTranslations(className1, id1); - - col = translationStore.getAllTranslations(); - - assertEquals( "Unexpected amount of translations received after delete", 2, col.size() ); - } - + + assertEquals( "regards", translationStore.getTranslation( className1, id1, Locale.UK, "name" ).getValue() ); + } + + @Test + public void testGetTranslations1() + { + translationStore.addTranslation( translation1a ); + translationStore.addTranslation( translation1b ); + translationStore.addTranslation( translation2a ); + translationStore.addTranslation( translation2b ); + translationStore.addTranslation( translation2c ); + + assertEquals( 2, translationStore.getTranslations( className1, id1, Locale.UK ).size() ); + assertTrue( translationStore.getTranslations( className1, id1, Locale.UK ).contains( translation1a ) ); + assertTrue( translationStore.getTranslations( className1, id1, Locale.UK ).contains( translation1b ) ); + } + + @Test + public void testGetTranslations2() + { + translationStore.addTranslation( translation1a ); + translationStore.addTranslation( translation1b ); + translationStore.addTranslation( translation2a ); + translationStore.addTranslation( translation2b ); + translationStore.addTranslation( translation2c ); + + assertEquals( 2, translationStore.getTranslations( className1, Locale.UK ).size() ); + assertTrue( translationStore.getTranslations( className1, id1, Locale.UK ).contains( translation1a ) ); + assertTrue( translationStore.getTranslations( className1, id1, Locale.UK ).contains( translation1b ) ); + } + + @Test + public void testGetAllTranslations() + { + translationStore.addTranslation( translation1a ); + translationStore.addTranslation( translation1b ); + translationStore.addTranslation( translation2a ); + translationStore.addTranslation( translation2b ); + translationStore.addTranslation( translation2c ); + + assertEquals( 5, translationStore.getAllTranslations().size() ); + } + @Test public void testGetAvailableLocales() throws Exception { translationStore.addTranslation( translation1a ); + translationStore.addTranslation( translation1b ); translationStore.addTranslation( translation2a ); - translationStore.addTranslation( translation3 ); + translationStore.addTranslation( translation2b ); + translationStore.addTranslation( translation2c ); Collection locales = translationStore.getAvailableLocales(); - assertEquals( "Unexpected size of available locales", 3, locales.size() ); + assertEquals( 3, locales.size() ); } }