2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.examples.mailreaderjpa;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import javax.persistence.EntityManager;
23 import javax.persistence.EntityManagerFactory;
24 import javax.persistence.EntityTransaction;
25 import javax.persistence.NoResultException;
26 import javax.persistence.PersistenceException;
27 import javax.persistence.PersistenceUnit;
28 import javax.persistence.Query;
29 import org.apache.mailreaderjpa.Protocol;
30 import org.apache.mailreaderjpa.Subscription;
31 import org.apache.mailreaderjpa.User;
32 import org.apache.shale.view.AbstractApplicationBean;
33
34 /***
35 * <p>Business logic for this example application. This bean is designed
36 * to be a managed bean with scope "application", so the implementation of
37 * each public method must be done in a threadsafe manner.</p>
38 *
39 * <p><strong>USAGE NOTE</strong>: The public methods of this class may throw
40 * unchecked exceptions if the underlying persistence framework has problems.
41 * If you intend to create graceful UIs for such exceptions, be sure to
42 * enclose calls to these methods in try/catch blocks.</p>
43 */
44 public class Logic extends AbstractApplicationBean {
45
46
47
48
49
50 /***
51 * <p>Name of the persistence context we will be using.</p>
52 */
53 private static final String PERSISTENCE_CONTEXT_NAME = "MailReaderJpa";
54
55
56 /***
57 * <p>Named query for selecting all <code>Protocol</code> instances.</p>
58 */
59 private static final String PROTOCOL_QUERY = "Protocol.findAll";
60
61
62 /***
63 * <p>Named query for finding a <code>User</code> by username.</p>
64 */
65 private static final String USERNAME_QUERY = "User.findByUsername";
66
67
68
69
70
71 /***
72 * <p>The entity manager factory we will be using. This field is package scoped
73 * for purposes of unit testing.</p>
74 */
75 @PersistenceUnit(name=PERSISTENCE_CONTEXT_NAME)
76 EntityManagerFactory emf;
77
78
79
80
81
82 /***
83 * <p>Log initialization of this business logic bean.</p>
84 */
85 public void init() {
86
87 log("Initializing business logic bean");
88
89 }
90
91
92 /***
93 * <p>On application shutdown, close the entity manager factory.</p>
94 */
95 public void destroy() {
96
97 log("Finalizing business logic bean");
98 emf.close();
99
100 }
101
102
103
104
105
106 /***
107 * <p>Return a <code>List</code> of the valid <code>Protocol</code>s for
108 * this application. If no valid protocols are defined, a zero length
109 * list will be returned.</p>
110 */
111 public List<Protocol> getProtocols() {
112
113 EntityManager em = entityManager();
114 try {
115 List<Protocol> protocols =
116 em.createNamedQuery(PROTOCOL_QUERY).getResultList();
117 if (protocols == null) {
118 protocols = new ArrayList<Protocol>();
119 }
120 return protocols;
121 } finally {
122 em.close();
123 }
124
125 }
126
127
128
129
130
131 /***
132 * <p>Authenticate the specified logon credentials, returning a corresponding
133 * <code>User</code> if they are valid or <code>null</code> otherwise.</p>
134 *
135 * @param username Username to authenticate
136 * @param password Password to authenticate
137 */
138 public User authenticate(String username, String password) {
139
140 User user = findUserByUsername(username);
141 if ((user != null)
142 && user.getPassword().equals(password)) {
143 return user;
144 } else {
145 return null;
146 }
147
148 }
149
150
151 /***
152 * <p>Bootstrap the contents of the database, if necessary. This is
153 * strictly a convenience mechanism so that the out-of-the-box content
154 * of the data matches that in the original demo. This is not typical
155 * of what you would need in a normal application.</p>
156 */
157 public void bootstrap() {
158
159 EntityManager em = entityManager();
160 EntityTransaction et = null;
161
162 try {
163
164
165 List<Protocol> protocols = em.createNamedQuery(PROTOCOL_QUERY).getResultList();
166 if ((protocols != null) && (protocols.size() > 0)) {
167 return;
168 }
169
170
171 et = em.getTransaction();
172 et.begin();
173
174
175 Protocol protocol1 = null;
176 protocol1 = new Protocol();
177 protocol1.setDescription("IMAP Protocol");
178 em.persist(protocol1);
179 Protocol protocol2 = new Protocol();
180 protocol2.setDescription("POP3 Protocol");
181 em.persist(protocol2);
182
183
184 User user = new User();
185 user.setUsername("user");
186 user.setPassword("pass");
187 user.setFullName("John Q. User");
188 user.setFromAddress("John.User@somewhere.com");
189 List<Subscription> list = new ArrayList<Subscription>();
190 user.setSubscriptions(list);
191 Subscription sub = null;
192 sub = new Subscription();
193 sub.setUser(user);
194 sub.setHost("mail.yahoo.com");
195 sub.setProtocol(protocol1);
196 sub.setUsername("jquser");
197 sub.setPassword("foo");
198 list.add(sub);
199 sub = new Subscription();
200 sub.setUser(user);
201 sub.setHost("mail.hotmail.com");
202 sub.setProtocol(protocol2);
203 sub.setUsername("user1234");
204 sub.setPassword("bar");
205 list.add(sub);
206 em.persist(user);
207
208
209 et.commit();
210
211 } finally {
212
213 if ((et != null) && et.isActive()) {
214 et.rollback();
215 }
216 em.close();
217
218 }
219
220 }
221
222
223 /***
224 * <p>Add the specified <code>Subscription</code> to the persistent database.</p>
225 *
226 * @param subscription Subscription to be added
227 */
228 public void createSubscription(Subscription subscription) {
229
230 EntityManager em = entityManager();
231 EntityTransaction et = null;
232 try {
233 et = em.getTransaction();
234 et.begin();
235 User user = em.find(User.class, subscription.getUser().getId());
236 user.addSubscription(subscription);
237 em.persist(subscription);
238 et.commit();
239 } catch (Exception e) {
240 log("Exception in createSubscription()", e);
241 throw new PersistenceException(e);
242 } finally {
243 if ((et != null) && et.isActive()) {
244 et.rollback();
245 }
246 em.close();
247 }
248
249
250 }
251
252
253 /***
254 * <p>Add the specified <code>User</code> (and any associated child
255 * <code>Subscription</code>s) to the persistent database.</p>
256 *
257 * @param user User to be added
258 */
259 public void createUser(User user) {
260
261 EntityManager em = entityManager();
262 EntityTransaction et = null;
263 try {
264 et = em.getTransaction();
265 et.begin();
266 em.persist(user);
267 et.commit();
268 } catch (Exception e) {
269 log("Exception in createUser()", e);
270 throw new PersistenceException(e);
271 } finally {
272 if ((et != null) && et.isActive()) {
273 et.rollback();
274 }
275 em.close();
276 }
277
278
279 }
280
281
282 /***
283 * <p>Delete the specified <code>Subscription</code> from the persistent database.</p>
284 *
285 * @param subscription Subscription to be deleted
286 */
287 public void deleteSubscription(Subscription subscription) {
288
289 EntityManager em = entityManager();
290 EntityTransaction et = null;
291 try {
292 et = em.getTransaction();
293 et.begin();
294 User user = em.find(User.class, subscription.getUser().getId());
295 user.removeSubscription(subscription);
296 subscription = em.merge(subscription);
297 em.remove(subscription);
298 et.commit();
299 } catch (Exception e) {
300 log("Exception in deleteSubscription()", e);
301 throw new PersistenceException(e);
302 } finally {
303 if ((et != null) && et.isActive()) {
304 et.rollback();
305 }
306 em.close();
307 }
308
309
310 }
311
312
313 /***
314 * <p>Delete the specified <code>User</code> (and any associated child
315 * <code>Subscription</code>s) from the persistent database.</p>
316 *
317 * @param user User to be deleted
318 */
319 public void deleteUser(User user) {
320
321 EntityManager em = entityManager();
322 EntityTransaction et = null;
323 try {
324 et = em.getTransaction();
325 et.begin();
326 user = em.merge(user);
327 em.remove(user);
328 et.commit();
329 } catch (Exception e) {
330 log("Exception in deleteUser()", e);
331 throw new PersistenceException(e);
332 } finally {
333 if ((et != null) && et.isActive()) {
334 et.rollback();
335 }
336 em.close();
337 }
338
339
340 }
341
342
343 /***
344 * <p>Return the <code>Protocol</code> with the specified id, if any;
345 * otherwise, return <code>null</code>.</p>
346 *
347 * @param id Protocol id to look up
348 */
349 public Protocol findProtocolById(int id) {
350
351 EntityManager em = entityManager();
352 try {
353 return em.find(Protocol.class, id);
354 } finally {
355 em.close();
356 }
357
358 }
359
360
361 /***
362 * <p>Return the <code>Subscription</code> with the specified id, if any;
363 * otherwise, return <code>null</code>.</p>
364 *
365 * @param id Subscription id to look up
366 */
367 public Subscription findSubscriptionById(int id) {
368
369 EntityManager em = entityManager();
370 try {
371 return em.find(Subscription.class, id);
372 } finally {
373 em.close();
374 }
375
376 }
377
378
379 /***
380 * <p>Return the <code>User</code> with the specified id, if any;
381 * otherwise, return <code>null</code>.</p>
382 *
383 * @param id User id to look up
384 */
385 public User findUserById(int id) {
386
387 EntityManager em = entityManager();
388 try {
389 return em.find(User.class, id);
390 } finally {
391 em.close();
392 }
393
394 }
395
396
397 /***
398 * <p>Return the <code>User</code> with the specified username, if any;
399 * otherwise, return <code>null</code>.</p>
400 *
401 * @param username Username to look up
402 */
403 public User findUserByUsername(String username) {
404
405 EntityManager em = entityManager();
406 User user = null;
407 try {
408 Query query = em.createNamedQuery(USERNAME_QUERY);
409 query.setParameter("username", username);
410 user = (User) query.getSingleResult();
411 return user;
412 } catch (NoResultException e) {
413 return null;
414 } finally {
415 em.close();
416 }
417
418 }
419
420
421 /***
422 * <p>Update the specified <code>Subscription</code> in the persistent database.</p>
423 *
424 * @param subscription Subscription to be updated
425 *
426 * @return The updated instance
427 */
428 public Subscription updateSubscription(Subscription subscription) {
429
430 EntityManager em = entityManager();
431 EntityTransaction et = null;
432 try {
433 et = em.getTransaction();
434 et.begin();
435 subscription = em.merge(subscription);
436 et.commit();
437 return subscription;
438 } catch (Exception e) {
439 log("Exception in updateSubscription()", e);
440 throw new PersistenceException(e);
441 } finally {
442 if ((et != null) && et.isActive()) {
443 et.rollback();
444 }
445 em.close();
446 }
447
448
449 }
450
451
452 /***
453 * <p>Update the specified <code>User</code> (and any associated child
454 * <code>Subscription</code>s) in the persistent database.</p>
455 *
456 * @param user User to be updated
457 *
458 * @return The updated instance
459 */
460 public User updateUser(User user) {
461
462 EntityManager em = entityManager();
463 EntityTransaction et = null;
464 try {
465 et = em.getTransaction();
466 et.begin();
467 user = em.merge(user);
468 et.commit();
469 return user;
470 } catch (Exception e) {
471 log("Exception in updateUser()", e);
472 throw new PersistenceException(e);
473 } finally {
474 if ((et != null) && et.isActive()) {
475 et.rollback();
476 }
477 em.close();
478 }
479
480
481 }
482
483
484
485
486
487 /***
488 * <p>Return an <code>EntityManager</code> that can be used to perform
489 * persistence operations.</p>
490 */
491 private EntityManager entityManager() {
492
493 return emf.createEntityManager();
494
495 }
496
497
498 }