Rev 1426 | Rev 1429 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
29 | jmachado | 1 | package pt.estgp.estgweb.utils; |
2 | |||
1426 | jmachado | 3 | import org.apache.log4j.Logger; |
4 | import pt.estgp.estgweb.Globals; |
||
5 | import pt.estgp.estgweb.services.email.SendEmailService; |
||
29 | jmachado | 6 | |
1426 | jmachado | 7 | import javax.mail.Message; |
8 | import java.io.Serializable; |
||
9 | import java.util.ArrayList; |
||
29 | jmachado | 10 | import java.util.List; |
1426 | jmachado | 11 | import java.util.regex.Matcher; |
298 | jmachado | 12 | import java.util.regex.Pattern; |
29 | jmachado | 13 | |
14 | /** |
||
15 | * @author Jorge Machado |
||
16 | * @date 20/Mar/2008 |
||
17 | * @time 18:32:21 |
||
18 | * @see pt.estgp.estgweb.utils |
||
19 | */ |
||
236 | jmachado | 20 | public class Email implements 1.5.0/docs/api/java/io/Serializable.html">Serializable |
29 | jmachado | 21 | { |
22 | |||
236 | jmachado | 23 | |
1426 | jmachado | 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(Email.class); |
25 | |||
26 | private boolean systemAssumingTestEmail = false; |
||
512 | jmachado | 27 | private boolean isHtml = false; |
29 | jmachado | 28 | private 1.5.0/docs/api/java/lang/String.html">String subject; |
29 | private List<String> recipients; |
||
419 | jmachado | 30 | private Message.RecipientType type = Message.RecipientType.TO; |
29 | jmachado | 31 | private 1.5.0/docs/api/java/lang/String.html">String from; |
32 | private 1.5.0/docs/api/java/lang/String.html">String emailTemplateName; |
||
33 | private List<String> arguments; |
||
872 | jmachado | 34 | private 1.5.0/docs/api/java/lang/String.html">String fromName = null; |
29 | jmachado | 35 | |
36 | |||
1427 | jmachado | 37 | public Email cloneEmailForJob(){ |
38 | Email email = new Email(subject,recipients,from,emailTemplateName,arguments,type); |
||
39 | email.systemAssumingTestEmail = this.systemAssumingTestEmail; |
||
40 | email.isHtml = this.isHtml; |
||
41 | email.subject = this.subject; |
||
42 | email.recipients = this.recipients; |
||
43 | email.type = this.type; |
||
44 | email.from = this.from; |
||
45 | email.emailTemplateName = this.emailTemplateName; |
||
46 | email.arguments = this.arguments; |
||
47 | email.fromName = this.fromName; |
||
48 | return email; |
||
49 | } |
||
50 | |||
51 | |||
512 | jmachado | 52 | public boolean isHtml() { |
53 | return isHtml; |
||
54 | } |
||
55 | |||
56 | public void setHtml(boolean html) { |
||
57 | isHtml = html; |
||
58 | } |
||
59 | |||
29 | jmachado | 60 | public Email(1.5.0/docs/api/java/lang/String.html">String subject, List<String> recipients, 1.5.0/docs/api/java/lang/String.html">String from, 1.5.0/docs/api/java/lang/String.html">String mailTemplateName, List<String> arguments) |
61 | { |
||
62 | this.subject = subject; |
||
1426 | jmachado | 63 | this.recipients = filterRecipients(recipients, arguments, mailTemplateName);; |
29 | jmachado | 64 | this.from = from; |
65 | this.emailTemplateName = mailTemplateName; |
||
66 | this.arguments = arguments; |
||
67 | } |
||
68 | |||
419 | jmachado | 69 | public Email(1.5.0/docs/api/java/lang/String.html">String subject, List<String> recipients, 1.5.0/docs/api/java/lang/String.html">String from, 1.5.0/docs/api/java/lang/String.html">String mailTemplateName, List<String> arguments, Message.RecipientType type) |
70 | { |
||
71 | this.type = type; |
||
72 | this.subject = subject; |
||
1426 | jmachado | 73 | this.recipients = filterRecipients(recipients, arguments, mailTemplateName); |
419 | jmachado | 74 | this.from = from; |
75 | this.emailTemplateName = mailTemplateName; |
||
76 | this.arguments = arguments; |
||
1426 | jmachado | 77 | |
78 | |||
419 | jmachado | 79 | } |
236 | jmachado | 80 | |
1426 | jmachado | 81 | private List<String> filterRecipients(List<String> recipients,List<String> arguments,1.5.0/docs/api/java/lang/String.html">String template) |
82 | { |
||
83 | if(Globals.TEST_ENVIRONEMENT) |
||
84 | { |
||
85 | systemAssumingTestEmail = true; |
||
86 | logger.info("SISTEMA EM MODO DE TESTES, iria enviar email para os seguintes recipientes:"); |
||
87 | 1.5.0/docs/api/java/lang/System.html">System.out.println(recipients); |
||
88 | logger.info("Mensagem que ia ser enviada:"); |
||
89 | logger.info(SendEmailService.getEmailTextMessage(template, arguments)); |
||
90 | logger.info("SISTEMA EM MODO DE TESTES: Vai enviar apenas para o mail de testes"); |
||
91 | recipients = new ArrayList<String>(); |
||
92 | recipients.add(Globals.ADMIN_EMAIL); |
||
93 | } |
||
94 | return recipients; |
||
95 | } |
||
96 | private List<String> filterRecipients(List<String> recipients) |
||
97 | { |
||
98 | if(Globals.TEST_ENVIRONEMENT) |
||
99 | { |
||
100 | systemAssumingTestEmail = true; |
||
101 | logger.info("SISTEMA EM MODO DE TESTES, iria enviar email para os seguintes recipientes:"); |
||
102 | 1.5.0/docs/api/java/lang/System.html">System.out.println(recipients); |
||
103 | logger.info("Nao se sabe a Mensagem que ia ser enviada"); |
||
104 | logger.info("SISTEMA EM MODO DE TESTES: Vai enviar apenas para o mail de testes"); |
||
105 | recipients = new ArrayList<String>(); |
||
106 | recipients.add(Globals.ADMIN_EMAIL); |
||
107 | } |
||
108 | return recipients; |
||
109 | } |
||
110 | |||
111 | public boolean isSystemAssumingTestEmail() { |
||
112 | return systemAssumingTestEmail; |
||
113 | } |
||
114 | |||
872 | jmachado | 115 | public 1.5.0/docs/api/java/lang/String.html">String getFromName() { |
116 | return fromName; |
||
117 | } |
||
419 | jmachado | 118 | |
872 | jmachado | 119 | public void setFromName(1.5.0/docs/api/java/lang/String.html">String fromName) { |
120 | this.fromName = fromName; |
||
121 | } |
||
122 | |||
236 | jmachado | 123 | public 1.5.0/docs/api/java/lang/String.html">String getSubject() |
29 | jmachado | 124 | { |
236 | jmachado | 125 | return subject; |
126 | } |
||
29 | jmachado | 127 | |
236 | jmachado | 128 | public List<String> getRecipients() |
129 | { |
||
130 | return recipients; |
||
29 | jmachado | 131 | } |
132 | |||
623 | jmachado | 133 | public void setRecipients(List<String> recipients) |
134 | { |
||
1426 | jmachado | 135 | systemAssumingTestEmail = true;//ja nao estao sobre |
136 | this.recipients = filterRecipients(recipients); |
||
137 | |||
623 | jmachado | 138 | } |
139 | |||
236 | jmachado | 140 | public 1.5.0/docs/api/java/lang/String.html">String getFrom() |
29 | jmachado | 141 | { |
236 | jmachado | 142 | return from; |
143 | } |
||
29 | jmachado | 144 | |
236 | jmachado | 145 | public 1.5.0/docs/api/java/lang/String.html">String getEmailTemplateName() |
146 | { |
||
147 | return emailTemplateName; |
||
29 | jmachado | 148 | } |
149 | |||
236 | jmachado | 150 | public List<String> getArguments() |
151 | { |
||
152 | return arguments; |
||
153 | } |
||
298 | jmachado | 154 | |
155 | public static boolean validEmail(1.5.0/docs/api/java/lang/String.html">String email) |
||
156 | { |
||
157 | if(email == null) |
||
158 | return false; |
||
159 | 1.5.0/docs/api/java/util/regex/Pattern.html">Pattern p = 1.5.0/docs/api/java/util/regex/Pattern.html">Pattern.compile(".+@.+\\.[a-z]+"); |
||
160 | //Match the given string with the pattern |
||
161 | 1.5.0/docs/api/java/util/regex/Matcher.html">Matcher m = p.matcher(email); |
||
162 | //check whether match is found |
||
163 | return m.matches(); |
||
164 | } |
||
165 | |||
166 | public static void main( 1.5.0/docs/api/java/lang/String.html">String[] args ) |
||
167 | { |
||
168 | 1.5.0/docs/api/java/lang/System.html">System.out.println(validEmail("")); |
||
169 | 1.5.0/docs/api/java/lang/System.html">System.out.println(validEmail("d.@")); |
||
170 | 1.5.0/docs/api/java/lang/System.html">System.out.println(validEmail("asd@.")); |
||
171 | 1.5.0/docs/api/java/lang/System.html">System.out.println(validEmail("a.sd@.p")); |
||
172 | 1.5.0/docs/api/java/lang/System.html">System.out.println(validEmail("@t.pt")); |
||
173 | 1.5.0/docs/api/java/lang/System.html">System.out.println(validEmail("e@.e")); |
||
174 | 1.5.0/docs/api/java/lang/System.html">System.out.println(validEmail("e@")); |
||
175 | 1.5.0/docs/api/java/lang/System.html">System.out.println(validEmail("e@e.pt")); |
||
176 | 1.5.0/docs/api/java/lang/System.html">System.out.println(validEmail("jmachado@estgp.pt")); |
||
177 | } |
||
419 | jmachado | 178 | |
179 | |||
180 | public Message.RecipientType getType() |
||
181 | { |
||
182 | return type; |
||
183 | } |
||
184 | |||
185 | public void setType(Message.RecipientType type) |
||
186 | { |
||
1427 | jmachado | 187 | //TODO WHEN BACK TO BCC REMOVE this |
188 | this.type = Message.RecipientType.TO; |
||
189 | //this.type = type; |
||
419 | jmachado | 190 | } |
1427 | jmachado | 191 | |
192 | |||
29 | jmachado | 193 | } |