r625 - nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api
Author: bleny Date: 2013-05-24 19:35:29 +0200 (Fri, 24 May 2013) New Revision: 625 Url: http://nuiton.org/projects/sandbox/repository/revisions/625 Log: add some helper methods in AbstractJpaDao Modified: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/AbstractJpaDao.java Modified: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/AbstractJpaDao.java =================================================================== --- nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/AbstractJpaDao.java 2013-05-24 17:04:41 UTC (rev 624) +++ nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/AbstractJpaDao.java 2013-05-24 17:35:29 UTC (rev 625) @@ -23,9 +23,14 @@ * #L% */ +import org.apache.commons.lang3.StringUtils; + import javax.persistence.EntityManager; import javax.persistence.TypedQuery; +import java.util.HashMap; +import java.util.LinkedList; import java.util.List; +import java.util.Map; /** * This abstract class gather all code which is common to all daos for all entities. @@ -95,12 +100,52 @@ return newInstance; } + protected TypedQuery<E> createQuery(String propertyName, + Object propertyValue, + Object... others) { + + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put(propertyName, propertyValue); + Object name = null; + for (int i = 0; i < others.length; ) { + try { + name = others[i++]; + propertyValue = others[i++]; + properties.put((String) name, propertyValue); + } catch (ClassCastException eee) { + throw new IllegalArgumentException( + "Les noms des propriétés doivent être des chaines et " + + "non pas " + propertyName.getClass().getName(), + eee); + } catch (ArrayIndexOutOfBoundsException eee) { + throw new IllegalArgumentException( + "Le nombre d'argument n'est pas un nombre pair: " + + (others.length + 2) + + " La dernière propriété était: " + name, eee); + } + } + return createQuery(properties); + } + + protected TypedQuery<E> createQuery(Map<String, Object> properties) { + List<String> whereClauses = new LinkedList<String>(); + for (String propertyName : properties.keySet()) { + whereClauses.add(propertyName + " = :" + propertyName); + } + String hql = "from " + getEntityClass().getSimpleName() + " where " + StringUtils.join(whereClauses, " and "); + TypedQuery<E> query = createQuery(hql); + for (Map.Entry<String, Object> property : properties.entrySet()) { + query.setParameter(property.getKey(), property.getValue()); + } + return query; + } + protected TypedQuery<E> createQuery(String hql) { TypedQuery<E> query = entityManager.createQuery(hql, getEntityClass()); return query; } - protected List<E> findAll(TypedQuery<E> query) { + protected List<E> findAll(TypedQuery <E> query) { return query.getResultList(); } @@ -129,4 +174,9 @@ return onlyElement; } + protected long count(TypedQuery<E> query) { + // XXX brendan 24/05/13 worst way to do, use hql + return findAll(query).size(); + } + }
participants (1)
-
bleny@users.nuiton.org