Skip to content

Commit 146c746

Browse files
Merge pull request #307 from cap-java/copybug
Copy Attachments to copy Links and type column value
2 parents cf1cd82 + a035b9c commit 146c746

File tree

7 files changed

+147
-69
lines changed

7 files changed

+147
-69
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,26 @@ annotate Attachments with @Common: {SideEffects #ContentChanged: {
744744
- Replace `AdminService` in `Action: 'AdminService.createLink'` with the name of your service.
745745
- Repeat for other entities and elements if you have defined multiple `composition of many Attachments`.
746746

747+
### Updating Tenant Databases for Link Feature
748+
To support the Link feature, additional database columns are introduced.
749+
Upon re-deployment of your multitenant application, you may encounter "invalid column" errors if tenant database containers are not updated.
750+
751+
To resolve this, ensure the following hook command is added to the mta.yaml for the sidecar application.
752+
```
753+
hooks:
754+
- name: upgrade-all
755+
type: task
756+
phases:
757+
- blue-green.application.before-start.idle
758+
- deploy.application.before-start
759+
parameters:
760+
name: upgrade
761+
memory: 512M
762+
disk-quota: 768M
763+
command: npx -p @sap/cds-mtx cds-mtx upgrade "*"
764+
```
765+
This will automatically update tenant databases during deployment. See this [example](https://github.com/vibhutikumar07/cloud-cap-samples-java/blob/31009de404af0ddc92b8c593b21395757ed053e6/mta.yaml#L71).
766+
747767
## Support for edit of link type attachments
748768

749769
This plugin provides the capability to update/edit the URL of attachments of link type.
@@ -834,6 +854,7 @@ annotate Attachments with @Common: {SideEffects #ContentChanged: {
834854
- Replace `AdminService` in `Action: 'AdminService.editLink'` with the name of your service.
835855
- Repeat for other entities and elements if you have defined multiple `composition of many Attachments`.
836856

857+
837858
## Known Restrictions
838859

839860
- UI5 Version 1.135.0: This version causes error in upload of attachments.

sdm/src/main/java/com/sap/cds/sdm/configuration/Registration.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ public void eventHandlers(CdsRuntimeConfigurer configurer) {
9797
dbQueryInstance,
9898
tokenHandlerInstance));
9999
configurer.eventHandler(
100-
new SDMCustomServiceHandler(sdmService, draftServiceList, tokenHandlerInstance));
100+
new SDMCustomServiceHandler(
101+
sdmService,
102+
draftServiceList,
103+
tokenHandlerInstance,
104+
dbQueryInstance,
105+
persistenceService));
101106
}
102107

103108
private AttachmentService buildAttachmentService() {

sdm/src/main/java/com/sap/cds/sdm/model/CmisDocument.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ public class CmisDocument {
2626
private String subdomain;
2727
private String url;
2828
private String contentId;
29+
private String type;
2930
}

sdm/src/main/java/com/sap/cds/sdm/persistence/DBQuery.java

Lines changed: 34 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.sap.cds.reflect.CdsEntity;
1111
import com.sap.cds.sdm.constants.SDMConstants;
1212
import com.sap.cds.sdm.model.CmisDocument;
13+
import com.sap.cds.sdm.service.handler.AttachmentCopyEventContext;
1314
import com.sap.cds.services.persistence.PersistenceService;
1415
import java.util.*;
1516

@@ -62,6 +63,38 @@ public CmisDocument getObjectIdForAttachmentID(
6263
return cmisDocument;
6364
}
6465

66+
public CmisDocument getAttachmentForObjectID(
67+
PersistenceService persistenceService, String id, AttachmentCopyEventContext context) {
68+
Optional<CdsEntity> attachmentEntity = context.getModel().findEntity(context.getFacet());
69+
CqnSelect q =
70+
Select.from(attachmentEntity.get())
71+
.columns("linkUrl", "type")
72+
.where(doc -> doc.get("objectId").eq(id));
73+
Result result = persistenceService.run(q);
74+
Optional<Row> res = result.first();
75+
CmisDocument cmisDocument = new CmisDocument();
76+
if (res.isPresent()) {
77+
Row row = res.get();
78+
cmisDocument.setType(row.get("type") != null ? row.get("type").toString() : null);
79+
cmisDocument.setUrl(row.get("linkUrl") != null ? row.get("linkUrl").toString() : null);
80+
} else {
81+
// Check in draft table as well
82+
attachmentEntity = context.getModel().findEntity(context.getFacet() + "_drafts");
83+
q =
84+
Select.from(attachmentEntity.get())
85+
.columns("linkUrl", "type")
86+
.where(doc -> doc.get("objectId").eq(id));
87+
result = persistenceService.run(q);
88+
res = result.first();
89+
if (res.isPresent()) {
90+
Row row = res.get();
91+
cmisDocument.setType(row.get("type") != null ? row.get("type").toString() : null);
92+
cmisDocument.setUrl(row.get("linkUrl") != null ? row.get("linkUrl").toString() : null);
93+
}
94+
}
95+
return cmisDocument;
96+
}
97+
6598
public Result getAttachmentsForUPIDAndRepository(
6699
CdsEntity attachmentEntity,
67100
PersistenceService persistenceService,
@@ -96,76 +129,14 @@ public void addAttachmentToDraft(
96129
updatedFields.put("repositoryId", repositoryId);
97130
updatedFields.put("folderId", cmisDocument.getFolderId());
98131
updatedFields.put("status", "Clean");
99-
String icon = getIconForMimeType(cmisDocument.getMimeType());
100-
updatedFields.put("type", icon);
101-
132+
updatedFields.put("type", "sap-icon://document");
102133
CqnUpdate updateQuery =
103134
Update.entity(attachmentEntity)
104135
.data(updatedFields)
105136
.where(doc -> doc.get("ID").eq(cmisDocument.getAttachmentId()));
106137
persistenceService.run(updateQuery);
107138
}
108139

109-
private static String getIconForMimeType(String mimeType) {
110-
if (isExcel(mimeType)) {
111-
return "sap-icon://excel-attachment";
112-
} else if (isImage(mimeType)) {
113-
return "sap-icon://attachment-photo";
114-
} else if (isText(mimeType)) {
115-
return "sap-icon://attachment-text-file";
116-
} else if (isPdf(mimeType)) {
117-
return "sap-icon://pdf-attachment";
118-
} else if (isPowerPoint(mimeType)) {
119-
return "sap-icon://ppt-attachment";
120-
} else if (isVideo(mimeType)) {
121-
return "sap-icon://attachment-video";
122-
} else if (isAudio(mimeType)) {
123-
return "sap-icon://attachment-audio";
124-
} else if (isZip(mimeType)) {
125-
return "sap-icon://attachment-zip-file";
126-
} else if (isHtml(mimeType)) {
127-
return "sap-icon://attachment-html";
128-
}
129-
return "sap-icon://document";
130-
}
131-
132-
private static boolean isExcel(String mimeType) {
133-
return mimeType.contains("vnd.ms-excel")
134-
|| mimeType.contains("vnd.openxmlformats-officedocument.spreadsheetml.sheet");
135-
}
136-
137-
private static boolean isImage(String mimeType) {
138-
return mimeType.contains("image");
139-
}
140-
141-
private static boolean isText(String mimeType) {
142-
return mimeType.contains("text");
143-
}
144-
145-
private static boolean isPdf(String mimeType) {
146-
return mimeType.contains("pdf");
147-
}
148-
149-
private static boolean isPowerPoint(String mimeType) {
150-
return mimeType.contains("powerpoint") || mimeType.contains("presentation");
151-
}
152-
153-
private static boolean isVideo(String mimeType) {
154-
return mimeType.contains("video");
155-
}
156-
157-
private static boolean isAudio(String mimeType) {
158-
return mimeType.contains("audio");
159-
}
160-
161-
private static boolean isZip(String mimeType) {
162-
return mimeType.contains("zip");
163-
}
164-
165-
private static boolean isHtml(String mimeType) {
166-
return mimeType.contains("html");
167-
}
168-
169140
public List<CmisDocument> getAttachmentsForFolder(
170141
String entity,
171142
PersistenceService persistenceService,

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
import com.sap.cds.sdm.handler.TokenHandler;
1010
import com.sap.cds.sdm.model.CmisDocument;
1111
import com.sap.cds.sdm.model.SDMCredentials;
12+
import com.sap.cds.sdm.persistence.DBQuery;
1213
import com.sap.cds.sdm.service.RegisterService;
1314
import com.sap.cds.sdm.service.SDMService;
1415
import com.sap.cds.services.ServiceException;
1516
import com.sap.cds.services.draft.DraftService;
1617
import com.sap.cds.services.handler.annotations.On;
1718
import com.sap.cds.services.handler.annotations.ServiceName;
19+
import com.sap.cds.services.persistence.PersistenceService;
1820
import java.io.IOException;
1921
import java.util.ArrayList;
2022
import java.util.HashMap;
@@ -26,14 +28,22 @@
2628
@ServiceName(value = "*", type = RegisterService.class)
2729
public class SDMCustomServiceHandler {
2830
private final SDMService sdmService;
31+
private final DBQuery dbQuery;
2932
private final List<DraftService> draftService;
3033
private final TokenHandler tokenHandler;
34+
private final PersistenceService persistenceService;
3135

3236
public SDMCustomServiceHandler(
33-
SDMService sdmService, List<DraftService> draftService, TokenHandler tokenHandler) {
37+
SDMService sdmService,
38+
List<DraftService> draftService,
39+
TokenHandler tokenHandler,
40+
DBQuery dbQuery,
41+
PersistenceService persistenceService) {
3442
this.sdmService = sdmService;
3543
this.draftService = draftService;
3644
this.tokenHandler = tokenHandler;
45+
this.dbQuery = dbQuery;
46+
this.persistenceService = persistenceService;
3747
}
3848

3949
@On(event = RegisterService.EVENT_COPY_ATTACHMENT)
@@ -62,12 +72,15 @@ public void copyAttachments(AttachmentCopyEventContext context) throws IOExcepti
6272
folderId = succinctProperties.getString("cmis:objectId");
6373
}
6474
CmisDocument cmisDocument = new CmisDocument();
65-
cmisDocument.setRepositoryId(repositoryId);
66-
cmisDocument.setFolderId(folderId);
75+
6776
List<String> objectIds = context.getObjectIds();
6877
List<List<String>> attachmentsMetadata = new ArrayList<>();
6978
for (String objectId : objectIds) {
79+
// get Link Url from objectId and set to cmisDocument
80+
cmisDocument = dbQuery.getAttachmentForObjectID(persistenceService, objectId, context);
7081
cmisDocument.setObjectId(objectId);
82+
cmisDocument.setRepositoryId(repositoryId);
83+
cmisDocument.setFolderId(folderId);
7184
try {
7285
attachmentsMetadata.add(
7386
sdmService.copyAttachment(cmisDocument, sdmCredentials, isSystemUser));
@@ -101,15 +114,21 @@ public void copyAttachments(AttachmentCopyEventContext context) throws IOExcepti
101114
for (List<String> attachmentMetadata : attachmentsMetadata) {
102115
String fileName = attachmentMetadata.get(0);
103116
String mimeType = attachmentMetadata.get(1);
117+
if (mimeType.equalsIgnoreCase("application/internet-shortcut")) {
118+
int dotIndex = fileName.lastIndexOf('.');
119+
fileName = fileName.substring(0, dotIndex);
120+
}
104121
String newObjectId = attachmentMetadata.get(2);
105122
updatedFields.put("objectId", newObjectId);
106123
updatedFields.put("repositoryId", repositoryId);
107124
updatedFields.put("folderId", folderId);
108125
updatedFields.put("status", "Clean");
109126
updatedFields.put("mimeType", mimeType);
127+
updatedFields.put("type", cmisDocument.getType());
110128
updatedFields.put("fileName", fileName);
111129
updatedFields.put("HasDraftEntity", false);
112130
updatedFields.put("HasActiveEntity", false);
131+
updatedFields.put("linkUrl", cmisDocument.getUrl());
113132
updatedFields.put(
114133
"contentId", newObjectId + ":" + folderId + ":" + context.getFacet() + ":" + mimeType);
115134
updatedFields.put(upIdKey, upID);

sdm/src/main/resources/cds/com.sap.cds/sdm/attachments.cds

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extend aspect Attachments with {
66
repositoryId : String;
77
objectId : String;
88
linkUrl : String default null;
9-
type : String @(UI: {IsImageURL: true}) default null;
9+
type : String @(UI: {IsImageURL: true}) default 'sap-icon://document';
1010
}
1111
annotate Attachments with @UI: {
1212
HeaderInfo: {

0 commit comments

Comments
 (0)