diff --git a/source/java/org/alfresco/repo/model/ml/MultilingualContentServiceImpl.java b/source/java/org/alfresco/repo/model/ml/MultilingualContentServiceImpl.java index fa5342d7da..4664eac3a3 100644 --- a/source/java/org/alfresco/repo/model/ml/MultilingualContentServiceImpl.java +++ b/source/java/org/alfresco/repo/model/ml/MultilingualContentServiceImpl.java @@ -129,9 +129,11 @@ public class MultilingualContentServiceImpl implements MultilingualContentServic * cm:mlDocument already applied. * * @param mlDocumentNodeRef an existing cm:mlDocument + * @param allowCreate true if a cm:mlContainer must be created if on doesn't exist, + * otherwise false if a parent cm:mlContainer is expected to exist. * @return Returns the cm:mlContainer parent */ - private NodeRef getOrCreateMLContainer(NodeRef mlDocumentNodeRef) + private NodeRef getOrCreateMLContainer(NodeRef mlDocumentNodeRef, boolean allowCreate) { if (!nodeService.hasAspect(mlDocumentNodeRef, ContentModel.ASPECT_MULTILINGUAL_DOCUMENT)) { @@ -147,16 +149,22 @@ public class MultilingualContentServiceImpl implements MultilingualContentServic RegexQNamePattern.MATCH_ALL); if (parentAssocRefs.size() == 0) { - // Create a ML container - mlContainerNodeRef = makeMLContainer(); - createAssociation = true; + if (allowCreate) + { + // Create a ML container + mlContainerNodeRef = makeMLContainer(); + createAssociation = true; + } + else + { + throw new AlfrescoRuntimeException("No multilingual container exists for document node: " + mlDocumentNodeRef); + } } else if (parentAssocRefs.size() == 1) { // Just get it ChildAssociationRef toKeepAssocRef = parentAssocRefs.get(0); mlContainerNodeRef = toKeepAssocRef.getParentRef(); - createAssociation = true; } else if (parentAssocRefs.size() > 1) { @@ -189,7 +197,7 @@ public class MultilingualContentServiceImpl implements MultilingualContentServic return mlContainerNodeRef; } - public NodeRef makeTranslation(NodeRef contentNodeRef, Locale locale) + private NodeRef makeTranslationImpl(NodeRef mlContainerNodeRef, NodeRef contentNodeRef, Locale locale) { // Add the aspect using the given locale, of necessary if (!nodeService.hasAspect(contentNodeRef, ContentModel.ASPECT_MULTILINGUAL_DOCUMENT)) @@ -203,20 +211,72 @@ public class MultilingualContentServiceImpl implements MultilingualContentServic // The aspect is present, so just ensure that the locale is correct nodeService.setProperty(contentNodeRef, ContentModel.PROP_LOCALE, locale); } - // Get or create the container - NodeRef mlContainerNodeRef = getOrCreateMLContainer(contentNodeRef); + // Do we make use of an existing container? + if (mlContainerNodeRef == null) + { + // Make one + mlContainerNodeRef = getOrCreateMLContainer(contentNodeRef, true); + } + else + { + // Use the existing container + nodeService.addChild( + mlContainerNodeRef, + contentNodeRef, + ContentModel.ASSOC_MULTILINGUAL_CHILD, + QNAME_ML_TRANSLATION); + } // done return mlContainerNodeRef; } + public NodeRef makeTranslation(NodeRef contentNodeRef, Locale locale) + { + NodeRef mlContainerNodeRef = makeTranslationImpl(null, contentNodeRef, locale); + // done + if (logger.isDebugEnabled()) + { + logger.debug("Made a translation: \n" + + " content: " + contentNodeRef + "\n" + + " locale: " + locale + "\n" + + " container: " + mlContainerNodeRef); + } + return mlContainerNodeRef; + } + public NodeRef addTranslation(NodeRef newTranslationNodeRef, NodeRef translationOfNodeRef, Locale locale) { - throw new UnsupportedOperationException(); + 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); + } + // Use the existing container to make the new content into a translation + makeTranslationImpl(mlContainerNodeRef, newTranslationNodeRef, locale); + // done + if (logger.isDebugEnabled()) + { + logger.debug("Added a translation: \n" + + " Translation of: " + translationOfNodeRef + " of type " + typeQName + "\n" + + " New translation: " + newTranslationNodeRef + "\n" + + " Locale: " + locale); + } + return mlContainerNodeRef; } public NodeRef getTranslationContainer(NodeRef translationNodeRef) { - throw new UnsupportedOperationException(); + NodeRef mlContainerNodeRef = getOrCreateMLContainer(translationNodeRef, false); + // done + return mlContainerNodeRef; } public NodeRef createEdition(NodeRef mlContainerNodeRef, NodeRef translationNodeRef) diff --git a/source/java/org/alfresco/repo/model/ml/MultilingualContentServiceImplTest.java b/source/java/org/alfresco/repo/model/ml/MultilingualContentServiceImplTest.java index 028b906c5f..5f0be55d43 100644 --- a/source/java/org/alfresco/repo/model/ml/MultilingualContentServiceImplTest.java +++ b/source/java/org/alfresco/repo/model/ml/MultilingualContentServiceImplTest.java @@ -127,5 +127,46 @@ public class MultilingualContentServiceImplTest extends TestCase NodeRef mlContainerNodeRef = multilingualContentService.makeTranslation(contentNodeRef, Locale.CHINESE); // Check it assertNotNull("Container not created", mlContainerNodeRef); + // Check the container child count + int childCount = nodeService.getChildAssocs(mlContainerNodeRef).size(); + assertEquals("Incorrect number of child nodes", 1, childCount); + } + + public void testAddTranslationUsingContainer() throws Exception + { + // Make a container with a single translation + NodeRef chineseContentNodeRef = createContent(); + NodeRef mlContainerNodeRef = multilingualContentService.makeTranslation(chineseContentNodeRef, Locale.CHINESE); + // Create some more content + NodeRef frenchContentNodeRef = createContent(); + // Make this a translation of the Chinese + NodeRef newMLContainerNodeRef = multilingualContentService.addTranslation( + frenchContentNodeRef, + mlContainerNodeRef, + Locale.FRENCH); + // Make sure that the original container was used + assertEquals("Existing container should have been used", mlContainerNodeRef, newMLContainerNodeRef); + // Check the container child count + int childCount = nodeService.getChildAssocs(mlContainerNodeRef).size(); + assertEquals("Incorrect number of child nodes", 2, childCount); + } + + public void testAddTranslationUsingContent() throws Exception + { + // Make a container with a single translation + NodeRef chineseContentNodeRef = createContent(); + NodeRef mlContainerNodeRef = multilingualContentService.makeTranslation(chineseContentNodeRef, Locale.CHINESE); + // Create some more content + NodeRef frenchContentNodeRef = createContent(); + // Make this a translation of the Chinese + NodeRef newMLContainerNodeRef = multilingualContentService.addTranslation( + frenchContentNodeRef, + chineseContentNodeRef, + Locale.FRENCH); + // Make sure that the original container was used + assertEquals("Existing container should have been used", mlContainerNodeRef, newMLContainerNodeRef); + // Check the container child count + int childCount = nodeService.getChildAssocs(mlContainerNodeRef).size(); + assertEquals("Incorrect number of child nodes", 2, childCount); } }