1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package ca.pjer.cm.module.store.defaultimpl;
19
20 import ca.pjer.cm.api.store.DuplicateObjectException;
21 import ca.pjer.cm.api.store.ObjectNotFoundException;
22 import ca.pjer.cm.api.store.StoreService;
23 import ca.pjer.cm.api.store.StoreServiceSystemException;
24 import ca.pjer.cm.api.discovery.DiscoveryService;
25 import ca.pjer.cm.api.metadata.MetaDataService;
26 import ca.pjer.cm.api.metadata.MetaData;
27 import ca.pjer.cm.api.metadata.MetaRelation;
28 import net.sf.hibernate.*;
29 import net.sf.hibernate.expression.Example;
30 import net.sf.hibernate.cfg.Configuration;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.commons.beanutils.BeanUtils;
34 import org.apache.hivemind.Initializable;
35
36 import java.io.Serializable;
37 import java.util.List;
38 import java.util.Enumeration;
39 import java.util.Iterator;
40 import java.net.URL;
41
42 /***
43 * TODO: comment me.<br />
44 * <br />
45 * $Id: StoreServiceImpl.java,v 1.2 2004/05/19 03:29:17 pjer Exp $<br />
46 * <br />
47 * $RCSfile: StoreServiceImpl.java,v $<br />
48 * $Revision: 1.2 $<br />
49 * $Author: pjer $<br />
50 * $Date: 2004/05/19 03:29:17 $<br />
51 */
52 public class StoreServiceImpl implements StoreService, Initializable {
53 private static final Log log = LogFactory.getLog(StoreServiceImpl.class);
54 private DiscoveryService discoveryService;
55 private MetaDataService metaDataService;
56
57 private SessionFactory sessionFactory;
58
59 private static final String STORE_MODULE = "META-INF/storemodule.hbm.xml";
60
61 public void setDiscoveryService(DiscoveryService discoveryService) {
62 this.discoveryService = discoveryService;
63 }
64
65 public void setMetaDataService(MetaDataService metaDataService) {
66 this.metaDataService = metaDataService;
67 }
68
69 private void discoverConfiguration(Configuration configuration) {
70 log.debug("discoverConfiguration");
71 Enumeration enumeration = null;
72 try {
73 enumeration = discoveryService.discover(STORE_MODULE);
74 } catch (Exception e) {
75 String s = "Could not discover [STORE_MODULE:" + STORE_MODULE + "] : " + e.getMessage();
76 log.error(s);
77 throw new RuntimeException(s, e);
78 }
79 try {
80 while (enumeration.hasMoreElements()) {
81 URL url = (URL) enumeration.nextElement();
82 log.debug("discovered [url:" + url + "]");
83 configuration.addURL(url);
84 }
85 } catch (MappingException e) {
86 String s = "Could not configure store service : " + e.getMessage();
87 log.error(s);
88 throw new RuntimeException(s, e);
89 }
90 }
91
92 public void initializeService() {
93 try {
94 Configuration configuration = new Configuration();
95 discoverConfiguration(configuration);
96 sessionFactory = configuration.buildSessionFactory();
97 } catch (HibernateException e) {
98 String s = "Could not build the SessionFactory : " + e.getMessage();
99 log.error(s);
100 e.printStackTrace();
101 throw new RuntimeException(s, e);
102 }
103 }
104
105 private Session openSession() throws StoreServiceSystemException {
106 try {
107 return sessionFactory.openSession();
108 } catch (HibernateException e) {
109 String s = "Could not open Session : " + e.getMessage();
110 log.error(s);
111 throw new StoreServiceSystemException(s, e);
112 }
113 }
114
115 private void closeSession(Session session) throws StoreServiceSystemException {
116 try {
117 session.close();
118 } catch (HibernateException e) {
119 String s = "Could not close Session : " + e.getMessage();
120 log.error(s);
121 throw new StoreServiceSystemException(s, e);
122 }
123 }
124
125 private Transaction beginTransaction(Session session) throws StoreServiceSystemException {
126 try {
127 return session.beginTransaction();
128 } catch (HibernateException e) {
129 String s = "Could not begin the transaction : " + e.getMessage();
130 log.error(s);
131 throw new StoreServiceSystemException(s, e);
132 }
133 }
134
135 private void commitTransaction(Transaction transaction) throws StoreServiceSystemException {
136 try {
137 transaction.commit();
138 } catch (HibernateException e) {
139 String s = "Could not commit the transaction : " + e.getMessage();
140 log.error(s);
141 throw new StoreServiceSystemException(s, e);
142 }
143 }
144
145 private void rollbackTransaction(Transaction transaction) throws StoreServiceSystemException {
146 try {
147 transaction.rollback();
148 } catch (HibernateException e) {
149 String s = "Could not rollback the transaction : " + e.getMessage();
150 log.error(s);
151 throw new StoreServiceSystemException(s, e);
152 }
153 }
154
155 public Object create(Object object) throws StoreServiceSystemException, DuplicateObjectException {
156 Session session = openSession();
157 Transaction transaction = beginTransaction(session);
158 try {
159 session.saveOrUpdate(object);
160 commitTransaction(transaction);
161 return object;
162 } catch (HibernateException e) {
163 String s = "Could not create [object:" + object + "] : " + e.getMessage();
164 log.error(s);
165 rollbackTransaction(transaction);
166 throw new StoreServiceSystemException(s, e);
167 } finally {
168 closeSession(session);
169 }
170 }
171
172 public Object update(Object object) throws StoreServiceSystemException, ObjectNotFoundException {
173 Session session = openSession();
174 Transaction transaction = beginTransaction(session);
175 try {
176 session.saveOrUpdate(object);
177 commitTransaction(transaction);
178 return object;
179 } catch (HibernateException e) {
180 String s = "Could not update [object:" + object + "] : " + e.getMessage();
181 log.error(s);
182 rollbackTransaction(transaction);
183 throw new StoreServiceSystemException(s, e);
184 } finally {
185 closeSession(session);
186 }
187 }
188
189 public Object get(Class clazz, Serializable id) throws StoreServiceSystemException, ObjectNotFoundException {
190 Session session = openSession();
191 Transaction transaction = beginTransaction(session);
192 try {
193 Object object = session.get(clazz, id);
194 if (object == null) {
195 String s = "Object could not be found [clazz:" + clazz + ", id:" + id + "]";
196 log.error(s);
197 rollbackTransaction(transaction);
198 throw new ObjectNotFoundException(s);
199 }
200 commitTransaction(transaction);
201 return object;
202 } catch (HibernateException e) {
203 String s = "Could not get [clazz:" + clazz + ", id:" + id + "] : " + e.getMessage();
204 log.error(s);
205 rollbackTransaction(transaction);
206 throw new StoreServiceSystemException(s, e);
207 } finally {
208 closeSession(session);
209 }
210 }
211
212 public List list(Class clazz) throws StoreServiceSystemException {
213 Session session = openSession();
214 Transaction transaction = beginTransaction(session);
215 try {
216 List list = session.createCriteria(clazz).list();
217 commitTransaction(transaction);
218 return list;
219 } catch (HibernateException e) {
220 String s = "Could not list [clazz:" + clazz + "] : " + e.getMessage();
221 log.error(s);
222 rollbackTransaction(transaction);
223 throw new StoreServiceSystemException(s, e);
224 } finally {
225 closeSession(session);
226 }
227 }
228
229 public List listFromTemplate(Object template) throws StoreServiceSystemException {
230 Session session = openSession();
231 Criteria criteria = null;
232 try {
233 criteria = session.createCriteria(template.getClass());
234 criteria = criteria.add(Example.create(criteria));
235 MetaData metaData = metaDataService.get(template.getClass());
236 addCriteriaFromMetaRelationList(criteria, template, metaData.getMetaRelationManyToManyList());
237 addCriteriaFromMetaRelationList(criteria, template, metaData.getMetaRelationManyToOneList());
238 addCriteriaFromMetaRelationList(criteria, template, metaData.getMetaRelationOneToManyList());
239 addCriteriaFromMetaRelationList(criteria, template, metaData.getMetaRelationOneToOneList());
240 } catch (Exception e) {
241 String s = "Could not create criteria from template [template:" + template + "] : " + e.getMessage();
242 log.error(s);
243 throw new StoreServiceSystemException(s, e);
244 }
245 Transaction transaction = beginTransaction(session);
246 try {
247 List list = criteria.list();
248 commitTransaction(transaction);
249 return list;
250 } catch (HibernateException e) {
251 String s = "Could not list [template:" + template + "] : " + e.getMessage();
252 log.error(s);
253 rollbackTransaction(transaction);
254 throw new StoreServiceSystemException(s, e);
255 } finally {
256 closeSession(session);
257 }
258 }
259
260 protected void addCriteriaFromMetaRelationList(Criteria criteria, Object template, List metaRelationList) throws Exception {
261 Iterator metaRelationListIterator = metaRelationList.iterator();
262 while (metaRelationListIterator.hasNext()) {
263 MetaRelation metaRelation = (MetaRelation) metaRelationListIterator.next();
264 String metaRelationName = metaRelation.getName();
265 Object relationTarget = BeanUtils.getProperty(template, metaRelationName);
266 if (relationTarget != null) {
267 criteria = criteria.createCriteria(metaRelationName);
268 criteria = criteria.add(Example.create(relationTarget));
269 }
270 }
271 }
272
273 public void remove(Object object) throws StoreServiceSystemException, ObjectNotFoundException {
274 Session session = openSession();
275 Transaction transaction = beginTransaction(session);
276 try {
277 session.delete(object);
278 commitTransaction(transaction);
279 } catch (HibernateException e) {
280 String s = "Could not delete [object:" + object + "] : " + e.getMessage();
281 log.error(s);
282 rollbackTransaction(transaction);
283 throw new StoreServiceSystemException(s, e);
284 } finally {
285 closeSession(session);
286 }
287 }
288 }