From cfb5cb2c6d0757f352958bab775eb00e4ca5040b Mon Sep 17 00:00:00 2001 From: mohit-singh4 <158050587+mohit-singh4@users.noreply.github.com> Date: Wed, 23 Apr 2025 12:16:57 +0530 Subject: [PATCH] [fix/MNT-24172-fixRecreationOfRendition2] Fix of recreation of rendition2 (#3155) Co-authored-by: mohit-singh4 --- .../executer/AbstractRenderingEngine.java | 26 +++++++- .../RenditionService2IntegrationTest.java | 59 +++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/repository/src/main/java/org/alfresco/repo/rendition/executer/AbstractRenderingEngine.java b/repository/src/main/java/org/alfresco/repo/rendition/executer/AbstractRenderingEngine.java index 2f0e30c3d8..0ced53aa60 100644 --- a/repository/src/main/java/org/alfresco/repo/rendition/executer/AbstractRenderingEngine.java +++ b/repository/src/main/java/org/alfresco/repo/rendition/executer/AbstractRenderingEngine.java @@ -2,7 +2,7 @@ * #%L * Alfresco Repository * %% - * Copyright (C) 2005 - 2016 Alfresco Software Limited + * Copyright (C) 2005 - 2025 Alfresco Software Limited * %% * This file is part of the Alfresco software. * If the software was purchased under a paid Alfresco license, the terms of @@ -994,7 +994,29 @@ public abstract class AbstractRenderingEngine extends ActionExecuterAbstractBase { String msg = "A rendition of name: " + renditionQName + " should have been created for source node: " + sourceNode; - throw new RenditionServiceException(msg); + if (logger.isDebugEnabled()) + { + logger.debug(msg); + } + // Check if the node has the applied renditioned aspect, and if it does, + // remove the existing rendition node and assign the newly created rendition node. + if (nodeService.hasAspect(sourceNode, RenditionModel.ASPECT_RENDITIONED)) + { + List renditions = nodeService.getChildAssocs(sourceNode, RenditionModel.ASSOC_RENDITION, renditionQName); + if (!renditions.isEmpty()) + { + ChildAssociationRef existingRendition = renditions.get(0); + nodeService.removeChild(sourceNode, existingRendition.getChildRef()); + renditionAssoc = renditionNode; + + if (logger.isDebugEnabled()) + { + logger.debug("Removing the existing rendition node that doesn't have contentData and " + + "assigning the newly created rendition node: " + renditionAssoc); + + } + } + } } // Return the link between the source and the new, final rendition return renditionAssoc; diff --git a/repository/src/test/java/org/alfresco/repo/rendition2/RenditionService2IntegrationTest.java b/repository/src/test/java/org/alfresco/repo/rendition2/RenditionService2IntegrationTest.java index 8807d6724e..b2fd768e7a 100644 --- a/repository/src/test/java/org/alfresco/repo/rendition2/RenditionService2IntegrationTest.java +++ b/repository/src/test/java/org/alfresco/repo/rendition2/RenditionService2IntegrationTest.java @@ -717,4 +717,63 @@ public class RenditionService2IntegrationTest extends AbstractRenditionIntegrati renditionService2.setEnabled(true); } } + + @Test + public void testRecreationOfRendition2() + { + renditionService2.setEnabled(true); + try + { + NodeRef sourceNodeRef = createSource(ADMIN, "quick.docx"); + assertNotNull("Node not generated", sourceNodeRef); + + // Get content hash code for the source node. + int sourceNodeContentHashCode = getSourceContentHashCode(sourceNodeRef); + + // Trigger the pdf rendition. + render(ADMIN, sourceNodeRef, PDF); + NodeRef pdfRenditionNodeRef = waitForRendition(ADMIN, sourceNodeRef, PDF, true); + assertNotNull("pdf rendition was not generated", pdfRenditionNodeRef); + assertNotNull("pdf rendition was not generated", + nodeService.getProperty(pdfRenditionNodeRef, PROP_CONTENT)); + + // Check the pdf rendition content hash code is valid + int pdfRenditionContentHashCode = getRenditionContentHashCode(pdfRenditionNodeRef); + assertEquals("pdf rendition content hash code is different from source node content hash code", + sourceNodeContentHashCode, pdfRenditionContentHashCode); + + // Calling 'clearRenditionContentData' method directly so that rendition content will be cleaned. + AuthenticationUtil.runAs( + (AuthenticationUtil.RunAsWork) () -> transactionService.getRetryingTransactionHelper() + .doInTransaction(() -> { + renditionService2.clearRenditionContentData(sourceNodeRef, PDF); + return null; + }), + ADMIN); + + assertNull("Rendition has content", nodeService.getProperty(pdfRenditionNodeRef, PROP_CONTENT)); + + pdfRenditionContentHashCode = getRenditionContentHashCode(pdfRenditionNodeRef); + assertFalse("Rendition has content hash code", + isValidRenditionContentHashCode(pdfRenditionContentHashCode)); + + renditionService2.setEnabled(false); + + final QName pdfRendDefQName = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "pdf"); + transactionService.getRetryingTransactionHelper() + .doInTransaction(() -> AuthenticationUtil.runAs( + () -> renditionService.render(sourceNodeRef, pdfRendDefQName), ADMIN)); + + renditionService2.setEnabled(true); + + NodeRef pdfRecreatedRenditionNodeRef = waitForRendition(ADMIN, sourceNodeRef, PDF, true); + assertNotEquals(" Rendition before deletion and after previewing are identical", + pdfRenditionNodeRef.getId(), pdfRecreatedRenditionNodeRef.getId()); + } + finally + { + renditionService2.setEnabled(true); + } + + } }