1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.smartlab.web.history;
24
25 import java.io.Serializable;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.List;
29
30 import net.smartlab.web.BusinessObjectFactory;
31 import net.smartlab.web.DAOException;
32 import net.smartlab.web.UndefinedKeyException;
33
34 import org.hibernate.Criteria;
35 import org.hibernate.HibernateException;
36 import org.hibernate.Session;
37 import org.hibernate.criterion.Expression;
38 import org.hibernate.criterion.Order;
39
40
41
42
43
44
45 public abstract class HistorizedBusinessObjectFactory extends BusinessObjectFactory {
46
47
48
49
50 protected HistorizedBusinessObjectFactory() {
51 super();
52 }
53
54 public Object searchLast(Serializable key) throws DAOException {
55 return this.searchLast(key, null, null);
56 }
57
58 public Object searchLast(Serializable key, Date start, Date end) throws DAOException {
59 key = this.getBaseFactory().convertKey(key);
60 Object actual = this.getBaseFactory().findByKey(key);
61 if (actual != null && end == null) {
62
63
64
65
66
67
68
69 return actual;
70 } else {
71 Session session = this.current();
72 try {
73 Criteria criteria = session.createCriteria(this.getMappedClass());
74 criteria.add(Expression.idEq(key));
75 criteria.add(Expression.between("lastModified", start, end));
76 criteria.addOrder(Order.desc("lastModified"));
77 HistorizedBusinessObject last = (HistorizedBusinessObject)criteria.uniqueResult();
78
79 return last;
80 } catch (HibernateException he) {
81 logger.debug("findByKey(key = " + key + ") - deserialization failed", he);
82 throw new UndefinedKeyException(key, this.getMappedClass(), he);
83 }
84 }
85 }
86
87 public Object searchFirst(Serializable key) throws DAOException {
88 return this.searchFirst(key, null, null);
89 }
90
91 public Object searchFirst(Serializable key, Date start, Date end) throws DAOException {
92 key = this.getBaseFactory().convertKey(key);
93 Session session = this.current();
94 try {
95 Criteria criteria = session.createCriteria(this.getMappedClass());
96 criteria.add(Expression.idEq(key));
97 criteria.add(Expression.between("lastModified", start, end));
98 criteria.addOrder(Order.asc("lastModified"));
99 HistorizedBusinessObject first = (HistorizedBusinessObject)criteria.uniqueResult();
100 if (first == null) {
101 return this.getBaseFactory().findByKey(key);
102 } else {
103 return first;
104 }
105 } catch (HibernateException he) {
106 logger.debug("findByKey(key = " + key + ") - deserialization failed", he);
107 throw new UndefinedKeyException(key, this.getMappedClass(), he);
108 }
109 }
110
111 public List searchHistory(Serializable key) throws DAOException {
112 return this.searchHistory(key, null, null);
113 }
114
115 public List searchHistory(Serializable key, Date start, Date end) throws DAOException {
116 List history = new ArrayList();
117 Session session = this.current();
118 try {
119 Criteria criteria = session.createCriteria(this.getMappedClass());
120 criteria.add(Expression.idEq(key));
121 criteria.add(Expression.between("lastModified", start, end));
122 criteria.addOrder(Order.asc("lastModified"));
123 history.addAll(criteria.list());
124 history.add(this.getBaseFactory().findByKey(key));
125 return history;
126 } catch (HibernateException he) {
127 logger.debug("findByKey(key = " + key + ") - deserialization failed", he);
128 throw new UndefinedKeyException(key, this.getMappedClass(), he);
129 }
130 }
131
132 protected abstract BusinessObjectFactory getBaseFactory();
133 }