de.fraunhofer.iosb.ilt.faaast.registry.jpa.util.EntityManagerHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of persistence-jpa Show documentation
Show all versions of persistence-jpa Show documentation
Relational database implementation of the FA³ST Registry.
The newest version!
/*
* Copyright (c) 2021 Fraunhofer IOSB, eine rechtlich nicht selbstaendige
* Einrichtung der Fraunhofer-Gesellschaft zur Foerderung der angewandten
* Forschung e.V.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.fraunhofer.iosb.ilt.faaast.registry.jpa.util;
import de.fraunhofer.iosb.ilt.faaast.registry.jpa.model.JpaAssetAdministrationShellDescriptor;
import de.fraunhofer.iosb.ilt.faaast.service.model.descriptor.AssetAdministrationShellDescriptor;
import jakarta.persistence.EntityManager;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.eclipse.digitaltwin.aas4j.v3.model.AssetKind;
/**
* Helper class for JPA objects.
*/
public class EntityManagerHelper {
private EntityManagerHelper() {}
/**
* Fetches all instances of a given type from the entityManager.
*
* @param the type to fetch
* @param entityManager the entityManager to use
* @param type the type to fetch
* @return all instances of given type
*/
public static List getAll(EntityManager entityManager, Class type) {
return getAll(entityManager, type, type);
}
/**
* Fetches all instances of a given type from the entityManager as a list of a desired return type.
*
* @param the return type
* @param the type to fetch
* @param entityManager the entityManager to use
* @param type the type to fetch
* @param returnType the type to return
* @return all instances of given type cast to return type
*/
public static List getAll(EntityManager entityManager, Class type, Class returnType) {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
var queryCriteria = builder.createQuery(type);
queryCriteria.select(queryCriteria.from(type));
var query = entityManager.createQuery(queryCriteria);
return query.getResultList().stream()
.map(returnType::cast)
.collect(Collectors.toList());
}
/**
* Fetches all instances of AssetAdministrationShellDescriptor, matching the given criteria.
*
* @param entityManager The entityManager to use.
* @param assetType The desired assetType.
* @param assetKind The desired assetKind.
* @return All instances matching the given criteria.
*/
public static List getAllAas(EntityManager entityManager, String assetType, AssetKind assetKind) {
if ((assetKind == null) && (assetType == null)) {
return getAll(entityManager, JpaAssetAdministrationShellDescriptor.class, AssetAdministrationShellDescriptor.class);
}
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
var queryCriteria = builder.createQuery(JpaAssetAdministrationShellDescriptor.class);
var root = queryCriteria.from(JpaAssetAdministrationShellDescriptor.class);
List predicates = new ArrayList<>();
if (assetType != null) {
predicates.add(builder.equal(root.get("assetType"), assetType));
}
if (assetKind != null) {
predicates.add(builder.equal(root.get("assetKind"), assetKind));
}
queryCriteria.select(root).where(predicates.toArray(Predicate[]::new));
var query = entityManager.createQuery(queryCriteria);
return query.getResultList().stream()
.map(AssetAdministrationShellDescriptor.class::cast)
.toList();
}
}