Skip to content

Commit 6770f59

Browse files
authored
Fixes Bug in MongoDBStorage with Entity in Entities (#968)
* Fixes Bug in MongoDBStorage with Entity in Entities * Adds ARE for create Submodel * Edits * Fixes ARE * Fixes ARE
1 parent 7cc2dfd commit 6770f59

File tree

8 files changed

+207
-37
lines changed

8 files changed

+207
-37
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*******************************************************************************
2+
* Copyright (C) 2026 the Eclipse BaSyx Authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*
23+
* SPDX-License-Identifier: MIT
24+
******************************************************************************/
25+
26+
package org.eclipse.digitaltwin.basyx.core.exceptions;
27+
28+
/**
29+
* Indicates that the requested submodel element is not a Data SubmodelElement
30+
*
31+
* @author fried
32+
*
33+
*/
34+
@SuppressWarnings("serial")
35+
public class SubmodelElementNotADataElementException extends RuntimeException {
36+
public SubmodelElementNotADataElementException() {
37+
}
38+
39+
public SubmodelElementNotADataElementException(String elementId) {
40+
super(getMsg(elementId));
41+
}
42+
43+
private static String getMsg(String elementId) {
44+
return "SubmodelElement with Id " + elementId + " is not a Data Element";
45+
}
46+
}

basyx.common/basyx.http/src/main/java/org/eclipse/digitaltwin/basyx/http/BaSyxExceptionHandler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,16 @@ public ResponseEntity<Object> handleFeatureNotSupportedException(FeatureNotSuppo
103103
public ResponseEntity<Object> handleNotInvokableException(NotInvokableException exception) {
104104
return buildResponse(exception.getMessage(), HttpStatus.METHOD_NOT_ALLOWED, exception);
105105
}
106-
106+
107107
@ExceptionHandler(ElementNotAFileException.class)
108108
public ResponseEntity<Object> handleElementNotAFileException(ElementNotAFileException exception) {
109109
return buildResponse(exception.getMessage(), HttpStatus.PRECONDITION_FAILED, exception);
110110
}
111+
112+
@ExceptionHandler(SubmodelElementNotADataElementException.class)
113+
public ResponseEntity<Object> handleSubmodelElementNotADataElementException(SubmodelElementNotADataElementException exception) {
114+
return buildResponse(exception.getMessage(), HttpStatus.BAD_REQUEST, exception);
115+
}
111116

112117
@ExceptionHandler(InsufficientPermissionException.class)
113118
public ResponseEntity<Object> handleInsufficientPermissionException(InsufficientPermissionException exception) {

basyx.submodelservice/basyx.submodelservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/InMemorySubmodelBackend.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.eclipse.digitaltwin.basyx.common.backend.inmemory.core.InMemoryCrudRepository;
3030
import org.eclipse.digitaltwin.basyx.core.exceptions.CollidingIdentifierException;
3131
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;
32+
import org.eclipse.digitaltwin.basyx.core.exceptions.SubmodelElementNotADataElementException;
3233
import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult;
3334
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
3435
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationSupport;
@@ -126,6 +127,13 @@ public synchronized void createSubmodelElement(String submodelId, String idShort
126127
} else if (parentSme instanceof Entity entity) {
127128
List<SubmodelElement> submodelElements = entity.getStatements();
128129
submodelElements.add(submodelElement);
130+
} else if (parentSme instanceof AnnotatedRelationshipElement are) {
131+
try {
132+
List<DataElement> annotations = are.getAnnotations();
133+
annotations.add((DataElement) submodelElement);
134+
} catch (ClassCastException e) {
135+
throw new SubmodelElementNotADataElementException(submodelElement.getIdShort());
136+
}
129137
}
130138
}
131139

basyx.submodelservice/basyx.submodelservice-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/backend/MongoDbSubmodelOperations.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@
3030
import java.util.List;
3131

3232
import org.bson.Document;
33-
import org.eclipse.digitaltwin.aas4j.v3.model.Entity;
34-
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
35-
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
36-
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection;
37-
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementList;
33+
import org.eclipse.digitaltwin.aas4j.v3.model.*;
3834
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;
35+
import org.eclipse.digitaltwin.basyx.core.exceptions.SubmodelElementNotADataElementException;
3936
import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult;
4037
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
4138
import org.eclipse.digitaltwin.basyx.submodelservice.backend.SubmodelOperations;
@@ -186,14 +183,22 @@ public void createSubmodelElement(String submodelId, String idShortPath, Submode
186183
} else if (parentSme instanceof Entity entity) {
187184
List<SubmodelElement> submodelElements = entity.getStatements();
188185
submodelElements.add(submodelElement);
186+
} else if (parentSme instanceof AnnotatedRelationshipElement are) {
187+
try {
188+
List<DataElement> annotations = are.getAnnotations();
189+
annotations.add((DataElement) submodelElement);
190+
} catch (ClassCastException e) {
191+
throw new SubmodelElementNotADataElementException(submodelElement.getIdShort());
192+
}
189193
}
190194

191195
updateSubmodelElement(submodelId, idShortPath, parentSme);
192196
}
193197

194198
@Override
195199
public void updateSubmodelElement(String submodelId, String idShortPath, SubmodelElement submodelElement) throws ElementDoesNotExistException {
196-
MongoFilterResult filterResult = MongoFilterBuilder.parse(idShortPath);
200+
List<SubmodelElement> parentElements = getParentElements(submodelId, idShortPath);
201+
MongoFilterResult filterResult = MongoFilterBuilder.parse(idShortPath, parentElements);
197202

198203
Query query = new Query(Criteria.where("_id").is(submodelId));
199204
Update update = new Update().set(filterResult.key(), submodelElement);
@@ -326,6 +331,36 @@ private boolean existsSubmodelElement(String submodelId, String idShortPath){
326331
}
327332
}
328333

334+
/**
335+
* Gets the list of parent SubmodelElements along the path.
336+
* This is used to determine if any parent is an Entity (which uses 'statements' instead of 'value').
337+
*/
338+
private List<SubmodelElement> getParentElements(String submodelId, String idShortPath) {
339+
List<SubmodelElement> parents = new ArrayList<>();
340+
String[] segments = idShortPath.split("\\.");
341+
342+
if (segments.length <= 1) {
343+
return parents;
344+
}
345+
346+
StringBuilder currentPath = new StringBuilder();
347+
for (int i = 0; i < segments.length - 1; i++) {
348+
if (currentPath.length() > 0) {
349+
currentPath.append(".");
350+
}
351+
currentPath.append(segments[i]);
352+
try {
353+
SubmodelElement element = getSubmodelElement(submodelId, currentPath.toString());
354+
parents.add(element);
355+
} catch (ElementDoesNotExistException e) {
356+
// Element doesn't exist, stop here
357+
break;
358+
}
359+
}
360+
361+
return parents;
362+
}
363+
329364
private static boolean hasLimit(PaginationInfo pInfo) {
330365
return pInfo.getLimit() != null && pInfo.getLimit() > 0;
331366
}

basyx.submodelservice/basyx.submodelservice-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/backend/MongoFilterBuilder.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
import java.util.Deque;
3030
import java.util.List;
3131

32+
import org.eclipse.digitaltwin.aas4j.v3.model.AnnotatedRelationshipElement;
33+
import org.eclipse.digitaltwin.aas4j.v3.model.Entity;
34+
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
3235
import org.eclipse.digitaltwin.basyx.submodelservice.backend.IdShortPathParser.GenericPath;
3336
import org.eclipse.digitaltwin.basyx.submodelservice.backend.IdShortPathParser.IdShortPath;
3437
import org.eclipse.digitaltwin.basyx.submodelservice.backend.IdShortPathParser.IndexPath;
@@ -50,17 +53,30 @@
5053
public final class MongoFilterBuilder{
5154
static final String KEY_VALUE = "value";
5255
static final String KEY_STATEMENTS = "statements";
56+
static final String KEY_ANNOTATIONS = "annotations";
5357
static final String KEY_SUBMODEL_ELEMENTS = "submodelElements";
5458
static final String KEY_ID_SHORT = "idShort";
5559

5660
public static MongoFilterResult parse(@NonNull String idShortPath) {
61+
return parse(idShortPath, List.of());
62+
}
63+
64+
/**
65+
* Parses the idShortPath and builds MongoDB filter result.
66+
*
67+
* @param idShortPath the path to parse
68+
* @param parentElements list of parent SubmodelElements along the path (used to determine if Entity uses 'statements')
69+
* @return MongoFilterResult with the update key and filters
70+
*/
71+
public static MongoFilterResult parse(@NonNull String idShortPath, @NonNull List<SubmodelElement> parentElements) {
5772
Deque<GenericPath> paths = IdShortPathParser.parse(idShortPath);
5873

5974
assert !paths.isEmpty();
6075

6176
StringBuilder updateKey = new StringBuilder();
6277
List<CriteriaDefinition> filterArray = new ArrayList<>();
6378
int filterCounter = 0;
79+
int parentIndex = 0;
6480

6581
updateKey.append(KEY_SUBMODEL_ELEMENTS);
6682

@@ -74,7 +90,11 @@ public static MongoFilterResult parse(@NonNull String idShortPath) {
7490

7591
while (!paths.isEmpty()) {
7692
GenericPath segment = paths.pop();
77-
updateKey.append(".").append(KEY_VALUE);
93+
// Determine the correct child key based on parent element type
94+
String childKey = getChildKey(parentElements, parentIndex);
95+
updateKey.append(".").append(childKey);
96+
parentIndex++;
97+
7898
if (segment instanceof IdShortPath idPath) {
7999
placeholder = "elem" + filterCounter++;
80100
updateKey.append(".$[").append(placeholder).append("]");
@@ -104,21 +124,44 @@ public static List<AggregationOperation> buildAggregationOperations(@NonNull Str
104124
currentPath = paths.pop();
105125
ops.add(new UnwindOperation(Fields.field("$"+KEY_VALUE), true));
106126
ops.add(new UnwindOperation(Fields.field("$"+KEY_STATEMENTS), true));
127+
ops.add(new UnwindOperation(Fields.field("$"+KEY_ANNOTATIONS), true));
107128
if (currentPath instanceof IdShortPath idPath) {
108129
Criteria inValue = Criteria.where(joinKeys(KEY_VALUE, KEY_ID_SHORT)).is(idPath.idShort());
109130
Criteria inStatements = Criteria.where(joinKeys(KEY_STATEMENTS, KEY_ID_SHORT)).is(idPath.idShort());
110-
ops.add(Aggregation.match(new Criteria().orOperator(inValue, inStatements)));
131+
Criteria inAnnotations = Criteria.where(joinKeys(KEY_ANNOTATIONS, KEY_ID_SHORT)).is(idPath.idShort());
132+
ops.add(Aggregation.match(new Criteria().orOperator(inValue, inStatements, inAnnotations)));
111133
} else if (currentPath instanceof IndexPath ixPath) {
112134
ops.add(Aggregation.skip(ixPath.index()));
113135
ops.add(Aggregation.limit(1));
114136
}
115-
ops.add(Aggregation.replaceRoot(IfNull.ifNull("$"+KEY_VALUE).then("$"+KEY_STATEMENTS)));
137+
ops.add(Aggregation.replaceRoot(
138+
IfNull.ifNull("$"+KEY_VALUE)
139+
.thenValueOf(IfNull.ifNull("$"+KEY_STATEMENTS).then("$"+KEY_ANNOTATIONS))));
116140

117141
}
118142

119143
return ops;
120144
}
121145

146+
/**
147+
* Determines the correct child key based on the parent element type.
148+
* - Entity uses 'statements'
149+
* - AnnotatedRelationshipElement uses 'annotations'
150+
* - Others (SubmodelElementList, SubmodelElementCollection) use 'value'
151+
*/
152+
private static String getChildKey(List<SubmodelElement> parentElements, int parentIndex) {
153+
if (parentIndex >= parentElements.size()) {
154+
return KEY_VALUE;
155+
}
156+
SubmodelElement parent = parentElements.get(parentIndex);
157+
if (parent instanceof Entity) {
158+
return KEY_STATEMENTS;
159+
} else if (parent instanceof AnnotatedRelationshipElement) {
160+
return KEY_ANNOTATIONS;
161+
}
162+
return KEY_VALUE;
163+
}
164+
122165
static CriteriaDefinition buildCriteria(String key, String value) {
123166
return Criteria.where(key).is(value);
124167
}

basyx.submodelservice/basyx.submodelservice-client/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/client/ConnectedSubmodelService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@
3838
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
3939
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultOperationRequest;
4040
import org.eclipse.digitaltwin.basyx.client.internal.ApiException;
41-
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;
42-
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementNotAFileException;
43-
import org.eclipse.digitaltwin.basyx.core.exceptions.FileDoesNotExistException;
44-
import org.eclipse.digitaltwin.basyx.core.exceptions.NotInvokableException;
45-
import org.eclipse.digitaltwin.basyx.core.exceptions.OperationDelegationException;
41+
import org.eclipse.digitaltwin.basyx.core.exceptions.*;
4642
import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult;
4743
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
4844
import org.eclipse.digitaltwin.basyx.http.Base64UrlEncoder;
@@ -226,6 +222,10 @@ private RuntimeException mapExceptionSubmodelElementAccess(String idShortPath, A
226222
if (e.getCode() == HttpStatus.NOT_FOUND.value()) {
227223
return new ElementDoesNotExistException(idShortPath);
228224
}
225+
System.out.println(e.getMessage());
226+
if (e.getCode() == HttpStatus.BAD_REQUEST.value() && e.getMessage().contains("is not a Data Element")) {
227+
return new SubmodelElementNotADataElementException(idShortPath);
228+
}
229229

230230
return e;
231231
}

basyx.submodelservice/basyx.submodelservice-core/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/pathparsing/HierarchicalSubmodelElementIdShortPathToken.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
package org.eclipse.digitaltwin.basyx.submodelservice.pathparsing;
2626

2727
import java.util.Collection;
28+
import java.util.stream.Collectors;
2829

30+
import org.eclipse.digitaltwin.aas4j.v3.model.AnnotatedRelationshipElement;
2931
import org.eclipse.digitaltwin.aas4j.v3.model.Entity;
3032
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
3133
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection;
@@ -59,6 +61,12 @@ public SubmodelElement getSubmodelElement(SubmodelElement rootElement) {
5961
Entity entity = (Entity) rootElement;
6062

6163
return filterSubmodelElement(entity.getStatements());
64+
} else if (rootElement instanceof AnnotatedRelationshipElement) {
65+
AnnotatedRelationshipElement are = (AnnotatedRelationshipElement) rootElement;
66+
67+
return filterSubmodelElement(are.getAnnotations().stream()
68+
.map(de -> (SubmodelElement) de)
69+
.collect(Collectors.toList()));
6270
}
6371

6472
throw new ElementDoesNotExistException(token);

basyx.submodelservice/basyx.submodelservice-core/src/test/java/org/eclipse/digitaltwin/basyx/submodelservice/SubmodelServiceSuite.java

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,9 @@
4141
import org.apache.commons.io.FileUtils;
4242
import org.apache.commons.io.FilenameUtils;
4343
import org.apache.commons.lang3.NotImplementedException;
44-
import org.eclipse.digitaltwin.aas4j.v3.model.DataTypeDefXsd;
45-
import org.eclipse.digitaltwin.aas4j.v3.model.Entity;
46-
import org.eclipse.digitaltwin.aas4j.v3.model.File;
47-
import org.eclipse.digitaltwin.aas4j.v3.model.LangStringTextType;
48-
import org.eclipse.digitaltwin.aas4j.v3.model.OperationVariable;
49-
import org.eclipse.digitaltwin.aas4j.v3.model.Property;
50-
import org.eclipse.digitaltwin.aas4j.v3.model.Range;
51-
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
52-
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
53-
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection;
54-
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementList;
55-
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultEntity;
56-
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultFile;
57-
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultLangStringTextType;
58-
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultProperty;
59-
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodel;
60-
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementCollection;
61-
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementList;
62-
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;
63-
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementNotAFileException;
64-
import org.eclipse.digitaltwin.basyx.core.exceptions.FileDoesNotExistException;
65-
import org.eclipse.digitaltwin.basyx.core.exceptions.NotInvokableException;
44+
import org.eclipse.digitaltwin.aas4j.v3.model.*;
45+
import org.eclipse.digitaltwin.aas4j.v3.model.impl.*;
46+
import org.eclipse.digitaltwin.basyx.core.exceptions.*;
6647
import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult;
6748
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
6849
import org.eclipse.digitaltwin.basyx.submodelservice.value.FileBlobValue;
@@ -670,6 +651,50 @@ public void patchSubmodelElements() {
670651
assertEquals(submodelElementsPatch, patchedSubmodel.getSubmodelElements());
671652
}
672653

654+
@Test
655+
public void addEntityToEntity() {
656+
List<SubmodelElement> submodelElements = new ArrayList<>();
657+
Entity entity = new DefaultEntity.Builder().idShort("MainEntity").build();
658+
submodelElements.add(entity);
659+
Submodel submodel = buildDummySubmodelWithSmElement(ID, submodelElements);
660+
SubmodelService submodelService = getSubmodelService(submodel);
661+
662+
Entity subEntity = new DefaultEntity.Builder().idShort("SubEntity").build();
663+
submodelService.createSubmodelElement("MainEntity", subEntity);
664+
665+
666+
Entity sub_subEntity = new DefaultEntity.Builder().idShort("Sub_SubEntity").build();
667+
submodelService.createSubmodelElement("MainEntity.SubEntity", sub_subEntity);
668+
669+
SubmodelElement sme = submodelService.getSubmodelElement("MainEntity.SubEntity.Sub_SubEntity");
670+
assertEquals(sub_subEntity.getIdShort(), sme.getIdShort());
671+
}
672+
673+
@Test
674+
public void addDataElementToARE(){
675+
List<SubmodelElement> submodelElements = new ArrayList<>();
676+
AnnotatedRelationshipElement topAre = new DefaultAnnotatedRelationshipElement.Builder().idShort("MainAre").build();
677+
Property property = createDummyProperty("Test");
678+
submodelElements.add(topAre);
679+
Submodel submodel = buildDummySubmodelWithSmElement(ID, submodelElements);
680+
SubmodelService submodelService = getSubmodelService(submodel);
681+
682+
submodelService.createSubmodelElement("MainAre", property);
683+
SubmodelElement sme = submodelService.getSubmodelElement("MainAre.Test");
684+
assertEquals(sme.getIdShort(), sme.getIdShort());
685+
}
686+
687+
@Test(expected = SubmodelElementNotADataElementException.class)
688+
public void addNonDataElementToAre(){
689+
List<SubmodelElement> submodelElements = new ArrayList<>();
690+
AnnotatedRelationshipElement topAre = new DefaultAnnotatedRelationshipElement.Builder().idShort("MainAre").build();
691+
AnnotatedRelationshipElement subAre = new DefaultAnnotatedRelationshipElement.Builder().idShort("SubAre").build();
692+
submodelElements.add(topAre);
693+
Submodel submodel = buildDummySubmodelWithSmElement(ID, submodelElements);
694+
SubmodelService submodelService = getSubmodelService(submodel);
695+
submodelService.createSubmodelElement("MainAre", subAre);
696+
}
697+
673698
protected Submodel buildDummySubmodelWithSmElement(String id, List<SubmodelElement> submodelElements) {
674699
return new DefaultSubmodel.Builder().id(id).submodelElements(submodelElements).build();
675700
}

0 commit comments

Comments
 (0)