5353import org .eclipse .basyx .vab .exception .provider .ResourceNotFoundException ;
5454import org .eclipse .basyx .vab .modelprovider .VABPathTools ;
5555import org .eclipse .basyx .vab .modelprovider .api .IModelProvider ;
56+ import org .eclipse .basyx .vab .modelprovider .lambda .VABLambdaProvider ;
5657import org .eclipse .basyx .vab .modelprovider .map .VABMapProvider ;
5758import org .eclipse .basyx .vab .protocol .http .connector .HTTPConnectorFactory ;
5859import org .springframework .data .mongodb .core .MongoOperations ;
@@ -205,12 +206,12 @@ public void setSubmodel(Submodel sm) {
205206 String id = sm .getIdentification ().getId ();
206207 this .setSubmodelId (id );
207208
208- Query hasId = query (where (SMIDPATH ).is (smId ));
209- Object replaced = mongoOps .findAndReplace (hasId , sm , collection );
209+ Submodel replaced = writeSubmodelInDB (sm );
210210 if (replaced == null ) {
211211 mongoOps .insert (sm , collection );
212212 }
213-
213+ // Remove mongoDB-specific map attribute from SM
214+ // mongoOps modify sm on save - thus _id has to be removed here...
214215 sm .remove ("_id" );
215216 }
216217
@@ -249,9 +250,7 @@ public void addSubmodelElement(ISubmodelElement elem) {
249250 Submodel sm = (Submodel ) getSubmodel ();
250251 // Add element
251252 sm .addSubmodelElement (elem );
252- // Replace db entry
253- Query hasId = query (where (SMIDPATH ).is (smId ));
254- mongoOps .findAndReplace (hasId , sm , collection );
253+ writeSubmodelInDB (sm );
255254 }
256255
257256 private ISubmodelElement getTopLevelSubmodelElement (String idShort ) {
@@ -278,9 +277,7 @@ private void deleteTopLevelSubmodelElement(String idShort) {
278277 Submodel sm = (Submodel ) getSubmodel ();
279278 // Remove element
280279 sm .getSubmodelElements ().remove (idShort );
281- // Replace db entry
282- Query hasId = query (where (SMIDPATH ).is (smId ));
283- mongoOps .findAndReplace (hasId , sm , collection );
280+ writeSubmodelInDB (sm );
284281 }
285282
286283 @ Override
@@ -298,16 +295,12 @@ private void addNestedSubmodelElement(List<String> idShorts, ISubmodelElement el
298295 ISubmodelElement parentElement = getNestedSubmodelElement (sm , idShorts );
299296 if (parentElement instanceof SubmodelElementCollection ) {
300297 ((SubmodelElementCollection ) parentElement ).addSubmodelElement (elem );
301- // Replace db entry
302- Query hasId = query (where (SMIDPATH ).is (smId ));
303- mongoOps .findAndReplace (hasId , sm , collection );
298+ writeSubmodelInDB (sm );
304299 }
305300 } else {
306301 // else => directly add it to the submodel
307302 sm .addSubmodelElement (elem );
308- // Replace db entry
309- Query hasId = query (where (SMIDPATH ).is (smId ));
310- mongoOps .findAndReplace (hasId , sm , collection );
303+ writeSubmodelInDB (sm );
311304 }
312305 }
313306
@@ -317,33 +310,20 @@ public Collection<ISubmodelElement> getSubmodelElements() {
317310 return sm .getSubmodelElements ().values ();
318311 }
319312
320- private void updateTopLevelSubmodelElement (String idShort , Object newValue ) {
321- // Get sm from db
322- Submodel sm = (Submodel ) getSubmodel ();
323- // Unwrap value
324- newValue = unwrapParameter (newValue );
325- // Get and update property value
326- getElementProvider (sm , idShort ).setValue (Property .VALUE , newValue );
327- // Replace db entry
328- Query hasId = query (where (SMIDPATH ).is (smId ));
329- mongoOps .findAndReplace (hasId , sm , collection );
330- }
331-
332313 @ SuppressWarnings ("unchecked" )
333- private void updateNestedSubmodelElement (List <String > idShorts , Object newValue ) {
314+ private void updateSubmodelElementInDB (List <String > idShorts , Object newValue ) {
334315 Submodel sm = (Submodel ) getSubmodel ();
335-
336- // Get parent SM element
337316 ISubmodelElement element = getNestedSubmodelElement (sm , idShorts );
338317
339- // Update value
340- IModelProvider mapProvider = new VABMapProvider ((Map <String , Object >) element );
341- IModelProvider elemProvider = SubmodelElementProvider .getElementProvider (mapProvider );
342- elemProvider .setValue (Property .VALUE , newValue );
318+ IModelProvider mapProvider = new VABLambdaProvider ((Map <String , Object >) element );
319+ SubmodelElementProvider smeProvider = new SubmodelElementProvider (mapProvider );
343320
344- // Replace db entry
345- Query hasId = query (where (SMIDPATH ).is (smId ));
346- mongoOps .findAndReplace (hasId , sm , collection );
321+ smeProvider .setValue (Property .VALUE , newValue );
322+ ISubmodelElement updatedElement = SubmodelElementFacadeFactory .createSubmodelElement ((Map <String , Object >) smeProvider .getValue ("" ));
323+
324+ sm .addSubmodelElement (updatedElement );
325+
326+ writeSubmodelInDB (sm );
347327 }
348328
349329 private Object getTopLevelSubmodelElementValue (String idShort ) {
@@ -354,8 +334,8 @@ private Object getTopLevelSubmodelElementValue(String idShort) {
354334 @ SuppressWarnings ("unchecked" )
355335 private Object getNestedSubmodelElementValue (List <String > idShorts ) {
356336 ISubmodelElement lastElement = getNestedSubmodelElement (idShorts );
357- IModelProvider mapProvider = new VABMapProvider ((Map <String , Object >) lastElement );
358- return SubmodelElementProvider . getElementProvider (mapProvider ).getValue ("/value" );
337+ IModelProvider mapProvider = new VABLambdaProvider ((Map <String , Object >) lastElement );
338+ return new SubmodelElementProvider (mapProvider ).getValue ("/value" );
359339 }
360340
361341 @ SuppressWarnings ("unchecked" )
@@ -373,10 +353,10 @@ protected Object unwrapParameter(Object parameter) {
373353 }
374354
375355 @ SuppressWarnings ("unchecked" )
376- private IModelProvider getElementProvider (Submodel sm , String idShortPath ) {
377- ISubmodelElement elem = sm .getSubmodelElement (idShortPath );
356+ private static SubmodelElementProvider getElementProvider (Submodel sm , String idShort ) {
357+ ISubmodelElement elem = sm .getSubmodelElement (idShort );
378358 IModelProvider mapProvider = new VABMapProvider ((Map <String , Object >) elem );
379- return SubmodelElementProvider . getElementProvider (mapProvider );
359+ return new SubmodelElementProvider (mapProvider );
380360 }
381361
382362 private ISubmodelElement getNestedSubmodelElement (Submodel sm , List <String > idShorts ) {
@@ -419,9 +399,7 @@ private void deleteNestedSubmodelElement(List<String> idShorts) {
419399 // Remove element
420400 SubmodelElementCollection coll = (SubmodelElementCollection ) parentElement ;
421401 coll .deleteSubmodelElement (idShorts .get (idShorts .size () - 1 ));
422- // Replace db entry
423- Query hasId = query (where (SMIDPATH ).is (smId ));
424- mongoOps .findAndReplace (hasId , sm , collection );
402+ writeSubmodelInDB (sm );
425403 }
426404
427405 private Object invokeNestedOperationAsync (List <String > idShorts , Object ... params ) {
@@ -459,13 +437,20 @@ public void deleteSubmodelElement(String idShortPath) {
459437
460438 @ Override
461439 public void updateSubmodelElement (String idShortPath , Object newValue ) {
462- if (idShortPath .contains ("/" )) {
463- String [] splitted = VABPathTools .splitPath (idShortPath );
464- List <String > idShorts = Arrays .asList (splitted );
465- updateNestedSubmodelElement (idShorts , newValue );
466- } else {
467- updateTopLevelSubmodelElement (idShortPath , newValue );
468- }
440+ String [] splitted = VABPathTools .splitPath (idShortPath );
441+ List <String > idShorts = Arrays .asList (splitted );
442+ updateSubmodelElementInDB (idShorts , newValue );
443+ }
444+
445+ /**
446+ * Returns the updated Submodel or null if not found
447+ *
448+ * @param sm
449+ * @return
450+ */
451+ private Submodel writeSubmodelInDB (Submodel sm ) {
452+ Query hasId = query (where (SMIDPATH ).is (smId ));
453+ return mongoOps .findAndReplace (hasId , sm , collection );
469454 }
470455
471456 @ Override
@@ -482,10 +467,7 @@ public Object getSubmodelElementValue(String idShortPath) {
482467 @ SuppressWarnings ("unchecked" )
483468 @ Override
484469 public Object invokeOperation (String idShortPath , Object ... params ) {
485-
486- String elementPath = VABPathTools .getParentPath (idShortPath );
487-
488- Operation operation = (Operation ) SubmodelElementFacadeFactory .createSubmodelElement ((Map <String , Object >) getSubmodelElement (elementPath ));
470+ Operation operation = (Operation ) SubmodelElementFacadeFactory .createSubmodelElement ((Map <String , Object >) getSubmodelElement (idShortPath ));
489471 if (!DelegatedInvocationManager .isDelegatingOperation (operation )) {
490472 throw new MalformedRequestException ("This backend supports only delegating operations." );
491473 }
0 commit comments