Skip to content

Commit aa8cd46

Browse files
committed
refactor: smrepository: update CrudSubmodelRepository to use SubmodelServiceFactory for operations
1 parent b838bad commit aa8cd46

File tree

10 files changed

+101
-61
lines changed

10 files changed

+101
-61
lines changed

basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/test/java/org/eclipse/digitaltwin/basyx/submodelrepository/TestInMemorySubmodelRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected boolean fileExistsInStorage(String fileValue) {
7070

7171
@Test
7272
public void getConfiguredInMemorySmRepositoryName() {
73-
SubmodelRepository repo = new CrudSubmodelRepository(new InMemorySubmodelBackend(), new InMemoryFileRepository(), CONFIGURED_SM_REPO_NAME);
73+
SubmodelRepository repo = new CrudSubmodelRepositoryFactory(new InMemorySubmodelBackend(), new InMemoryFileRepository(), CONFIGURED_SM_REPO_NAME).create();
7474

7575
assertEquals(CONFIGURED_SM_REPO_NAME, repo.getName());
7676
}

basyx.submodelrepository/basyx.submodelrepository-backend/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/backend/CrudSubmodelRepository.java

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
3030
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
3131
import org.eclipse.digitaltwin.basyx.core.exceptions.*;
32-
import org.eclipse.digitaltwin.basyx.core.filerepository.FileRepository;
3332
import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult;
3433
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
3534
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationSupport;
3635
import org.eclipse.digitaltwin.basyx.serialization.SubmodelMetadataUtil;
3736
import org.eclipse.digitaltwin.basyx.submodelrepository.SubmodelRepository;
37+
import org.eclipse.digitaltwin.basyx.submodelservice.SubmodelService;
38+
import org.eclipse.digitaltwin.basyx.submodelservice.SubmodelServiceFactory;
3839
import org.eclipse.digitaltwin.basyx.submodelservice.backend.SubmodelBackend;
39-
import org.eclipse.digitaltwin.basyx.submodelservice.backend.SubmodelFileOperations;
4040
import org.eclipse.digitaltwin.basyx.submodelservice.value.SubmodelElementValue;
4141
import org.eclipse.digitaltwin.basyx.submodelservice.value.SubmodelValueOnly;
4242
import org.springframework.data.repository.CrudRepository;
@@ -56,19 +56,19 @@
5656
public class CrudSubmodelRepository implements SubmodelRepository {
5757

5858
private final SubmodelBackend submodelBackend;
59+
private final SubmodelServiceFactory submodelServiceFactory;
5960
private final String submodelRepositoryName;
60-
private final SubmodelFileOperations submodelFileOperations;
6161

62-
public CrudSubmodelRepository(SubmodelBackend submodelBackend, FileRepository fileRepository, String submodelRepositoryName) {
62+
public CrudSubmodelRepository(SubmodelBackend submodelBackend, SubmodelServiceFactory submodelServiceFactory, String submodelRepositoryName) {
6363
this.submodelBackend = submodelBackend;
64-
this.submodelFileOperations = new SubmodelFileOperations(fileRepository, submodelBackend);
64+
this.submodelServiceFactory = submodelServiceFactory;
6565
this.submodelRepositoryName = submodelRepositoryName;
6666

6767
}
6868

69-
public CrudSubmodelRepository(SubmodelBackend submodelBackend, FileRepository fileRepository, String submodelRepositoryName,
69+
public CrudSubmodelRepository(SubmodelBackend submodelBackend, SubmodelServiceFactory submodelServiceFactory, String submodelRepositoryName,
7070
Collection<Submodel> submodels) {
71-
this(submodelBackend, fileRepository, submodelRepositoryName);
71+
this(submodelBackend, submodelServiceFactory, submodelRepositoryName);
7272

7373
initializeRemoteCollection(submodels);
7474
}
@@ -143,51 +143,47 @@ public void deleteSubmodel(String submodelId) throws ElementDoesNotExistExceptio
143143

144144
@Override
145145
public CursorResult<List<SubmodelElement>> getSubmodelElements(String submodelId, PaginationInfo pInfo) throws ElementDoesNotExistException {
146-
return submodelBackend.getSubmodelElements(submodelId, pInfo);
146+
return getService(submodelId).getSubmodelElements(pInfo);
147147
}
148148

149149
@Override
150150
public SubmodelElement getSubmodelElement(String submodelId, String smeIdShortPath) throws ElementDoesNotExistException {
151-
return submodelBackend.getSubmodelElement(submodelId, smeIdShortPath);
151+
return getService(submodelId).getSubmodelElement(smeIdShortPath);
152152
}
153153

154154
@Override
155155
public SubmodelElementValue getSubmodelElementValue(String submodelId, String smeIdShort) throws ElementDoesNotExistException {
156-
return submodelBackend.getSubmodelElementValue(submodelId, smeIdShort);
156+
return getService(submodelId).getSubmodelElementValue(smeIdShort);
157157
}
158158

159159
@Override
160160
public void setSubmodelElementValue(String submodelId, String smeIdShort, SubmodelElementValue value) throws ElementDoesNotExistException {
161-
submodelBackend.setSubmodelElementValue(submodelId, smeIdShort, value);
161+
getService(submodelId).setSubmodelElementValue(smeIdShort, value);
162162
}
163163

164164
@Override
165165
public void createSubmodelElement(String submodelId, SubmodelElement smElement) {
166-
submodelBackend.createSubmodelElement(submodelId, smElement);
166+
getService(submodelId).createSubmodelElement(smElement);
167167
}
168168

169169
@Override
170170
public void createSubmodelElement(String submodelId, String idShortPath, SubmodelElement smElement) throws ElementDoesNotExistException {
171-
submodelBackend.createSubmodelElement(submodelId, idShortPath, smElement);
171+
getService(submodelId).createSubmodelElement(idShortPath, smElement);
172172
}
173173

174174
@Override
175175
public void updateSubmodelElement(String submodelId, String idShortPath, SubmodelElement submodelElement) throws ElementDoesNotExistException {
176-
deleteAssociatedFileIfAny(submodelId, idShortPath);
177-
178-
submodelBackend.updateSubmodelElement(submodelId, idShortPath, submodelElement);
176+
getService(submodelId).updateSubmodelElement(idShortPath, submodelElement);
179177
}
180178

181179
@Override
182180
public void deleteSubmodelElement(String submodelId, String idShortPath) throws ElementDoesNotExistException {
183-
deleteAssociatedFileIfAny(submodelId, idShortPath);
184-
185-
submodelBackend.deleteSubmodelElement(submodelId, idShortPath);
181+
getService(submodelId).deleteSubmodelElement(idShortPath);
186182
}
187183

188184
@Override
189185
public OperationVariable[] invokeOperation(String submodelId, String idShortPath, OperationVariable[] input) throws ElementDoesNotExistException {
190-
throw new NotInvokableException(idShortPath);
186+
return getService(submodelId).invokeOperation(idShortPath, input);
191187
}
192188

193189
@Override
@@ -197,35 +193,34 @@ public SubmodelValueOnly getSubmodelByIdValueOnly(String submodelId) throws Elem
197193

198194
@Override
199195
public Submodel getSubmodelByIdMetadata(String submodelId) throws ElementDoesNotExistException {
200-
201196
Submodel submodel = getSubmodel(submodelId);
202197

203198
return SubmodelMetadataUtil.extractMetadata(submodel);
204199
}
205200

206201
@Override
207202
public java.io.File getFileByPathSubmodel(String submodelId, String idShortPath) throws ElementDoesNotExistException, ElementNotAFileException, FileDoesNotExistException {
208-
return submodelFileOperations.getFile(submodelId, idShortPath);
203+
return getService(submodelId).getFileByPath(idShortPath);
209204
}
210205

211206
@Override
212207
public void setFileValue(String submodelId, String idShortPath, String fileName, InputStream inputStream) throws ElementDoesNotExistException, ElementNotAFileException {
213-
submodelFileOperations.setFileValue(submodelId, idShortPath, fileName, inputStream);
208+
getService(submodelId).setFileValue(idShortPath, fileName, inputStream);
214209
}
215210

216211
@Override
217212
public void deleteFileValue(String submodelId, String idShortPath) throws ElementDoesNotExistException, ElementNotAFileException, FileDoesNotExistException {
218-
submodelFileOperations.deleteFileValue(submodelId, idShortPath);
213+
getService(submodelId).deleteFileValue(idShortPath);
219214
}
220215

221216
@Override
222217
public void patchSubmodelElements(String submodelId, List<SubmodelElement> submodelElementList) {
223-
submodelBackend.patchSubmodelElements(submodelId, submodelElementList);
218+
getService(submodelId).patchSubmodelElements(submodelElementList);
224219
}
225220

226221
@Override
227222
public InputStream getFileByFilePath(String submodelId, String filePath) {
228-
return submodelFileOperations.getInputStream(filePath);
223+
return getService(submodelId).getFileByFilePath(filePath);
229224
}
230225

231226
private void initializeRemoteCollection(@NonNull Collection<Submodel> submodels) {
@@ -274,11 +269,8 @@ private void throwIfSubmodelDoesNotExist(String submodelId) {
274269
throw new ElementDoesNotExistException(submodelId);
275270
}
276271

277-
private void deleteAssociatedFileIfAny(String submodelId, String idShortPath) {
278-
try {
279-
submodelFileOperations.deleteFileValue(submodelId, idShortPath);
280-
} catch (Exception e) {
281-
}
272+
private SubmodelService getService(String submodelId) {
273+
return submodelServiceFactory.create(submodelId);
282274
}
283275

284276
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
/*******************************************************************************
22
* Copyright (C) 2024 the Eclipse BaSyx Authors
3-
*
3+
*
44
* Permission is hereby granted, free of charge, to any person obtaining
55
* a copy of this software and associated documentation files (the
66
* "Software"), to deal in the Software without restriction, including
77
* without limitation the rights to use, copy, modify, merge, publish,
88
* distribute, sublicense, and/or sell copies of the Software, and to
99
* permit persons to whom the Software is furnished to do so, subject to
1010
* the following conditions:
11-
*
11+
*
1212
* The above copyright notice and this permission notice shall be
1313
* included in all copies or substantial portions of the Software.
14-
*
14+
*
1515
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1616
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1717
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1818
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
1919
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
2020
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2121
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-
*
22+
*
2323
* SPDX-License-Identifier: MIT
2424
******************************************************************************/
2525

@@ -29,6 +29,8 @@
2929
import org.eclipse.digitaltwin.basyx.core.filerepository.FileRepository;
3030
import org.eclipse.digitaltwin.basyx.submodelrepository.SubmodelRepository;
3131
import org.eclipse.digitaltwin.basyx.submodelrepository.SubmodelRepositoryFactory;
32+
import org.eclipse.digitaltwin.basyx.submodelservice.SubmodelServiceFactory;
33+
import org.eclipse.digitaltwin.basyx.submodelservice.backend.CrudSubmodelServiceFactory;
3234
import org.eclipse.digitaltwin.basyx.submodelservice.backend.SubmodelBackend;
3335
import org.springframework.beans.factory.annotation.Autowired;
3436
import org.springframework.beans.factory.annotation.Value;
@@ -41,40 +43,48 @@
4143
/**
4244
* Simple Submodel repository factory that creates a
4345
* {@link CrudSubmodelRepository} with a backend provider and a service factory
44-
*
46+
*
4547
* @author mateusmolina, danish
46-
*
4748
*/
4849
@Component
4950
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${basyx.backend:}')")
5051
public class CrudSubmodelRepositoryFactory implements SubmodelRepositoryFactory {
5152

52-
static final String DEFAULT_REPOSITORY_NAME = "sm-repo";
53+
static final String DEFAULT_REPOSITORY_NAME = "sm-repo";
5354

54-
private final SubmodelBackend backend;
55-
private final FileRepository fileRepository;
56-
private final String submodelRepositoryName;
57-
private Optional<Collection<Submodel>> submodels = Optional.empty();
55+
private final SubmodelBackend backend;
56+
private final SubmodelServiceFactory submodelServiceFactory;
57+
private final String submodelRepositoryName;
58+
private Optional<Collection<Submodel>> submodels = Optional.empty();
5859

59-
@Autowired
60-
public CrudSubmodelRepositoryFactory(SubmodelBackend submodelRepositoryBackend, FileRepository fileRepository, @Value("${basyx.smrepo.name:" + DEFAULT_REPOSITORY_NAME + "}") String submodelRepositoryName) {
61-
this.backend = submodelRepositoryBackend;
62-
this.fileRepository = fileRepository;
63-
this.submodelRepositoryName = submodelRepositoryName;
64-
}
60+
@Autowired
61+
public CrudSubmodelRepositoryFactory(SubmodelBackend submodelRepositoryBackend, SubmodelServiceFactory submodelServiceFactory, @Value("${basyx.smrepo.name:" + DEFAULT_REPOSITORY_NAME + "}") String submodelRepositoryName) {
62+
this.backend = submodelRepositoryBackend;
63+
this.submodelServiceFactory = submodelServiceFactory;
64+
this.submodelRepositoryName = submodelRepositoryName;
65+
}
6566

66-
public CrudSubmodelRepositoryFactory(SubmodelBackend submodelBackend, FileRepository fileRepository) {
67-
this(submodelBackend, fileRepository, DEFAULT_REPOSITORY_NAME);
68-
}
67+
public CrudSubmodelRepositoryFactory(SubmodelBackend submodelBackend, SubmodelServiceFactory submodelServiceFactory) {
68+
this(submodelBackend, submodelServiceFactory, DEFAULT_REPOSITORY_NAME);
69+
}
6970

70-
public CrudSubmodelRepositoryFactory withRemoteCollection(Collection<Submodel> submodels) {
71-
this.submodels = Optional.of(submodels);
72-
return this;
71+
public CrudSubmodelRepositoryFactory(SubmodelBackend submodelBackend, FileRepository fileRepository) {
72+
this(submodelBackend, new CrudSubmodelServiceFactory(submodelBackend, fileRepository));
7373
}
7474

75-
@Override
76-
public SubmodelRepository create() {
77-
return submodels.map(submodelCollection -> new CrudSubmodelRepository(backend, fileRepository, submodelRepositoryName, submodelCollection)).orElseGet(() -> new CrudSubmodelRepository(backend, fileRepository, submodelRepositoryName));
75+
public CrudSubmodelRepositoryFactory(SubmodelBackend submodelBackend, FileRepository fileRepository, String submodelRepositoryName) {
76+
this(submodelBackend, new CrudSubmodelServiceFactory(submodelBackend, fileRepository), submodelRepositoryName);
77+
}
78+
79+
public CrudSubmodelRepositoryFactory withRemoteCollection(Collection<Submodel> submodels) {
80+
this.submodels = Optional.of(submodels);
81+
return this;
82+
}
83+
84+
@Override
85+
public SubmodelRepository create() {
86+
return submodels.map(submodelCollection -> new CrudSubmodelRepository(backend, submodelServiceFactory, submodelRepositoryName, submodelCollection))
87+
.orElseGet(() -> new CrudSubmodelRepository(backend, submodelServiceFactory, submodelRepositoryName));
7888
}
7989

8090
}

basyx.submodelrepository/basyx.submodelrepository.component/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/component/SubmodelRepositoryConfiguration.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ public SubmodelRepository getSubmodelRepository(SubmodelRepositoryFactory aasRep
5656
return new DecoratedSubmodelRepositoryFactory(aasRepositoryFactory, features).create();
5757
}
5858

59+
@Primary
60+
@Bean
61+
public SubmodelServiceFactory getSubmodelServiceFactory(SubmodelServiceFactory aasServiceFactory, List<SubmodelServiceFeature> features) {
62+
return new DecoratedSubmodelServiceFactory(aasServiceFactory, features);
63+
}
5964
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ public class CrudSubmodelService implements SubmodelService {
2626
private final SubmodelFileOperations submodelFileOperations;
2727

2828
public CrudSubmodelService(SubmodelBackend submodelRepositoryBackend, FileRepository fileRepository, @NonNull Submodel submodel) {
29+
this(submodelRepositoryBackend, fileRepository, submodel.getIdShort());
30+
}
31+
32+
public CrudSubmodelService(SubmodelBackend submodelRepositoryBackend, FileRepository fileRepository, @NonNull String submodelId) {
2933
this.backend = submodelRepositoryBackend;
30-
this.submodelId = submodel.getId();
34+
this.submodelId = submodelId;
3135
this.submodelFileOperations = new SubmodelFileOperations(fileRepository, submodelRepositoryBackend);
32-
hostSubmodel(submodel);
3336
}
3437

3538
@Override

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@ public CrudSubmodelServiceFactory(SubmodelBackend backend, FileRepository fileRe
2323
public SubmodelService create(Submodel submodel) {
2424
return new CrudSubmodelService(backend, fileRepository, submodel);
2525
}
26-
26+
27+
@Override
28+
public SubmodelService create(String submodelId) {
29+
return new CrudSubmodelService(backend, fileRepository, submodelId);
30+
}
31+
2732
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,14 @@ public interface SubmodelServiceFactory {
4242
* @return
4343
*/
4444
public SubmodelService create(Submodel submodel);
45+
46+
/**
47+
* Creates a new SubmodelService containing the Submodel with the given id
48+
*
49+
* The submodel is assumed to be already stored in the backend
50+
*
51+
* @param submodelId
52+
* @return the created SubmodelService
53+
*/
54+
public SubmodelService create(String submodelId);
4555
}

basyx.submodelservice/basyx.submodelservice-core/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/feature/DecoratedSubmodelServiceFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@ public DecoratedSubmodelServiceFactory(SubmodelServiceFactory toDecorate, List<S
4848
public SubmodelService create(Submodel submodel) {
4949
return getDecorated().create(submodel);
5050
}
51+
52+
@Override
53+
public SubmodelService create(String submodelId) {
54+
return getDecorated().create(submodelId);
55+
}
5156
}

basyx.submodelservice/basyx.submodelservice-feature-mqtt/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/feature/mqtt/MqttSubmodelServiceFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@ public MqttSubmodelServiceFactory(SubmodelServiceFactory decorated, IMqttClient
5151
public SubmodelService create(Submodel submodel) {
5252
return new MqttSubmodelService(decorated.create(submodel), client, topicFactory);
5353
}
54+
55+
@Override
56+
public SubmodelService create(String submodelId) {
57+
return new MqttSubmodelService(decorated.create(submodelId), client, topicFactory);
58+
}
5459
}

basyx.submodelservice/basyx.submodelservice-feature-operation-dispatching/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/feature/operationdispatching/OperationDispatchingServiceFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ public OperationDispatchingServiceFactory(SubmodelServiceFactory decorated, Oper
4747
public SubmodelService create(Submodel submodel) {
4848
return new OperationDispatcherSubmodelService(decorated.create(submodel), executorProvider);
4949
}
50+
51+
@Override
52+
public SubmodelService create(String submodelId) {
53+
return new OperationDispatcherSubmodelService(decorated.create(submodelId), executorProvider);
54+
}
5055
}

0 commit comments

Comments
 (0)