Skip to content

Commit e7b80ac

Browse files
Changes
1 parent 9b281bd commit e7b80ac

4 files changed

Lines changed: 96 additions & 45 deletions

File tree

sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandler.java

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ private void processEntity(
166166
String targetEntity = context.getTarget().getQualifiedName();
167167
List<Map<String, Object>> attachments =
168168
AttachmentsHandlerUtils.fetchAttachments(targetEntity, entity, attachmentCompositionName);
169+
List<String> virusDetectedFiles = new ArrayList<>();
170+
List<String> virusScanInProgressFiles = new ArrayList<>();
171+
169172
if (attachments != null) {
170173
for (Map<String, Object> attachment : attachments) {
171174
processAttachment(
@@ -179,8 +182,32 @@ private void processEntity(
179182
composition,
180183
attachmentEntity,
181184
secondaryPropertiesWithInvalidDefinitions,
182-
noSDMRoles);
185+
noSDMRoles,
186+
virusDetectedFiles,
187+
virusScanInProgressFiles);
188+
}
189+
190+
// Throw exception if any files failed virus scan
191+
if (!virusDetectedFiles.isEmpty() || !virusScanInProgressFiles.isEmpty()) {
192+
StringBuilder errorMessage = new StringBuilder();
193+
if (!virusDetectedFiles.isEmpty()) {
194+
errorMessage
195+
.append("Virus detected in the following file(s): ")
196+
.append(String.join(", ", virusDetectedFiles))
197+
.append(". Please delete them.");
198+
}
199+
if (!virusScanInProgressFiles.isEmpty()) {
200+
if (errorMessage.length() > 0) {
201+
errorMessage.append(" ");
202+
}
203+
errorMessage
204+
.append("Virus scanning is in progress for the following file(s): ")
205+
.append(String.join(", ", virusScanInProgressFiles))
206+
.append(". Please refresh the page to see the effect.");
207+
}
208+
throw new ServiceException(errorMessage.toString());
183209
}
210+
184211
SecondaryPropertiesKey secondaryPropertiesKey =
185212
new SecondaryPropertiesKey(); // Emptying cache after attachments are updated in loop
186213
secondaryPropertiesKey.setRepositoryId(SDMConstants.REPOSITORY_ID);
@@ -199,7 +226,9 @@ private void processAttachment(
199226
String composition,
200227
Optional<CdsEntity> attachmentEntity,
201228
Map<String, String> secondaryPropertiesWithInvalidDefinitions,
202-
List<String> noSDMRoles)
229+
List<String> noSDMRoles,
230+
List<String> virusDetectedFiles,
231+
List<String> virusScanInProgressFiles)
203232
throws IOException {
204233
String id = (String) attachment.get("ID");
205234
String filenameInRequest = (String) attachment.get("fileName");
@@ -208,22 +237,23 @@ private void processAttachment(
208237

209238
// Fetch original data from DB and SDM
210239
String fileNameInDB;
211-
Optional<CdsEntity> attachmentDraftEntity =
212-
context.getModel().findEntity(attachmentEntity.get().getQualifiedName() + "_drafts");
213240
CmisDocument cmisDocument =
214-
dbQuery.getAttachmentForID(
215-
attachmentEntity.get(), persistenceService, id, attachmentDraftEntity.get());
241+
dbQuery.getAttachmentForID(attachmentEntity.get(), persistenceService, id);
216242

217243
fileNameInDB = cmisDocument.getFileName();
218-
if (cmisDocument.getUploadStatus() != null
219-
&& cmisDocument
220-
.getUploadStatus()
221-
.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_VIRUS_DETECTED))
222-
throw new ServiceException("Virus Detected in this file kindly delete it.");
223-
if (cmisDocument.getUploadStatus() != null
224-
&& cmisDocument.getUploadStatus().equalsIgnoreCase(SDMConstants.VIRUS_SCAN_INPROGRESS))
225-
throw new ServiceException(
226-
"Virus Scanning is in Progress. Refresh the page to see the effect");
244+
245+
// Collect files with virus-related upload statuses
246+
if (attachment.get("uploadStatus") != null) {
247+
String uploadStatus = attachment.get("uploadStatus").toString();
248+
if (uploadStatus.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_VIRUS_DETECTED)) {
249+
virusDetectedFiles.add(fileNameInDB != null ? fileNameInDB : filenameInRequest);
250+
return; // Skip further processing for this attachment
251+
}
252+
if (uploadStatus.equalsIgnoreCase(SDMConstants.VIRUS_SCAN_INPROGRESS)) {
253+
virusScanInProgressFiles.add(fileNameInDB != null ? fileNameInDB : filenameInRequest);
254+
return; // Skip further processing for this attachment
255+
}
256+
}
227257
SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials();
228258
String fileNameInSDM = null, descriptionInSDM = null;
229259
JSONObject sdmAttachmentData =

sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMUpdateAttachmentsHandler.java

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ private void processAttachments(
178178
Map<String, String> secondaryPropertiesWithInvalidDefinitions,
179179
List<String> noSDMRoles)
180180
throws IOException {
181+
List<String> virusDetectedFiles = new ArrayList<>();
182+
List<String> virusScanInProgressFiles = new ArrayList<>();
183+
181184
Iterator<Map<String, Object>> iterator = attachments.iterator();
182185
while (iterator.hasNext()) {
183186
Map<String, Object> attachment = iterator.next();
@@ -191,8 +194,32 @@ private void processAttachments(
191194
filesWithUnsupportedProperties,
192195
badRequest,
193196
secondaryPropertiesWithInvalidDefinitions,
194-
noSDMRoles);
197+
noSDMRoles,
198+
virusDetectedFiles,
199+
virusScanInProgressFiles);
200+
}
201+
202+
// Throw exception if any files failed virus scan
203+
if (!virusDetectedFiles.isEmpty() || !virusScanInProgressFiles.isEmpty()) {
204+
StringBuilder errorMessage = new StringBuilder();
205+
if (!virusDetectedFiles.isEmpty()) {
206+
errorMessage
207+
.append("Virus detected in the following file(s): ")
208+
.append(String.join(", ", virusDetectedFiles))
209+
.append(". Please delete them.");
210+
}
211+
if (!virusScanInProgressFiles.isEmpty()) {
212+
if (errorMessage.length() > 0) {
213+
errorMessage.append(" ");
214+
}
215+
errorMessage
216+
.append("Virus scanning is in progress for the following file(s): ")
217+
.append(String.join(", ", virusScanInProgressFiles))
218+
.append(". Please refresh the page to see the effect.");
219+
}
220+
throw new ServiceException(errorMessage.toString());
195221
}
222+
196223
SecondaryPropertiesKey secondaryPropertiesKey = new SecondaryPropertiesKey();
197224
secondaryPropertiesKey.setRepositoryId(SDMConstants.REPOSITORY_ID);
198225
Cache<SecondaryPropertiesKey, ?> cache = CacheConfig.getSecondaryPropertiesCache();
@@ -211,7 +238,9 @@ public void processAttachment(
211238
List<String> filesWithUnsupportedProperties,
212239
Map<String, String> badRequest,
213240
Map<String, String> secondaryPropertiesWithInvalidDefinitions,
214-
List<String> noSDMRoles)
241+
List<String> noSDMRoles,
242+
List<String> virusDetectedFiles,
243+
List<String> virusScanInProgressFiles)
215244
throws IOException {
216245
String id = (String) attachment.get("ID");
217246
String filenameInRequest = (String) attachment.get("fileName");
@@ -223,22 +252,23 @@ public void processAttachment(
223252
Map<String, String> secondaryTypeProperties =
224253
SDMUtils.getSecondaryTypeProperties(attachmentEntity, attachment);
225254
String fileNameInDB;
226-
Optional<CdsEntity> attachmentDraftEntity =
227-
context.getModel().findEntity(attachmentEntity.get().getQualifiedName() + "_drafts");
228255
CmisDocument cmisDocument =
229-
dbQuery.getAttachmentForID(
230-
attachmentEntity.get(), persistenceService, id, attachmentDraftEntity.get());
256+
dbQuery.getAttachmentForID(attachmentEntity.get(), persistenceService, id);
231257
SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials();
232258
fileNameInDB = cmisDocument.getFileName();
233-
if (cmisDocument.getUploadStatus() != null
234-
&& cmisDocument
235-
.getUploadStatus()
236-
.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_VIRUS_DETECTED))
237-
throw new ServiceException("Virus Detected in this file kindly delete it.");
238-
if (cmisDocument.getUploadStatus() != null
239-
&& cmisDocument.getUploadStatus().equalsIgnoreCase(SDMConstants.VIRUS_SCAN_INPROGRESS))
240-
throw new ServiceException(
241-
"Virus Scanning is in Progress. Refresh the page to see the effect");
259+
260+
// Collect files with virus-related upload statuses
261+
if (attachment.get("uploadStatus") != null) {
262+
String uploadStatus = attachment.get("uploadStatus").toString();
263+
if (uploadStatus.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_VIRUS_DETECTED)) {
264+
virusDetectedFiles.add(fileNameInDB != null ? fileNameInDB : filenameInRequest);
265+
return; // Skip further processing for this attachment
266+
}
267+
if (uploadStatus.equalsIgnoreCase(SDMConstants.VIRUS_SCAN_INPROGRESS)) {
268+
virusScanInProgressFiles.add(fileNameInDB != null ? fileNameInDB : filenameInRequest);
269+
return; // Skip further processing for this attachment
270+
}
271+
}
242272

243273
// Fetch from SDM if not in DB
244274
String descriptionInDB = null;

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -388,26 +388,14 @@ public Result getAttachmentsForUPIDAndRepository(
388388
}
389389

390390
public CmisDocument getAttachmentForID(
391-
CdsEntity attachmentEntity,
392-
PersistenceService persistenceService,
393-
String id,
394-
CdsEntity attachmentDraftEntity) {
391+
CdsEntity attachmentEntity, PersistenceService persistenceService, String id) {
395392
CqnSelect q =
396393
Select.from(attachmentEntity).columns("fileName").where(doc -> doc.get("ID").eq(id));
397394
Result result = persistenceService.run(q);
398395
CmisDocument cmisDocument = new CmisDocument();
399396
for (Row row : result.list()) {
400397
cmisDocument.setFileName(row.get("fileName").toString());
401398
}
402-
q =
403-
Select.from(attachmentDraftEntity)
404-
.columns("uploadStatus")
405-
.where(doc -> doc.get("ID").eq(id));
406-
result = persistenceService.run(q);
407-
for (Row row : result.list()) {
408-
cmisDocument.setUploadStatus(
409-
row.get("uploadStatus") != null ? row.get("uploadStatus").toString() : null);
410-
}
411399
return cmisDocument;
412400
}
413401

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ extend aspect Attachments with {
2020
objectId : String;
2121
linkUrl : String default null;
2222
type : String @(UI: {IsImageURL: true}) default 'sap-icon://document';
23-
uploadStatus : UploadStatusCode default 'Upload InProgress';
23+
uploadStatus : UploadStatusCode default 'UploadInProgress' @(
24+
Common.Text: statusNav.name,
25+
Common.TextArrangement: #TextOnly
26+
);
2427
statusNav : Association to one ScanStates on statusNav.code = uploadStatus;
2528
}
2629

2730
entity ScanStates : CodeList {
2831
key code : UploadStatusCode @Common.Text: name @Common.TextArrangement: #TextOnly;
29-
name : localized String(64) @Common.Label: '{i18n>Status}';
32+
name : String(64) @Common.Label: '{i18n>Status}';
3033
criticality : Integer @UI.Hidden;
3134
}
3235

0 commit comments

Comments
 (0)