Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</developers>

<properties>
<revision>1.6.2-SNAPSHOT</revision>
<revision>1.0.0-RC1</revision>
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public void processBefore(CdsCreateEventContext context, List<CdsData> data) thr
context.getTarget().getQualifiedName(),
entityData);
logger.info("Attachment compositions present in CDS Model : " + attachmentCompositionDetails);

updateName(context, data, attachmentCompositionDetails);
}
}
Expand Down Expand Up @@ -202,55 +201,45 @@ private void processAttachment(
List<String> noSDMRoles)
throws IOException {
String id = (String) attachment.get("ID");
String fileNameInDB;
fileNameInDB =
dbQuery.getAttachmentForID(
attachmentEntity.get(),
persistenceService,
id); // Fetching the name of the file from DB
String filenameInRequest =
(String) attachment.get("fileName"); // Fetching the name of the file from request
String filenameInRequest = (String) attachment.get("fileName");
String descriptionInRequest = (String) attachment.get("note");
String objectId = (String) attachment.get("objectId");

// Fetch original data from DB and SDM
String fileNameInDB =
dbQuery.getAttachmentForID(attachmentEntity.get(), persistenceService, id);
SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials();
String fileNameInSDM =
sdmService.getObject(
objectId,
sdmCredentials,
context
.getUserInfo()
.isSystemUser()); // Fetch original filename from SDM since it's null in attachments
// table until save; needed to revert UI-modified names on error.
List<String> sdmAttachmentData =
AttachmentsHandlerUtils.fetchAttachmentDataFromSDM(
sdmService, objectId, sdmCredentials, context.getUserInfo().isSystemUser());
String fileNameInSDM = sdmAttachmentData.get(0);
String descriptionInSDM = sdmAttachmentData.get(1);

Map<String, String> secondaryTypeProperties =
SDMUtils.getSecondaryTypeProperties(
attachmentEntity,
attachment); // Fetching the secondary type properties from the attachment entity
Map<String, String> propertiesInDB;
propertiesInDB =
SDMUtils.getSecondaryTypeProperties(attachmentEntity, attachment);
Map<String, String> propertiesInDB =
dbQuery.getPropertiesForID(
attachmentEntity.get(),
persistenceService,
id,
secondaryTypeProperties); // Fetching the values of the properties from the DB
attachmentEntity.get(), persistenceService, id, secondaryTypeProperties);

// Get the updated secondary properties
// Prepare document and updated properties
Map<String, String> updatedSecondaryProperties =
SDMUtils.getUpdatedSecondaryProperties(
attachmentEntity,
attachment,
persistenceService,
secondaryTypeProperties,
propertiesInDB);
if (SDMUtils.hasRestrictedCharactersInName(filenameInRequest)) {
fileNameWithRestrictedCharacters.add(filenameInRequest);
}
CmisDocument cmisDocument = new CmisDocument();
cmisDocument.setFileName(filenameInRequest);
cmisDocument.setObjectId(objectId);
if (fileNameInDB == null || !fileNameInDB.equals(filenameInRequest)) {
updatedSecondaryProperties.put("filename", filenameInRequest);
}
CmisDocument cmisDocument =
AttachmentsHandlerUtils.prepareCmisDocument(
filenameInRequest, descriptionInRequest, objectId);

// Update filename and description properties
AttachmentsHandlerUtils.updateFilenameProperty(
fileNameInDB, filenameInRequest, updatedSecondaryProperties);
AttachmentsHandlerUtils.updateDescriptionProperty(
descriptionInSDM, descriptionInRequest, updatedSecondaryProperties);

// Send update to SDM and handle response
try {
int responseCode =
sdmService.updateAttachments(
Expand All @@ -259,66 +248,29 @@ private void processAttachment(
updatedSecondaryProperties,
secondaryPropertiesWithInvalidDefinitions,
context.getUserInfo().isSystemUser());
switch (responseCode) {
case 403:
// SDM Roles for user are missing
noSDMRoles.add(fileNameInSDM);
replacePropertiesInAttachment(
attachment, fileNameInSDM, propertiesInDB, secondaryTypeProperties);
break;
case 404:
filesNotFound.add(filenameInRequest);
replacePropertiesInAttachment(
attachment, filenameInRequest, propertiesInDB, secondaryTypeProperties);
break;
case 200:
case 201:
// Success cases, do nothing
break;

default:
throw new ServiceException(SDMConstants.SDM_ROLES_ERROR_MESSAGE, null);
}
AttachmentsHandlerUtils.handleSDMUpdateResponse(
responseCode,
attachment,
fileNameInSDM,
filenameInRequest,
propertiesInDB,
secondaryTypeProperties,
descriptionInSDM,
noSDMRoles,
duplicateFileNameList,
filesNotFound);
} catch (ServiceException e) {
// This exception is thrown when there are unsupported properties in the request
if (e.getMessage().startsWith(SDMConstants.UNSUPPORTED_PROPERTIES)) {
String unsupportedDetails =
e.getMessage().substring(SDMConstants.UNSUPPORTED_PROPERTIES.length()).trim();
filesWithUnsupportedProperties.add(unsupportedDetails);
replacePropertiesInAttachment(
attachment, fileNameInSDM, propertiesInDB, secondaryTypeProperties);
} else {
badRequest.put(filenameInRequest, e.getMessage());
replacePropertiesInAttachment(
attachment, filenameInRequest, propertiesInDB, secondaryTypeProperties);
}
}
}

private void replacePropertiesInAttachment(
Map<String, Object> attachment,
String fileName,
Map<String, String> propertiesInDB,
Map<String, String> secondaryTypeProperties) {
if (propertiesInDB != null) {
for (Map.Entry<String, String> entry : propertiesInDB.entrySet()) {
String dbKey = entry.getKey();
String dbValue = entry.getValue();

// Find the key in secondaryTypeProperties where the value matches dbKey
String secondaryKey =
secondaryTypeProperties.entrySet().stream()
.filter(e -> e.getValue().equals(dbKey))
.map(Map.Entry::getKey)
.findFirst()
.orElse(null);

if (secondaryKey != null) {
attachment.replace(secondaryKey, dbValue);
}
}
AttachmentsHandlerUtils.handleSDMServiceException(
e,
attachment,
fileNameInSDM,
filenameInRequest,
propertiesInDB,
secondaryTypeProperties,
descriptionInSDM,
filesWithUnsupportedProperties,
badRequest);
}
attachment.replace("fileName", fileName);
}

private void handleWarnings(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,138 +211,89 @@ public void processAttachment(
List<String> noSDMRoles)
throws IOException {
String id = (String) attachment.get("ID");
String filenameInRequest = (String) attachment.get("fileName");
String descriptionInRequest = (String) attachment.get("note");
String objectId = (String) attachment.get("objectId");

Map<String, String> secondaryTypeProperties =
SDMUtils.getSecondaryTypeProperties(
attachmentEntity,
attachment); // Fetching the secondary type properties from the attachment entity
String fileNameInDB;
fileNameInDB = dbQuery.getAttachmentForID(attachmentEntity.get(), persistenceService, id);
if (fileNameInDB
== null) { // On entity UPDATE, fetch original attachment name from SDM to revert property
// values if needed.
String objectId = (String) attachment.get("objectId");
SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials();
fileNameInDB =
sdmService.getObject(objectId, sdmCredentials, context.getUserInfo().isSystemUser());
SDMUtils.getSecondaryTypeProperties(attachmentEntity, attachment);
String fileNameInDB =
dbQuery.getAttachmentForID(attachmentEntity.get(), persistenceService, id);
SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials();

// Fetch from SDM if not in DB
String descriptionInDB = null;
if (fileNameInDB == null) {
List<String> sdmAttachmentData =
AttachmentsHandlerUtils.fetchAttachmentDataFromSDM(
sdmService, objectId, sdmCredentials, context.getUserInfo().isSystemUser());
fileNameInDB = sdmAttachmentData.get(0);
descriptionInDB = sdmAttachmentData.get(1);
}
Map<String, String> propertiesInDB;
propertiesInDB =

Map<String, String> propertiesInDB =
dbQuery.getPropertiesForID(
attachmentEntity.get(),
persistenceService,
id,
secondaryTypeProperties); // Fetching the values of the properties from the DB
attachmentEntity.get(), persistenceService, id, secondaryTypeProperties);

// Extract note (description) from DB if it exists
if (propertiesInDB != null && propertiesInDB.containsKey("note")) {
descriptionInDB = propertiesInDB.get("note");
}

// Prepare document and updated properties
Map<String, String> updatedSecondaryProperties =
SDMUtils.getUpdatedSecondaryProperties(
attachmentEntity,
attachment,
persistenceService,
secondaryTypeProperties,
propertiesInDB);
String filenameInRequest = (String) attachment.get("fileName");
CmisDocument cmisDocument =
AttachmentsHandlerUtils.prepareCmisDocument(
filenameInRequest, descriptionInRequest, objectId);

String objectId = (String) attachment.get("objectId");
if (Boolean.TRUE.equals(
SDMUtils.hasRestrictedCharactersInName(
filenameInRequest))) { // Check if the filename contains restricted characters and stop
// further processing if it does (Request not sent to SDM)
fileNameWithRestrictedCharacters.add(filenameInRequest);
replacePropertiesInAttachment(
attachment, fileNameInDB, propertiesInDB, secondaryTypeProperties);
return;
}
CmisDocument cmisDocument = new CmisDocument();
cmisDocument.setFileName(filenameInRequest);
cmisDocument.setObjectId(objectId);
if (fileNameInDB == null) {
if (filenameInRequest != null) {
updatedSecondaryProperties.put("filename", filenameInRequest);
} else {
throw new ServiceException("Filename cannot be empty");
}
} else {
if (filenameInRequest == null) {
throw new ServiceException("Filename cannot be empty");
} else if (!fileNameInDB.equals(filenameInRequest)) {
updatedSecondaryProperties.put("filename", filenameInRequest);
}
}
if (!updatedSecondaryProperties.isEmpty()) {
try {
int responseCode =
sdmService.updateAttachments(
tokenHandler.getSDMCredentials(),
cmisDocument,
updatedSecondaryProperties,
secondaryPropertiesWithInvalidDefinitions,
context.getUserInfo().isSystemUser());
switch (responseCode) {
case 403:
// SDM Roles for user are missing
noSDMRoles.add(fileNameInDB);
replacePropertiesInAttachment(
attachment, fileNameInDB, propertiesInDB, secondaryTypeProperties);
break;
case 409:
duplicateFileNameList.add(filenameInRequest);
replacePropertiesInAttachment(
attachment, fileNameInDB, propertiesInDB, secondaryTypeProperties);
break;
case 404:
filesNotFound.add(fileNameInDB);
replacePropertiesInAttachment(
attachment, fileNameInDB, propertiesInDB, secondaryTypeProperties);
break;
case 200:
case 201:
// Success cases, do nothing
break;
// Update filename and description properties
AttachmentsHandlerUtils.updateFilenameProperty(
fileNameInDB, filenameInRequest, updatedSecondaryProperties);
AttachmentsHandlerUtils.updateDescriptionProperty(
descriptionInDB, descriptionInRequest, updatedSecondaryProperties);

default:
throw new ServiceException(SDMConstants.SDM_ROLES_ERROR_MESSAGE, (Object[]) null);
}
} catch (ServiceException e) {
// This exception is thrown when there are unsupported properties in the request
if (e.getMessage().startsWith(SDMConstants.UNSUPPORTED_PROPERTIES)) {
String unsupportedDetails =
e.getMessage().substring(SDMConstants.UNSUPPORTED_PROPERTIES.length()).trim();
filesWithUnsupportedProperties.add(unsupportedDetails);
replacePropertiesInAttachment(
attachment, fileNameInDB, propertiesInDB, secondaryTypeProperties);
} else {
badRequest.put(fileNameInDB, e.getMessage());
replacePropertiesInAttachment(
attachment, fileNameInDB, propertiesInDB, secondaryTypeProperties);
}
}
// Send update to SDM only if there are changes
if (updatedSecondaryProperties.isEmpty()) {
return;
}
}

private void replacePropertiesInAttachment(
Map<String, Object> attachment,
String fileName,
Map<String, String> propertiesInDB,
Map<String, String> secondaryTypeProperties) {
if (propertiesInDB != null) {
for (Map.Entry<String, String> entry : propertiesInDB.entrySet()) {
String dbKey = entry.getKey();
String dbValue = entry.getValue();

// Find the key in secondaryTypeProperties where the value matches dbKey
String secondaryKey =
secondaryTypeProperties.entrySet().stream()
.filter(e -> e.getValue().equals(dbKey))
.map(Map.Entry::getKey)
.findFirst()
.orElse(null);

if (secondaryKey != null) {
attachment.replace(secondaryKey, dbValue);
}
}
try {
int responseCode =
sdmService.updateAttachments(
tokenHandler.getSDMCredentials(),
cmisDocument,
updatedSecondaryProperties,
secondaryPropertiesWithInvalidDefinitions,
context.getUserInfo().isSystemUser());
AttachmentsHandlerUtils.handleSDMUpdateResponse(
responseCode,
attachment,
fileNameInDB,
filenameInRequest,
propertiesInDB,
secondaryTypeProperties,
descriptionInDB,
noSDMRoles,
duplicateFileNameList,
filesNotFound);
} catch (ServiceException e) {
AttachmentsHandlerUtils.handleSDMServiceException(
e,
attachment,
fileNameInDB,
filenameInRequest,
propertiesInDB,
secondaryTypeProperties,
descriptionInDB,
filesWithUnsupportedProperties,
badRequest);
}
attachment.replace("fileName", fileName);
}

private void handleWarnings(
Expand Down
Loading
Loading