@@ -115,12 +115,10 @@ public void addSubmodelReference(@NonNull String aasId, @NonNull Reference submo
115115 String newKeyValue = submodelReference .getKeys ().get (0 ).getValue ();
116116 Query query = new Query (new Criteria ().andOperator (Criteria .where ("_id" ).is (aasId ), Criteria .where (KEY_SMREF ).not ().elemMatch (Criteria .where ("keys.0.value" ).is (newKeyValue ))));
117117 Update update = new Update ().push (KEY_SMREF , submodelReference );
118- UpdateResult result = mongoOperations .updateFirst (query , update , collectionName );
119-
120- if (result .getMatchedCount () != 0 )
118+ UpdateResult result = mongoOperations .updateFirst (query , update , collectionName ); if (result .getMatchedCount () != 0 )
121119 return ;
122120
123- if (existsAas (aasId ))
121+ if (! existsAas (aasId ))
124122 throw new ElementDoesNotExistException (aasId );
125123
126124 throw new CollidingSubmodelReferenceException (newKeyValue );
@@ -130,30 +128,25 @@ public void addSubmodelReference(@NonNull String aasId, @NonNull Reference submo
130128 public void removeSubmodelReference (@ NonNull String aasId , @ NonNull String submodelId ) throws ElementDoesNotExistException {
131129 Query query = new Query (Criteria .where ("_id" ).is (aasId ));
132130 Update update = new Update ().pull (KEY_SMREF , Query .query (Criteria .where ("keys.value" ).is (submodelId )).getQueryObject ());
133- UpdateResult result = mongoOperations .updateFirst (query , update , collectionName );
134-
135- if (result .getModifiedCount () != 0 )
131+ UpdateResult result = mongoOperations .updateFirst (query , update , collectionName ); if (result .getModifiedCount () != 0 )
136132 return ;
137133
138- if (existsAas (aasId ))
134+ if (! existsAas (aasId ))
139135 throw new ElementDoesNotExistException (aasId );
140136
141137 throw new ElementDoesNotExistException (submodelId );
142- }
143-
144- @ Override
138+ } @ Override
145139 public void setAssetInformation (@ NonNull String aasId , @ NonNull AssetInformation aasInfo ) {
146140 Query query = new Query (Criteria .where ("_id" ).is (aasId ));
147141
148142 Update update = new Update ().set (KEY_ASSETINFORMATION , aasInfo );
149143
150144 UpdateResult result = mongoOperations .updateFirst (query , update , collectionName );
151145
152- // Second check for the case where the update was not performed because the
153- // aasInfo is the
154- // same as the existing one
155- if (result .getModifiedCount () == 0 && existsAas (aasId ))
146+ // Check if the AAS exists when no modification was made
147+ if (result .getModifiedCount () == 0 && !existsAas (aasId )) {
156148 throw new ElementDoesNotExistException (aasId );
149+ }
157150 }
158151
159152 @ Override
@@ -169,39 +162,52 @@ public AssetInformation getAssetInformation(@NonNull String aasId) {
169162 throw new ElementDoesNotExistException (aasId );
170163
171164 return aasInfo ;
172- }
173-
174- @ Override
165+ } @ Override
175166 public Iterable <AssetAdministrationShell > getAllAas (List <SpecificAssetId > assetIds , String idShort ) {
176167 Query query = new Query ();
177- Criteria criteria = new Criteria ();
168+ List < Criteria > allCriteria = new ArrayList <> ();
178169
179- List <String > globalAssetId = assetIds .stream ()
180- .filter (assetId -> "globalAssetId" .equals (assetId .getName ()))
181- .map (SpecificAssetId ::getValue )
182- .collect (Collectors .toList ());
170+ // Handle asset ID filtering
183171 if (assetIds != null && !assetIds .isEmpty ()) {
184172 List <Criteria > assetIdCriteria = new ArrayList <>();
173+
185174 for (SpecificAssetId assetId : assetIds ) {
186- assetIdCriteria .add (Criteria .where ("assetInformation.specificAssetIds.name" ).is (assetId .getName ()));
187- assetIdCriteria .add (Criteria .where ("assetInformation.specificAssetIds.value" ).is (assetId .getValue ()));
175+ if ("globalAssetId" .equals (assetId .getName ())) {
176+ // Handle globalAssetId
177+ assetIdCriteria .add (Criteria .where ("assetInformation.globalAssetId" ).is (assetId .getValue ()));
178+ } else {
179+ // Handle specific asset IDs with elemMatch for proper array matching
180+ assetIdCriteria .add (Criteria .where ("assetInformation.specificAssetIds" ).elemMatch (
181+ Criteria .where ("name" ).is (assetId .getName ()).and ("value" ).is (assetId .getValue ())
182+ ));
183+ }
184+ }
185+
186+ // If we have multiple asset IDs, any one of them should match (OR operation)
187+ if (assetIdCriteria .size () == 1 ) {
188+ allCriteria .add (assetIdCriteria .get (0 ));
189+ } else if (assetIdCriteria .size () > 1 ) {
190+ allCriteria .add (new Criteria ().orOperator (assetIdCriteria .toArray (new Criteria [0 ])));
188191 }
189- criteria .andOperator (assetIdCriteria );
190192 }
193+
194+ // Handle idShort filtering
191195 if (idShort != null && !idShort .isEmpty ()) {
192- criteria . and ( "idShort" ).is (idShort );
196+ allCriteria . add ( Criteria . where ( "idShort" ).is (idShort ) );
193197 }
194- query .addCriteria (criteria );
195-
196- for (String id : globalAssetId ) {
197- query .addCriteria (Criteria .where ("assetInformation.globalAssetId" ).is (id ));
198+
199+ // Combine all criteria with AND operation
200+ if (!allCriteria .isEmpty ()) {
201+ if (allCriteria .size () == 1 ) {
202+ query .addCriteria (allCriteria .get (0 ));
203+ } else {
204+ query .addCriteria (new Criteria ().andOperator (allCriteria .toArray (new Criteria [0 ])));
205+ }
198206 }
199207
200208 return mongoOperations .find (query , AssetAdministrationShell .class , collectionName );
201- }
202-
203- private boolean existsAas (String aasId ) {
204- return !mongoOperations .exists (new Query (Criteria .where ("_id" ).is (aasId )), AssetAdministrationShell .class , collectionName );
209+ } private boolean existsAas (String aasId ) {
210+ return mongoOperations .exists (new Query (Criteria .where ("_id" ).is (aasId )), AssetAdministrationShell .class , collectionName );
205211 }
206212
207213 private static String extractSubmodelId (Reference reference ) {
0 commit comments