1 | |
package org.jtheque.films.services.impl.utils.closures; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import org.jtheque.core.managers.Managers; |
20 | |
import org.jtheque.core.managers.beans.IBeansManager; |
21 | |
import org.jtheque.core.managers.error.IErrorManager; |
22 | |
import org.jtheque.core.managers.error.InternationalizedError; |
23 | |
import org.jtheque.core.managers.language.ILanguageManager; |
24 | |
import org.jtheque.core.managers.log.IJThequeLogger; |
25 | |
import org.jtheque.core.managers.log.Logger; |
26 | |
import org.jtheque.core.managers.view.able.IViewManager; |
27 | |
import org.jtheque.core.utils.MailUtils; |
28 | |
import org.jtheque.films.IFilmsModule; |
29 | |
import org.jtheque.films.persistence.dao.able.IDaoFilms; |
30 | |
import org.jtheque.primary.od.able.Lending; |
31 | |
import org.jtheque.primary.services.able.ILendingsService; |
32 | |
import org.jtheque.utils.bean.Email; |
33 | |
import org.jtheque.utils.collections.Closure; |
34 | |
|
35 | |
import javax.annotation.Resource; |
36 | |
import javax.mail.MessagingException; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public final class LendingCheckClosure implements Closure<Lending> { |
44 | |
@Resource |
45 | |
private ILendingsService lendingsService; |
46 | |
|
47 | |
@Logger |
48 | |
private IJThequeLogger logger; |
49 | |
|
50 | |
@Resource |
51 | |
private IFilmsModule filmsModule; |
52 | |
|
53 | |
@Resource |
54 | |
private IDaoFilms daoFilms; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public LendingCheckClosure() { |
60 | 0 | super(); |
61 | |
|
62 | 0 | Managers.getManager(IBeansManager.class).inject(this); |
63 | 0 | } |
64 | |
|
65 | |
@Override |
66 | |
public void execute(Lending lending) { |
67 | 0 | if (lendingsService.isLate(lending, filmsModule.getConfiguration().getTimeBeforeAutomaticSend())) { |
68 | 0 | if (filmsModule.getConfiguration().areMailSendAutomatically()) { |
69 | 0 | sendMailToBorrower(lending); |
70 | |
} |
71 | |
|
72 | 0 | Object[] objects = new Object[3]; |
73 | 0 | objects[0] = daoFilms.getFilm(lending.getTheOther()).getDisplayableText(); |
74 | 0 | objects[1] = lending.getThePerson().getDisplayableText(); |
75 | 0 | objects[2] = lending.getDate().toString(); |
76 | |
|
77 | 0 | if (filmsModule.getConfiguration().alertWithMail()) { |
78 | 0 | sendMailToUser(objects); |
79 | |
} |
80 | |
|
81 | 0 | if (filmsModule.getConfiguration().alertWithDialog()) { |
82 | 0 | Managers.getManager(IViewManager.class).displayText( |
83 | |
Managers.getManager(ILanguageManager.class).getMessage("lendings.dialogs.message", objects)); |
84 | |
} |
85 | |
} |
86 | 0 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
private void sendMailToUser(Object[] objects) { |
94 | 0 | Email mail = new Email(); |
95 | |
|
96 | 0 | mail.setFrom("JTheque@jtheque.com"); |
97 | 0 | mail.setMessage(Managers.getManager(ILanguageManager.class).getMessage("lendings.mail.user.message", objects)); |
98 | 0 | mail.setSubject(Managers.getManager(ILanguageManager.class).getMessage("lendings.mail.user.subject")); |
99 | 0 | mail.setTo(new String[]{Managers.getCore().getConfiguration().getUserEmail()}); |
100 | |
|
101 | |
try { |
102 | 0 | MailUtils.send(mail, Managers.getCore().getConfiguration().getSmtpHost()); |
103 | 0 | } catch (MessagingException e) { |
104 | 0 | Managers.getManager(IErrorManager.class).addError(new InternationalizedError("jtheque.errors.mail")); |
105 | |
|
106 | 0 | logger.error(e); |
107 | 0 | } |
108 | 0 | } |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
private void sendMailToBorrower(Lending lending) { |
116 | 0 | Email mail = new Email(); |
117 | |
|
118 | 0 | mail.setFrom(Managers.getCore().getConfiguration().getUserEmail()); |
119 | |
|
120 | 0 | String message = filmsModule.getConfiguration().getAutomaticMail(); |
121 | |
|
122 | 0 | message = message.replaceAll("\\$\\{emprunteur:firstname\\}", lending.getThePerson().getFirstName()); |
123 | 0 | message = message.replaceAll("\\$\\{emprunteur:name\\}", lending.getThePerson().getName()); |
124 | 0 | message = message.replaceAll("\\$\\{emprunt:date\\}", lending.getDate().toString()); |
125 | 0 | message = message.replaceAll("\\$\\{film:title\\}", daoFilms.getFilm(lending.getTheOther()).getTitle()); |
126 | |
|
127 | 0 | mail.setMessage(message); |
128 | 0 | mail.setSubject(Managers.getManager(ILanguageManager.class).getMessage("lendings.mail.borrower.subject")); |
129 | 0 | mail.setTo(new String[]{lending.getThePerson().getEmail()}); |
130 | |
|
131 | |
try { |
132 | 0 | MailUtils.send(mail, Managers.getCore().getConfiguration().getSmtpHost()); |
133 | 0 | } catch (MessagingException e) { |
134 | 0 | Managers.getManager(IErrorManager.class).addError(new InternationalizedError("jtheque.errors.mail")); |
135 | |
|
136 | 0 | logger.error(e); |
137 | 0 | } |
138 | 0 | } |
139 | |
} |