|
40 | 40 | import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo; |
41 | 41 | import org.eclipse.digitaltwin.basyx.submodelservice.backend.SubmodelOperations; |
42 | 42 | 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; |
43 | 45 | import org.eclipse.digitaltwin.basyx.submodelservice.value.SubmodelElementValue; |
44 | 46 | import org.eclipse.digitaltwin.basyx.submodelservice.value.factory.SubmodelElementValueMapperFactory; |
45 | 47 | import org.eclipse.digitaltwin.basyx.submodelservice.value.mapper.ValueMapper; |
@@ -178,20 +180,77 @@ public void updateSubmodelElement(String submodelId, String idShortPath, Submode |
178 | 180 | } |
179 | 181 |
|
180 | 182 | @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 { |
183 | 184 |
|
| 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()); |
184 | 192 | 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 | | - |
189 | 193 | UpdateResult result = mongoOperations.updateFirst(query, update, collectionName); |
190 | 194 | if (result.getModifiedCount() == 0) { |
191 | 195 | if (!existsSubmodel(submodelId)) |
192 | 196 | 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 | + } |
194 | 252 | } |
| 253 | + return -1; |
195 | 254 | } |
196 | 255 |
|
197 | 256 | @Override |
|
0 commit comments