Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -193,7 +193,8 @@ public void createSubmodelElement(String submodelId, String idShortPath, Submode

@Override
public void updateSubmodelElement(String submodelId, String idShortPath, SubmodelElement submodelElement) throws ElementDoesNotExistException {
MongoFilterResult filterResult = MongoFilterBuilder.parse(idShortPath);
List<SubmodelElement> parentElements = getParentElements(submodelId, idShortPath);
MongoFilterResult filterResult = MongoFilterBuilder.parse(idShortPath, parentElements);

Query query = new Query(Criteria.where("_id").is(submodelId));
Update update = new Update().set(filterResult.key(), submodelElement);
Expand Down Expand Up @@ -326,6 +327,36 @@ private boolean existsSubmodelElement(String submodelId, String idShortPath){
}
}

/**
* Gets the list of parent SubmodelElements along the path.
* This is used to determine if any parent is an Entity (which uses 'statements' instead of 'value').
*/
private List<SubmodelElement> getParentElements(String submodelId, String idShortPath) {
List<SubmodelElement> parents = new ArrayList<>();
String[] segments = idShortPath.split("\\.");

if (segments.length <= 1) {
return parents;
}

StringBuilder currentPath = new StringBuilder();
for (int i = 0; i < segments.length - 1; i++) {
if (currentPath.length() > 0) {
currentPath.append(".");
}
currentPath.append(segments[i]);
try {
SubmodelElement element = getSubmodelElement(submodelId, currentPath.toString());
parents.add(element);
} catch (ElementDoesNotExistException e) {
// Element doesn't exist, stop here
break;
}
}

return parents;
}

private static boolean hasLimit(PaginationInfo pInfo) {
return pInfo.getLimit() != null && pInfo.getLimit() > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.Deque;
import java.util.List;

import org.eclipse.digitaltwin.aas4j.v3.model.Entity;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
import org.eclipse.digitaltwin.basyx.submodelservice.backend.IdShortPathParser.GenericPath;
import org.eclipse.digitaltwin.basyx.submodelservice.backend.IdShortPathParser.IdShortPath;
import org.eclipse.digitaltwin.basyx.submodelservice.backend.IdShortPathParser.IndexPath;
Expand All @@ -54,13 +56,25 @@ public final class MongoFilterBuilder{
static final String KEY_ID_SHORT = "idShort";

public static MongoFilterResult parse(@NonNull String idShortPath) {
return parse(idShortPath, List.of());
}

/**
* Parses the idShortPath and builds MongoDB filter result.
*
* @param idShortPath the path to parse
* @param parentElements list of parent SubmodelElements along the path (used to determine if Entity uses 'statements')
* @return MongoFilterResult with the update key and filters
*/
public static MongoFilterResult parse(@NonNull String idShortPath, @NonNull List<SubmodelElement> parentElements) {
Deque<GenericPath> paths = IdShortPathParser.parse(idShortPath);

assert !paths.isEmpty();

StringBuilder updateKey = new StringBuilder();
List<CriteriaDefinition> filterArray = new ArrayList<>();
int filterCounter = 0;
int parentIndex = 0;

updateKey.append(KEY_SUBMODEL_ELEMENTS);

Expand All @@ -74,7 +88,14 @@ public static MongoFilterResult parse(@NonNull String idShortPath) {

while (!paths.isEmpty()) {
GenericPath segment = paths.pop();
updateKey.append(".").append(KEY_VALUE);
// Determine whether to use 'statements' (for Entity) or 'value' (for others)
String childKey = KEY_VALUE;
if (parentIndex < parentElements.size() && parentElements.get(parentIndex) instanceof Entity) {
childKey = KEY_STATEMENTS;
}
updateKey.append(".").append(childKey);
parentIndex++;

if (segment instanceof IdShortPath idPath) {
placeholder = "elem" + filterCounter++;
updateKey.append(".$[").append(placeholder).append("]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,25 @@ public void patchSubmodelElements() {
assertEquals(submodelElementsPatch, patchedSubmodel.getSubmodelElements());
}

@Test
public void addEntityToEntity() {
List<SubmodelElement> submodelElements = new ArrayList<>();
Entity entity = new DefaultEntity.Builder().idShort("MainEntity").build();
submodelElements.add(entity);
Submodel submodel = buildDummySubmodelWithSmElement(ID, submodelElements);
SubmodelService submodelService = getSubmodelService(submodel);

Entity subEntity = new DefaultEntity.Builder().idShort("SubEntity").build();
submodelService.createSubmodelElement("MainEntity", subEntity);


Entity sub_subEntity = new DefaultEntity.Builder().idShort("Sub_SubEntity").build();
submodelService.createSubmodelElement("MainEntity.SubEntity", sub_subEntity);

SubmodelElement sme = submodelService.getSubmodelElement("MainEntity.SubEntity.Sub_SubEntity");
assertEquals(sub_subEntity.getIdShort(), sme.getIdShort());
}

protected Submodel buildDummySubmodelWithSmElement(String id, List<SubmodelElement> submodelElements) {
return new DefaultSubmodel.Builder().id(id).submodelElements(submodelElements).build();
}
Expand Down