Skip to content

Commit 5ffe640

Browse files
Sonar fix
1 parent c83df0a commit 5ffe640

2 files changed

Lines changed: 157 additions & 53 deletions

File tree

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

Lines changed: 157 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -325,83 +325,191 @@ public void processAttachment(
325325

326326
Map<String, String> secondaryTypeProperties =
327327
SDMUtils.getSecondaryTypeProperties(attachmentEntity, attachment);
328-
String fileNameInDB;
329328
CmisDocument cmisDocument =
330329
dbQuery.getAttachmentForID(attachmentEntity.get(), persistenceService, id);
330+
String fileNameInDB = cmisDocument.getFileName();
331+
332+
// Check for upload status issues
333+
if (handleUploadStatusCheck(
334+
attachment,
335+
fileNameInDB,
336+
filenameInRequest,
337+
virusDetectedFiles,
338+
virusScanInProgressFiles,
339+
scanFailedFiles,
340+
uploadInProgressFiles)) {
341+
return;
342+
}
343+
344+
// Fetch file details from SDM if needed
331345
SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials();
332-
fileNameInDB = cmisDocument.getFileName();
333-
String uploadStatus = "";
334-
// Collect files with virus-related upload statuses
346+
AttachmentDetails details =
347+
fetchAttachmentDetails(
348+
fileNameInDB,
349+
descriptionInRequest,
350+
objectId,
351+
sdmCredentials,
352+
context.getUserInfo().isSystemUser());
353+
354+
Map<String, String> propertiesInDB =
355+
dbQuery.getPropertiesForID(
356+
attachmentEntity.get(), persistenceService, id, secondaryTypeProperties);
357+
358+
Map<String, String> updatedSecondaryProperties =
359+
prepareUpdatedProperties(
360+
attachmentEntity,
361+
attachment,
362+
filenameInRequest,
363+
descriptionInRequest,
364+
details.fileNameInDB,
365+
details.descriptionInDB,
366+
secondaryTypeProperties,
367+
propertiesInDB);
368+
369+
if (updatedSecondaryProperties.isEmpty()) {
370+
logger.debug("No changes detected for attachment ID: {}, skipping SDM update", id);
371+
return;
372+
}
373+
374+
updateAttachmentInSDM(
375+
attachmentEntity,
376+
context,
377+
attachment,
378+
id,
379+
filenameInRequest,
380+
descriptionInRequest,
381+
objectId,
382+
details.fileNameInDB,
383+
details.descriptionInDB,
384+
propertiesInDB,
385+
secondaryTypeProperties,
386+
updatedSecondaryProperties,
387+
secondaryPropertiesWithInvalidDefinitions,
388+
noSDMRoles,
389+
duplicateFileNameList,
390+
filesNotFound,
391+
filesWithUnsupportedProperties,
392+
badRequest);
393+
}
394+
395+
private boolean handleUploadStatusCheck(
396+
Map<String, Object> attachment,
397+
String fileNameInDB,
398+
String filenameInRequest,
399+
List<String> virusDetectedFiles,
400+
List<String> virusScanInProgressFiles,
401+
List<String> scanFailedFiles,
402+
List<String> uploadInProgressFiles) {
335403
Map<String, Object> readonlyData = (Map<String, Object>) attachment.get(SDM_READONLY_CONTEXT);
336-
if (readonlyData != null && readonlyData.get("uploadStatus") != null) {
337-
uploadStatus = readonlyData.get("uploadStatus").toString();
338-
if (uploadStatus.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_VIRUS_DETECTED)) {
339-
virusDetectedFiles.add(fileNameInDB != null ? fileNameInDB : filenameInRequest);
340-
return; // Skip further processing for this attachment
341-
}
342-
if (uploadStatus.equalsIgnoreCase(SDMConstants.VIRUS_SCAN_INPROGRESS)) {
343-
virusScanInProgressFiles.add(fileNameInDB != null ? fileNameInDB : filenameInRequest);
344-
return; // Skip further processing for this attachment
345-
}
346-
if (uploadStatus.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_SCAN_FAILED)) {
347-
scanFailedFiles.add(fileNameInDB != null ? fileNameInDB : filenameInRequest);
348-
return; // Skip further processing for this attachment
349-
}
350-
if (uploadStatus.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_IN_PROGRESS)) {
351-
uploadInProgressFiles.add(fileNameInDB != null ? fileNameInDB : filenameInRequest);
352-
return; // Skip further processing for this attachment
353-
}
354-
attachment.put("uploadStatus", uploadStatus);
404+
if (readonlyData == null || readonlyData.get("uploadStatus") == null) {
405+
return false;
406+
}
407+
408+
String uploadStatus = readonlyData.get("uploadStatus").toString();
409+
String fileName = fileNameInDB != null ? fileNameInDB : filenameInRequest;
410+
411+
if (uploadStatus.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_VIRUS_DETECTED)) {
412+
virusDetectedFiles.add(fileName);
413+
return true;
355414
}
356-
// Fetch from SDM if not in DB
415+
if (uploadStatus.equalsIgnoreCase(SDMConstants.VIRUS_SCAN_INPROGRESS)) {
416+
virusScanInProgressFiles.add(fileName);
417+
return true;
418+
}
419+
if (uploadStatus.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_SCAN_FAILED)) {
420+
scanFailedFiles.add(fileName);
421+
return true;
422+
}
423+
if (uploadStatus.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_IN_PROGRESS)) {
424+
uploadInProgressFiles.add(fileName);
425+
return true;
426+
}
427+
428+
attachment.put("uploadStatus", uploadStatus);
429+
return false;
430+
}
431+
432+
private AttachmentDetails fetchAttachmentDetails(
433+
String fileNameInDB,
434+
String descriptionInRequest,
435+
String objectId,
436+
SDMCredentials sdmCredentials,
437+
boolean isSystemUser)
438+
throws IOException {
439+
String finalFileNameInDB = fileNameInDB;
357440
String descriptionInDB = null;
441+
358442
if (fileNameInDB == null || descriptionInRequest != null) {
359443
JSONObject sdmAttachmentData =
360444
AttachmentsHandlerUtils.fetchAttachmentDataFromSDM(
361-
sdmService, objectId, sdmCredentials, context.getUserInfo().isSystemUser());
445+
sdmService, objectId, sdmCredentials, isSystemUser);
362446
JSONObject succinctProperties = sdmAttachmentData.getJSONObject("succinctProperties");
447+
363448
if (succinctProperties.has("cmis:name")) {
364-
fileNameInDB = succinctProperties.getString("cmis:name");
449+
finalFileNameInDB = succinctProperties.getString("cmis:name");
365450
}
366451
if (succinctProperties.has("cmis:description")) {
367452
descriptionInDB = succinctProperties.getString("cmis:description");
368453
}
369454
}
370455

371-
Map<String, String> propertiesInDB =
372-
dbQuery.getPropertiesForID(
373-
attachmentEntity.get(), persistenceService, id, secondaryTypeProperties);
374-
// Prepare document and updated properties
456+
return new AttachmentDetails(finalFileNameInDB, descriptionInDB);
457+
}
375458

459+
private Map<String, String> prepareUpdatedProperties(
460+
Optional<CdsEntity> attachmentEntity,
461+
Map<String, Object> attachment,
462+
String filenameInRequest,
463+
String descriptionInRequest,
464+
String fileNameInDB,
465+
String descriptionInDB,
466+
Map<String, String> secondaryTypeProperties,
467+
Map<String, String> propertiesInDB) {
376468
Map<String, String> updatedSecondaryProperties =
377469
SDMUtils.getUpdatedSecondaryProperties(
378470
attachmentEntity,
379471
attachment,
380472
persistenceService,
381473
secondaryTypeProperties,
382474
propertiesInDB);
383-
cmisDocument =
384-
AttachmentsHandlerUtils.prepareCmisDocument(
385-
filenameInRequest, descriptionInRequest, objectId);
386475

387-
// Update filename and description properties
388476
AttachmentsHandlerUtils.updateFilenameProperty(
389477
fileNameInDB, filenameInRequest, fileNameInDB, updatedSecondaryProperties);
390478

391479
AttachmentsHandlerUtils.updateDescriptionProperty(
392480
null, descriptionInRequest, descriptionInDB, updatedSecondaryProperties, true);
393481

394-
// Send update to SDM only if there are changes
395-
if (updatedSecondaryProperties.isEmpty()) {
396-
logger.debug("No changes detected for attachment ID: {}, skipping SDM update", id);
397-
return;
398-
}
482+
return updatedSecondaryProperties;
483+
}
399484

485+
private void updateAttachmentInSDM(
486+
Optional<CdsEntity> attachmentEntity,
487+
CdsUpdateEventContext context,
488+
Map<String, Object> attachment,
489+
String id,
490+
String filenameInRequest,
491+
String descriptionInRequest,
492+
String objectId,
493+
String fileNameInDB,
494+
String descriptionInDB,
495+
Map<String, String> propertiesInDB,
496+
Map<String, String> secondaryTypeProperties,
497+
Map<String, String> updatedSecondaryProperties,
498+
Map<String, String> secondaryPropertiesWithInvalidDefinitions,
499+
List<String> noSDMRoles,
500+
List<String> duplicateFileNameList,
501+
List<String> filesNotFound,
502+
List<String> filesWithUnsupportedProperties,
503+
Map<String, String> badRequest) {
400504
logger.debug(
401505
"Updating attachment in SDM - ID: {}, properties count: {}",
402506
id,
403507
updatedSecondaryProperties.size());
404508

509+
CmisDocument cmisDocument =
510+
AttachmentsHandlerUtils.prepareCmisDocument(
511+
filenameInRequest, descriptionInRequest, objectId);
512+
405513
try {
406514
int responseCode =
407515
sdmService.updateAttachments(
@@ -442,6 +550,16 @@ public void processAttachment(
442550
}
443551
}
444552

553+
private static class AttachmentDetails {
554+
final String fileNameInDB;
555+
final String descriptionInDB;
556+
557+
AttachmentDetails(String fileNameInDB, String descriptionInDB) {
558+
this.fileNameInDB = fileNameInDB;
559+
this.descriptionInDB = descriptionInDB;
560+
}
561+
}
562+
445563
private void handleWarnings(
446564
CdsUpdateEventContext context,
447565
List<String> duplicateFileNameList,

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,6 @@ public CmisDocument getAttachmentForObjectID(
156156
return cmisDocument;
157157
}
158158

159-
private CmisDocument mapRowToCmisDocument(Optional<Row> optionalRow) {
160-
CmisDocument cmisDocument = new CmisDocument();
161-
if (optionalRow.isPresent()) {
162-
Row row = optionalRow.get();
163-
cmisDocument.setType(row.get("type") != null ? row.get("type").toString() : null);
164-
cmisDocument.setUrl(row.get("linkUrl") != null ? row.get("linkUrl").toString() : null);
165-
cmisDocument.setUploadStatus(
166-
row.get("uploadStatus") != null
167-
? row.get("uploadStatus").toString()
168-
: SDMConstants.UPLOAD_STATUS_IN_PROGRESS);
169-
}
170-
return cmisDocument;
171-
}
172-
173159
/**
174160
* Retrieves valid secondary properties for the target attachment entity. Used to determine which
175161
* properties from SDM should be persisted to the database.

0 commit comments

Comments
 (0)