Skip to content

Commit ceed361

Browse files
Merge branch 'develop' into discardDraftIdBug
2 parents 634d8bd + 8e2b927 commit ceed361

19 files changed

Lines changed: 1281 additions & 924 deletions

File tree

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

Lines changed: 46 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public void processBefore(CdsCreateEventContext context, List<CdsData> data) thr
6363
context.getTarget().getQualifiedName(),
6464
entityData);
6565
logger.info("Attachment compositions present in CDS Model : " + attachmentCompositionDetails);
66-
6766
updateName(context, data, attachmentCompositionDetails);
6867
}
6968
}
@@ -202,55 +201,45 @@ private void processAttachment(
202201
List<String> noSDMRoles)
203202
throws IOException {
204203
String id = (String) attachment.get("ID");
205-
String fileNameInDB;
206-
fileNameInDB =
207-
dbQuery.getAttachmentForID(
208-
attachmentEntity.get(),
209-
persistenceService,
210-
id); // Fetching the name of the file from DB
211-
String filenameInRequest =
212-
(String) attachment.get("fileName"); // Fetching the name of the file from request
204+
String filenameInRequest = (String) attachment.get("fileName");
205+
String descriptionInRequest = (String) attachment.get("note");
213206
String objectId = (String) attachment.get("objectId");
207+
208+
// Fetch original data from DB and SDM
209+
String fileNameInDB =
210+
dbQuery.getAttachmentForID(attachmentEntity.get(), persistenceService, id);
214211
SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials();
215-
String fileNameInSDM =
216-
sdmService.getObject(
217-
objectId,
218-
sdmCredentials,
219-
context
220-
.getUserInfo()
221-
.isSystemUser()); // Fetch original filename from SDM since it's null in attachments
222-
// table until save; needed to revert UI-modified names on error.
212+
List<String> sdmAttachmentData =
213+
AttachmentsHandlerUtils.fetchAttachmentDataFromSDM(
214+
sdmService, objectId, sdmCredentials, context.getUserInfo().isSystemUser());
215+
String fileNameInSDM = sdmAttachmentData.get(0);
216+
String descriptionInSDM = sdmAttachmentData.get(1);
223217

224218
Map<String, String> secondaryTypeProperties =
225-
SDMUtils.getSecondaryTypeProperties(
226-
attachmentEntity,
227-
attachment); // Fetching the secondary type properties from the attachment entity
228-
Map<String, String> propertiesInDB;
229-
propertiesInDB =
219+
SDMUtils.getSecondaryTypeProperties(attachmentEntity, attachment);
220+
Map<String, String> propertiesInDB =
230221
dbQuery.getPropertiesForID(
231-
attachmentEntity.get(),
232-
persistenceService,
233-
id,
234-
secondaryTypeProperties); // Fetching the values of the properties from the DB
222+
attachmentEntity.get(), persistenceService, id, secondaryTypeProperties);
235223

236-
// Get the updated secondary properties
224+
// Prepare document and updated properties
237225
Map<String, String> updatedSecondaryProperties =
238226
SDMUtils.getUpdatedSecondaryProperties(
239227
attachmentEntity,
240228
attachment,
241229
persistenceService,
242230
secondaryTypeProperties,
243231
propertiesInDB);
244-
if (SDMUtils.hasRestrictedCharactersInName(filenameInRequest)) {
245-
fileNameWithRestrictedCharacters.add(filenameInRequest);
246-
}
247-
CmisDocument cmisDocument = new CmisDocument();
248-
cmisDocument.setFileName(filenameInRequest);
249-
cmisDocument.setObjectId(objectId);
250-
if (fileNameInDB == null || !fileNameInDB.equals(filenameInRequest)) {
251-
updatedSecondaryProperties.put("filename", filenameInRequest);
252-
}
232+
CmisDocument cmisDocument =
233+
AttachmentsHandlerUtils.prepareCmisDocument(
234+
filenameInRequest, descriptionInRequest, objectId);
235+
236+
// Update filename and description properties
237+
AttachmentsHandlerUtils.updateFilenameProperty(
238+
fileNameInDB, filenameInRequest, updatedSecondaryProperties);
239+
AttachmentsHandlerUtils.updateDescriptionProperty(
240+
descriptionInSDM, descriptionInRequest, updatedSecondaryProperties);
253241

242+
// Send update to SDM and handle response
254243
try {
255244
int responseCode =
256245
sdmService.updateAttachments(
@@ -259,66 +248,29 @@ private void processAttachment(
259248
updatedSecondaryProperties,
260249
secondaryPropertiesWithInvalidDefinitions,
261250
context.getUserInfo().isSystemUser());
262-
switch (responseCode) {
263-
case 403:
264-
// SDM Roles for user are missing
265-
noSDMRoles.add(fileNameInSDM);
266-
replacePropertiesInAttachment(
267-
attachment, fileNameInSDM, propertiesInDB, secondaryTypeProperties);
268-
break;
269-
case 404:
270-
filesNotFound.add(filenameInRequest);
271-
replacePropertiesInAttachment(
272-
attachment, filenameInRequest, propertiesInDB, secondaryTypeProperties);
273-
break;
274-
case 200:
275-
case 201:
276-
// Success cases, do nothing
277-
break;
278-
279-
default:
280-
throw new ServiceException(SDMConstants.SDM_ROLES_ERROR_MESSAGE, null);
281-
}
251+
AttachmentsHandlerUtils.handleSDMUpdateResponse(
252+
responseCode,
253+
attachment,
254+
fileNameInSDM,
255+
filenameInRequest,
256+
propertiesInDB,
257+
secondaryTypeProperties,
258+
descriptionInSDM,
259+
noSDMRoles,
260+
duplicateFileNameList,
261+
filesNotFound);
282262
} catch (ServiceException e) {
283-
// This exception is thrown when there are unsupported properties in the request
284-
if (e.getMessage().startsWith(SDMConstants.UNSUPPORTED_PROPERTIES)) {
285-
String unsupportedDetails =
286-
e.getMessage().substring(SDMConstants.UNSUPPORTED_PROPERTIES.length()).trim();
287-
filesWithUnsupportedProperties.add(unsupportedDetails);
288-
replacePropertiesInAttachment(
289-
attachment, fileNameInSDM, propertiesInDB, secondaryTypeProperties);
290-
} else {
291-
badRequest.put(filenameInRequest, e.getMessage());
292-
replacePropertiesInAttachment(
293-
attachment, filenameInRequest, propertiesInDB, secondaryTypeProperties);
294-
}
295-
}
296-
}
297-
298-
private void replacePropertiesInAttachment(
299-
Map<String, Object> attachment,
300-
String fileName,
301-
Map<String, String> propertiesInDB,
302-
Map<String, String> secondaryTypeProperties) {
303-
if (propertiesInDB != null) {
304-
for (Map.Entry<String, String> entry : propertiesInDB.entrySet()) {
305-
String dbKey = entry.getKey();
306-
String dbValue = entry.getValue();
307-
308-
// Find the key in secondaryTypeProperties where the value matches dbKey
309-
String secondaryKey =
310-
secondaryTypeProperties.entrySet().stream()
311-
.filter(e -> e.getValue().equals(dbKey))
312-
.map(Map.Entry::getKey)
313-
.findFirst()
314-
.orElse(null);
315-
316-
if (secondaryKey != null) {
317-
attachment.replace(secondaryKey, dbValue);
318-
}
319-
}
263+
AttachmentsHandlerUtils.handleSDMServiceException(
264+
e,
265+
attachment,
266+
fileNameInSDM,
267+
filenameInRequest,
268+
propertiesInDB,
269+
secondaryTypeProperties,
270+
descriptionInSDM,
271+
filesWithUnsupportedProperties,
272+
badRequest);
320273
}
321-
attachment.replace("fileName", fileName);
322274
}
323275

324276
private void handleWarnings(

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

Lines changed: 67 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)