dao.EposDataModelDAO Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of db-api Show documentation
Show all versions of db-api Show documentation
EPOS Database APIs useful to interact with EPOS Metadata Catalogue
package dao;
import model.Versioningstatus;
import org.epos.handler.dbapi.service.DBService;
import jakarta.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;
public class EposDataModelDAO {
private final DBService entityManager;
public EposDataModelDAO(){
this.entityManager = new DBService();
}
public Boolean createObject(T entity) {
try {
EntityManager em = entityManager.getEntityManager();
em.getTransaction().begin();
em.persist(entity);
em.getTransaction().commit();
return true;
}catch (Exception exception){
System.err.println(exception.getLocalizedMessage());
return false;
}
}
public List getOneFromDBBySpecificKey(String key, String value, Class obj){
EntityManager em = entityManager.getEntityManager();
em.getTransaction().begin();
List resultList = em.createQuery(
"SELECT c FROM "+obj.getSimpleName()+" c WHERE c."+key+ " LIKE :value")
.setParameter("value", value)
.getResultList();
em.getTransaction().commit();
return resultList;
}
public List getOneFromDBByInstanceId(String instanceId, Class obj){
EntityManager em = entityManager.getEntityManager();
em.getTransaction().begin();
List resultList = em.createQuery(
"SELECT c FROM "+obj.getSimpleName()+" c WHERE c.instanceId LIKE :instanceId")
.setParameter("instanceId", instanceId)
.getResultList();
em.getTransaction().commit();
return resultList;
}
public List getOneFromDBByMetaId(String metaId, Class obj){
EntityManager em = entityManager.getEntityManager();
em.getTransaction().begin();
List resultList = em.createQuery(
"SELECT c FROM "+obj.getSimpleName()+" c WHERE c.metaId LIKE :metaId")
.setParameter("metaId", metaId)
.getResultList();
em.getTransaction().commit();
return resultList;
}
public List getOneFromDBByUID(String uid, Class obj){
EntityManager em = entityManager.getEntityManager();
em.getTransaction().begin();
List resultList = em.createQuery(
"SELECT c FROM "+obj.getSimpleName()+" c WHERE c.uid LIKE :uid")
.setParameter("uid", uid)
.getResultList();
em.getTransaction().commit();
return resultList;
}
public List getOneFromDBByVersionID(String versionId, Class obj){
EntityManager em = entityManager.getEntityManager();
em.getTransaction().begin();
List resultList = em.createQuery(
"SELECT c FROM "+obj.getSimpleName()+" c WHERE c.versionId LIKE :versionId")
.setParameter("versionId", versionId)
.getResultList();
em.getTransaction().commit();
return resultList;
}
public List getOneFromDB(String instanceId, String metaId, String uid, String versionId, Class obj){
List resultList = new ArrayList();
if(instanceId!=null) {
resultList.addAll(getOneFromDBByInstanceId(instanceId,obj));
return resultList;
}
if(metaId!=null) {
resultList.addAll(getOneFromDBByMetaId(metaId,obj));
return resultList;
}
if(uid!=null) {
resultList.addAll(getOneFromDBByUID(uid,obj));
return resultList;
}
if(versionId!=null) {
resultList.addAll(getOneFromDBByVersionID(versionId,obj));
return resultList;
}
return resultList;
}
public List getVersionsFromDBByVersionId(String versionId){
Versioningstatus objReturn = entityManager.getEntityManager().find(Versioningstatus.class, versionId);
return objReturn==null? List.of() : List.of(objReturn);
}
public List getAllFromDB(Class obj){
EntityManager em = entityManager.getEntityManager();
em.getTransaction().begin();
List resultList = em.createQuery(
"SELECT c FROM "+obj.getSimpleName()+" c")
.getResultList();
em.getTransaction().commit();
return resultList;
}
public Boolean updateObject(T obj) {
if (obj == null) return false;
try {
EntityManager em = entityManager.getEntityManager();
em.getTransaction().begin();
em.merge(obj);
em.getTransaction().commit();
return true;
}catch(Exception exception){
System.err.println(exception.getLocalizedMessage());
return false;
}
}
public Boolean deleteObject(T obj) {
try {
EntityManager em = entityManager.getEntityManager();
if (!em.contains(obj)) {
em.getTransaction().begin();
T target = em.merge(obj);
em.remove(target);
em.getTransaction().commit();
}
return true;
}catch(Exception exception){
System.err.println(exception.getLocalizedMessage());
return false;
}
}
}