From 16261c92e5819c9e6125cbf9231b2b8d4cb6bab2 Mon Sep 17 00:00:00 2001 From: Derek Hulley Date: Tue, 9 Jan 2007 17:53:02 +0000 Subject: [PATCH] Implemented helper methods for retrieving translations git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4768 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../ml/MultilingualContentServiceImpl.java | 74 ++++++++++++++++++- .../MultilingualContentServiceImplTest.java | 19 +++-- .../cmr/ml/MultilingualContentService.java | 32 ++++++++ 3 files changed, 118 insertions(+), 7 deletions(-) diff --git a/source/java/org/alfresco/repo/model/ml/MultilingualContentServiceImpl.java b/source/java/org/alfresco/repo/model/ml/MultilingualContentServiceImpl.java index e324c37762..d4deede7bb 100644 --- a/source/java/org/alfresco/repo/model/ml/MultilingualContentServiceImpl.java +++ b/source/java/org/alfresco/repo/model/ml/MultilingualContentServiceImpl.java @@ -16,10 +16,14 @@ */ package org.alfresco.repo.model.ml; +import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; +import java.util.Set; import org.alfresco.error.AlfrescoRuntimeException; +import org.alfresco.i18n.I18NUtil; import org.alfresco.model.ContentModel; import org.alfresco.service.cmr.ml.MultilingualContentService; import org.alfresco.service.cmr.repository.ChildAssociationRef; @@ -231,6 +235,7 @@ public class MultilingualContentServiceImpl implements MultilingualContentServic return mlContainerNodeRef; } + /** @inheritDoc */ public NodeRef makeTranslation(NodeRef contentNodeRef, Locale locale) { NodeRef mlContainerNodeRef = makeTranslationImpl(null, contentNodeRef, locale); @@ -244,7 +249,8 @@ public class MultilingualContentServiceImpl implements MultilingualContentServic } return mlContainerNodeRef; } - + + /** @inheritDoc */ public NodeRef addTranslation(NodeRef newTranslationNodeRef, NodeRef translationOfNodeRef, Locale locale) { NodeRef mlContainerNodeRef = null; @@ -273,6 +279,7 @@ public class MultilingualContentServiceImpl implements MultilingualContentServic return mlContainerNodeRef; } + /** @inheritDoc */ public NodeRef getTranslationContainer(NodeRef translationNodeRef) { NodeRef mlContainerNodeRef = getOrCreateMLContainer(translationNodeRef, false); @@ -280,6 +287,7 @@ public class MultilingualContentServiceImpl implements MultilingualContentServic return mlContainerNodeRef; } + /** @inheritDoc */ public void createEdition(NodeRef mlContainerNodeRef, NodeRef translationNodeRef) { // Ensure that the translation given is one of the children @@ -326,4 +334,68 @@ public class MultilingualContentServiceImpl implements MultilingualContentServic " Current Version: " + mlContainerVersionLabel); } } + + /** @inheritDoc */ + public Map getTranslations(NodeRef translationOfNodeRef) + { + NodeRef mlContainerNodeRef = null; + // Were we given the translation or the container + QName typeQName = nodeService.getType(translationOfNodeRef); + if (typeQName.equals(ContentModel.TYPE_MULTILINGUAL_CONTAINER)) + { + // We have the container + mlContainerNodeRef = translationOfNodeRef; + } + else + { + // Get the container + mlContainerNodeRef = getOrCreateMLContainer(translationOfNodeRef, false); + } + // Get all the children + List assocRefs = nodeService.getChildAssocs( + mlContainerNodeRef, + ContentModel.ASSOC_MULTILINGUAL_CHILD, + RegexQNamePattern.MATCH_ALL); + // Iterate over them and build the map + Map nodeRefsByLocale = new HashMap(13); + for (ChildAssociationRef assocRef : assocRefs) + { + NodeRef nodeRef = assocRef.getChildRef(); + // Get the locale + Locale locale = (Locale) nodeService.getProperty(nodeRef, ContentModel.PROP_LOCALE); + // Map it + nodeRefsByLocale.put(locale, nodeRef); + } + // Done + if (logger.isDebugEnabled()) + { + logger.debug("Found all translations: \n" + + " Node: " + translationOfNodeRef + " (type " + typeQName + ")\n" + + " Map: " + nodeRefsByLocale); + } + return nodeRefsByLocale; + } + + /** @inheritDoc */ + public NodeRef getTranslationForLocale(NodeRef translationNodeRef, Locale locale) + { + // Get the container + getOrCreateMLContainer(translationNodeRef, false); + // Get all the translations + Map nodeRefsByLocale = getTranslations(translationNodeRef); + // Get the closest matching locale + Set locales = nodeRefsByLocale.keySet(); + Locale nearestLocale = I18NUtil.getNearestLocale(locale, locales); + NodeRef nearestNodeRef = nodeRefsByLocale.get(nearestLocale); + // Done + if (logger.isDebugEnabled()) + { + logger.debug("Found nearest locale: \n" + + " Given node: " + translationNodeRef + "\n" + + " Given locale: " + locale + "\n" + + " Found node: " + nearestNodeRef + "\n" + + " Found locale: " + nearestLocale); + } + return nearestNodeRef; + } } diff --git a/source/java/org/alfresco/repo/model/ml/MultilingualContentServiceImplTest.java b/source/java/org/alfresco/repo/model/ml/MultilingualContentServiceImplTest.java index d8bafd84ed..861d04f24c 100644 --- a/source/java/org/alfresco/repo/model/ml/MultilingualContentServiceImplTest.java +++ b/source/java/org/alfresco/repo/model/ml/MultilingualContentServiceImplTest.java @@ -17,8 +17,10 @@ package org.alfresco.repo.model.ml; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Locale; +import java.util.Map; import junit.framework.TestCase; @@ -212,11 +214,16 @@ public class MultilingualContentServiceImplTest extends TestCase // Now be sure that we can get the required information using the version service VersionHistory mlContainerVersionHistory = versionService.getVersionHistory(mlContainerNodeRef); - Version mlContainerRootVersion = mlContainerVersionHistory.getRootVersion(); - NodeRef mlContainerRootVersionNodeRef = mlContainerRootVersion.getVersionedNodeRef(); - // Get the root version's children - List mlContainerRootVersionChildren = nodeService.getChildAssocs(mlContainerRootVersionNodeRef); - // Check them -// assertEquals("Incorrect number of child nodes for root version", 3, mlContainerRootVersionChildren.size()); + Collection mlContainerVersions = mlContainerVersionHistory.getAllVersions(); + // Loop through and get all the children of each version + for (Version mlContainerVersion : mlContainerVersions) + { + NodeRef versionedMLContainerNodeRef = mlContainerVersion.getFrozenStateNodeRef(); + // Get all the children + Map translationsByLocale = multilingualContentService.getTranslations( + versionedMLContainerNodeRef); + // Count the children + int count = translationsByLocale.size(); + } } } diff --git a/source/java/org/alfresco/service/cmr/ml/MultilingualContentService.java b/source/java/org/alfresco/service/cmr/ml/MultilingualContentService.java index c067d32843..28462f2d70 100644 --- a/source/java/org/alfresco/service/cmr/ml/MultilingualContentService.java +++ b/source/java/org/alfresco/service/cmr/ml/MultilingualContentService.java @@ -16,10 +16,15 @@ */ package org.alfresco.service.cmr.ml; +import java.util.Collection; +import java.util.List; import java.util.Locale; +import java.util.Map; +import java.util.Set; import org.alfresco.service.Auditable; import org.alfresco.service.PublicService; +import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.NodeRef; /** @@ -79,4 +84,31 @@ public interface MultilingualContentService */ @Auditable(key = Auditable.Key.ARG_0, parameters = {"mlContainerNodeRef", "translationNodeRef"}) void createEdition(NodeRef mlContainerNodeRef, NodeRef translationNodeRef); + + /** + * Gets the set of sibling translations associated with the given cm:mlDocument or + * cm:mlContainer. + * + * @param translationOfNodeRef An existing cm:mlDocument or cm:mlContainer + * @return Returns a map of translation nodes keyed by locale + */ + @Auditable(key = Auditable.Key.ARG_0, parameters = {"translationOfNodeRef"}) + Map getTranslations(NodeRef translationOfNodeRef); + + /** + * Given a cm:mlDocument, this method attempts to find the best translation for the given + * locale. If there is not even a + * {@link org.alfresco.i18n.I18NUtil#getNearestLocale(Locale, Set) partial match}, then null + * is returned. + * + * @param translationNodeRef the cm:mlDocument + * @param locale the target locale + * @return Returns Returns the best match for the locale, or null if there + * is no near match. + * + * @see #getTranslations(NodeRef) + * @see org.alfresco.i18n.I18NUtil#getNearestLocale(Locale, Set) + */ + @Auditable(key = Auditable.Key.ARG_0, parameters = {"translationNodeRef", "locale"}) + NodeRef getTranslationForLocale(NodeRef translationNodeRef, Locale locale); }