@@ -211,138 +211,89 @@ public void processAttachment(
211211 List <String > noSDMRoles )
212212 throws IOException {
213213 String id = (String ) attachment .get ("ID" );
214+ String filenameInRequest = (String ) attachment .get ("fileName" );
215+ String descriptionInRequest = (String ) attachment .get ("note" );
216+ String objectId = (String ) attachment .get ("objectId" );
217+
214218 Map <String , String > secondaryTypeProperties =
215- SDMUtils .getSecondaryTypeProperties (
216- attachmentEntity ,
217- attachment ); // Fetching the secondary type properties from the attachment entity
218- String fileNameInDB ;
219- fileNameInDB = dbQuery .getAttachmentForID (attachmentEntity .get (), persistenceService , id );
220- if (fileNameInDB
221- == null ) { // On entity UPDATE, fetch original attachment name from SDM to revert property
222- // values if needed.
223- String objectId = (String ) attachment .get ("objectId" );
224- SDMCredentials sdmCredentials = tokenHandler .getSDMCredentials ();
225- fileNameInDB =
226- sdmService .getObject (objectId , sdmCredentials , context .getUserInfo ().isSystemUser ());
219+ SDMUtils .getSecondaryTypeProperties (attachmentEntity , attachment );
220+ String fileNameInDB =
221+ dbQuery .getAttachmentForID (attachmentEntity .get (), persistenceService , id );
222+ SDMCredentials sdmCredentials = tokenHandler .getSDMCredentials ();
223+
224+ // Fetch from SDM if not in DB
225+ String descriptionInDB = null ;
226+ if (fileNameInDB == null ) {
227+ List <String > sdmAttachmentData =
228+ AttachmentsHandlerUtils .fetchAttachmentDataFromSDM (
229+ sdmService , objectId , sdmCredentials , context .getUserInfo ().isSystemUser ());
230+ fileNameInDB = sdmAttachmentData .get (0 );
231+ descriptionInDB = sdmAttachmentData .get (1 );
227232 }
228- Map < String , String > propertiesInDB ;
229- propertiesInDB =
233+
234+ Map < String , String > propertiesInDB =
230235 dbQuery .getPropertiesForID (
231- attachmentEntity .get (),
232- persistenceService ,
233- id ,
234- secondaryTypeProperties ); // Fetching the values of the properties from the DB
236+ attachmentEntity .get (), persistenceService , id , secondaryTypeProperties );
235237
238+ // Extract note (description) from DB if it exists
239+ if (propertiesInDB != null && propertiesInDB .containsKey ("note" )) {
240+ descriptionInDB = propertiesInDB .get ("note" );
241+ }
242+
243+ // Prepare document and updated properties
236244 Map <String , String > updatedSecondaryProperties =
237245 SDMUtils .getUpdatedSecondaryProperties (
238246 attachmentEntity ,
239247 attachment ,
240248 persistenceService ,
241249 secondaryTypeProperties ,
242250 propertiesInDB );
243- String filenameInRequest = (String ) attachment .get ("fileName" );
251+ CmisDocument cmisDocument =
252+ AttachmentsHandlerUtils .prepareCmisDocument (
253+ filenameInRequest , descriptionInRequest , objectId );
244254
245- String objectId = (String ) attachment .get ("objectId" );
246- if (Boolean .TRUE .equals (
247- SDMUtils .hasRestrictedCharactersInName (
248- filenameInRequest ))) { // Check if the filename contains restricted characters and stop
249- // further processing if it does (Request not sent to SDM)
250- fileNameWithRestrictedCharacters .add (filenameInRequest );
251- replacePropertiesInAttachment (
252- attachment , fileNameInDB , propertiesInDB , secondaryTypeProperties );
253- return ;
254- }
255- CmisDocument cmisDocument = new CmisDocument ();
256- cmisDocument .setFileName (filenameInRequest );
257- cmisDocument .setObjectId (objectId );
258- if (fileNameInDB == null ) {
259- if (filenameInRequest != null ) {
260- updatedSecondaryProperties .put ("filename" , filenameInRequest );
261- } else {
262- throw new ServiceException ("Filename cannot be empty" );
263- }
264- } else {
265- if (filenameInRequest == null ) {
266- throw new ServiceException ("Filename cannot be empty" );
267- } else if (!fileNameInDB .equals (filenameInRequest )) {
268- updatedSecondaryProperties .put ("filename" , filenameInRequest );
269- }
270- }
271- if (!updatedSecondaryProperties .isEmpty ()) {
272- try {
273- int responseCode =
274- sdmService .updateAttachments (
275- tokenHandler .getSDMCredentials (),
276- cmisDocument ,
277- updatedSecondaryProperties ,
278- secondaryPropertiesWithInvalidDefinitions ,
279- context .getUserInfo ().isSystemUser ());
280- switch (responseCode ) {
281- case 403 :
282- // SDM Roles for user are missing
283- noSDMRoles .add (fileNameInDB );
284- replacePropertiesInAttachment (
285- attachment , fileNameInDB , propertiesInDB , secondaryTypeProperties );
286- break ;
287- case 409 :
288- duplicateFileNameList .add (filenameInRequest );
289- replacePropertiesInAttachment (
290- attachment , fileNameInDB , propertiesInDB , secondaryTypeProperties );
291- break ;
292- case 404 :
293- filesNotFound .add (fileNameInDB );
294- replacePropertiesInAttachment (
295- attachment , fileNameInDB , propertiesInDB , secondaryTypeProperties );
296- break ;
297- case 200 :
298- case 201 :
299- // Success cases, do nothing
300- break ;
255+ // Update filename and description properties
256+ AttachmentsHandlerUtils .updateFilenameProperty (
257+ fileNameInDB , filenameInRequest , updatedSecondaryProperties );
258+ AttachmentsHandlerUtils .updateDescriptionProperty (
259+ descriptionInDB , descriptionInRequest , updatedSecondaryProperties );
301260
302- default :
303- throw new ServiceException (SDMConstants .SDM_ROLES_ERROR_MESSAGE , (Object []) null );
304- }
305- } catch (ServiceException e ) {
306- // This exception is thrown when there are unsupported properties in the request
307- if (e .getMessage ().startsWith (SDMConstants .UNSUPPORTED_PROPERTIES )) {
308- String unsupportedDetails =
309- e .getMessage ().substring (SDMConstants .UNSUPPORTED_PROPERTIES .length ()).trim ();
310- filesWithUnsupportedProperties .add (unsupportedDetails );
311- replacePropertiesInAttachment (
312- attachment , fileNameInDB , propertiesInDB , secondaryTypeProperties );
313- } else {
314- badRequest .put (fileNameInDB , e .getMessage ());
315- replacePropertiesInAttachment (
316- attachment , fileNameInDB , propertiesInDB , secondaryTypeProperties );
317- }
318- }
261+ // Send update to SDM only if there are changes
262+ if (updatedSecondaryProperties .isEmpty ()) {
263+ return ;
319264 }
320- }
321-
322- private void replacePropertiesInAttachment (
323- Map <String , Object > attachment ,
324- String fileName ,
325- Map <String , String > propertiesInDB ,
326- Map <String , String > secondaryTypeProperties ) {
327- if (propertiesInDB != null ) {
328- for (Map .Entry <String , String > entry : propertiesInDB .entrySet ()) {
329- String dbKey = entry .getKey ();
330- String dbValue = entry .getValue ();
331-
332- // Find the key in secondaryTypeProperties where the value matches dbKey
333- String secondaryKey =
334- secondaryTypeProperties .entrySet ().stream ()
335- .filter (e -> e .getValue ().equals (dbKey ))
336- .map (Map .Entry ::getKey )
337- .findFirst ()
338- .orElse (null );
339265
340- if (secondaryKey != null ) {
341- attachment .replace (secondaryKey , dbValue );
342- }
343- }
266+ try {
267+ int responseCode =
268+ sdmService .updateAttachments (
269+ tokenHandler .getSDMCredentials (),
270+ cmisDocument ,
271+ updatedSecondaryProperties ,
272+ secondaryPropertiesWithInvalidDefinitions ,
273+ context .getUserInfo ().isSystemUser ());
274+ AttachmentsHandlerUtils .handleSDMUpdateResponse (
275+ responseCode ,
276+ attachment ,
277+ fileNameInDB ,
278+ filenameInRequest ,
279+ propertiesInDB ,
280+ secondaryTypeProperties ,
281+ descriptionInDB ,
282+ noSDMRoles ,
283+ duplicateFileNameList ,
284+ filesNotFound );
285+ } catch (ServiceException e ) {
286+ AttachmentsHandlerUtils .handleSDMServiceException (
287+ e ,
288+ attachment ,
289+ fileNameInDB ,
290+ filenameInRequest ,
291+ propertiesInDB ,
292+ secondaryTypeProperties ,
293+ descriptionInDB ,
294+ filesWithUnsupportedProperties ,
295+ badRequest );
344296 }
345- attachment .replace ("fileName" , fileName );
346297 }
347298
348299 private void handleWarnings (
0 commit comments