use of io.adminshell.aas.v3.model.KeyElements in project FAAAST-Service by FraunhoferIOSB.
the class ReferablePersistenceManager method getSubmodelElements.
/**
* Get the submodel elements associated to the reference.
* Supported are two possible parents of submodel elements:
* <p>
* <ul>
* <li>{@link io.adminshell.aas.v3.model.Submodel}
* <li>{@link io.adminshell.aas.v3.model.SubmodelElementCollection}
* </ul>
* <p>
* If the semanticId is not null the submodel element list filtered by the semantic id
*
* @param reference to the submodel or submodel element collection
* @param semanticId of the submodel elements
* @return a list of the submodel elements associated to the parent reference
*/
public List<SubmodelElement> getSubmodelElements(Reference reference, Reference semanticId) throws ResourceNotFoundException {
if (reference == null) {
return null;
}
if (reference.getKeys() != null && reference.getKeys().size() > 0) {
List<SubmodelElement> submodelElements = null;
KeyElements lastKeyElementOfReference = reference.getKeys().get(reference.getKeys().size() - 1).getType();
if (lastKeyElementOfReference == KeyElements.SUBMODEL) {
Submodel submodel = AasUtils.resolve(reference, this.aasEnvironment, Submodel.class);
if (submodel == null) {
throw new ResourceNotFoundException(String.format("Resource not found with reference {}", AasUtils.asString(reference)));
}
Submodel deepCopiedSubmodel = DeepCopyHelper.deepCopy(submodel, submodel.getClass());
submodelElements = deepCopiedSubmodel.getSubmodelElements();
} else if (lastKeyElementOfReference == KeyElements.SUBMODEL_ELEMENT_COLLECTION) {
SubmodelElementCollection submodelElementCollection = AasUtils.resolve(reference, this.aasEnvironment, SubmodelElementCollection.class);
if (submodelElementCollection == null) {
throw new ResourceNotFoundException(String.format("Resource not found with reference {}", AasUtils.asString(reference)));
}
SubmodelElementCollection deepCopiedSubmodelElementCollection = DeepCopyHelper.deepCopy(submodelElementCollection, submodelElementCollection.getClass());
submodelElements = new ArrayList<>(deepCopiedSubmodelElementCollection.getValues());
}
if (semanticId != null) {
assert submodelElements != null;
submodelElements = submodelElements.stream().filter(x -> x.getSemanticId() != null && x.getSemanticId().equals(semanticId)).collect(Collectors.toList());
}
return submodelElements;
}
return null;
}
use of io.adminshell.aas.v3.model.KeyElements in project FAAAST-Service by FraunhoferIOSB.
the class ReferablePersistenceManager method putSubmodelElement.
/**
* Create or update a submodel element.
* Parent reference and reference of the submodel element must not both be null.
* Otherwise the location of the submodel element cannot be determined.
* Supported parent references could be references to a
* <ul>
* <li>{@link io.adminshell.aas.v3.model.Submodel} or to a
* <li>{@link io.adminshell.aas.v3.model.SubmodelElementCollection}
* </ul>
* To add a new submodel element give the parent reference and the submodel element.
* To update an existing submodel element give the reference to the submodel element and the submodel element.
*
* @param parent reference to the parent
* @param referenceToSubmodelElement reference to the submodel element
* @param submodelElement which should be updated or created
* @return the updated or created submodel element
*/
public SubmodelElement putSubmodelElement(Reference parent, Reference referenceToSubmodelElement, SubmodelElement submodelElement) throws ResourceNotFoundException {
if ((parent == null && referenceToSubmodelElement == null) || submodelElement == null) {
return null;
}
KeyElements lastKeyElementOfParent;
if (parent != null && parent.getKeys() != null && parent.getKeys().size() > 0) {
lastKeyElementOfParent = parent.getKeys().get(parent.getKeys().size() - 1).getType();
} else if (referenceToSubmodelElement != null && referenceToSubmodelElement.getKeys() != null && referenceToSubmodelElement.getKeys().size() > 1) {
lastKeyElementOfParent = referenceToSubmodelElement.getKeys().get(referenceToSubmodelElement.getKeys().size() - 2).getType();
parent = new DefaultReference.Builder().keys(referenceToSubmodelElement.getKeys().subList(0, referenceToSubmodelElement.getKeys().size() - 1)).build();
} else {
return null;
}
Predicate<SubmodelElement> filter = x -> x.getIdShort().equalsIgnoreCase(submodelElement.getIdShort());
if (lastKeyElementOfParent == KeyElements.SUBMODEL) {
Submodel submodel = AasUtils.resolve(parent, this.aasEnvironment, Submodel.class);
if (submodel == null) {
throw new ResourceNotFoundException(String.format("Resource not found with reference {}", AasUtils.asString(parent)));
}
submodel.getSubmodelElements().removeIf(filter);
submodel.getSubmodelElements().add(submodelElement);
} else if (lastKeyElementOfParent == KeyElements.SUBMODEL_ELEMENT_COLLECTION) {
SubmodelElementCollection submodelElementCollection = AasUtils.resolve(parent, this.aasEnvironment, SubmodelElementCollection.class);
if (submodelElementCollection == null) {
throw new ResourceNotFoundException(String.format("Resource not found with reference {}", AasUtils.asString(parent)));
}
submodelElementCollection.getValues().removeIf(filter);
submodelElementCollection.getValues().add(submodelElement);
} else {
return null;
}
return submodelElement;
}
use of io.adminshell.aas.v3.model.KeyElements in project FAAAST-Service by FraunhoferIOSB.
the class ReferablePersistenceManager method remove.
/**
* Remove a {@link io.adminshell.aas.v3.model.Referable}
*
* @param reference of the referable which should be removed
*/
public void remove(Reference reference) throws ResourceNotFoundException {
if (reference == null) {
return;
}
if (reference.getKeys() != null && reference.getKeys().size() > 0) {
KeyElements lastKeyElementOfReference = reference.getKeys().get(reference.getKeys().size() - 1).getType();
Class clazz = AasUtils.keyTypeToClass(lastKeyElementOfReference);
if (Identifiable.class.isAssignableFrom(clazz)) {
// TODO: Build Identifier and forward remove reuquest to remove(Identifier id)
return;
}
if (SubmodelElement.class.isAssignableFrom(clazz)) {
clazz = SubmodelElement.class;
}
Referable referable = AasUtils.resolve(reference, this.aasEnvironment);
if (referable == null) {
throw new ResourceNotFoundException(String.format("Resource not found with reference {}", AasUtils.asString(reference)));
}
if (reference.getKeys().size() > 1) {
Reference parent = new DefaultReference.Builder().keys(reference.getKeys().subList(0, reference.getKeys().size() - 1)).build();
Referable parentReferable = AasUtils.resolve(parent, this.aasEnvironment);
Method method = EnvironmentHelper.getGetReferableListMethod(clazz, parentReferable);
if (method != null) {
try {
List<Referable> referableList = (List<Referable>) method.invoke(parentReferable);
referableList.remove(referable);
} catch (InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
Aggregations