1 package org.marketchangers.prayer.hibernate;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.Iterator;
6 import java.util.LinkedList;
7 import java.util.List;
8
9 import net.sf.hibernate.Hibernate;
10 import net.sf.hibernate.HibernateException;
11 import net.sf.hibernate.Session;
12 import net.sf.hibernate.type.Type;
13
14 import org.marketchangers.prayer.PrayerCategory;
15 import org.marketchangers.prayer.PrayerComment;
16 import org.marketchangers.prayer.PrayerRequest;
17 import org.marketchangers.prayer.PrayerRequestManager;
18 import org.marketchangers.prayer.PrayerRequestQuery;
19 import org.marketchangers.prayer.PrayerRequestSearcher;
20 import org.marketchangers.prayer.UserContract;
21 import org.nanocontainer.hibernate.SessionProvider;
22
23 /***
24 * @author <a href="mailto:jwilliams@wc-group.com">Jason Williams</a>
25 * @author <a href="mailto:mtodd@wc-group.com">Matthew Todd</a>
26 */
27 public class HibernatePrayerRequestManager implements PrayerRequestManager {
28 private SessionProvider provider;
29 private PrayerRequestSearcher searcher;
30
31 public HibernatePrayerRequestManager(
32 PrayerRequestSearcher searcher,
33 SessionProvider provider) {
34 this.provider = provider;
35 this.searcher = searcher;
36 }
37
38 public boolean submitPrayerRequest(PrayerRequest prayerRequest) {
39 try {
40 Session session = provider.getSession();
41 session.save(prayerRequest);
42 provider.commit();
43 } catch (HibernateException he) {
44 he.printStackTrace();
45 provider.reset();
46 return false;
47 }
48
49
50 searcher.index(prayerRequest);
51
52 return true;
53 }
54
55 public boolean createPrayerCategory(PrayerCategory category) {
56 try {
57 Session session = provider.getSession();
58 session.save(category);
59 provider.commit();
60 } catch (HibernateException he) {
61 he.printStackTrace();
62 provider.reset();
63 return false;
64 }
65
66 return true;
67 }
68
69 public PrayerCategory getPrayerCategory(String category) {
70 PrayerCategory prayerCategory = null;
71
72 try {
73 List results =
74 provider.getSession().find(
75 "from PrayerCategory as pc where pc.name = ?",
76 category,
77 Hibernate.STRING);
78
79 if (!results.isEmpty()) {
80 prayerCategory = (PrayerCategory)results.get(0);
81 }
82
83 provider.close();
84
85 } catch (HibernateException he) {
86 he.printStackTrace();
87 provider.reset();
88 return null;
89 }
90
91 return prayerCategory;
92 }
93
94 public List getCategoriesForRequestor(String user) {
95 List results = Collections.EMPTY_LIST;
96
97 try {
98 results =
99 provider.getSession().find(
100 "from PrayerCategory as pc where pc.requestorUserId = ?",
101 user,
102 Hibernate.STRING);
103
104 provider.close();
105 } catch (HibernateException he) {
106 results = Collections.EMPTY_LIST;
107 provider.reset();
108 he.printStackTrace();
109 }
110
111 return results;
112 }
113
114 public List getCategoriesForIntercessor(String user) {
115
116 return null;
117 }
118
119 public List getRequestorsForIntercessor(String user) {
120 List requestors = new LinkedList();
121
122 try {
123 Iterator contracts =
124 provider.getSession().iterate(
125 "from UserContract as uc where uc.status = ? and uc.requesteeUserId = ?",
126 new Object[] { UserContract.ACCEPTED, user },
127 new Type[] { Hibernate.STRING, Hibernate.STRING });
128
129 while (contracts.hasNext()) {
130 UserContract contract = (UserContract)contracts.next();
131 requestors.add(contract.getRequestorUserId());
132 }
133
134 provider.close();
135
136 } catch (HibernateException he) {
137 provider.reset();
138 he.printStackTrace();
139 }
140
141 return requestors;
142 }
143
144 public List getRequestorOrganizationsForIntercessor(String user) {
145
146 return null;
147 }
148
149
150
151
152
153
154
155
156 public List getCurrentIntercessors(String requestorUserId) {
157 List results = Collections.EMPTY_LIST;
158
159 try {
160 results =
161 provider.getSession().find(
162 "from UserContract as uc where requestorUserId = ?",
163 requestorUserId,
164 Hibernate.STRING);
165
166 provider.close();
167 } catch (HibernateException he) {
168 results = Collections.EMPTY_LIST;
169 he.printStackTrace();
170 }
171
172 return results;
173 }
174
175
176
177
178
179
180
181
182 public List getCurrentRequestors(String intercessorUserId) {
183 List results = Collections.EMPTY_LIST;
184
185 try {
186 results =
187 provider.getSession().find(
188 "from UserContract as uc where requesteeUserId = ? and status != 'declined' and status != 'cancelled'",
189 intercessorUserId,
190 Hibernate.STRING);
191
192 provider.close();
193 } catch (HibernateException he) {
194 results = Collections.EMPTY_LIST;
195 he.printStackTrace();
196 }
197
198 return results;
199 }
200
201 public boolean removeContract(UserContract contract) {
202 int deletedEntries;
203
204 try {
205 deletedEntries =
206 provider.getSession().delete(
207 "from UserContract as uc where uc.id = ?",
208 contract.getId(),
209 Hibernate.INTEGER);
210
211 if (deletedEntries > 0) {
212 provider.commit();
213 } else {
214 provider.close();
215 }
216 } catch (HibernateException he) {
217 he.printStackTrace();
218 provider.reset();
219 return false;
220 }
221
222 return deletedEntries > 0;
223 }
224
225 public boolean addContract(UserContract contract) {
226 List results = Collections.EMPTY_LIST;
227
228 if ((contract == null) || (!contract.validate())) {
229 return false;
230 }
231
232 try {
233 Session session = provider.getSession();
234
235
236 results =
237 session.find(
238 "from UserContract as uc where uc.requesteeUserId = ? and uc.requestorUserId = ?",
239 new Object[] {
240 contract.getRequesteeUserId(),
241 contract.getRequestorUserId()},
242 new Type[] { Hibernate.STRING, Hibernate.STRING });
243
244 if (results.isEmpty()) {
245 session.save(contract);
246 provider.commit();
247 } else {
248 provider.close();
249 return false;
250 }
251 } catch (HibernateException he) {
252 he.printStackTrace();
253 provider.reset();
254 return false;
255 }
256
257 return true;
258 }
259
260
261
262
263
264
265
266
267
268
269 public boolean updateContractStatus(UserContract contract) {
270 if (contract != null) {
271 try {
272 Session session = provider.getSession();
273 UserContract storedContract =
274 (UserContract)session.get(
275 UserContract.class,
276 contract.getId());
277 if (storedContract != null) {
278 storedContract.setStatus(contract.getStatus());
279 session.update(storedContract);
280 provider.commit();
281 return true;
282 }
283 } catch (HibernateException he) {
284 he.printStackTrace();
285 provider.reset();
286 }
287 }
288 return false;
289 }
290
291
292
293
294
295
296
297 public boolean addComment(PrayerComment comment) {
298 try {
299 Session session = provider.getSession();
300 session.save(comment);
301 provider.commit();
302 } catch (HibernateException he) {
303 he.printStackTrace();
304 provider.reset();
305 return false;
306 }
307 return true;
308 }
309
310
311
312
313
314
315
316 public boolean updateStatus(PrayerRequest prayerRequest) {
317 try {
318 Session session = provider.getSession();
319 PrayerRequest request =
320 (PrayerRequest)session.get(
321 PrayerRequest.class,
322 prayerRequest.getId());
323 request.setStatus(prayerRequest.getStatus());
324 session.update(request);
325 provider.commit();
326
327
328 searcher.updateStatus(request);
329 } catch (HibernateException he) {
330 he.printStackTrace();
331 provider.reset();
332 return false;
333 }
334 return true;
335 }
336
337
338
339
340
341
342
343
344
345 public PrayerRequest getPrayerRequestById(String user, Integer id) {
346 PrayerRequest request;
347
348 try {
349 Session session = provider.getSession();
350
351 request = (PrayerRequest) session.get(PrayerRequest.class, id);
352
353 if (!request.getRequestorUserId().equals(user)) {
354 Collection contracts =
355 session.find(
356 "from UserContract c where requestorUserId = ? and requesteeUserId = ? and status = ?",
357 new Object[] {
358 request.getRequestorUserId(),
359 user,
360 UserContract.ACCEPTED },
361 new Type[] {
362 Hibernate.STRING,
363 Hibernate.STRING,
364 Hibernate.STRING });
365
366 request = contracts.isEmpty() ? null : request;
367 }
368
369 provider.close();
370
371 } catch (HibernateException he) {
372 request = null;
373 provider.reset();
374 he.printStackTrace();
375 }
376
377 return request;
378 }
379
380
381
382
383
384 public List getCommentsByPrayerId(Integer prayerRequestId) {
385 List comments = Collections.EMPTY_LIST;
386
387 try {
388 comments =
389 provider.getSession().find(
390 "from PrayerComment as pc where pc.parentRequest.id = ? order by pc.postDate desc",
391 prayerRequestId,
392 Hibernate.INTEGER);
393 provider.close();
394 } catch (HibernateException he) {
395 comments = Collections.EMPTY_LIST;
396 provider.reset();
397 he.printStackTrace();
398 }
399
400 return comments;
401 }
402
403
404
405
406
407
408
409
410
411
412
413
414 public List getPrayerRequestList(String user, String role) {
415 List results = Collections.EMPTY_LIST;
416
417 if (role.equals("requestor")) {
418 try {
419 results =
420 provider.getSession().find(
421 "from PrayerRequest as pr where requestorUserId = ?",
422 user,
423 Hibernate.STRING);
424
425 provider.close();
426 } catch (HibernateException he) {
427 results = Collections.EMPTY_LIST;
428 he.printStackTrace();
429 }
430 } else if (role.equals("intercessor")) {
431
432
433 results = Collections.EMPTY_LIST;
434 }
435 return results;
436 }
437
438 public List search(String user, PrayerRequestQuery query) {
439 List searchResults = new LinkedList();
440 List allowedRequestors = getRequestorsForIntercessor(user);
441
442
443
444
445 if (query.getRequestorUserIds().size() == 0) {
446 query.getRequestorUserIds().addAll(allowedRequestors);
447 } else {
448 query.getRequestorUserIds().retainAll(allowedRequestors);
449 }
450
451 Iterator prayerRequestIds = searcher.search(query).iterator();
452
453
454 while (prayerRequestIds.hasNext()) {
455 Integer id = (Integer) prayerRequestIds.next();
456 searchResults.add(getPrayerRequestById(user, id));
457 }
458
459 return searchResults;
460 }
461 }