@@ -74,11 +74,9 @@ public void updateName(
7474 Map <String , String > propertyTitles = new HashMap <>();
7575 Map <String , String > secondaryPropertiesWithInvalidDefinitions = new HashMap <>();
7676 String targetEntity = context .getTarget ().getQualifiedName ();
77- Set <String > duplicateFilenames =
78- SDMUtils .isFileNameDuplicateInDrafts (data , attachmentCompositionName , targetEntity );
79- if (!duplicateFilenames .isEmpty ()) {
80- handleDuplicateFilenames (context , duplicateFilenames );
81- } else {
77+ Boolean isError = false ;
78+ isError = AttachmentsHandlerUtils .validateFileNames (context , data , attachmentCompositionName );
79+ if (!isError ) {
8280 List <String > fileNameWithRestrictedCharacters = new ArrayList <>();
8381 List <String > duplicateFileNameList = new ArrayList <>();
8482 List <String > filesNotFound = new ArrayList <>();
@@ -89,14 +87,19 @@ public void updateName(
8987 List <Map <String , Object >> attachments =
9088 AttachmentsHandlerUtils .fetchAttachments (
9189 targetEntity , entity , attachmentCompositionName );
90+ if (attachments == null || attachments .isEmpty ()) {
91+ logger .info (
92+ "No attachments found for composition [{}] in entity [{}]. Skipping processing." ,
93+ attachmentCompositionName ,
94+ targetEntity );
95+ continue ;
96+ }
9297 Optional <CdsEntity > attachmentEntity =
9398 context .getModel ().findEntity (attachmentCompositionDefinition );
94- if (attachments != null && !attachments .isEmpty ()) {
95- propertyTitles = SDMUtils .getPropertyTitles (attachmentEntity , attachments .get (0 ));
96- secondaryPropertiesWithInvalidDefinitions =
97- SDMUtils .getSecondaryPropertiesWithInvalidDefinition (
98- attachmentEntity , attachments .get (0 ));
99- }
99+ propertyTitles = SDMUtils .getPropertyTitles (attachmentEntity , attachments .get (0 ));
100+ secondaryPropertiesWithInvalidDefinitions =
101+ SDMUtils .getSecondaryPropertiesWithInvalidDefinition (
102+ attachmentEntity , attachments .get (0 ));
100103 processEntity (
101104 context ,
102105 entity ,
@@ -123,16 +126,6 @@ public void updateName(
123126 }
124127 }
125128
126- private void handleDuplicateFilenames (
127- CdsCreateEventContext context , Set <String > duplicateFilenames ) {
128- context
129- .getMessages ()
130- .error (
131- String .format (
132- SDMConstants .DUPLICATE_FILE_IN_DRAFT_ERROR_MESSAGE ,
133- String .join (", " , duplicateFilenames )));
134- }
135-
136129 private void processEntity (
137130 CdsCreateEventContext context ,
138131 Map <String , Object > entity ,
@@ -225,82 +218,56 @@ private void processAttachment(
225218 persistenceService ,
226219 secondaryTypeProperties ,
227220 propertiesInDB );
228-
229- if (Boolean .TRUE .equals (SDMUtils .isRestrictedCharactersInName (filenameInRequest ))) {
221+ if (SDMUtils .hasRestrictedCharactersInName (filenameInRequest )) {
230222 fileNameWithRestrictedCharacters .add (filenameInRequest );
231- replacePropertiesInAttachment (
232- attachment ,
233- fileNameInSDM ,
234- propertiesInDB ,
235- secondaryTypeProperties ); // In this case we immediately stop the processing (Request
236- // isn't sent to SDM)
237- } else {
238- CmisDocument cmisDocument = new CmisDocument ();
239- cmisDocument .setFileName (filenameInRequest );
240- cmisDocument .setObjectId (objectId );
241- if (fileNameInDB
242- == null ) { // If the file name in DB is null, it means that the file is being created for
243- // the first time
244- if (filenameInRequest != null ) {
245- updatedSecondaryProperties .put ("filename" , filenameInRequest );
246- } else {
247- throw new ServiceException ("Filename cannot be empty" );
248- }
249- } else {
250- if (filenameInRequest == null ) {
251- throw new ServiceException ("Filename cannot be empty" );
252- } else if (!fileNameInDB .equals (
253- filenameInRequest )) { // If the file name in DB is not equal to the file name in
254- // request, it means that the file name has been modified
255- updatedSecondaryProperties .put ("filename" , filenameInRequest );
256- }
257- }
258- try {
259- int responseCode =
260- sdmService .updateAttachments (
261- sdmCredentials ,
262- cmisDocument ,
263- updatedSecondaryProperties ,
264- secondaryPropertiesWithInvalidDefinitions ,
265- context .getUserInfo ().isSystemUser ());
266- switch (responseCode ) {
267- case 403 :
268- // SDM Roles for user are missing
269- noSDMRoles .add (fileNameInSDM );
270- replacePropertiesInAttachment (
271- attachment , fileNameInSDM , propertiesInDB , secondaryTypeProperties );
272- break ;
273- case 409 :
274- duplicateFileNameList .add (filenameInRequest );
275- replacePropertiesInAttachment (
276- attachment , fileNameInSDM , propertiesInDB , secondaryTypeProperties );
277- break ;
278- case 404 :
279- filesNotFound .add (filenameInRequest );
280- replacePropertiesInAttachment (
281- attachment , filenameInRequest , propertiesInDB , secondaryTypeProperties );
282- break ;
283- case 200 :
284- case 201 :
285- // Success cases, do nothing
286- break ;
223+ }
224+ CmisDocument cmisDocument = new CmisDocument ();
225+ cmisDocument .setFileName (filenameInRequest );
226+ cmisDocument .setObjectId (objectId );
227+ if (fileNameInDB == null || !fileNameInDB .equals (filenameInRequest )) {
228+ updatedSecondaryProperties .put ("filename" , filenameInRequest );
229+ }
287230
288- default :
289- throw new ServiceException (SDMConstants .SDM_ROLES_ERROR_MESSAGE , null );
290- }
291- } catch (ServiceException e ) {
292- // This exception is thrown when there are unsupported properties in the request
293- if (e .getMessage ().startsWith (SDMConstants .UNSUPPORTED_PROPERTIES )) {
294- String unsupportedDetails =
295- e .getMessage ().substring (SDMConstants .UNSUPPORTED_PROPERTIES .length ()).trim ();
296- filesWithUnsupportedProperties .add (unsupportedDetails );
231+ try {
232+ int responseCode =
233+ sdmService .updateAttachments (
234+ sdmCredentials ,
235+ cmisDocument ,
236+ updatedSecondaryProperties ,
237+ secondaryPropertiesWithInvalidDefinitions ,
238+ context .getUserInfo ().isSystemUser ());
239+ switch (responseCode ) {
240+ case 403 :
241+ // SDM Roles for user are missing
242+ noSDMRoles .add (fileNameInSDM );
297243 replacePropertiesInAttachment (
298244 attachment , fileNameInSDM , propertiesInDB , secondaryTypeProperties );
299- } else {
300- badRequest .put (filenameInRequest , e .getMessage ());
245+ break ;
246+ case 404 :
247+ filesNotFound .add (filenameInRequest );
301248 replacePropertiesInAttachment (
302249 attachment , filenameInRequest , propertiesInDB , secondaryTypeProperties );
303- }
250+ break ;
251+ case 200 :
252+ case 201 :
253+ // Success cases, do nothing
254+ break ;
255+
256+ default :
257+ throw new ServiceException (SDMConstants .SDM_ROLES_ERROR_MESSAGE , null );
258+ }
259+ } catch (ServiceException e ) {
260+ // This exception is thrown when there are unsupported properties in the request
261+ if (e .getMessage ().startsWith (SDMConstants .UNSUPPORTED_PROPERTIES )) {
262+ String unsupportedDetails =
263+ e .getMessage ().substring (SDMConstants .UNSUPPORTED_PROPERTIES .length ()).trim ();
264+ filesWithUnsupportedProperties .add (unsupportedDetails );
265+ replacePropertiesInAttachment (
266+ attachment , fileNameInSDM , propertiesInDB , secondaryTypeProperties );
267+ } else {
268+ badRequest .put (filenameInRequest , e .getMessage ());
269+ replacePropertiesInAttachment (
270+ attachment , filenameInRequest , propertiesInDB , secondaryTypeProperties );
304271 }
305272 }
306273 }
@@ -343,15 +310,12 @@ private void handleWarnings(
343310 if (!fileNameWithRestrictedCharacters .isEmpty ()) {
344311 context
345312 .getMessages ()
346- .warn (SDMConstants .nameConstraintMessage (fileNameWithRestrictedCharacters , "Rename" ));
313+ .warn (SDMConstants .nameConstraintMessage (fileNameWithRestrictedCharacters ));
347314 }
348315 if (!duplicateFileNameList .isEmpty ()) {
349316 context
350317 .getMessages ()
351- .warn (
352- String .format (
353- SDMConstants .FILES_RENAME_WARNING_MESSAGE ,
354- String .join (", " , duplicateFileNameList )));
318+ .warn (String .format (SDMConstants .duplicateFilenameFormat (duplicateFileNameList )));
355319 }
356320 if (!filesNotFound .isEmpty ()) {
357321 context .getMessages ().warn (SDMConstants .fileNotFound (filesNotFound ));
0 commit comments