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); + } + + } }