Skip to content

Commit 8171d34

Browse files
authored
Fixes MongoDB null-field bug (#711)
1 parent 7cd48d8 commit 8171d34

File tree

1 file changed

+66
-7
lines changed
  • basyx.submodelservice/basyx.submodelservice-backend-mongodb/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/backend

1 file changed

+66
-7
lines changed

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

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
4141
import org.eclipse.digitaltwin.basyx.submodelservice.backend.SubmodelOperations;
4242
import org.eclipse.digitaltwin.basyx.submodelservice.backend.MongoFilterBuilder.MongoFilterResult;
43+
import org.eclipse.digitaltwin.basyx.submodelservice.pathparsing.HierarchicalSubmodelElementParser;
44+
import org.eclipse.digitaltwin.basyx.submodelservice.pathparsing.SubmodelElementIdShortHelper;
4345
import org.eclipse.digitaltwin.basyx.submodelservice.value.SubmodelElementValue;
4446
import org.eclipse.digitaltwin.basyx.submodelservice.value.factory.SubmodelElementValueMapperFactory;
4547
import org.eclipse.digitaltwin.basyx.submodelservice.value.mapper.ValueMapper;
@@ -178,20 +180,77 @@ public void updateSubmodelElement(String submodelId, String idShortPath, Submode
178180
}
179181

180182
@Override
181-
public void deleteSubmodelElement(String submodelId, String idShortPath) throws ElementDoesNotExistException {
182-
MongoFilterResult filterResult = MongoFilterBuilder.parse(idShortPath);
183+
public synchronized void deleteSubmodelElement(String submodelId, String idShortPath) throws ElementDoesNotExistException {
183184

185+
Submodel submodel = getSubmodel(submodelId);
186+
if (!SubmodelElementIdShortHelper.isNestedIdShortPath(idShortPath)) {
187+
deleteFlatSubmodelElement(submodel, idShortPath);
188+
}else {
189+
deleteNestedSubmodelElement(submodel, idShortPath);
190+
}
191+
Update update = new Update().set(SUBMODEL_ELEMENTS_KEY, submodel.getSubmodelElements());
184192
Query query = new Query(Criteria.where("_id").is(submodelId));
185-
Update update = new Update().unset(filterResult.key());
186-
187-
filterResult.filters().forEach(update::filterArray);
188-
189193
UpdateResult result = mongoOperations.updateFirst(query, update, collectionName);
190194
if (result.getModifiedCount() == 0) {
191195
if (!existsSubmodel(submodelId))
192196
throw new ElementDoesNotExistException(submodelId);
193-
throw new ElementDoesNotExistException(idShortPath);
197+
if (!existsSubmodelElement(submodelId, idShortPath))
198+
throw new ElementDoesNotExistException(idShortPath);
199+
}
200+
}
201+
202+
private Submodel getSubmodel(String submodelId) throws ElementDoesNotExistException {
203+
Query query = new Query(Criteria.where("_id").is(submodelId));
204+
Submodel submodel = mongoOperations.findOne(query, Submodel.class, collectionName);
205+
206+
if (submodel == null) {
207+
throw new ElementDoesNotExistException(submodelId);
208+
}
209+
210+
return submodel;
211+
}
212+
213+
private static void deleteNestedSubmodelElement(Submodel submodel, String idShortPath) {
214+
HierarchicalSubmodelElementParser parser = new HierarchicalSubmodelElementParser(submodel);
215+
SubmodelElement sme = parser.getSubmodelElementFromIdShortPath(idShortPath);
216+
if (SubmodelElementIdShortHelper.isDirectParentASubmodelElementList(idShortPath)) {
217+
deleteNestedSubmodelElementFromList(parser, idShortPath, sme);
218+
} else {
219+
deleteNestedSubmodelElementFromCollectionOrEntity(parser, idShortPath, sme);
220+
}
221+
}
222+
private static void deleteNestedSubmodelElementFromList(HierarchicalSubmodelElementParser parser, String idShortPath, SubmodelElement sme) {
223+
String collectionId = SubmodelElementIdShortHelper.extractDirectParentSubmodelElementListIdShort(idShortPath);
224+
SubmodelElementList list = (SubmodelElementList) parser.getSubmodelElementFromIdShortPath(collectionId);
225+
list.getValue().remove(sme);
226+
}
227+
228+
private static void deleteNestedSubmodelElementFromCollectionOrEntity(HierarchicalSubmodelElementParser parser, String idShortPath, SubmodelElement sme) {
229+
String collectionId = SubmodelElementIdShortHelper.extractDirectParentSubmodelElementCollectionIdShort(idShortPath);
230+
SubmodelElement parent = parser.getSubmodelElementFromIdShortPath(collectionId);
231+
if (parent instanceof SubmodelElementCollection collection) {
232+
collection.getValue().remove(sme);
233+
} else if (parent instanceof Entity entity) {
234+
entity.getStatements().remove(sme);
235+
}
236+
}
237+
238+
private static void deleteFlatSubmodelElement(Submodel submodel, String idShortPath) throws ElementDoesNotExistException {
239+
int index = findIndexOfElementTobeDeleted(submodel, idShortPath);
240+
if (index >= 0) {
241+
submodel.getSubmodelElements().remove(index);
242+
return;
243+
}
244+
throw new ElementDoesNotExistException();
245+
}
246+
247+
private static int findIndexOfElementTobeDeleted(Submodel submodel, String idShortPath) {
248+
for (SubmodelElement sme : submodel.getSubmodelElements()) {
249+
if (sme.getIdShort().equals(idShortPath)) {
250+
return submodel.getSubmodelElements().indexOf(sme);
251+
}
194252
}
253+
return -1;
195254
}
196255

197256
@Override

0 commit comments

Comments
 (0)