Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public SDMCreateAttachmentsHandler(
@Before
@HandlerOrder(HandlerOrder.EARLY)
public void processBefore(CdsCreateEventContext context, List<CdsData> data) throws IOException {
// Get comprehensive attachment composition details for each entity
logger.info("Target Entity : " + context.getTarget().getQualifiedName());
for (CdsData entityData : data) {
Map<String, Map<String, String>> attachmentCompositionDetails =
AttachmentsHandlerUtils.getAttachmentCompositionDetails(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ private static void processDirectAttachmentComposition(
: null;

if (isDirectAttachmentTargetAspect(targetAspect)) {
String serviceName = entity.getQualifiedName().split("\\.")[0];
String entityName = entity.getName();
String directPath = serviceName + "." + entityName + "." + compositionName;
String directPath = entity.getQualifiedName() + "." + compositionName;
pathMapping.put(directPath, directPath);
}
}
Expand Down Expand Up @@ -165,7 +163,9 @@ private static void processAttachmentPaths(
String entityPath = buildEntityPath(entity, targetEntity, attachmentPath);
String actualPath = buildActualPath(entity, compositionName, attachmentPath);

if (entityPath != null && actualPath != null) {
// Only add the mapping if both paths are non-null and the key doesn't already exist
// This preserves direct attachment mappings from being overwritten by nested ones
if (entityPath != null && actualPath != null && !pathMapping.containsKey(entityPath)) {
pathMapping.put(entityPath, actualPath);
}
}
Expand Down Expand Up @@ -219,17 +219,20 @@ private static String buildEntityPath(
try {
String[] pathParts = attachmentPath.split("\\.");
if (pathParts.length >= 3) {
// Get the service name (first part)
String serviceName = pathParts[0];

// Get the target entity name (without service prefix)
String targetEntityName = targetEntity.getName();

// Get the attachment part (last part)
String attachmentPart = pathParts[pathParts.length - 1];

// Build the entity path: ServiceName.EntityName.attachments
return serviceName + "." + targetEntityName + "." + attachmentPart;
// For nested compositions, use the full target entity path to ensure uniqueness
// For direct attachments on the parent entity, the targetEntity equals parentEntity
String entityPath;
if (targetEntity.getQualifiedName().equals(parentEntity.getQualifiedName())) {
// Direct attachment: use parent entity path
entityPath = parentEntity.getQualifiedName() + "." + attachmentPart;
} else {
// Nested attachment: use target entity path to ensure uniqueness
entityPath = targetEntity.getQualifiedName() + "." + attachmentPart;
}
return entityPath;
}
} catch (Exception e) {
logger.warn(SDMConstants.FETCH_ATTACHMENT_COMPOSITION_ERROR, e.getMessage());
Expand All @@ -242,15 +245,15 @@ private static String buildActualPath(
try {
String[] pathParts = attachmentPath.split("\\.");
if (pathParts.length >= 3) {
// Get the service name (first part)
String serviceName = pathParts[0];

// Replace the entity name with the composition property name
// Keep the attachment part (last part)
// Get the attachment part (last part)
String attachmentPart = pathParts[pathParts.length - 1];

// Build the new path: ServiceName.compositionPropertyName.attachments
return serviceName + "." + compositionPropertyName + "." + attachmentPart;
// Build the new path using parent entity qualified name + composition property name
return parentEntity.getQualifiedName()
+ "."
+ compositionPropertyName
+ "."
+ attachmentPart;
}
} catch (Exception e) {
logger.warn(SDMConstants.FETCH_ATTACHMENT_COMPOSITION_ERROR, e.getMessage());
Expand Down
Loading