Skip to content

Commit c83df0a

Browse files
Sonar fix
1 parent 3344387 commit c83df0a

4 files changed

Lines changed: 153 additions & 79 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void eventHandlers(CdsRuntimeConfigurer configurer) {
7272
DBQuery dbQueryInstance = DBQuery.getDBQueryInstance();
7373
SDMService sdmService = new SDMServiceImpl(binding, connectionPool, tokenHandlerInstance);
7474
DocumentUploadService documentService =
75-
new DocumentUploadService(binding, connectionPool, tokenHandlerInstance, dbQueryInstance);
75+
new DocumentUploadService(binding, connectionPool, tokenHandlerInstance);
7676
configurer.eventHandler(
7777
buildReadHandler(persistenceService, sdmService, tokenHandlerInstance, dbQueryInstance));
7878
configurer.eventHandler(

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

Lines changed: 151 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -254,34 +254,11 @@ private void processEntity(
254254
}
255255

256256
// Throw exception if any files failed virus scan or scan failed
257-
if (!virusDetectedFiles.isEmpty()
258-
|| !virusScanInProgressFiles.isEmpty()
259-
|| !scanFailedFiles.isEmpty()
260-
|| !uploadInProgressFiles.isEmpty()) {
261-
StringBuilder errorMessage = new StringBuilder();
262-
if (!virusDetectedFiles.isEmpty()) {
263-
errorMessage.append(SDMErrorMessages.virusDetectedFilesMessage(virusDetectedFiles));
264-
}
265-
if (!virusScanInProgressFiles.isEmpty()) {
266-
if (errorMessage.length() > 0) {
267-
errorMessage.append(" ");
268-
}
269-
errorMessage.append(
270-
SDMErrorMessages.virusScanInProgressFilesMessage(virusScanInProgressFiles));
271-
}
272-
if (!scanFailedFiles.isEmpty()) {
273-
if (errorMessage.length() > 0) {
274-
errorMessage.append(" ");
275-
}
276-
errorMessage.append(SDMErrorMessages.scanFailedFilesMessage(scanFailedFiles));
277-
}
278-
if (!uploadInProgressFiles.isEmpty()) {
279-
if (errorMessage.length() > 0) {
280-
errorMessage.append(" ");
281-
}
282-
errorMessage.append(SDMErrorMessages.uploadInProgressFilesMessage(uploadInProgressFiles));
283-
}
284-
throw new ServiceException(errorMessage.toString());
257+
String errorMessage =
258+
buildErrorMessage(
259+
virusDetectedFiles, virusScanInProgressFiles, scanFailedFiles, uploadInProgressFiles);
260+
if (!errorMessage.isEmpty()) {
261+
throw new ServiceException(errorMessage);
285262
}
286263

287264
SecondaryPropertiesKey secondaryPropertiesKey =
@@ -313,49 +290,125 @@ private void processAttachment(
313290
String descriptionInRequest = (String) attachment.get("note");
314291
String objectId = (String) attachment.get("objectId");
315292

316-
// Fetch original data from DB and SDM
317-
String fileNameInDB;
293+
// Fetch original data from DB
318294
CmisDocument cmisDocument =
319295
dbQuery.getAttachmentForID(attachmentEntity.get(), persistenceService, id);
296+
String fileNameInDB = cmisDocument.getFileName();
297+
298+
// Check upload status and collect problematic files
299+
if (checkUploadStatus(
300+
attachment,
301+
fileNameInDB,
302+
filenameInRequest,
303+
virusDetectedFiles,
304+
virusScanInProgressFiles,
305+
scanFailedFiles,
306+
uploadInProgressFiles)) {
307+
return; // Skip further processing if upload status is problematic
308+
}
320309

321-
fileNameInDB = cmisDocument.getFileName();
322-
String uploadStatus = "";
323-
// Collect files with virus-related upload statuses
310+
// Fetch data from SDM
311+
SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials();
312+
SDMAttachmentData sdmData = fetchSDMData(context, objectId, sdmCredentials);
313+
314+
// Prepare and update attachment in SDM
315+
updateAndSendToSDM(
316+
context,
317+
attachment,
318+
id,
319+
objectId,
320+
filenameInRequest,
321+
descriptionInRequest,
322+
fileNameInDB,
323+
sdmData.fileNameInSDM,
324+
sdmData.descriptionInSDM,
325+
sdmCredentials,
326+
attachmentEntity,
327+
secondaryPropertiesWithInvalidDefinitions,
328+
noSDMRoles,
329+
duplicateFileNameList,
330+
filesNotFound,
331+
filesWithUnsupportedProperties,
332+
badRequest);
333+
}
334+
335+
private boolean checkUploadStatus(
336+
Map<String, Object> attachment,
337+
String fileNameInDB,
338+
String filenameInRequest,
339+
List<String> virusDetectedFiles,
340+
List<String> virusScanInProgressFiles,
341+
List<String> scanFailedFiles,
342+
List<String> uploadInProgressFiles) {
324343
Map<String, Object> readonlyData = (Map<String, Object>) attachment.get(SDM_READONLY_CONTEXT);
325-
if (readonlyData != null && readonlyData.get("uploadStatus") != null) {
326-
uploadStatus = readonlyData.get("uploadStatus").toString();
327-
if (uploadStatus.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_VIRUS_DETECTED)) {
328-
virusDetectedFiles.add(fileNameInDB != null ? fileNameInDB : filenameInRequest);
329-
return; // Skip further processing for this attachment
330-
}
331-
if (uploadStatus.equalsIgnoreCase(SDMConstants.VIRUS_SCAN_INPROGRESS)) {
332-
virusScanInProgressFiles.add(fileNameInDB != null ? fileNameInDB : filenameInRequest);
333-
return; // Skip further processing for this attachment
334-
}
335-
if (uploadStatus.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_IN_PROGRESS)) {
336-
uploadInProgressFiles.add(fileNameInDB != null ? fileNameInDB : filenameInRequest);
337-
return; // Skip further processing for this attachment
338-
}
339-
if (uploadStatus.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_SCAN_FAILED)) {
340-
scanFailedFiles.add(fileNameInDB != null ? fileNameInDB : filenameInRequest);
341-
return; // Skip further processing for this attachment
342-
}
343-
attachment.put("uploadStatus", uploadStatus);
344+
if (readonlyData == null || readonlyData.get("uploadStatus") == null) {
345+
return false;
344346
}
345347

346-
SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials();
347-
String fileNameInSDM = null, descriptionInSDM = null;
348+
String uploadStatus = readonlyData.get("uploadStatus").toString();
349+
String fileName = fileNameInDB != null ? fileNameInDB : filenameInRequest;
350+
351+
if (uploadStatus.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_VIRUS_DETECTED)) {
352+
virusDetectedFiles.add(fileName);
353+
return true;
354+
}
355+
if (uploadStatus.equalsIgnoreCase(SDMConstants.VIRUS_SCAN_INPROGRESS)) {
356+
virusScanInProgressFiles.add(fileName);
357+
return true;
358+
}
359+
if (uploadStatus.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_IN_PROGRESS)) {
360+
uploadInProgressFiles.add(fileName);
361+
return true;
362+
}
363+
if (uploadStatus.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_SCAN_FAILED)) {
364+
scanFailedFiles.add(fileName);
365+
return true;
366+
}
367+
368+
attachment.put("uploadStatus", uploadStatus);
369+
return false;
370+
}
371+
372+
private SDMAttachmentData fetchSDMData(
373+
CdsCreateEventContext context, String objectId, SDMCredentials sdmCredentials)
374+
throws IOException {
348375
JSONObject sdmAttachmentData =
349376
AttachmentsHandlerUtils.fetchAttachmentDataFromSDM(
350377
sdmService, objectId, sdmCredentials, context.getUserInfo().isSystemUser());
351378
JSONObject succinctProperties = sdmAttachmentData.getJSONObject("succinctProperties");
379+
380+
String fileNameInSDM = null;
381+
String descriptionInSDM = null;
382+
352383
if (succinctProperties.has("cmis:name")) {
353384
fileNameInSDM = succinctProperties.getString("cmis:name");
354385
}
355386
if (succinctProperties.has("cmis:description")) {
356387
descriptionInSDM = succinctProperties.getString("cmis:description");
357388
}
358389

390+
return new SDMAttachmentData(fileNameInSDM, descriptionInSDM);
391+
}
392+
393+
private void updateAndSendToSDM(
394+
CdsCreateEventContext context,
395+
Map<String, Object> attachment,
396+
String id,
397+
String objectId,
398+
String filenameInRequest,
399+
String descriptionInRequest,
400+
String fileNameInDB,
401+
String fileNameInSDM,
402+
String descriptionInSDM,
403+
SDMCredentials sdmCredentials,
404+
Optional<CdsEntity> attachmentEntity,
405+
Map<String, String> secondaryPropertiesWithInvalidDefinitions,
406+
List<String> noSDMRoles,
407+
List<String> duplicateFileNameList,
408+
List<String> filesNotFound,
409+
List<String> filesWithUnsupportedProperties,
410+
Map<String, String> badRequest)
411+
throws IOException {
359412
Map<String, String> secondaryTypeProperties =
360413
SDMUtils.getSecondaryTypeProperties(attachmentEntity, attachment);
361414
Map<String, String> propertiesInDB =
@@ -364,19 +417,17 @@ private void processAttachment(
364417

365418
logger.debug("Processing attachment creation - ID: {}, objectId: {}", id, objectId);
366419

367-
// Prepare document and updated properties
368420
Map<String, String> updatedSecondaryProperties =
369421
SDMUtils.getUpdatedSecondaryProperties(
370422
attachmentEntity,
371423
attachment,
372424
persistenceService,
373425
secondaryTypeProperties,
374426
propertiesInDB);
375-
cmisDocument =
427+
CmisDocument cmisDocument =
376428
AttachmentsHandlerUtils.prepareCmisDocument(
377429
filenameInRequest, descriptionInRequest, objectId);
378430

379-
// Update filename and description properties
380431
AttachmentsHandlerUtils.updateFilenameProperty(
381432
fileNameInDB, filenameInRequest, fileNameInSDM, updatedSecondaryProperties);
382433
AttachmentsHandlerUtils.updateDescriptionProperty(
@@ -386,7 +437,6 @@ private void processAttachment(
386437
updatedSecondaryProperties,
387438
false);
388439

389-
// Send update to SDM and handle response
390440
logger.debug(
391441
"Creating attachment in SDM - ID: {}, properties count: {}",
392442
id,
@@ -413,7 +463,6 @@ private void processAttachment(
413463
noSDMRoles,
414464
duplicateFileNameList,
415465
filesNotFound);
416-
417466
} catch (ServiceException e) {
418467
AttachmentsHandlerUtils.handleSDMServiceException(
419468
e,
@@ -488,4 +537,47 @@ private void handleWarnings(
488537
+ contextInfo);
489538
}
490539
}
540+
541+
private String buildErrorMessage(
542+
List<String> virusDetectedFiles,
543+
List<String> virusScanInProgressFiles,
544+
List<String> scanFailedFiles,
545+
List<String> uploadInProgressFiles) {
546+
StringBuilder errorMessage = new StringBuilder();
547+
548+
if (!virusDetectedFiles.isEmpty()) {
549+
errorMessage.append(SDMErrorMessages.virusDetectedFilesMessage(virusDetectedFiles));
550+
}
551+
if (!virusScanInProgressFiles.isEmpty()) {
552+
appendWithSpace(errorMessage);
553+
errorMessage.append(
554+
SDMErrorMessages.virusScanInProgressFilesMessage(virusScanInProgressFiles));
555+
}
556+
if (!scanFailedFiles.isEmpty()) {
557+
appendWithSpace(errorMessage);
558+
errorMessage.append(SDMErrorMessages.scanFailedFilesMessage(scanFailedFiles));
559+
}
560+
if (!uploadInProgressFiles.isEmpty()) {
561+
appendWithSpace(errorMessage);
562+
errorMessage.append(SDMErrorMessages.uploadInProgressFilesMessage(uploadInProgressFiles));
563+
}
564+
565+
return errorMessage.toString();
566+
}
567+
568+
private void appendWithSpace(StringBuilder sb) {
569+
if (sb.length() > 0) {
570+
sb.append(" ");
571+
}
572+
}
573+
574+
private static class SDMAttachmentData {
575+
final String fileNameInSDM;
576+
final String descriptionInSDM;
577+
578+
SDMAttachmentData(String fileNameInSDM, String descriptionInSDM) {
579+
this.fileNameInSDM = fileNameInSDM;
580+
this.descriptionInSDM = descriptionInSDM;
581+
}
582+
}
491583
}

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,16 +156,6 @@ public CmisDocument getAttachmentForObjectID(
156156
return cmisDocument;
157157
}
158158

159-
private CmisDocument queryAttachmentData(
160-
PersistenceService persistenceService, CdsEntity entity, String objectId) {
161-
CqnSelect query =
162-
Select.from(entity)
163-
.columns("linkUrl", "type", "uploadStatus")
164-
.where(doc -> doc.get("objectId").eq(objectId));
165-
Result result = persistenceService.run(query);
166-
return mapRowToCmisDocument(result.first());
167-
}
168-
169159
private CmisDocument mapRowToCmisDocument(Optional<Row> optionalRow) {
170160
CmisDocument cmisDocument = new CmisDocument();
171161
if (optionalRow.isPresent()) {
@@ -180,10 +170,6 @@ private CmisDocument mapRowToCmisDocument(Optional<Row> optionalRow) {
180170
return cmisDocument;
181171
}
182172

183-
private boolean isEmptyCmisDocument(CmisDocument doc) {
184-
return doc.getType() == null && doc.getUrl() == null && doc.getUploadStatus() == null;
185-
}
186-
187173
/**
188174
* Retrieves valid secondary properties for the target attachment entity. Used to determine which
189175
* properties from SDM should be persisted to the database.

sdm/src/main/java/com/sap/cds/sdm/service/DocumentUploadService.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.sap.cds.sdm.handler.TokenHandler;
1212
import com.sap.cds.sdm.model.CmisDocument;
1313
import com.sap.cds.sdm.model.SDMCredentials;
14-
import com.sap.cds.sdm.persistence.DBQuery;
1514
import com.sap.cds.sdm.utilities.SDMUtils;
1615
import com.sap.cds.services.ServiceException;
1716
import com.sap.cds.services.environment.CdsProperties;
@@ -41,19 +40,16 @@ public class DocumentUploadService {
4140
private final ServiceBinding binding;
4241
private final CdsProperties.ConnectionPool connectionPool;
4342
private final TokenHandler tokenHandler;
44-
private DBQuery dbQuery;
4543

4644
public DocumentUploadService(
4745
ServiceBinding binding,
4846
CdsProperties.ConnectionPool connectionPool,
49-
TokenHandler tokenHandler,
50-
DBQuery dbQuery) {
47+
TokenHandler tokenHandler) {
5148
logger.info("DocumentUploadService is instantiated");
5249

5350
this.connectionPool = connectionPool;
5451
this.binding = binding;
5552
this.tokenHandler = tokenHandler;
56-
this.dbQuery = dbQuery;
5753
}
5854

5955
/*

0 commit comments

Comments
 (0)