01: package org.springunit.framework.samples.jpetstore.dao;
02:
03: import java.util.List;
04:
05: import org.springframework.dao.DataAccessException;
06: import org.springunit.framework.samples.jpetstore.domain.Persistable;
07:
08: public abstract class AbstractDao<V extends Persistable> implements
09: Dao<V> {
10:
11: public V create(V item) throws DataAccessException {
12: saveOrUpdate(item);
13: return item;
14: }
15:
16: public List<? extends V> create(List<? extends V> items)
17: throws DataAccessException {
18: for (V item : items) {
19: saveOrUpdate(item);
20: }
21: return items;
22: }
23:
24: public void delete(V item) throws DataAccessException {
25: remove(item);
26: }
27:
28: public void delete(List<? extends V> items)
29: throws DataAccessException {
30: for (V item : items) {
31: remove(item);
32: }
33: }
34:
35: public List<? extends V> read() throws DataAccessException {
36: return findAll();
37: }
38:
39: public V read(int id) throws DataAccessException {
40: return find(getPersistableClass(), id);
41: }
42:
43: public void update(V item) throws DataAccessException {
44: saveOrUpdate(item);
45: }
46:
47: public void update(List<? extends V> items)
48: throws DataAccessException {
49: for (V item : items) {
50: saveOrUpdate(item);
51: }
52: }
53:
54: protected abstract V find(Class clazz, int id);
55:
56: protected List<? extends V> findAll() throws DataAccessException {
57: return findByNamedQuery(getQueryNameForFindAll());
58: }
59:
60: protected abstract List<? extends V> findByNamedQuery(
61: String queryName) throws DataAccessException;
62:
63: protected abstract List<? extends V> findByNamedQueryAndNamedParam(
64: String queryName, String paramName, Object value)
65: throws DataAccessException;
66:
67: protected abstract List<? extends V> findByNamedQueryAndNamedParam(
68: String queryName, String[] paramNames, Object[] values)
69: throws DataAccessException;
70:
71: protected abstract Class getPersistableClass();
72:
73: protected abstract String getQueryNameForFindAll();
74:
75: protected abstract void remove(V item) throws DataAccessException;
76:
77: protected abstract void saveOrUpdate(V item)
78: throws DataAccessException;
79:
80: }
|