Author: athimel Date: 2013-12-18 10:48:00 +0100 (Wed, 18 Dec 2013) New Revision: 2927 Url: http://nuiton.org/projects/topia/repository/revisions/2927 Log: fixes #2970 Remove unecessary hardcoded or parameterized types in AbstractTopiaDao Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/AbstractTopiaDao.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/AbstractTopiaDao.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/AbstractTopiaDao.java 2013-12-17 16:02:49 UTC (rev 2926) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/AbstractTopiaDao.java 2013-12-18 09:48:00 UTC (rev 2927) @@ -390,163 +390,96 @@ protected long count(String hql, Map<String, Object> hqlParameters) { Preconditions.checkArgument(hql.toLowerCase().trim().startsWith("select count(")); - Long result = findUnique(hql, hqlParameters, Long.class); + Long result = findUnique(hql, hqlParameters); return result; } - protected E findUnique(String hql, Map<String, Object> hqlParameters) throws TopiaNoResultException { - E result = findUnique(hql, hqlParameters, getEntityClass()); - return result; - } - - protected E findUniqueOrNull(String hql, Map<String, Object> hqlParameters) throws TopiaNonUniqueResultException { - E result = findUniqueOrNull(hql, hqlParameters, getEntityClass()); - return result; - } - - protected Optional<E> tryFindUnique(String hql, Map<String, Object> hqlParameters) { - Optional<E> result = tryFindUnique(hql, hqlParameters, getEntityClass()); - return result; - } - - protected E findFirst(String hql, Map<String, Object> hqlParameters) throws TopiaNoResultException { - E result = findFirst(hql, hqlParameters, getEntityClass()); - return result; - } - - protected E findFirstOrNull(String hql, Map<String, Object> hqlParameters) throws QueryMissingOrderException { - E result = findFirstOrNull(hql, hqlParameters, getEntityClass()); - return result; - } - - protected Optional<E> tryFindFirst(String hql, Map<String, Object> hqlParameters) throws QueryMissingOrderException { - Optional<E> result = tryFindFirst(hql, hqlParameters, getEntityClass()); - return result; - } - - protected E findAny(String hql, Map<String, Object> hqlParameters) throws TopiaNoResultException { - E result = findAny(hql, hqlParameters, getEntityClass()); - return result; - } - - protected E findAnyOrNull(String hql, Map<String, Object> hqlParameters) { - E result = findAnyOrNull(hql, hqlParameters, getEntityClass()); - return result; - } - - protected Optional<E> tryFindAny(String hql, Map<String, Object> hqlParameters) { - Optional<E> result = tryFindAny(hql, hqlParameters, getEntityClass()); - return result; - } - - protected <R> R findUnique(String hql, Map<String, Object> hqlParameters, Class<R> type) throws TopiaNoResultException, TopiaNonUniqueResultException { - R result = findUniqueOrNull(hql, hqlParameters, type); + protected <K> K findUnique(String hql, Map<String, Object> hqlParameters) throws TopiaNoResultException, TopiaNonUniqueResultException { + K result = findUniqueOrNull(hql, hqlParameters); if (result == null) { throw new TopiaNoResultException(hql, hqlParameters); } return result; } - protected <R> Optional<R> tryFindUnique(String hql, Map<String, Object> hqlParameters, Class<R> type) throws TopiaNonUniqueResultException { - R uniqueOrNull = findUniqueOrNull(hql, hqlParameters, type); - Optional<R> result = Optional.fromNullable(uniqueOrNull); + protected <K> Optional<K> tryFindUnique(String hql, Map<String, Object> hqlParameters) throws TopiaNonUniqueResultException { + K uniqueOrNull = findUniqueOrNull(hql, hqlParameters); + Optional<K> result = Optional.fromNullable(uniqueOrNull); return result; } - protected <R> R findUniqueOrNull(String hql, Map<String, Object> hqlParameters, Class<R> type) throws TopiaNonUniqueResultException { - List<R> results = find(hql, hqlParameters, type, 0, 1); + protected <K> K findUniqueOrNull(String hql, Map<String, Object> hqlParameters) throws TopiaNonUniqueResultException { + List<K> results = find(hql, hqlParameters, 0, 1); // If there is more than 1 result, throw an exception if (results.size() > 1) { throw new TopiaNonUniqueResultException(hql, hqlParameters); } // otherwise return the first one, or null - R result = Iterables.getOnlyElement(results, null); + K result = Iterables.getOnlyElement(results, null); return result; } - protected <R> R findFirst(String hql, Map<String, Object> hqlParameters, Class<R> type) throws QueryMissingOrderException { - R result = findFirstOrNull(hql, hqlParameters, type); + protected <K> K findFirst(String hql, Map<String, Object> hqlParameters) throws QueryMissingOrderException { + K result = findFirstOrNull(hql, hqlParameters); if (result == null) { throw new TopiaNoResultException(hql, hqlParameters); } return result; } - protected <R> Optional<R> tryFindFirst(String hql, Map<String, Object> hqlParameters, Class<R> type) throws QueryMissingOrderException { - R firstOrNull = findFirstOrNull(hql, hqlParameters, type); - Optional<R> result = Optional.fromNullable(firstOrNull); + protected <K> Optional<K> tryFindFirst(String hql, Map<String, Object> hqlParameters) throws QueryMissingOrderException { + K firstOrNull = findFirstOrNull(hql, hqlParameters); + Optional<K> result = Optional.fromNullable(firstOrNull); return result; } - protected <R> R findFirstOrNull(String hql, Map<String, Object> hqlParameters, Class<R> type) throws QueryMissingOrderException { + protected <K> K findFirstOrNull(String hql, Map<String, Object> hqlParameters) throws QueryMissingOrderException { if ( ! hql.toLowerCase().contains("order by")) { throw new QueryMissingOrderException(hql, hqlParameters); } - R result = findAnyOrNull(hql, hqlParameters, type); + K result = findAnyOrNull(hql, hqlParameters); return result; } - protected <R> R findAny(String hql, Map<String, Object> hqlParameters, Class<R> type) throws TopiaNoResultException { - R result = findAnyOrNull(hql, hqlParameters, type); + protected <K> K findAny(String hql, Map<String, Object> hqlParameters) throws TopiaNoResultException { + K result = findAnyOrNull(hql, hqlParameters); if (result == null) { throw new TopiaNoResultException(hql, hqlParameters); } return result; } - protected <R> Optional<R> tryFindAny(String hql, Map<String, Object> hqlParameters, Class<R> type) { - R anyOrNull = findAnyOrNull(hql, hqlParameters, type); - Optional<R> result = Optional.fromNullable(anyOrNull); + protected <K> Optional<K> tryFindAny(String hql, Map<String, Object> hqlParameters) { + K anyOrNull = findAnyOrNull(hql, hqlParameters); + Optional<K> result = Optional.fromNullable(anyOrNull); return result; } - protected <R> R findAnyOrNull(String hql, Map<String, Object> hqlParameters, Class<R> type) { + protected <K> K findAnyOrNull(String hql, Map<String, Object> hqlParameters) { Preconditions.checkNotNull(hql); Preconditions.checkNotNull(hqlParameters); - List<R> results = find(hql, hqlParameters, type, 0, 0); - R result = Iterables.getOnlyElement(results, null); + List<K> results = find(hql, hqlParameters, 0, 0); + K result = Iterables.getOnlyElement(results, null); return result; } - protected List<E> findAll(String hql, Map<String, Object> hqlParameters) { + protected <K> List<K> findAll(String hql, Map<String, Object> hqlParameters) { Preconditions.checkNotNull(hql); Preconditions.checkNotNull(hqlParameters); - List<E> result = topiaJpaSupport.findAll(hql, hqlParameters); + List<K> result = topiaJpaSupport.findAll(hql, hqlParameters); return result; } - protected List<E> find(String hql, Map<String, Object> hqlParameters, int startIndex, int endIndex) { + protected <K> List<K> find(String hql, Map<String, Object> hqlParameters, int startIndex, int endIndex) { Preconditions.checkNotNull(hql); Preconditions.checkNotNull(hqlParameters); - List<E> result = topiaJpaSupport.find(hql, startIndex, endIndex, hqlParameters); + List<K> result = topiaJpaSupport.find(hql, startIndex, endIndex, hqlParameters); return result; } - protected List<E> find(String hql, Map<String, Object> hqlParameters, TopiaPagerBean pager) { - List<E> result = find(hql, hqlParameters, getEntityClass(), pager); - return result; - } - - protected <R> List<R> findAll(String hql, Map<String, Object> hqlParameters, Class<R> type) { + protected <K> List<K> find(String hql, Map<String, Object> hqlParameters, TopiaPagerBean pager) { Preconditions.checkNotNull(hql); Preconditions.checkNotNull(hqlParameters); - Preconditions.checkNotNull(type); - List<R> result = topiaJpaSupport.findAll(hql, hqlParameters); - return result; - } - - protected <R> List<R> find(String hql, Map<String, Object> hqlParameters, Class<R> type, int startIndex, int endIndex) { - Preconditions.checkNotNull(hql); - Preconditions.checkNotNull(hqlParameters); - Preconditions.checkNotNull(type); - List<R> result = topiaJpaSupport.find(hql, startIndex, endIndex, hqlParameters); - return result; - } - - protected <R> List<R> find(String hql, Map<String, Object> hqlParameters, Class<R> type, TopiaPagerBean pager) { - Preconditions.checkNotNull(hql); - Preconditions.checkNotNull(hqlParameters); - Preconditions.checkNotNull(type); Preconditions.checkNotNull(pager); if (StringUtils.isNotBlank(pager.getSortColumn())) { @@ -556,7 +489,7 @@ } } - List<R> result = topiaJpaSupport.find( + List<K> result = topiaJpaSupport.find( hql, (int) pager.getRecordStartIndex(), (int) pager.getRecordEndIndex() - 1, @@ -565,26 +498,19 @@ return result; } - protected Iterable<E> findAllLazy(String hql, Map<String, Object> hqlParameters) { - Iterable<E> result = findAllLazy(hql, hqlParameters, getEntityClass()); - return result; - } + protected <K> Iterable<K> findAllLazy(String hql, Map<String, Object> hqlParameters) { - protected <R> Iterable<R> findAllLazy(String hql, Map<String, Object> hqlParameters, Class<R> type) { - Preconditions.checkNotNull(hql); Preconditions.checkNotNull(hqlParameters); - Preconditions.checkNotNull(type); - final Iterator<R> iterator = new FindAllIterator<E, R>(this, - type, + final Iterator<K> iterator = new FindAllIterator<E, K>(this, batchSize, hql, hqlParameters); - Iterable<R> result = new Iterable<R>() { + Iterable<K> result = new Iterable<K>() { @Override - public Iterator<R> iterator() { + public Iterator<K> iterator() { return iterator; } }; @@ -667,13 +593,12 @@ // return meta; // } - public static class FindAllIterator<E extends TopiaEntity, R> implements Iterator<R> { + public static class FindAllIterator<E extends TopiaEntity, K> implements Iterator<K> { - protected Iterator<R> data; + protected Iterator<K> data; protected final AbstractTopiaDao<E> dao; - protected final Class<R> type; protected final String hql; @@ -682,12 +607,10 @@ protected TopiaPagerBean pager; public FindAllIterator(AbstractTopiaDao<E> dao, - Class<R> type, int batchSize, String hql, Map<String, Object> params) { this.dao = dao; - this.type = type; this.hql = hql; this.params = params; @@ -717,7 +640,7 @@ } - public R next() { + public K next() { if (!hasNext()) { throw new NoSuchElementException(); } @@ -731,11 +654,12 @@ PagerBeanUtil.computeRecordIndexesAndPagesNumber(pager); // load new window of data - data = dao.find(hql, params, type, pager).iterator(); + List<K> values = dao.find(hql, params, pager); + data = values.iterator(); } - R next = data.next(); + K next = data.next(); return next; } @@ -1011,13 +935,13 @@ @Override public List<String> findAllIds() { String hqlWithSelectClause = "select topiaId " + hql; - return topiaDAO.findAll(hqlWithSelectClause, hqlParameters, String.class); + return topiaDAO.findAll(hqlWithSelectClause, hqlParameters); } @Override public List<String> findIds(int startIndex, int endIndex) { String hqlWithSelectClause = "select topiaId " + hql; - return topiaDAO.find(hqlWithSelectClause, hqlParameters, String.class, startIndex, endIndex); + return topiaDAO.find(hqlWithSelectClause, hqlParameters, startIndex, endIndex); } }