Skip to content

Commit ff9c59d

Browse files
committed
1224 Option to copy links and notes when a document is copied into
another folder
1 parent bb71e51 commit ff9c59d

File tree

21 files changed

+446
-209
lines changed

21 files changed

+446
-209
lines changed

logicaldoc-cmis/src/main/java/com/logicaldoc/cmis/LDRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ public String createDocumentFromSource(CallContext context, String sourceId, Str
487487
transaction.setUser(getSessionUser());
488488
transaction.setComment("");
489489

490-
Document newDoc = documentManager.copyToFolder(doc, target, transaction);
490+
Document newDoc = documentManager.copyToFolder(doc, target, transaction, true, true);
491491
return getId(newDoc);
492492

493493
} catch (Exception t) {

logicaldoc-core/src/main/java/com/logicaldoc/core/automation/DocTool.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,14 @@ public void move(Document doc, String targetPath, String username) {
328328
*
329329
* @param doc the document
330330
* @param targetPath the full path of the target folder
331+
* @param links if the links must be copied too
332+
* @param notes if the notes must be copied too
333+
*
331334
* @param username the user in whose name the method is run
332335
*
333336
* @return the new document created
334337
*/
335-
public Document copy(Document doc, String targetPath, String username) {
338+
public Document copy(Document doc, String targetPath, boolean links, boolean notes, String username) {
336339
User user = new SecurityTool().getUser(username);
337340

338341
DocumentManager manager = (DocumentManager) Context.get().getBean(DocumentManager.class);
@@ -344,13 +347,26 @@ public Document copy(Document doc, String targetPath, String username) {
344347
transaction.setUser(user);
345348

346349
try {
347-
return manager.copyToFolder(doc, folder, transaction);
350+
return manager.copyToFolder(doc, folder, transaction, links, notes);
348351
} catch (PersistenceException | IOException e) {
349352
log.error(e.getMessage(), e);
350353
return null;
351354
}
352355
}
353356

357+
/**
358+
* Copies a document into a target folder
359+
*
360+
* @param doc the document
361+
* @param targetPath the full path of the target folder
362+
* @param username the user in whose name the method is run
363+
*
364+
* @return the new document created
365+
*/
366+
public Document copy(Document doc, String targetPath, String username) {
367+
return copy(doc, targetPath, false, false, username);
368+
}
369+
354370
/**
355371
* Links two documents
356372
*

logicaldoc-core/src/main/java/com/logicaldoc/core/document/DocumentLink.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ public Document getDocument2() {
4242
public void setDocument2(Document document2) {
4343
this.document2 = document2;
4444
}
45-
}
45+
}

logicaldoc-core/src/main/java/com/logicaldoc/core/document/DocumentManager.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,15 @@ public long index(long docId, String content, DocumentHistory transaction)
257257
* @param doc The document to move
258258
* @param folder The target folder
259259
* @param transaction entry to log the event (set the user)
260+
* @param links if links must be copied too
261+
* @param notes if notes and annotations must be copied too
262+
*
260263
* @return The created document
261264
*
262265
* @throws PersistenceException error at data layer
263266
* @throws IOException I/O error
264267
*/
265-
public Document copyToFolder(Document doc, Folder folder, DocumentHistory transaction)
268+
public Document copyToFolder(Document doc, Folder folder, DocumentHistory transaction, boolean links, boolean notes)
266269
throws PersistenceException, IOException;
267270

268271
/**

logicaldoc-core/src/main/java/com/logicaldoc/core/document/DocumentManagerImpl.java

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.logicaldoc.core.conversion.FormatConverterManager;
3131
import com.logicaldoc.core.document.dao.DocumentDAO;
3232
import com.logicaldoc.core.document.dao.DocumentHistoryDAO;
33+
import com.logicaldoc.core.document.dao.DocumentLinkDAO;
3334
import com.logicaldoc.core.document.dao.DocumentNoteDAO;
3435
import com.logicaldoc.core.document.dao.VersionDAO;
3536
import com.logicaldoc.core.folder.Folder;
@@ -83,6 +84,8 @@ public class DocumentManagerImpl implements DocumentManager {
8384

8485
private DocumentDAO documentDAO;
8586

87+
private DocumentLinkDAO documentLinkDAO;
88+
8689
private DocumentNoteDAO documentNoteDAO;
8790

8891
private FolderDAO folderDAO;
@@ -987,7 +990,7 @@ public int countPages(Document doc) {
987990
}
988991
}
989992

990-
public Document copyToFolder(Document doc, Folder folder, DocumentHistory transaction)
993+
public Document copyToFolder(Document doc, Folder folder, DocumentHistory transaction, boolean links, boolean notes)
991994
throws PersistenceException, IOException {
992995
validateTransaction(transaction);
993996

@@ -999,8 +1002,7 @@ public Document copyToFolder(Document doc, Folder folder, DocumentHistory transa
9991002
}
10001003

10011004
String resource = storer.getResourceName(doc, null, null);
1002-
InputStream is = storer.getStream(doc.getId(), resource);
1003-
try {
1005+
try (InputStream is = storer.getStream(doc.getId(), resource);) {
10041006
Document cloned = new Document(doc);
10051007
cloned.setId(0);
10061008
if (doc.getFolder().getId() != folder.getId())
@@ -1026,11 +1028,51 @@ public Document copyToFolder(Document doc, Folder folder, DocumentHistory transa
10261028
copyEvent.setComment(newPath + "/" + createdDocument.getFileName());
10271029
documentDAO.saveDocumentHistory(doc, copyEvent);
10281030

1031+
if (links)
1032+
copyLinks(doc, createdDocument);
1033+
1034+
if (notes)
1035+
copyNotes(doc, createdDocument);
1036+
10291037
return createdDocument;
1030-
} finally {
1031-
if (is != null)
1032-
is.close();
1033-
is = null;
1038+
}
1039+
}
1040+
1041+
private void copyNotes(Document sourceDocument, Document createdDocument) {
1042+
List<DocumentNote> docNotes = documentNoteDAO.findByDocId(sourceDocument.getId(), sourceDocument.getFileVersion());
1043+
for (DocumentNote docNote : docNotes) {
1044+
DocumentNote newNote = new DocumentNote(docNote);
1045+
newNote.setDocId(createdDocument.getId());
1046+
newNote.setFileVersion(null);
1047+
1048+
try {
1049+
documentNoteDAO.store(newNote);
1050+
} catch (PersistenceException e) {
1051+
log.warn("Error copying note {}", docNote);
1052+
}
1053+
}
1054+
}
1055+
1056+
private void copyLinks(Document sourceDocument, Document createdDocument) {
1057+
List<DocumentLink> docLinks = documentLinkDAO.findByDocId(sourceDocument.getId());
1058+
1059+
for (DocumentLink docLink : docLinks) {
1060+
DocumentLink newLink = new DocumentLink();
1061+
newLink.setTenantId(docLink.getTenantId());
1062+
newLink.setType(docLink.getType());
1063+
if (docLink.getDocument1().getId() == sourceDocument.getId()) {
1064+
newLink.setDocument1(createdDocument);
1065+
newLink.setDocument2(docLink.getDocument2());
1066+
} else {
1067+
newLink.setDocument2(createdDocument);
1068+
newLink.setDocument1(docLink.getDocument1());
1069+
}
1070+
1071+
try {
1072+
documentLinkDAO.store(newLink);
1073+
} catch (PersistenceException e) {
1074+
log.warn("Error copying link {}", docLink);
1075+
}
10341076
}
10351077
}
10361078

@@ -1149,7 +1191,7 @@ public Document replaceAlias(long aliasId, DocumentHistory transaction) throws P
11491191
documentDAO.delete(aliasId, transaction);
11501192

11511193
try {
1152-
return copyToFolder(originalDoc, folder, new DocumentHistory(transaction));
1194+
return copyToFolder(originalDoc, folder, new DocumentHistory(transaction), true, true);
11531195
} catch (IOException e) {
11541196
throw new PersistenceException(e);
11551197
}
@@ -1400,8 +1442,7 @@ public void archiveDocuments(long[] docIds, DocumentHistory transaction) throws
14001442
}
14011443

14021444
@Override
1403-
public Ticket createTicket(
1404-
Ticket ticket, DocumentHistory transaction)
1445+
public Ticket createTicket(Ticket ticket, DocumentHistory transaction)
14051446
throws PersistenceException, PermissionException {
14061447
validateTransaction(transaction);
14071448

@@ -1557,10 +1598,10 @@ public int enforceFilesIntoFolderStorage(long rootFolderId, DocumentHistory tran
15571598
targetStorage);
15581599

15591600
List<Document> documents = documentDAO.findByFolder(folderId, null);
1560-
1601+
15611602
for (Document document : documents) {
15621603
int movedFiles = storer.moveResourcesToStore(document.getId(), targetStorage);
1563-
1604+
15641605
if (movedFiles > 0) {
15651606
totalMovedFiles += movedFiles;
15661607
try {
@@ -1714,4 +1755,8 @@ private File mergePdf(File[] pdfs) throws IOException {
17141755
FileUtil.strongDelete(tempDir);
17151756
}
17161757
}
1758+
1759+
public void setDocumentLinkDAO(DocumentLinkDAO documentLinkDAO) {
1760+
this.documentLinkDAO = documentLinkDAO;
1761+
}
17171762
}

logicaldoc-core/src/main/java/com/logicaldoc/core/document/DocumentNote.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.Date;
44

5+
import org.apache.commons.lang.StringUtils;
6+
57
import com.logicaldoc.core.PersistentObject;
68

79
/**
@@ -114,8 +116,7 @@ public DocumentNote(DocumentNote source) {
114116
this.type = source.type;
115117
this.recipient = source.recipient;
116118
this.recipientEmail = source.recipientEmail;
117-
118-
setFileVersion(source.getFileVersion());
119+
this.setTenantId(source.getTenantId());
119120
}
120121

121122
public long getDocId() {
@@ -293,4 +294,9 @@ public double getRotation() {
293294
public void setRotation(double rotation) {
294295
this.rotation = rotation;
295296
}
297+
298+
@Override
299+
public String toString() {
300+
return StringUtils.abbreviate(message, 40) + "(" + getId() + ")";
301+
}
296302
}

logicaldoc-core/src/main/java/com/logicaldoc/core/document/dao/HibernateDocumentNoteDAO.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,32 @@ public HibernateDocumentNoteDAO() {
3939
}
4040

4141
@Override
42-
public void store(DocumentNote note, DocumentHistory transaction) throws PersistenceException {
43-
super.store(note);
44-
42+
public void store(DocumentNote note) throws PersistenceException {
4543
DocumentDAO documentDao = (DocumentDAO) Context.get().getBean(DocumentDAO.class);
4644
Document doc = documentDao.findById(note.getDocId());
47-
if (doc != null && doc.getIndexed() == AbstractDocument.INDEX_INDEXED) {
45+
if (doc == null)
46+
throw new PersistenceException("Cannot save note for undexisting document " + note.getDocId());
47+
48+
if (note.getFileVersion() == null)
49+
note.setFileVersion(doc.getFileVersion());
50+
51+
super.store(note);
52+
53+
if (doc.getIndexed() == AbstractDocument.INDEX_INDEXED) {
4854
documentDao.initialize(doc);
4955
doc.setIndexed(AbstractDocument.INDEX_TO_INDEX);
5056
documentDao.store(doc);
5157
}
52-
58+
}
59+
60+
@Override
61+
public void store(DocumentNote note, DocumentHistory transaction) throws PersistenceException {
62+
this.store(note);
63+
5364
try {
5465
if (transaction != null) {
66+
DocumentDAO documentDao = (DocumentDAO) Context.get().getBean(DocumentDAO.class);
67+
Document doc = documentDao.findById(note.getDocId());
5568
transaction.setEvent(DocumentEvent.NEW_NOTE.toString());
5669
documentDao.saveDocumentHistory(doc, transaction);
5770
}

logicaldoc-core/src/main/resources/context/context-core.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@
298298
<property name="config" ref="ContextProperties" />
299299
<property name="documentDAO" ref="DocumentDAO" />
300300
<property name="documentNoteDAO" ref="DocumentNoteDAO" />
301+
<property name="documentLinkDAO" ref="DocumentLinkDAO" />
301302
<property name="folderDAO" ref="FolderDAO" />
302303
<property name="versionDAO" ref="VersionDAO" />
303304
<property name="ticketDAO" ref="TicketDAO" />

logicaldoc-core/src/test/java/com/logicaldoc/core/document/DocumentManagerImplTest.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNotNull;
55
import static org.junit.Assert.assertNull;
6+
import static org.junit.Assert.assertTrue;
67

78
import java.io.File;
89
import java.io.FileInputStream;
@@ -12,6 +13,7 @@
1213
import java.sql.SQLException;
1314
import java.util.Arrays;
1415
import java.util.Calendar;
16+
import java.util.List;
1517
import java.util.concurrent.CountDownLatch;
1618
import java.util.concurrent.TimeUnit;
1719

@@ -21,6 +23,7 @@
2123
import com.logicaldoc.core.AbstractCoreTestCase;
2224
import com.logicaldoc.core.PersistenceException;
2325
import com.logicaldoc.core.document.dao.DocumentDAO;
26+
import com.logicaldoc.core.document.dao.DocumentLinkDAO;
2427
import com.logicaldoc.core.document.dao.DocumentNoteDAO;
2528
import com.logicaldoc.core.document.dao.VersionDAO;
2629
import com.logicaldoc.core.folder.Folder;
@@ -58,6 +61,8 @@ public class DocumentManagerImplTest extends AbstractCoreTestCase {
5861

5962
private DocumentNoteDAO documentNoteDao;
6063

64+
private DocumentLinkDAO documentLinkDao;
65+
6166
private MockStorer storer;
6267

6368
// Instance under test
@@ -73,6 +78,7 @@ public void setUp() throws FileNotFoundException, IOException, SQLException {
7378
userDao = (UserDAO) context.getBean("UserDAO");
7479
folderDao = (FolderDAO) context.getBean("FolderDAO");
7580
documentNoteDao = (DocumentNoteDAO) context.getBean("DocumentNoteDAO");
81+
documentLinkDao = (DocumentLinkDAO) context.getBean("DocumentLinkDAO");
7682
storer = (MockStorer) context.getBean("Storer");
7783

7884
// Make sure that this is a DocumentManagerImpl instance
@@ -113,7 +119,7 @@ public void testUpdate() throws PersistenceException, InterruptedException {
113119
}
114120

115121
@Test
116-
public void testcreateTicket() throws PersistenceException, PermissionException, InterruptedException {
122+
public void testCreateTicket() throws PersistenceException, PermissionException, InterruptedException {
117123
Document doc = docDao.findById(1);
118124
Assert.assertNotNull(doc);
119125

@@ -198,11 +204,43 @@ public void testCopyToFolder() throws PersistenceException, IOException {
198204
Folder newFolder = folderDao.findById(6);
199205
docDao.initialize(doc);
200206

207+
List<DocumentLink> originalLinks = documentLinkDao.findByDocId(doc.getId());
208+
Assert.assertFalse(originalLinks.isEmpty());
209+
210+
List<DocumentNote> originalNotes = documentNoteDao.findByDocId(doc.getId(), null);
211+
Assert.assertFalse(originalNotes.isEmpty());
212+
201213
try {
202214
storer.setUseDummyFile(true);
203-
Document newDoc = documentManager.copyToFolder(doc, newFolder, transaction);
215+
Document newDoc = documentManager.copyToFolder(doc, newFolder, transaction, false, false);
204216
Assert.assertNotSame(doc.getId(), newDoc.getId());
205217
Assert.assertEquals(newFolder, newDoc.getFolder());
218+
Assert.assertTrue(documentLinkDao.findByDocId(newDoc.getId()).isEmpty());
219+
Assert.assertTrue(documentNoteDao.findByDocId(newDoc.getId(), null).isEmpty());
220+
} finally {
221+
storer.setUseDummyFile(false);
222+
}
223+
224+
try {
225+
storer.setUseDummyFile(true);
226+
Document newDoc = documentManager.copyToFolder(doc, newFolder, transaction, true, true);
227+
Assert.assertNotSame(doc.getId(), newDoc.getId());
228+
Assert.assertEquals(newFolder, newDoc.getFolder());
229+
230+
List<DocumentLink> links = documentLinkDao.findByDocId(newDoc.getId());
231+
Assert.assertEquals(originalLinks.size(), links.size());
232+
for (DocumentLink link : links) {
233+
assertNotNull(link.getDocument1());
234+
assertNotNull(link.getDocument2());
235+
assertTrue(newDoc.getId() == link.getDocument1().getId() || newDoc.getId() == link.getDocument2().getId());
236+
}
237+
238+
List<DocumentNote> notes = documentNoteDao.findByDocId(newDoc.getId(), null);
239+
Assert.assertEquals(originalNotes.size(), notes.size());
240+
for (DocumentNote note : notes) {
241+
assertEquals(newDoc.getId(), note.getDocId());
242+
assertEquals(newDoc.getFileVersion(), note.getFileVersion());
243+
}
206244
} finally {
207245
storer.setUseDummyFile(false);
208246
}
@@ -295,7 +333,7 @@ public void testEnforceFilesIntoFolderStorage()
295333
transaction = new DocumentHistory();
296334
transaction.setUser(user);
297335
documentManager.enforceFilesIntoFolderStorage(folder.getId(), transaction);
298-
336+
299337
waiting();
300338
Assert.assertTrue(new File(store2Root + "/1/doc/" + doc.getFileVersion()).exists());
301339
}

0 commit comments

Comments
 (0)