View Javadoc

1   /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   * For further informations on the SmartWeb Framework please visit
20   *
21   *                        http://smartweb.sourceforge.net
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   * TODO documentation
42   * 
43   * @author rlogiacco
44   */
45  public abstract class HistorizedBusinessObjectFactory extends BusinessObjectFactory {
46  
47  	/**
48  	 * TODO documentation
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  			 * Wrapper wrapper = new Wrapper((BusinessObject)actual); return
64  			 * (HistorizedBusinessObject
65  			 * )Proxy.newProxyInstance(Thread.currentThread
66  			 * ().getContextClassLoader(), new Class[] {actual.getClass(),
67  			 * HistorizedBusinessObject.class}, wrapper);
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  				// FIX ME
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 }