@@ -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}
0 commit comments