summaryrefslogtreecommitdiffstats
path: root/src/main/java/nu/xss/jpa/dao
diff options
context:
space:
mode:
authorSebastian2012-08-24 12:06:20 +0200
committerSebastian2012-08-24 12:06:20 +0200
commit85490a5f712c89a27fb8aaea10dd634af30b65bd (patch)
tree860a994e28446f90e4c90c94f37e3550f6baeb44 /src/main/java/nu/xss/jpa/dao
parentadd support for filter, change sorting/pagination api, cleanup (diff)
downloadxss-jpa-85490a5f712c89a27fb8aaea10dd634af30b65bd.tar.gz
xss-jpa-85490a5f712c89a27fb8aaea10dd634af30b65bd.tar.xz
xss-jpa-85490a5f712c89a27fb8aaea10dd634af30b65bd.zip
add checkstyle, javadoc
Diffstat (limited to 'src/main/java/nu/xss/jpa/dao')
-rw-r--r--src/main/java/nu/xss/jpa/dao/Dao.java175
-rw-r--r--src/main/java/nu/xss/jpa/dao/GenericJpaDao.java194
-rw-r--r--src/main/java/nu/xss/jpa/dao/package-info.java4
3 files changed, 314 insertions, 59 deletions
diff --git a/src/main/java/nu/xss/jpa/dao/Dao.java b/src/main/java/nu/xss/jpa/dao/Dao.java
index 86444f4..c360667 100644
--- a/src/main/java/nu/xss/jpa/dao/Dao.java
+++ b/src/main/java/nu/xss/jpa/dao/Dao.java
@@ -1,3 +1,23 @@
+/**
+ * This file is part of xss-jpa.
+ *
+ * Copyright 2012 Sebastian Schmelzer
+ * http://xss.nu/
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
package nu.xss.jpa.dao;
import java.util.List;
@@ -6,24 +26,143 @@ import nu.xss.jpa.query.Pagination;
import nu.xss.jpa.query.Sort;
import nu.xss.jpa.query.filter.Filter;
+/**
+ * The Interface Dao.
+ *
+ * @param <E>
+ * the element type
+ * @param <K>
+ * the key type
+ */
public interface Dao<E, K> {
- void save(E entity);
- void merge(E entity);
- void delete(E entity);
-
- E findById(K id);
-
- int count();
- int count(Filter... filter);
-
- List<E> findAll();
- List<E> findAll(Sort sort);
- List<E> findAll(Pagination pagination);
- List<E> findAll(Sort sort, Pagination pagination);
-
- List<E> findAll(Filter... filter);
- List<E> findAll(Sort sort, Filter... filter);
- List<E> findAll(Pagination pagination, Filter... filter);
- List<E> findAll(Sort sort, Pagination pagination, Filter... filter);
+
+ /**
+ * Save.
+ *
+ * @param entity
+ * the entity
+ */
+ void save(E entity);
+
+ /**
+ * Merge.
+ *
+ * @param entity
+ * the entity
+ */
+ void merge(E entity);
+
+ /**
+ * Delete.
+ *
+ * @param entity
+ * the entity
+ */
+ void delete(E entity);
+
+ /**
+ * Find by id.
+ *
+ * @param id
+ * the id
+ * @return the e
+ */
+ E findById(K id);
+
+ /**
+ * Count.
+ *
+ * @return the int
+ */
+ int count();
+
+ /**
+ * Count.
+ *
+ * @param filter
+ * the filter
+ * @return the int
+ */
+ int count(Filter... filter);
+
+ /**
+ * Find all.
+ *
+ * @return the list
+ */
+ List<E> findAll();
+
+ /**
+ * Find all.
+ *
+ * @param sort
+ * the sort
+ * @return the list
+ */
+ List<E> findAll(Sort sort);
+
+ /**
+ * Find all.
+ *
+ * @param pagination
+ * the pagination
+ * @return the list
+ */
+ List<E> findAll(Pagination pagination);
+
+ /**
+ * Find all.
+ *
+ * @param sort
+ * the sort
+ * @param pagination
+ * the pagination
+ * @return the list
+ */
+ List<E> findAll(Sort sort, Pagination pagination);
+
+ /**
+ * Find all.
+ *
+ * @param filter
+ * the filter
+ * @return the list
+ */
+ List<E> findAll(Filter... filter);
+
+ /**
+ * Find all.
+ *
+ * @param sort
+ * the sort
+ * @param filter
+ * the filter
+ * @return the list
+ */
+ List<E> findAll(Sort sort, Filter... filter);
+
+ /**
+ * Find all.
+ *
+ * @param pagination
+ * the pagination
+ * @param filter
+ * the filter
+ * @return the list
+ */
+ List<E> findAll(Pagination pagination, Filter... filter);
+
+ /**
+ * Find all.
+ *
+ * @param sort
+ * the sort
+ * @param pagination
+ * the pagination
+ * @param filter
+ * the filter
+ * @return the list
+ */
+ List<E> findAll(Sort sort, Pagination pagination, Filter... filter);
}
diff --git a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java
index 0c06505..20be33f 100644
--- a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java
+++ b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java
@@ -1,3 +1,23 @@
+/**
+ * This file is part of xss-jpa.
+ *
+ * Copyright 2012 Sebastian Schmelzer
+ * http://xss.nu/
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
package nu.xss.jpa.dao;
import java.io.Serializable;
@@ -25,17 +45,33 @@ import nu.xss.jpa.query.filter.Filter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+/**
+ * The Class GenericJpaDao.
+ *
+ * @param <E>
+ * the element type
+ * @param <K>
+ * the key type
+ */
public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
Dao<E, K>, Serializable {
+ /** The Constant serialVersionUID. */
private static final long serialVersionUID = 4998055731089977476L;
- protected final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+ /** The logger. */
+ private Logger logger = LoggerFactory.getLogger(this.getClass());
+ /** The entity manager. */
@PersistenceContext
- protected EntityManager entityManager;
+ private EntityManager entityManager;
- protected Class<E> entity;
+ /** The entity. */
+ private Class<E> entityClass;
+ /**
+ * Instantiates a new generic jpa dao.
+ */
@SuppressWarnings("unchecked")
public GenericJpaDao() {
ParameterizedType genericSuperclass = (ParameterizedType) getClass()
@@ -44,23 +80,26 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
if (type instanceof Class) {
- this.entity = (Class<E>) type;
+ this.entityClass = (Class<E>) type;
} else if (type instanceof ParameterizedType) {
- this.entity = (Class<E>) ((ParameterizedType)type).getRawType();
+ this.entityClass = (Class<E>) ((ParameterizedType) type).getRawType();
}
}
- public void save(E entity) {
+ @Override
+ public void save(final E entity) {
entityManager.persist(entity);
logger.info("Saved entity: {}.", entity.toString());
}
- public void merge(E entity) {
+ @Override
+ public void merge(final E entity) {
entityManager.merge(entity);
logger.info("Updated unmanaged entity: {}.", entity.toString());
}
- public void delete(E entity) {
+ @Override
+ public void delete(final E entity) {
final E persistentEntity = findById(entity.getId());
if (persistentEntity != null) {
entityManager.remove(persistentEntity);
@@ -68,8 +107,9 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
logger.info("Deleted entity: {}.", entity.toString());
}
- public E findById(K id) {
- return entityManager.find(entity, id);
+ @Override
+ public final E findById(final K id) {
+ return entityManager.find(entityClass, id);
}
@Override
@@ -79,73 +119,114 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
}
@Override
- public int count(Filter... filter) {
+ public int count(final Filter... filter) {
// TODO Auto-generated method stub
return 0;
}
@Override
- public List<E> findAll() {
+ public final List<E> findAll() {
return find(null, null, null);
}
@Override
- public List<E> findAll(Sort sort) {
+ public final List<E> findAll(final Sort sort) {
return find(null, sort, null);
}
@Override
- public List<E> findAll(Pagination pagination) {
+ public final List<E> findAll(final Pagination pagination) {
return find(null, null, pagination);
}
@Override
- public List<E> findAll(Sort sort, Pagination pagination) {
+ public final List<E> findAll(final Sort sort, final Pagination pagination) {
return find(null, sort, pagination);
}
@Override
- public List<E> findAll(Filter... filter) {
+ public final List<E> findAll(final Filter... filter) {
return find(null, null, null, filter);
}
@Override
- public List<E> findAll(Sort sort, Filter... filter) {
+ public final List<E> findAll(final Sort sort, final Filter... filter) {
return find(null, sort, null, filter);
}
@Override
- public List<E> findAll(Pagination pagination, Filter... filter) {
+ public final List<E> findAll(final Pagination pagination, final Filter... filter) {
return find(null, null, pagination, filter);
}
@Override
- public List<E> findAll(Sort sort, Pagination pagination, Filter... filter) {
+ public final List<E> findAll(final Sort sort, final Pagination pagination, final Filter... filter) {
return find(null, sort, pagination, filter);
}
- protected List<E> find(CriteriaQuery<E> query) {
+ /**
+ * Find.
+ *
+ * @param query
+ * the query
+ * @return the list
+ */
+ protected final List<E> find(final CriteriaQuery<E> query) {
return find(query, null, null);
}
- protected List<E> find(CriteriaQuery<E> query, Pagination pagination) {
+ /**
+ * Find.
+ *
+ * @param query
+ * the query
+ * @param pagination
+ * the pagination
+ * @return the list
+ */
+ protected final List<E> find(final CriteriaQuery<E> query, final Pagination pagination) {
return find(query, null, pagination);
}
- protected List<E> find(CriteriaQuery<E> query, Sort sort) {
+ /**
+ * Find.
+ *
+ * @param query
+ * the query
+ * @param sort
+ * the sort
+ * @return the list
+ */
+ protected final List<E> find(final CriteriaQuery<E> query, final Sort sort) {
return find(query, sort, null);
}
- protected List<E> find(CriteriaQuery<E> query, Sort sort, Pagination pagination, Filter... filter) {
+ /**
+ * Find.
+ *
+ * @param query
+ * the query
+ * @param sort
+ * the sort
+ * @param pagination
+ * the pagination
+ * @param filter
+ * the filter
+ * @return the list
+ */
+ protected List<E> find(final CriteriaQuery<E> query, final Sort sort, final Pagination pagination,
+ final Filter... filter) {
+
Root<E> root;
+ CriteriaQuery<E> q = query;
// check if we have a 'simple' query
- if (query == null) {
- query = createQuery();
+ if (q == null) {
+ q = createQuery();
}
- root = query.from(this.entity);
- query.select(root);
+ root = q.from(this.entityClass);
+ q.select(root);
if (filter != null) {
Set<Predicate> predicates = new HashSet<Predicate>();
@@ -165,31 +246,45 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
// TODO: Multcolum sort
if (sort != null) {
if (sort.isAsc()) {
- query.orderBy(getCriteriaBuilder().asc(root.get(sort.getColumn())));
+ q.orderBy(getCriteriaBuilder().asc(root.get(sort.getColumn())));
} else {
- query.orderBy(getCriteriaBuilder().desc(root.get(sort.getColumn())));
+ q.orderBy(getCriteriaBuilder().desc(root.get(sort.getColumn())));
}
}
// Result pagination
- TypedQuery<E> q = entityManager.createQuery(query);
+ TypedQuery<E> tq = entityManager.createQuery(q);
if (pagination != null) {
- if ( pagination.getCount() > 0 ) {
- q.setFirstResult(pagination.getCount());
+ if (pagination.getCount() > 0) {
+ tq.setFirstResult(pagination.getCount());
}
- if ( pagination.getOffset() > 0) {
- q.setMaxResults(pagination.getOffset());
+ if (pagination.getOffset() > 0) {
+ tq.setMaxResults(pagination.getOffset());
}
}
- return q.getResultList();
+ return tq.getResultList();
}
- protected E findSingle(CriteriaQuery<E> query) {
+ /**
+ * Find single.
+ *
+ * @param query
+ * the query
+ * @return the e
+ */
+ protected final E findSingle(final CriteriaQuery<E> query) {
return entityManager.createQuery(query).getSingleResult();
}
- protected E findSingleOrNull(CriteriaQuery<E> query) {
+ /**
+ * Find single or null.
+ *
+ * @param query
+ * the query
+ * @return the e
+ */
+ protected final E findSingleOrNull(final CriteriaQuery<E> query) {
try {
return findSingle(query);
} catch (final NoResultException e) {
@@ -200,7 +295,14 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
}
}
- protected E findSingleFirstOrNull(CriteriaQuery<E> query) {
+ /**
+ * Find single first or null.
+ *
+ * @param query
+ * the query
+ * @return the e
+ */
+ protected final E findSingleFirstOrNull(final CriteriaQuery<E> query) {
try {
return findSingle(query);
} catch (final NoResultException e) {
@@ -212,12 +314,22 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
}
- protected CriteriaBuilder getCriteriaBuilder() {
+ /**
+ * Gets the criteria builder.
+ *
+ * @return the criteria builder
+ */
+ protected final CriteriaBuilder getCriteriaBuilder() {
return entityManager.getCriteriaBuilder();
}
- protected CriteriaQuery<E> createQuery() {
- CriteriaQuery<E> c = getCriteriaBuilder().createQuery(this.entity);
+ /**
+ * Creates the query.
+ *
+ * @return the criteria query
+ */
+ protected final CriteriaQuery<E> createQuery() {
+ CriteriaQuery<E> c = getCriteriaBuilder().createQuery(this.entityClass);
return c;
}
diff --git a/src/main/java/nu/xss/jpa/dao/package-info.java b/src/main/java/nu/xss/jpa/dao/package-info.java
new file mode 100644
index 0000000..7b478b0
--- /dev/null
+++ b/src/main/java/nu/xss/jpa/dao/package-info.java
@@ -0,0 +1,4 @@
+/**
+ * Provides...
+ */
+package nu.xss.jpa.dao; \ No newline at end of file