
com.flowlogix.jeedao.impl.AbstractFacade Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of flowlogix-jee Show documentation
Show all versions of flowlogix-jee Show documentation
Flow Logix Components for Jakarta EE
/*
* Copyright 2014 lprimak.
*
* 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 com.flowlogix.jeedao.impl;
import com.flowlogix.jeedao.QueryCriteria;
import com.flowlogix.jeedao.TypedNativeQuery;
import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Delegate;
/**
* Base class for DAOs
*
* @author Petro
* @param Entity Type
* @param Primary Key Type
*/
public @RequiredArgsConstructor abstract class AbstractFacade
{
public AbstractFacade()
{
entityClass = null;
}
public void create(T entity)
{
getEntityManager().persist(entity);
}
public void edit(T entity)
{
getEntityManager().merge(entity);
}
public void remove(T entity)
{
getEntityManager().remove(getEntityManager().merge(entity));
}
public T find(I id)
{
return getEntityManager().find(getEntityClass(), id);
}
public List findAll()
{
CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(getEntityClass());
Root root = cq.from(getEntityClass());
cq.select(root);
addToCriteria(getEntityManager().getCriteriaBuilder(), root, cq);
TypedQuery tq = getEntityManager().createQuery(cq);
addHints(tq, false);
return tq.getResultList();
}
public List findRange(int[] range)
{
CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(getEntityClass());
Root root = cq.from(getEntityClass());
cq.select(root);
addToCriteria(getEntityManager().getCriteriaBuilder(), root, cq);
TypedQuery q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0]);
q.setFirstResult(range[0]);
addHints(q, true);
return q.getResultList();
}
public int count()
{
CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(Long.class);
Root rt = cq.from(getEntityClass());
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
addToCountCriteria(getEntityManager().getCriteriaBuilder(), rt, cq);
TypedQuery q = getEntityManager().createQuery(cq);
return q.getSingleResult().intValue();
}
protected QueryCriteria buildQueryCriteria()
{
return buildQueryCriteria(getEntityClass());
}
protected QueryCriteria buildQueryCriteria(Class cls)
{
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(cls);
return new QueryCriteria<>(cb, cq.from(cls), cq);
}
protected TypedNativeQuery createNativeQuery(String sql, Class resultClass)
{
Query q = getEntityManager().createNativeQuery(sql, resultClass);
return new TypedNativeQueryImpl<>(q);
}
protected TypedNativeQuery createNativeQuery(String sql, String resultMapping)
{
Query q = getEntityManager().createNativeQuery(sql, resultMapping);
return new TypedNativeQueryImpl<>(q);
}
/**
* Override the default entity class getter
* @return Entity class
*/
public Class getEntityClass()
{
if(entityClass == null)
{
throw new IllegalStateException("Entity Class not overridden and is not set");
}
return entityClass;
}
/**
* Return Entity Manager
* @return Entity Manager
*/
protected abstract EntityManager getEntityManager();
/**
* Add additional criteria
* @param cb
* @param root
* @param cq
*/
protected void addToCriteria(CriteriaBuilder cb, Root root, CriteriaQuery cq) { /* override */ };
/**
* Add additional criteria for count() operation
* @param cb
* @param root
* @param cq
*/
protected void addToCountCriteria(CriteriaBuilder cb, Root root, CriteriaQuery cq) { /* override */ };
/**
* add hints to query
* @param tq
* @param isRange
*/
protected void addHints(TypedQuery tq, boolean isRange) {}
public void checkForRequiredXA()
{
if(xaEnabled && !isXA())
{
throw new IllegalStateException("XA Transaction Required but not enabled - forgot to call markForXA(true)?");
}
}
/**
* mark transaction for XA
* @param tf True/False
*/
public void markForXA(boolean tf)
{
xaFlag.setXA(tf && xaEnabled);
}
private final Class entityClass;
private @Inject @Delegate XAFlag xaFlag;
private static final boolean xaEnabled = Boolean.getBoolean(AbstractFacade.XA_ENABLED_PROP);
public static final String XA_ENABLED_PROP = "com.flowlogix.XA_ENABLED";
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy