From cacba164a640795a0be8f483d09b5445afc2495c Mon Sep 17 00:00:00 2001 From: car031 Date: Fri, 3 May 2024 21:01:08 +0200 Subject: [PATCH] Make more usable the document merge --- .../logicaldoc/core/automation/DocTool.java | 40 +++++++++++- .../core/document/DocumentManagerImpl.java | 36 +---------- .../com/logicaldoc/core/util/MergeUtil.java | 61 +++++++++++++++++++ 3 files changed, 100 insertions(+), 37 deletions(-) create mode 100644 logicaldoc-core/src/main/java/com/logicaldoc/core/util/MergeUtil.java diff --git a/logicaldoc-core/src/main/java/com/logicaldoc/core/automation/DocTool.java b/logicaldoc-core/src/main/java/com/logicaldoc/core/automation/DocTool.java index 72b785f1f..1e121fdeb 100644 --- a/logicaldoc-core/src/main/java/com/logicaldoc/core/automation/DocTool.java +++ b/logicaldoc-core/src/main/java/com/logicaldoc/core/automation/DocTool.java @@ -278,15 +278,37 @@ public void store(Document doc) { * @param username the user in whose name the method is run */ public void store(Document doc, String username) { - User user = new SecurityTool().getUser(username); - DocumentHistory transaction = new DocumentHistory(); transaction.setDocument(doc); transaction.setDate(new Date()); - transaction.setUser(user); + transaction.setUser(new SecurityTool().getUser(username)); store(doc, transaction); } + /** + * Created a new document into the database + * + * @param doc the document that carries the metadata + * @param file the document's content + * @param username the user in whose name the method is run + * + * @return the created document + */ + public Document create(Document doc, File file, String username) { + DocumentHistory transaction = new DocumentHistory(); + transaction.setDocument(doc); + transaction.setDate(new Date()); + transaction.setUser(new SecurityTool().getUser(username)); + + DocumentManager manager = (DocumentManager) Context.get().getBean(DocumentManager.class); + try { + return manager.create(file, doc, transaction); + } catch (PersistenceException e) { + log.error(e.getMessage(), e); + return null; + } + } + /** * Initializes lazy loaded collections * @@ -322,6 +344,18 @@ public void move(Document doc, String targetPath, String username) { } } + /** + * Instantiate a new document that is a clone of the given one. The returned + * clone is not persisted. + * + * @param doc The source document to clone + * + * @return The cloned document + */ + public Document clone(Document doc) { + return new Document(doc); + } + /** * Copies a document into a target folder * diff --git a/logicaldoc-core/src/main/java/com/logicaldoc/core/document/DocumentManagerImpl.java b/logicaldoc-core/src/main/java/com/logicaldoc/core/document/DocumentManagerImpl.java index 1dfec19c9..d8a5bf4db 100644 --- a/logicaldoc-core/src/main/java/com/logicaldoc/core/document/DocumentManagerImpl.java +++ b/logicaldoc-core/src/main/java/com/logicaldoc/core/document/DocumentManagerImpl.java @@ -24,8 +24,6 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; -import org.apache.pdfbox.io.MemoryUsageSetting; -import org.apache.pdfbox.multipdf.PDFMergerUtility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -54,6 +52,7 @@ import com.logicaldoc.core.threading.ThreadPools; import com.logicaldoc.core.ticket.Ticket; import com.logicaldoc.core.ticket.TicketDAO; +import com.logicaldoc.core.util.MergeUtil; import com.logicaldoc.util.Context; import com.logicaldoc.util.config.ContextProperties; import com.logicaldoc.util.html.HTMLSanitizer; @@ -1682,7 +1681,7 @@ public Document merge(Collection documents, long targetFolderId, Strin Arrays.sort(pdfs, (o1, o2) -> o1.getName().compareTo(o2.getName())); // Merge all the PDFs - bigPdf = mergePdf(List.of(pdfs)); + bigPdf = MergeUtil.mergePdf(List.of(pdfs)); // Add an history entry to track the export of the document DocumentDAO docDao = (DocumentDAO) Context.get().getBean(DocumentDAO.class); @@ -1747,37 +1746,6 @@ private File preparePdfs(User user, List docIds) throws IOException { return tempDir; } - /** - * Merges different PDFs into a single PDF- - * - * @param pdfs ordered list of pdf files to be merged - * @return The merged Pdf file - * - * @throws IOException - */ - private File mergePdf(List pdfs) throws IOException { - File tempDir = null; - try { - Path tempPath = Files.createTempDirectory(MERGE); - tempDir = tempPath.toFile(); - - File dst = FileUtil.createTempFile(MERGE, ".pdf"); - - PDFMergerUtility merger = new PDFMergerUtility(); - for (File file : pdfs) - merger.addSource(file); - - merger.setDestinationFileName(dst.getAbsolutePath()); - MemoryUsageSetting memoryUsage = MemoryUsageSetting.setupTempFileOnly(); - memoryUsage.setTempDir(tempDir); - merger.mergeDocuments(memoryUsage); - - return dst; - } finally { - FileUtil.strongDelete(tempDir); - } - } - public void setDocumentLinkDAO(DocumentLinkDAO documentLinkDAO) { this.documentLinkDAO = documentLinkDAO; } diff --git a/logicaldoc-core/src/main/java/com/logicaldoc/core/util/MergeUtil.java b/logicaldoc-core/src/main/java/com/logicaldoc/core/util/MergeUtil.java new file mode 100644 index 000000000..a2a27e186 --- /dev/null +++ b/logicaldoc-core/src/main/java/com/logicaldoc/core/util/MergeUtil.java @@ -0,0 +1,61 @@ +package com.logicaldoc.core.util; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import org.apache.pdfbox.io.MemoryUsageSetting; +import org.apache.pdfbox.multipdf.PDFMergerUtility; + +import com.logicaldoc.util.io.FileUtil; + +/** + * An utility to merge files + * + * @author Marco Meschieri - LogicalDOC + * @since 8.9.2 + * + */ +public class MergeUtil { + + private static final String MERGE = "merge"; + + private MergeUtil() { + // Not instantiable class + } + + /** + * Merges different PDFs into a single PDF- + * + * @param pdfs ordered list of pdf files to be merged + * @return The merged Pdf file + * + * @throws IOException + */ + public static File mergePdf(List pdfs) throws IOException { + File tempDir = null; + try { + Path tempPath = Files.createTempDirectory(MERGE); + tempDir = tempPath.toFile(); + + File dst = FileUtil.createTempFile(MERGE, ".pdf"); + + PDFMergerUtility merger = new PDFMergerUtility(); + for (File file : pdfs) { + merger.addSource(file); + System.out.println("Added "+file.getPath()); + } + + merger.setDestinationFileName(dst.getAbsolutePath()); + MemoryUsageSetting memoryUsage = MemoryUsageSetting.setupTempFileOnly(); + memoryUsage.setTempDir(tempDir); + merger.mergeDocuments(memoryUsage); + + return dst; + } finally { + FileUtil.strongDelete(tempDir); + } + } +}