Skip to content

Commit c888d77

Browse files
committed
fix for copy link
1 parent 30fb4d8 commit c888d77

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMCustomServiceHandler.java

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ public class SDMCustomServiceHandler {
3333
private final TokenHandler tokenHandler;
3434
private final PersistenceService persistenceService;
3535

36+
// Result class for copyAttachmentsToSDM method
37+
private static class CopyAttachmentsResult {
38+
private final List<List<String>> attachmentsMetadata;
39+
private final List<CmisDocument> populatedDocuments;
40+
41+
public CopyAttachmentsResult(
42+
List<List<String>> attachmentsMetadata, List<CmisDocument> populatedDocuments) {
43+
this.attachmentsMetadata = attachmentsMetadata;
44+
this.populatedDocuments = populatedDocuments;
45+
}
46+
47+
public List<List<String>> getAttachmentsMetadata() {
48+
return attachmentsMetadata;
49+
}
50+
51+
public List<CmisDocument> getPopulatedDocuments() {
52+
return populatedDocuments;
53+
}
54+
}
55+
3656
public SDMCustomServiceHandler(
3757
SDMService sdmService,
3858
List<DraftService> draftService,
@@ -70,27 +90,28 @@ public void copyAttachments(AttachmentCopyEventContext context) throws IOExcepti
7090
!= null;
7191
String folderId = ensureFolderExists(folderName, repositoryId, sdmCredentials, isSystemUser);
7292

73-
CmisDocument cmisDocument = new CmisDocument();
7493
List<String> objectIds = context.getObjectIds();
7594
System.out.println("objectIds: " + objectIds);
7695

77-
List<List<String>> attachmentsMetadata =
96+
CopyAttachmentsResult copyResult =
7897
copyAttachmentsToSDM(
7998
context, objectIds, folderId, repositoryId, sdmCredentials, isSystemUser, folderExists);
8099

100+
List<List<String>> attachmentsMetadata = copyResult.getAttachmentsMetadata();
101+
List<CmisDocument> populatedDocuments = copyResult.getPopulatedDocuments();
81102
System.out.println("attachmentsMetadata: " + attachmentsMetadata);
82103

83104
String upIdKey = resolveUpIdKey(context, parentEntity, compositionName);
84105
System.out.println("upIdKey: " + upIdKey);
85106
createDraftEntries(
86107
attachmentsMetadata,
108+
populatedDocuments,
87109
parentEntity,
88110
compositionName,
89111
upID,
90112
upIdKey,
91113
repositoryId,
92-
folderId,
93-
cmisDocument);
114+
folderId);
94115

95116
context.setCompleted();
96117
}
@@ -113,7 +134,7 @@ private String ensureFolderExists(
113134
return folderId;
114135
}
115136

116-
private List<List<String>> copyAttachmentsToSDM(
137+
private CopyAttachmentsResult copyAttachmentsToSDM(
117138
AttachmentCopyEventContext context,
118139
List<String> objectIds,
119140
String folderId,
@@ -123,6 +144,7 @@ private List<List<String>> copyAttachmentsToSDM(
123144
boolean folderExists)
124145
throws IOException {
125146
List<List<String>> attachmentsMetadata = new ArrayList<>();
147+
List<CmisDocument> populatedDocuments = new ArrayList<>();
126148

127149
for (String objectId : objectIds) {
128150
CmisDocument cmisDocument =
@@ -132,14 +154,21 @@ private List<List<String>> copyAttachmentsToSDM(
132154
cmisDocument.setRepositoryId(repositoryId);
133155
cmisDocument.setFolderId(folderId);
134156

157+
// Create individual document for each attachment with its own type and linkUrl
158+
CmisDocument populatedDocument = new CmisDocument();
159+
populatedDocument.setType(cmisDocument.getType());
160+
populatedDocument.setUrl(cmisDocument.getUrl());
161+
populatedDocuments.add(populatedDocument);
162+
135163
try {
136164
attachmentsMetadata.add(
137165
sdmService.copyAttachment(cmisDocument, sdmCredentials, isSystemUser));
138166
} catch (ServiceException e) {
139167
handleCopyFailure(context, folderId, folderExists, attachmentsMetadata, e);
140168
}
141169
}
142-
return attachmentsMetadata;
170+
171+
return new CopyAttachmentsResult(attachmentsMetadata, populatedDocuments);
143172
}
144173

145174
private void handleCopyFailure(
@@ -206,16 +235,19 @@ private String resolveUpIdKey(
206235

207236
private void createDraftEntries(
208237
List<List<String>> attachmentsMetadata,
238+
List<CmisDocument> populatedDocuments,
209239
String parentEntity,
210240
String compositionName,
211241
String upID,
212242
String upIdKey,
213243
String repositoryId,
214-
String folderId,
215-
CmisDocument cmisDocument) {
216-
Map<String, Object> updatedFields = new HashMap<>();
244+
String folderId) {
245+
246+
for (int i = 0; i < attachmentsMetadata.size(); i++) {
247+
List<String> attachmentMetadata = attachmentsMetadata.get(i);
248+
CmisDocument cmisDocument = populatedDocuments.get(i);
249+
Map<String, Object> updatedFields = new HashMap<>();
217250

218-
for (List<String> attachmentMetadata : attachmentsMetadata) {
219251
String fileName = attachmentMetadata.get(0);
220252
System.out.println("fileName: " + fileName);
221253
String mimeType = attachmentMetadata.get(1);
@@ -231,11 +263,11 @@ private void createDraftEntries(
231263
updatedFields.put("folderId", folderId);
232264
updatedFields.put("status", "Clean");
233265
updatedFields.put("mimeType", mimeType);
234-
updatedFields.put("type", cmisDocument.getType());
266+
updatedFields.put("type", cmisDocument.getType()); // Individual type for each attachment
235267
updatedFields.put("fileName", fileName);
236268
updatedFields.put("HasDraftEntity", false);
237269
updatedFields.put("HasActiveEntity", false);
238-
updatedFields.put("linkUrl", cmisDocument.getUrl());
270+
updatedFields.put("linkUrl", cmisDocument.getUrl()); // Individual linkUrl for each attachment
239271
updatedFields.put(
240272
"contentId",
241273
newObjectId

0 commit comments

Comments
 (0)