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
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ public SDMCreateAttachmentsHandler(
@Before
@HandlerOrder(HandlerOrder.DEFAULT)
public void processBefore(CdsCreateEventContext context, List<CdsData> data) throws IOException {
logger.info("Target Entity : " + context.getTarget().getQualifiedName());
logger.debug("CDS Data attachments : " + data);
logger.info(
"START: Process attachments before persistence for entity: {}",
context.getTarget().getQualifiedName());
logger.debug("Number of entities to process: {}", data.size());

for (CdsData entityData : data) {
Map<String, Map<String, String>> attachmentCompositionDetails =
Expand All @@ -70,21 +72,23 @@ public void processBefore(CdsCreateEventContext context, List<CdsData> data) thr
persistenceService,
context.getTarget().getQualifiedName(),
entityData);
logger.info("Attachment compositions present in CDS Model : " + attachmentCompositionDetails);
logger.debug("Attachment compositions present: {}", attachmentCompositionDetails.keySet());
updateName(context, data, attachmentCompositionDetails);
// Remove uploadStatus from attachment data to prevent validation errors
cleanupReadonlyContextsForAttachments(context, entityData, attachmentCompositionDetails);
}
logger.info("END: Process attachments before persistence");
}

@After
@HandlerOrder(HandlerOrder.LATE)
public void processAfter(CdsCreateEventContext context, List<CdsData> data) {
// Update uploadStatus to Success after entity is persisted
logger.info(
"Post-processing attachments after persistence for entity: {}",
"START: Post-processing attachments after persistence for entity: {}",
context.getTarget().getQualifiedName());

int totalProcessed = 0;
for (CdsData entityData : data) {
Map<String, Map<String, String>> attachmentCompositionDetails =
AttachmentsHandlerUtils.getAttachmentCompositionDetails(
Expand All @@ -107,23 +111,29 @@ public void processAfter(CdsCreateEventContext context, List<CdsData> data) {
targetEntity, entityData, attachmentCompositionName);

if (attachments != null) {
logger.debug(
"Processing {} attachments for composition: {}",
attachments.size(),
attachmentCompositionName);
for (Map<String, Object> attachment : attachments) {
String id = (String) attachment.get("ID");
String uploadStatus = (String) attachment.get("uploadStatus");
if (id != null) {
CmisDocument cmisDocument = new CmisDocument();
cmisDocument.setAttachmentId(id);
cmisDocument.setUploadStatus(uploadStatus);
logger.debug("Saving uploadStatus: {} for attachment ID: {}", uploadStatus, id);
// Update uploadStatus to Success in database if it was InProgress
dbQuery.saveUploadStatusToAttachment(
attachmentEntity.get(), persistenceService, cmisDocument);
logger.debug("Updated uploadStatus to Success for attachment ID: {}", id);
totalProcessed++;
}
}
}
}
}
}
logger.info("END: Post-processing completed. Processed {} attachments", totalProcessed);
}

@Before
Expand Down Expand Up @@ -277,10 +287,16 @@ private void processAttachment(
List<String> scanFailedFiles,
List<String> uploadInProgressFiles)
throws IOException {
long startTime = System.currentTimeMillis();
String id = (String) attachment.get("ID");
String filenameInRequest = (String) attachment.get("fileName");
String descriptionInRequest = (String) attachment.get("note");
String objectId = (String) attachment.get("objectId");
logger.debug(
"START: Process attachment - ID: {}, fileName: {}, objectId: {}",
id,
filenameInRequest,
objectId);

// Fetch original data from DB
CmisDocument cmisDocument =
Expand All @@ -290,6 +306,7 @@ private void processAttachment(
// Check upload status and collect problematic files
if (checkUploadStatus(
attachment, fileNameInDB, filenameInRequest, scanFailedFiles, uploadInProgressFiles)) {
logger.debug("Upload status check failed, skipping further processing for ID: {}", id);
return; // Skip further processing if upload status is problematic
}

Expand All @@ -316,6 +333,10 @@ private void processAttachment(
filesNotFound,
filesWithUnsupportedProperties,
badRequest);
logger.debug(
"END: Process attachment - ID: {} completed in {} ms",
id,
(System.currentTimeMillis() - startTime));
}

private boolean checkUploadStatus(
Expand All @@ -333,10 +354,12 @@ private boolean checkUploadStatus(
String fileName = fileNameInDB != null ? fileNameInDB : filenameInRequest;

if (uploadStatus.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_IN_PROGRESS)) {
logger.warn("Upload in progress for file: {}", fileName);
uploadInProgressFiles.add(fileName);
return true;
}
if (uploadStatus.equalsIgnoreCase(SDMConstants.UPLOAD_STATUS_SCAN_FAILED)) {
logger.warn("Scan failed for file: {}", fileName);
scanFailedFiles.add(fileName);
return true;
}
Expand All @@ -348,6 +371,7 @@ private boolean checkUploadStatus(
private SDMAttachmentData fetchSDMData(
CdsCreateEventContext context, String objectId, SDMCredentials sdmCredentials)
throws IOException {
logger.debug("Fetching attachment data from SDM for objectId: {}", objectId);
JSONObject sdmAttachmentData =
AttachmentsHandlerUtils.fetchAttachmentDataFromSDM(
sdmService, objectId, sdmCredentials, context.getUserInfo().isSystemUser());
Expand All @@ -362,6 +386,10 @@ private SDMAttachmentData fetchSDMData(
if (succinctProperties.has("cmis:description")) {
descriptionInSDM = succinctProperties.getString("cmis:description");
}
logger.debug(
"Retrieved from SDM - fileName: {}, hasDescription: {}",
fileNameInSDM,
descriptionInSDM != null);

return new SDMAttachmentData(fileNameInSDM, descriptionInSDM);
}
Expand Down Expand Up @@ -414,8 +442,9 @@ private void updateAndSendToSDM(
false);

logger.debug(
"Creating attachment in SDM - ID: {}, properties count: {}",
"Creating attachment in SDM - ID: {}, fileName: {}, properties count: {}",
id,
filenameInRequest,
updatedSecondaryProperties.size());

try {
Expand All @@ -427,7 +456,7 @@ private void updateAndSendToSDM(
secondaryPropertiesWithInvalidDefinitions,
context.getUserInfo().isSystemUser());

logger.debug("SDM create response code: {} for attachment ID: {}", responseCode, id);
logger.info("SDM update response code: {} for attachment ID: {}", responseCode, id);
AttachmentsHandlerUtils.handleSDMUpdateResponse(
responseCode,
attachment,
Expand All @@ -440,6 +469,7 @@ private void updateAndSendToSDM(
duplicateFileNameList,
filesNotFound);
} catch (ServiceException e) {
logger.error("Error updating attachment {} in SDM: {}", id, e.getMessage(), e);
AttachmentsHandlerUtils.handleSDMServiceException(
e,
attachment,
Expand All @@ -464,20 +494,24 @@ private void handleWarnings(
List<String> noSDMRoles,
String contextInfo) {
if (!fileNameWithRestrictedCharacters.isEmpty()) {
logger.warn(
"Files with restricted characters in filename: {}", fileNameWithRestrictedCharacters);
context
.getMessages()
.warn(
SDMErrorMessages.nameConstraintMessage(fileNameWithRestrictedCharacters)
+ contextInfo);
}
if (!duplicateFileNameList.isEmpty()) {
logger.warn("Duplicate filenames detected: {}", duplicateFileNameList);
context
.getMessages()
.warn(
String.format(SDMErrorMessages.duplicateFilenameFormat(duplicateFileNameList))
+ contextInfo);
}
if (!filesNotFound.isEmpty()) {
logger.warn("Files not found in SDM: {}", filesNotFound);
context.getMessages().warn(SDMErrorMessages.fileNotFound(filesNotFound) + contextInfo);
}
if (!filesWithUnsupportedProperties.isEmpty()) {
Expand All @@ -494,6 +528,7 @@ private void handleWarnings(
invalidPropertyNames.add(propertyTitles.get(file));
}
if (!invalidPropertyNames.isEmpty()) {
logger.warn("Files with unsupported properties: {}", invalidPropertyNames);
context
.getMessages()
.warn(
Expand All @@ -502,9 +537,11 @@ private void handleWarnings(
}

if (!badRequest.isEmpty()) {
logger.warn("Bad request errors: {}", badRequest.keySet());
context.getMessages().warn(SDMErrorMessages.badRequestMessage(badRequest) + contextInfo);
}
if (!noSDMRoles.isEmpty()) {
logger.warn("No SDM roles for files: {}", noSDMRoles);
context
.getMessages()
.warn(
Expand Down Expand Up @@ -545,7 +582,7 @@ private void cleanupReadonlyContextsForAttachments(
for (Map.Entry<String, Map<String, String>> entry : attachmentCompositionDetails.entrySet()) {
String attachmentCompositionName = entry.getValue().get("name");

logger.info(
logger.debug(
"Cleaning up SDM_READONLY_CONTEXT for composition: {}", attachmentCompositionName);

// Fetch attachments for this specific composition
Expand All @@ -554,23 +591,23 @@ private void cleanupReadonlyContextsForAttachments(
targetEntity, entityData, attachmentCompositionName);

if (attachments != null && !attachments.isEmpty()) {
logger.info(
logger.debug(
"Found {} attachments in composition: {}",
attachments.size(),
attachmentCompositionName);

for (int i = 0; i < attachments.size(); i++) {
Map<String, Object> attachment = attachments.get(i);
if (attachment.containsKey(SDM_READONLY_CONTEXT)) {
logger.info(
" Removing SDM_READONLY_CONTEXT from attachment [{}] in {}",
logger.debug(
"Removing SDM_READONLY_CONTEXT from attachment [{}] in {}",
i,
attachmentCompositionName);
attachment.remove(SDM_READONLY_CONTEXT);
}
}
} else {
logger.info("No attachments found for composition: {}", attachmentCompositionName);
logger.debug("No attachments found for composition: {}", attachmentCompositionName);
}
}
}
Expand Down
Loading
Loading