Rev 1429 | Rev 1490 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1429 | jmachado | 1 | package pt.estgp.estgweb.services.email; |
2 | |||
3 | import jomm.dao.impl.AbstractDao; |
||
4 | import org.apache.log4j.Logger; |
||
5 | import pt.estgp.estgweb.Globals; |
||
6 | import pt.estgp.estgweb.domain.User; |
||
7 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
||
8 | import pt.estgp.estgweb.services.authenticate.AuthenticateService; |
||
9 | import pt.estgp.estgweb.services.jobs.JobHandler; |
||
10 | import pt.estgp.estgweb.utils.Email; |
||
11 | import pt.estgp.estgweb.utils.EmailPersonalizedCertificated; |
||
12 | |||
13 | import java.io.Serializable; |
||
14 | import java.util.ArrayList; |
||
15 | import java.util.List; |
||
16 | |||
17 | /** |
||
18 | * @author Jorge Machado |
||
19 | * @date 11/Jul/2008 |
||
20 | * @see pt.estgp.estgweb.services.sms |
||
21 | */ |
||
22 | public class EMAILJobPersonalizedCertificated implements JobHandler, 1.5.0/docs/api/java/io/Serializable.html">Serializable |
||
23 | { |
||
24 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(EMAILJobPersonalizedCertificated.class); |
||
25 | |||
26 | private EmailPersonalizedCertificated email; |
||
27 | |||
28 | |||
29 | |||
30 | |||
31 | public EMAILJobPersonalizedCertificated(EmailPersonalizedCertificated email) |
||
32 | { |
||
33 | this.email = email; |
||
34 | } |
||
35 | |||
36 | |||
37 | private static final int pauseTime = 1000; |
||
38 | |||
39 | |||
40 | public void runJob() |
||
41 | { |
||
42 | final EmailPersonalizedCertificated cloned = email.cloneEmailForJob(); |
||
43 | |||
44 | |||
45 | logger.info("Opening new Thread to send emails"); |
||
46 | new 1.5.0/docs/api/java/lang/Thread.html">Thread( |
||
47 | new 1.5.0/docs/api/java/lang/Runnable.html">Runnable() { |
||
48 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
49 | public void run() { |
||
50 | if(Globals.TEST_ENVIRONEMENT && !cloned.isSystemAssumingTestEmail()) |
||
51 | { |
||
52 | 1.5.0/docs/api/java/lang/String.html">String warn = "System in TEST Environement - will not send EMAIL"; |
||
53 | logger.warn(warn); |
||
54 | return; |
||
55 | } |
||
56 | try |
||
57 | { |
||
58 | 1.5.0/docs/api/java/lang/String.html">String URL_PREFIX = cloned.getUrlPatternWithoutCetificate(); |
||
59 | if(URL_PREFIX.indexOf("?") < 0) |
||
60 | URL_PREFIX = URL_PREFIX + "?certificate="; |
||
61 | else |
||
62 | URL_PREFIX = URL_PREFIX + "&certificate="; |
||
63 | |||
64 | cloned.setRecipients(new ArrayList<String>()); |
||
65 | |||
1450 | jmachado | 66 | SendEmailService sendEmailService = new SendEmailService(); |
67 | |||
1429 | jmachado | 68 | for(long userId: cloned.getUserIds()) |
69 | { |
||
70 | |||
71 | try{ |
||
72 | AbstractDao.getCurrentSession().beginTransaction(); |
||
73 | AbstractDao.getCurrentSession().clear(); |
||
74 | User user = DaoFactory.getUserDaoImpl().load(userId); |
||
75 | 1.5.0/docs/api/java/lang/String.html">String certificate = new AuthenticateService().createCertificate(user); |
||
76 | 1.5.0/docs/api/java/lang/String.html">String emailStr = user.getEmail(); |
||
77 | 1.5.0/docs/api/java/lang/String.html">String outEmail = user.getOutEmail(); |
||
78 | 1.5.0/docs/api/java/lang/String.html">String preferrefEmail = user.getPreferrefEmail(); |
||
79 | 1.5.0/docs/api/java/lang/String.html">String toEmail = null; |
||
80 | |||
81 | if(preferrefEmail != null && Email.validEmail(preferrefEmail)) |
||
82 | toEmail = preferrefEmail; |
||
83 | else if(emailStr != null && Email.validEmail(emailStr)) |
||
84 | toEmail = emailStr; |
||
85 | else if(outEmail != null && Email.validEmail(outEmail)) |
||
86 | toEmail = outEmail; |
||
87 | 1.5.0/docs/api/java/lang/String.html">String nome = user.getName(); |
||
88 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
89 | |||
90 | if(toEmail != null) |
||
91 | { |
||
92 | List<String> ccEmails = new ArrayList<String>(); |
||
93 | if(emailStr != null && Email.validEmail(emailStr) && !toEmail.equals(emailStr)) |
||
94 | ccEmails.add(emailStr); |
||
95 | if(outEmail != null && Email.validEmail(outEmail) && !toEmail.equals(outEmail)) |
||
96 | ccEmails.add(outEmail); |
||
97 | |||
98 | |||
99 | cloned.setRecipientsCC(ccEmails); |
||
100 | cloned.getRecipients().clear(); |
||
101 | cloned.getRecipients().add(toEmail); |
||
102 | |||
103 | 1.5.0/docs/api/java/lang/String.html">String finalUrl = URL_PREFIX + certificate; |
||
104 | |||
1450 | jmachado | 105 | sendEmailService.sendEmail(cloned, finalUrl, nome); |
1429 | jmachado | 106 | |
107 | try { |
||
108 | logger.info("Watting to send another email: sent to" + toEmail); |
||
109 | //Thread.sleep(pauseTime); |
||
110 | 1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(pauseTime); |
||
111 | } catch (1.5.0/docs/api/java/lang/InterruptedException.html">InterruptedException e) { |
||
112 | logger.error(e,e); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | |||
117 | }catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
||
118 | { |
||
119 | logger.error(e,e); |
||
120 | } |
||
121 | |||
122 | } |
||
123 | |||
124 | logger.info("No more user ids to send email. exiting"); |
||
125 | |||
126 | |||
127 | } |
||
128 | catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
||
129 | { |
||
130 | logger.error("Sending EMAIL:" + e.toString(),e); |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | ).start(); |
||
135 | } |
||
136 | |||
137 | |||
138 | |||
139 | } |