[MNT-24555] Set download node name on node creation (#2901)

* [MNT-24555] Set download node name on node creation

* [MNT-24555] PMD scan changes
This commit is contained in:
Tiago Salvado
2024-09-19 09:28:52 +01:00
committed by GitHub
parent b7642b5813
commit 1c1c9704a1
7 changed files with 84 additions and 9 deletions

View File

@@ -238,6 +238,17 @@ public interface CustomModelService
*/ */
public NodeRef createDownloadNode(String modelName, boolean withAssociatedForm); public NodeRef createDownloadNode(String modelName, boolean withAssociatedForm);
/**
* Creates a downloadable archive file containing the custom model file and
* if specified, its associated Share extension module file.
*
* @param modelName the model name to be exported
* @param withAssociatedForm whether Share extension module file should be
* included or not
* @return reference to the node which will contain the archive file
*/
NodeRef createDownloadNode(String modelName, boolean withAssociatedForm, String downloadNodeName);
/** /**
* Compiles the {@link M2Model}. * Compiles the {@link M2Model}.
* *

View File

@@ -712,8 +712,7 @@ public class CustomModelsImpl implements CustomModels
boolean withForm = Boolean.valueOf(propName); boolean withForm = Boolean.valueOf(propName);
try try
{ {
NodeRef nodeRef = customModelService.createDownloadNode(modelName, withForm); NodeRef nodeRef = customModelService.createDownloadNode(modelName, withForm, modelName + DownloadsImpl.DEFAULT_ARCHIVE_EXTENSION);
nodeService.setProperty(nodeRef, ContentModel.PROP_NAME, modelName + DownloadsImpl.DEFAULT_ARCHIVE_EXTENSION);
return new CustomModelDownload(nodeRef); return new CustomModelDownload(nodeRef);
} }
catch (Exception ex) catch (Exception ex)

View File

@@ -1077,6 +1077,12 @@ public class CustomModelServiceImpl implements CustomModelService
@Override @Override
public NodeRef createDownloadNode(final String modelFileName, boolean withAssociatedForm) public NodeRef createDownloadNode(final String modelFileName, boolean withAssociatedForm)
{
return createDownloadNode(modelFileName, withAssociatedForm, null);
}
@Override
public NodeRef createDownloadNode(final String modelFileName, boolean withAssociatedForm, String downloadNodeName)
{ {
List<NodeRef> nodesToBeDownloaded = new ArrayList<>(2); List<NodeRef> nodesToBeDownloaded = new ArrayList<>(2);
@@ -1137,7 +1143,7 @@ public class CustomModelServiceImpl implements CustomModelService
try try
{ {
NodeRef archiveNodeRef = downloadService.createDownload(nodesToBeDownloaded.toArray(new NodeRef[nodesToBeDownloaded.size()]), false); NodeRef archiveNodeRef = downloadService.createDownload(nodesToBeDownloaded.toArray(new NodeRef[0]), false, downloadNodeName);
if (logger.isDebugEnabled()) if (logger.isDebugEnabled())
{ {

View File

@@ -28,12 +28,14 @@ package org.alfresco.repo.download;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.download.cannedquery.DownloadEntity; import org.alfresco.repo.download.cannedquery.DownloadEntity;
import org.alfresco.repo.transaction.RetryingTransactionHelper; import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.download.DownloadService; import org.alfresco.service.cmr.download.DownloadService;
import org.alfresco.service.cmr.download.DownloadStatus; import org.alfresco.service.cmr.download.DownloadStatus;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.util.ParameterCheck; import org.alfresco.util.ParameterCheck;
/** /**
@@ -50,6 +52,7 @@ public class DownloadServiceImpl implements DownloadService {
private ActionServiceHelper actionServiceHelper; private ActionServiceHelper actionServiceHelper;
private DownloadStorage downloadStorage; private DownloadStorage downloadStorage;
private RetryingTransactionHelper transactionHelper; private RetryingTransactionHelper transactionHelper;
private NodeService nodeService;
// Dependency setters // Dependency setters
public void setActionServiceHelper(ActionServiceHelper actionServiceHelper) public void setActionServiceHelper(ActionServiceHelper actionServiceHelper)
@@ -66,9 +69,19 @@ public class DownloadServiceImpl implements DownloadService {
{ {
this.downloadStorage = downloadStorage; this.downloadStorage = downloadStorage;
} }
@Override public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
@Override
public NodeRef createDownload(final NodeRef[] requestedNodes, final boolean recursive) { public NodeRef createDownload(final NodeRef[] requestedNodes, final boolean recursive) {
return createDownload(requestedNodes, recursive, null);
}
@Override
public NodeRef createDownload(final NodeRef[] requestedNodes, final boolean recursive, String downloadNodeName) {
ParameterCheck.mandatory("nodeRefs", requestedNodes); ParameterCheck.mandatory("nodeRefs", requestedNodes);
if (requestedNodes.length < 1) if (requestedNodes.length < 1)
{ {
@@ -91,7 +104,12 @@ public class DownloadServiceImpl implements DownloadService {
{ {
downloadStorage.addNodeToDownload(downloadNode, node); downloadStorage.addNodeToDownload(downloadNode, node);
} }
if (downloadNodeName != null)
{
nodeService.setProperty(downloadNode, ContentModel.PROP_NAME, downloadNodeName);
}
return downloadNode; return downloadNode;
} }
}, false, true); }, false, true);

View File

@@ -40,7 +40,7 @@ import org.alfresco.service.cmr.repository.NodeRef;
public interface DownloadService public interface DownloadService
{ {
/** /**
* Start the creation of a downlaodable archive file containing the content * Start the creation of a downloadable archive file containing the content
* from the given nodeRefs. * from the given nodeRefs.
* *
* Implementations are expected to do this asynchronously, with clients * Implementations are expected to do this asynchronously, with clients
@@ -50,10 +50,27 @@ public interface DownloadService
* extended in the future, to support additional archive types. * extended in the future, to support additional archive types.
* *
* @param nodeRefs NodeRefs of content to be added to the archive file * @param nodeRefs NodeRefs of content to be added to the archive file
* @param recusirsive Recurse into container nodes * @param recursive Recurse into container nodes
* @return Reference to node which will eventually contain the archive file * @return Reference to node which will eventually contain the archive file
*/ */
public NodeRef createDownload(NodeRef[] nodeRefs, boolean recusirsive); NodeRef createDownload(NodeRef[] nodeRefs, boolean recursive);
/**
* Start the creation of a downloadable archive file containing the content
* from the given nodeRefs.
*
* Implementations are expected to do this asynchronously, with clients
* using the returned NodeRef to check on progress.
* Initially, only zip files will be supported, however this could be
* extended in the future, to support additional archive types.
*
* @param nodeRefs NodeRefs of content to be added to the archive file
* @param recursive Recurse into container nodes
* @param downloadNodeName Download node name
* @return Reference to node which will eventually contain the archive file
*/
NodeRef createDownload(NodeRef[] nodeRefs, boolean recursive, String downloadNodeName);
/** /**
* Get the status of the of the download identified by downloadNode. * Get the status of the of the download identified by downloadNode.

View File

@@ -115,6 +115,7 @@
<property name="actionServiceHelper" ref="downloadActionServiceHelper"/> <property name="actionServiceHelper" ref="downloadActionServiceHelper"/>
<property name="downloadStorage" ref="downloadStorage"/> <property name="downloadStorage" ref="downloadStorage"/>
<property name="transactionHelper" ref="retryingTransactionHelper"/> <property name="transactionHelper" ref="retryingTransactionHelper"/>
<property name="nodeService" ref="NodeService"/>
</bean> </bean>
<bean id="downloadCleanerSchedulerAccessor" class="org.springframework.scheduling.quartz.SchedulerAccessorBean"> <bean id="downloadCleanerSchedulerAccessor" class="org.springframework.scheduling.quartz.SchedulerAccessorBean">

View File

@@ -349,6 +349,29 @@ public class DownloadServiceIntegrationTest
validateEntries(entryNames, allEntries, true); validateEntries(entryNames, allEntries, true);
} }
@Test public void createDownloadWithName() throws IOException, InterruptedException
{
// Initiate the download
final NodeRef downloadNode = DOWNLOAD_SERVICE.createDownload(new NodeRef[] {rootFile}, false, "test.zip");
Assert.assertNotNull(downloadNode);
testNodes.addNodeRef(downloadNode);
// Validate that the download node has been persisted correctly.
TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Object>()
{
@Override
public Object execute() throws Throwable
{
Map<QName, Serializable> properties = NODE_SERVICE.getProperties(downloadNode);
Assert.assertEquals(Boolean.FALSE, properties.get(DownloadModel.PROP_RECURSIVE));
Assert.assertEquals("test.zip",properties.get(ContentModel.PROP_NAME));
return null;
}
});
}
private void validateEntries(final Set<String> entryNames, final Set<String> expectedEntries, boolean onlyExpected) private void validateEntries(final Set<String> entryNames, final Set<String> expectedEntries, boolean onlyExpected)
{ {
Set<String> copy = new TreeSet<String>(entryNames); Set<String> copy = new TreeSet<String>(entryNames);