Skip to content
Closed
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 @@ -194,7 +194,7 @@ private static void processAttachmentPaths(
List<String> attachmentPaths) {
for (String attachmentPath : attachmentPaths) {
String entityPath = buildEntityPath(entity, targetEntity, attachmentPath);
String actualPath = buildActualPath(entity, compositionName, attachmentPath);
String actualPath = buildActualPath(entity, compositionName, targetEntity, attachmentPath);

// 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
Expand Down Expand Up @@ -272,8 +272,9 @@ private static String buildEntityPath(
// Direct attachment: use parent entity path
entityPath = parentEntity.getQualifiedName() + "." + attachmentPart;
} else {
// Nested attachment: use target entity path to ensure uniqueness
entityPath = targetEntity.getQualifiedName() + "." + attachmentPart;
// Nested attachment: use attachmentPath as-is — it already encodes the owning entity
// (e.g. "AdminService.Sections.attachments"), ensuring uniqueness at any depth.
entityPath = attachmentPath;
}
return entityPath;
}
Expand All @@ -284,26 +285,62 @@ private static String buildEntityPath(
}

private static String buildActualPath(
CdsEntity parentEntity, String compositionPropertyName, String attachmentPath) {
CdsEntity parentEntity,
String compositionPropertyName,
CdsEntity targetEntity,
String attachmentPath) {
try {
String[] pathParts = attachmentPath.split("\\.");
if (pathParts.length >= 3) {
// Get the attachment part (last part)
String attachmentPart = pathParts[pathParts.length - 1];

// Build the new path using parent entity qualified name + composition property name
return parentEntity.getQualifiedName()
+ "."
+ compositionPropertyName
+ "."
+ attachmentPart;
String ownerEntityQN = attachmentPath.substring(0, attachmentPath.lastIndexOf('.'));

if (ownerEntityQN.equals(targetEntity.getQualifiedName())) {
return parentEntity.getQualifiedName()
+ "."
+ compositionPropertyName
+ "."
+ attachmentPart;
} else {
String intermediatePath = findPathToEntity(targetEntity, ownerEntityQN, new HashSet<>());
if (intermediatePath != null) {
return parentEntity.getQualifiedName()
+ "."
+ compositionPropertyName
+ "."
+ intermediatePath
+ "."
+ attachmentPart;
}
}
}
} catch (Exception e) {
logger.warn(SDMUtils.getErrorMessage("FETCH_ATTACHMENT_COMPOSITION_ERROR"), e.getMessage());
}
return null;
}

private static String findPathToEntity(
CdsEntity fromEntity, String toEntityQN, Set<String> visited) {
if (visited.contains(fromEntity.getQualifiedName())) return null;
visited.add(fromEntity.getQualifiedName());
List<?> comps = fromEntity.compositions().collect(java.util.stream.Collectors.toList());
for (Object comp : comps) {
com.sap.cds.reflect.CdsElement element = (com.sap.cds.reflect.CdsElement) comp;
if (!element.getType().isAssociation()) continue;
CdsAssociationType assocType = (CdsAssociationType) element.getType();
CdsEntity target = assocType.getTarget();
if (toEntityQN.equals(target.getQualifiedName())) {
return element.getName();
}
String subPath = findPathToEntity(target, toEntityQN, visited);
if (subPath != null) {
return element.getName() + "." + subPath;
}
}
return null;
}

private static List<Map<String, Object>> findNestedAttachments(
Map<String, Object> entity, String attachmentKey, String parentKey, String currentParentKey) {
List<Map<String, Object>> result = new ArrayList<>();
Expand Down
Loading