org.fryske_akademy.ejb.AbstractCrudService Maven / Gradle / Ivy
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.fryske_akademy.ejb;
/*-
* #%L
* ejbCrudApi
* %%
* Copyright (C) 2018 Fryske Akademy
* %%
* 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.
* #L%
*/
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.TransactionAttribute;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
/**
*
* This base class enables you to create minimal crud service beans. All you
* have to do is override, implement getEntityManager and add annotation @Stateless, @Local(CrudReadService.class, CrudWriteService.class).
*
* Inject the crud interfaces using @Inject in your frontend (problems with
* inhertitance and @EJB).
*
This base class declares appropriate transactions and an EDITORROLE role to
protect write operations.
*
* An injected {@link JpqlBuilder} is used for building where and order by
* clauses in dynamic queries, i.e. used in
* {@link #findDynamic(java.lang.Integer, java.lang.Integer, java.util.Map, java.util.List, java.lang.Class) }.
* The JpqlBuilder is also used for setting parameter values.
*
* @author eduard
*/
@DeclareRoles("editor")
@TransactionAttribute
public abstract class AbstractCrudService implements CrudReadService, CrudWriteService {
@EJB
private JpqlBuilder jpqlBuilder;
private static final Logger LOGGER = Logger.getLogger(AbstractCrudService.class.getName());
protected abstract EntityManager getEntityManager();
@Override
public T find(Serializable id, Class type) {
return getEntityManager().find(type, id);
}
@Override
public List findAll(Class type) {
CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(type));
return getEntityManager().createQuery(cq).getResultList();
}
/**
*
* @param
* @param first defaults to 0
* @param pageSize defaults to {@link #DEFAULT_PAGE_SIZE}, negative value
* means no limit
* @param sort
* @param params
* @param type
* @return
*/
@Override
public List findDynamic(Integer first, Integer pageSize, Map sort, List params, Class type) {
String jpql = "from " + type.getSimpleName() + " e" + jpqlBuilder.whereClause(params) + jpqlBuilder.orderClause(sort);
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(jpql);
}
TypedQuery q = getEntityManager().createQuery(jpql, type);
jpqlBuilder.setWhereParams(q, params);
q.setFirstResult(first == null ? 0 : first);
if (!(pageSize < 0)) {
q.setMaxResults(pageSize == null ? DEFAULT_PAGE_SIZE : pageSize);
}
return (List) q.getResultList();
}
@Override
public int countDynamic(List params, Class type) {
String jpql = "select count(*) from " + type.getSimpleName() + " e" + jpqlBuilder.whereClause(params);
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(jpql);
}
return doCount(getEntityManager().createQuery(jpql, Long.class), params);
}
/**
*
* @param
* @param namedQuery
* @param params
* @param first defaults to 0
* @param max defaults to {@link #DEFAULT_PAGE_SIZE}, negative value means
* no limit
* @param type
* @return
*/
@Override
public List find(String namedQuery, List params, Integer first, Integer max, Class type) {
return doFind(getEntityManager().createNamedQuery(namedQuery, type), params, first, max);
}
/**
*
* @param
* @param namedNativeQuery
* @param params
* @param first
* @param max defaults to {@link #DEFAULT_PAGE_SIZE}, negative value means
* no limit
* @param type
* @return
*/
@Override
public List findNative(String namedNativeQuery, List params, Integer first, Integer max, Class type) {
return doFind(getEntityManager().createNamedQuery(namedNativeQuery), params, first, max);
}
@Override
public int count(String namedQuery, List params) {
return doCount(getEntityManager().createNamedQuery(namedQuery), params);
}
/**
* default page size (max results), used when no maximum is given.
*/
public static final int DEFAULT_PAGE_SIZE = 30;
@Override
public T findOne(String namedQuery, List params, Class type) {
TypedQuery q = getEntityManager().createNamedQuery(namedQuery, type);
jpqlBuilder.setWhereParams(q, params);
try {
return q.getSingleResult();
} catch (NoResultException e) {
return null;
}
}
public static final String EDITORROLE = "editor";
@Override
@RolesAllowed(value = {EDITORROLE})
public T create(T t) {
getEntityManager().persist(t);
return t;
}
@Override
@RolesAllowed(value = {EDITORROLE})
public T update(T t) {
return getEntityManager().merge(t);
}
@Override
@RolesAllowed(value = {EDITORROLE})
public void delete(Serializable t) {
getEntityManager().remove(getEntityManager().merge(t));
}
private List doFind(Query q, List params, Integer first, Integer max) {
jpqlBuilder.setWhereParams(q, params);
q.setFirstResult(first == null ? 0 : first);
if (max!=null) {
if (max>=0) {
q.setMaxResults(max);
}
} else {
q.setMaxResults(DEFAULT_PAGE_SIZE);
}
return q.getResultList();
}
private int doCount(Query q, List params) {
jpqlBuilder.setWhereParams(q, params);
return Integer.valueOf(String.valueOf(q.getSingleResult()));
}
}