Rev 1260 | Rev 1306 | 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.services.email; |
2 | |||
236 | jmachado | 3 | import jomm.utils.TemplateUtils; |
4 | import org.apache.log4j.Logger; |
||
5 | import pt.estgp.estgweb.Globals; |
||
929 | jmachado | 6 | import pt.estgp.estgweb.domain.*; |
7 | import pt.estgp.estgweb.domain.enums.LangEnum; |
||
8 | import pt.estgp.estgweb.services.expceptions.ServiceException; |
||
751 | jmachado | 9 | import pt.estgp.estgweb.utils.ConfigProperties; |
929 | jmachado | 10 | import pt.estgp.estgweb.utils.Email; |
358 | jmachado | 11 | import pt.estgp.estgweb.web.utils.DatesUtils; |
236 | jmachado | 12 | import pt.utl.ist.berserk.logic.serviceManager.IService; |
13 | |||
929 | jmachado | 14 | import javax.mail.*; |
15 | import javax.mail.internet.InternetAddress; |
||
236 | jmachado | 16 | import javax.mail.internet.MimeMessage; |
872 | jmachado | 17 | import java.io.UnsupportedEncodingException; |
929 | jmachado | 18 | import java.text.MessageFormat; |
236 | jmachado | 19 | import java.util.ArrayList; |
929 | jmachado | 20 | import java.util.Date; |
29 | jmachado | 21 | import java.util.List; |
236 | jmachado | 22 | import java.util.Properties; |
29 | jmachado | 23 | |
24 | /** |
||
25 | * @author Jorge Machado |
||
26 | * @date 20/Mar/2008 |
||
27 | * @time 18:31:00 |
||
28 | * @see pt.estgp.estgweb.services.email |
||
29 | */ |
||
236 | jmachado | 30 | public class SendEmailService implements IService |
29 | jmachado | 31 | { |
236 | jmachado | 32 | 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(SendEmailService.class); |
33 | |||
34 | private static final 1.5.0/docs/api/java/lang/String.html">String gradeUnitTemplate = Globals.TEMPLATE_EMAIL_PATH + "/" + Globals.EMAIL_GRADE_UNIT_TEMPLATE; |
||
35 | |||
1260 | jmachado | 36 | static 1.5.0/docs/api/java/lang/String.html">String[] serverEmails; |
37 | static int serverEmailPos = -1; |
||
38 | |||
39 | static{ |
||
40 | serverEmails = new 1.5.0/docs/api/java/lang/String.html">String[Globals.EMAIL_SERVER_USER_COUNT]; |
||
41 | for(int i = 1;i<= Globals.EMAIL_SERVER_USER_COUNT;i++) |
||
42 | { |
||
43 | serverEmails[i-1] = Globals.EMAIL_SERVER_USER_PREFIX + i + Globals.EMAIL_SERVER_USER_SUFIX; |
||
44 | } |
||
45 | } |
||
46 | private static synchronized 1.5.0/docs/api/java/lang/String.html">String getRoundRobinEmailUser() |
||
47 | { |
||
48 | serverEmailPos++; |
||
49 | if(serverEmailPos >= Globals.EMAIL_SERVER_USER_COUNT) |
||
50 | { |
||
51 | serverEmailPos = 0; |
||
52 | } |
||
53 | return serverEmails[serverEmailPos]; |
||
54 | } |
||
236 | jmachado | 55 | public void sendEmail(Email email) throws ServiceException |
29 | jmachado | 56 | { |
236 | jmachado | 57 | try{ |
1260 | jmachado | 58 | final 1.5.0/docs/api/java/lang/String.html">String sendFromAddress = getRoundRobinEmailUser(); |
236 | jmachado | 59 | |
60 | // Set up properties for mail session |
||
61 | 1.5.0/docs/api/java/util/Properties.html">Properties props = 1.5.0/docs/api/java/lang/System.html">System.getProperties(); |
||
62 | props.put("mail.smtp.host", Globals.EMAIL_SERVER_HOST); |
||
929 | jmachado | 63 | final 1.5.0/docs/api/java/lang/String.html">String username = Globals.EMAIL_SERVER_USERNAME; |
64 | final 1.5.0/docs/api/java/lang/String.html">String password = Globals.EMAIL_SERVER_PASSWORD; |
||
65 | javax.mail.1.5.0/docs/api/java/net/Authenticator.html">Authenticator authenticator = null; |
||
66 | if(Globals.EMAIL_SERVER_SECURITY != null && Globals.EMAIL_SERVER_SECURITY.compareTo("TLS")==0) |
||
67 | { |
||
68 | props.put("mail.smtp.auth", "true"); |
||
69 | props.put("mail.smtp.starttls.enable", "true"); |
||
70 | props.put("mail.smtp.port", ""+ Globals.EMAIL_SERVER_SECURITY_TLS); |
||
71 | authenticator = new javax.mail.1.5.0/docs/api/java/net/Authenticator.html">Authenticator() { |
||
72 | protected 1.5.0/docs/api/java/net/PasswordAuthentication.html">PasswordAuthentication getPasswordAuthentication() { |
||
1260 | jmachado | 73 | return new 1.5.0/docs/api/java/net/PasswordAuthentication.html">PasswordAuthentication(sendFromAddress, password); |
929 | jmachado | 74 | } |
75 | }; |
||
76 | } |
||
77 | else if(Globals.EMAIL_SERVER_SECURITY != null && Globals.EMAIL_SERVER_SECURITY.compareTo("SSL")==0) |
||
78 | { |
||
79 | props.put("mail.smtp.socketFactory.port", Globals.EMAIL_SERVER_SECURITY_SSL); |
||
80 | props.put("mail.smtp.socketFactory.class", |
||
81 | "javax.net.ssl.SSLSocketFactory"); |
||
82 | props.put("mail.smtp.auth", "true"); |
||
83 | props.put("mail.smtp.port", Globals.EMAIL_SERVER_SECURITY_SSL); |
||
84 | authenticator = new javax.mail.1.5.0/docs/api/java/net/Authenticator.html">Authenticator() { |
||
85 | protected 1.5.0/docs/api/java/net/PasswordAuthentication.html">PasswordAuthentication getPasswordAuthentication() { |
||
1260 | jmachado | 86 | return new 1.5.0/docs/api/java/net/PasswordAuthentication.html">PasswordAuthentication(sendFromAddress,password); |
929 | jmachado | 87 | } |
88 | }; |
||
89 | } |
||
90 | else |
||
91 | { |
||
236 | jmachado | 92 | |
929 | jmachado | 93 | } |
94 | |||
95 | |||
96 | |||
97 | |||
98 | |||
99 | |||
236 | jmachado | 100 | // Get session |
930 | jmachado | 101 | Session session = Session.getDefaultInstance(props, authenticator); |
933 | jmachado | 102 | session.setDebug(true); |
103 | session.setDebugOut(1.5.0/docs/api/java/lang/System.html">System.out); |
||
236 | jmachado | 104 | |
105 | // Create message |
||
106 | MimeMessage message = new MimeMessage(session); |
||
107 | // Set the recipients of the message |
||
298 | jmachado | 108 | boolean valid = false; |
109 | for (1.5.0/docs/api/java/lang/String.html">String recipient : email.getRecipients()) |
||
110 | { |
||
111 | if(Email.validEmail(recipient)) |
||
112 | { |
||
113 | valid=true; |
||
419 | jmachado | 114 | message.addRecipient(email.getType(),new InternetAddress(recipient)); |
811 | jmachado | 115 | logger.info("Adding recipient to transport:"+recipient); |
298 | jmachado | 116 | } |
236 | jmachado | 117 | } |
298 | jmachado | 118 | if(valid) |
119 | { |
||
811 | jmachado | 120 | |
298 | jmachado | 121 | 1.5.0/docs/api/java/lang/String.html">String content = TemplateUtils.getOrLoadTemplate(Globals.TEMPLATE_EMAIL_PATH + "/" + email.getEmailTemplateName()); |
122 | // Format the mail message |
||
936 | jmachado | 123 | 1.5.0/docs/api/java/lang/String.html">String fullMessage = 1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat.format(content, email.getArguments().toArray()); |
236 | jmachado | 124 | |
935 | jmachado | 125 | |
126 | |||
1260 | jmachado | 127 | logger.info("FROM:" + email.getFrom() + " but using: " + sendFromAddress); |
932 | jmachado | 128 | //if(email.getFromName() != null) |
872 | jmachado | 129 | try { |
1260 | jmachado | 130 | message.setFrom(new InternetAddress(sendFromAddress,sendFromAddress)); |
872 | jmachado | 131 | } catch (1.5.0/docs/api/java/io/UnsupportedEncodingException.html">UnsupportedEncodingException e) { |
132 | logger.error(e,e); |
||
1260 | jmachado | 133 | //message.setFrom(new InternetAddress(sendFromAddress)); |
872 | jmachado | 134 | } |
932 | jmachado | 135 | //else |
136 | // message.setFrom(new InternetAddress(Globals.SYSTEM_EMAIL_BOX)); |
||
1269 | jmachado | 137 | message.setSubject(email.getSubject() + " - não responda para este endereço","utf-8"); |
936 | jmachado | 138 | |
139 | if(email.isHtml()) |
||
140 | { |
||
141 | |||
992 | jmachado | 142 | fullMessage += email.getFrom() != null && email.getFrom().length()>0 && !email.getFrom().equals(Globals.SYSTEM_EMAIL_BOX) ? "<br/><br/>Responda para " + email.getFrom() : "<br/><br/>Por favor não responda a este email"; |
936 | jmachado | 143 | fullMessage += " <br/> <br/> <h5> Mensagem Segura - enviada através da Google Apps Api através de baco 2015 </h5>"; |
144 | |||
145 | } |
||
146 | else |
||
147 | { |
||
992 | jmachado | 148 | fullMessage += email.getFrom() != null && email.getFrom().length()>0 && !email.getFrom().equals(Globals.SYSTEM_EMAIL_BOX) ? "\n\nResponda para " + email.getFrom() : "\n\nPor favor não responda a este email"; |
936 | jmachado | 149 | fullMessage += " \n\n Mensagem Segura - enviada através da Google Apps Api através de baco 2015"; |
150 | } |
||
151 | |||
492 | jmachado | 152 | message.setText(fullMessage,"utf-8"); |
236 | jmachado | 153 | |
490 | jmachado | 154 | |
512 | jmachado | 155 | if(email.isHtml()) |
935 | jmachado | 156 | { |
512 | jmachado | 157 | message.setHeader("Content-Type","text/html; charset=\"utf-8\""); |
935 | jmachado | 158 | } |
512 | jmachado | 159 | else |
935 | jmachado | 160 | { |
512 | jmachado | 161 | message.setHeader("Content-Type","text/plain; charset=\"utf-8\""); |
935 | jmachado | 162 | } |
490 | jmachado | 163 | message.setHeader("Content-Transfer-Encoding", "quoted-printable"); |
811 | jmachado | 164 | logger.info("Sending email to transport:"+fullMessage); |
492 | jmachado | 165 | |
811 | jmachado | 166 | |
933 | jmachado | 167 | |
298 | jmachado | 168 | Transport.send(message); |
169 | } |
||
236 | jmachado | 170 | } |
171 | catch(MessagingException e) |
||
172 | { |
||
173 | logger.error(e,e); |
||
174 | throw new ServiceException(e.toString()); |
||
175 | } |
||
29 | jmachado | 176 | } |
236 | jmachado | 177 | |
178 | public Email getSendUrlToFriendEmail(1.5.0/docs/api/java/lang/String.html">String url, 1.5.0/docs/api/java/lang/String.html">String from, LangEnum langEnum, List<String> recipients) |
||
179 | { |
||
180 | List<String> arguments = new ArrayList<String>(); |
||
181 | arguments.add(from); |
||
182 | arguments.add(url); |
||
183 | 1.5.0/docs/api/java/lang/String.html">String subjectTxt; |
||
184 | 1.5.0/docs/api/java/lang/String.html">String template; |
||
185 | |||
186 | if(langEnum == LangEnum.PORTUGUESE) |
||
187 | { |
||
188 | template = Globals.EMAIL_SEND_URL_FRIEND_TEMPLATE_PT; |
||
237 | jmachado | 189 | subjectTxt = Globals.EMAIL_SEND_URL_FRIEND_SUBJECT_PT; |
236 | jmachado | 190 | } |
191 | else |
||
192 | { |
||
193 | template = Globals.EMAIL_SEND_URL_FRIEND_TEMPLATE_EN; |
||
237 | jmachado | 194 | subjectTxt = Globals.EMAIL_SEND_URL_FRIEND_SUBJECT_EN; |
236 | jmachado | 195 | } |
196 | |||
197 | 1.5.0/docs/api/java/lang/String.html">String subject = java.text.1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat.format(subjectTxt,new 1.5.0/docs/api/java/lang/String.html">String[]{from}); |
||
198 | return new Email(subject ,recipients, Globals.SYSTEM_EMAIL_BOX,template,arguments); |
||
199 | } |
||
200 | |||
201 | public boolean sendUrlToFriend(1.5.0/docs/api/java/lang/String.html">String url, 1.5.0/docs/api/java/lang/String.html">String from, LangEnum langEnum, List<String> recipients) |
||
202 | { |
||
203 | try |
||
204 | { |
||
205 | sendEmail(getSendUrlToFriendEmail(url,from,langEnum,recipients)); |
||
206 | return true; |
||
207 | } |
||
208 | catch (ServiceException e) |
||
209 | { |
||
210 | return false; |
||
211 | } |
||
212 | } |
||
213 | |||
237 | jmachado | 214 | public boolean sendCourseUnitAnnouncement(CourseUnitAnnouncement announcement, LangEnum langEnum, 1.5.0/docs/api/java/lang/String.html">String from, List<User> users) |
215 | { |
||
216 | try |
||
217 | { |
||
218 | sendEmail(getCourseUnitAnnouncementEmail(announcement,langEnum,from,users)); |
||
219 | return true; |
||
220 | } |
||
221 | catch (ServiceException e) |
||
222 | { |
||
223 | return false; |
||
224 | } |
||
225 | } |
||
236 | jmachado | 226 | |
1089 | jmachado | 227 | public static class SendTo |
512 | jmachado | 228 | { |
1089 | jmachado | 229 | public 1.5.0/docs/api/java/lang/String.html">String name; |
230 | public 1.5.0/docs/api/java/lang/String.html">String email; |
||
231 | public 1.5.0/docs/api/java/lang/String.html">String outEmail; |
||
232 | } |
||
233 | |||
234 | public Email getMessageEmail(1.5.0/docs/api/java/lang/String.html">String from, 1.5.0/docs/api/java/lang/String.html">String subject,1.5.0/docs/api/java/lang/String.html">String text, LangEnum langEnum, 1.5.0/docs/api/java/lang/String.html">String target, List<String> emails, User user) |
||
235 | { |
||
1260 | jmachado | 236 | return getMessageEmailAdulteredFromEmail(null,from,subject,text,langEnum,target,emails,user); |
237 | } |
||
238 | |||
239 | public Email getMessageEmailAdulteredFromEmail(1.5.0/docs/api/java/lang/String.html">String fromEmail, 1.5.0/docs/api/java/lang/String.html">String from, 1.5.0/docs/api/java/lang/String.html">String subject,1.5.0/docs/api/java/lang/String.html">String text, LangEnum langEnum, 1.5.0/docs/api/java/lang/String.html">String target, List<String> emails, User user) |
||
240 | { |
||
512 | jmachado | 241 | // # 0 : From |
242 | // # 1 : Subject |
||
243 | // # 2 : Text |
||
244 | // # 3 : person |
||
245 | // # 4 : target |
||
246 | List<String> arguments = new ArrayList<String>(); |
||
247 | arguments.add(from); |
||
248 | arguments.add(subject); |
||
249 | arguments.add(text); |
||
250 | arguments.add(user.getName()); |
||
251 | arguments.add(target); |
||
1260 | jmachado | 252 | arguments.add(fromEmail); |
512 | jmachado | 253 | |
254 | 1.5.0/docs/api/java/lang/String.html">String template; |
||
255 | |||
256 | if(langEnum == LangEnum.PORTUGUESE) |
||
257 | { |
||
258 | template = Globals.EMAIL_MESSAGE_TEMPLATE_PT; |
||
259 | } |
||
260 | else |
||
261 | { |
||
262 | template = Globals.EMAIL_MESSAGE_TEMPLATE_EN; |
||
263 | } |
||
264 | |||
265 | List<String> recipients = new ArrayList<String>(); |
||
1089 | jmachado | 266 | for(1.5.0/docs/api/java/lang/String.html">String e: emails) |
267 | { |
||
268 | if(Email.validEmail(e)) |
||
269 | recipients.add(e); |
||
270 | } |
||
271 | logger.info("subject: " + subject); |
||
1260 | jmachado | 272 | if(fromEmail == null) |
273 | logger.info("user: " + user.getEmail()); |
||
274 | else |
||
275 | logger.info("user altered to: " + fromEmail); |
||
1089 | jmachado | 276 | logger.info("template: " + template); |
277 | logger.info("arguments:"); |
||
278 | for(1.5.0/docs/api/java/lang/String.html">String arg: arguments) |
||
279 | logger.info(arg); |
||
280 | logger.info("recipients:"); |
||
281 | for(1.5.0/docs/api/java/lang/String.html">String rec: recipients) |
||
282 | logger.info(rec); |
||
283 | logger.info("arguments end"); |
||
1260 | jmachado | 284 | fromEmail = fromEmail == null ? user.getEmail() : fromEmail; |
285 | Email email = new Email(subject ,recipients,fromEmail,template,arguments,Message.RecipientType.BCC); |
||
1089 | jmachado | 286 | email.setFromName(from + " (ESTG)"); |
287 | return email; |
||
288 | } |
||
289 | |||
290 | public Email getMessageEmailOld(1.5.0/docs/api/java/lang/String.html">String from, 1.5.0/docs/api/java/lang/String.html">String subject,1.5.0/docs/api/java/lang/String.html">String text, LangEnum langEnum, 1.5.0/docs/api/java/lang/String.html">String target, List<User> users, User user) |
||
291 | { |
||
292 | // # 0 : From |
||
293 | // # 1 : Subject |
||
294 | // # 2 : Text |
||
295 | // # 3 : person |
||
296 | // # 4 : target |
||
297 | List<String> arguments = new ArrayList<String>(); |
||
298 | arguments.add(from); |
||
299 | arguments.add(subject); |
||
300 | arguments.add(text); |
||
301 | arguments.add(user.getName()); |
||
302 | arguments.add(target); |
||
303 | |||
304 | 1.5.0/docs/api/java/lang/String.html">String template; |
||
305 | |||
306 | if(langEnum == LangEnum.PORTUGUESE) |
||
307 | { |
||
308 | template = Globals.EMAIL_MESSAGE_TEMPLATE_PT; |
||
309 | } |
||
310 | else |
||
311 | { |
||
312 | template = Globals.EMAIL_MESSAGE_TEMPLATE_EN; |
||
313 | } |
||
314 | |||
315 | List<String> recipients = new ArrayList<String>(); |
||
512 | jmachado | 316 | for(User u: users) |
317 | { |
||
318 | if(Email.validEmail(u.getEmail())) |
||
319 | recipients.add(u.getEmail()); |
||
320 | if(Email.validEmail(u.getOutEmail())) |
||
321 | recipients.add(u.getOutEmail()); |
||
322 | } |
||
810 | jmachado | 323 | logger.info("subject: " + subject); |
324 | logger.info("user: " + user.getEmail()); |
||
325 | logger.info("template: " + template); |
||
326 | logger.info("arguments:"); |
||
327 | for(1.5.0/docs/api/java/lang/String.html">String arg: arguments) |
||
328 | logger.info(arg); |
||
329 | logger.info("recipients:"); |
||
330 | for(1.5.0/docs/api/java/lang/String.html">String rec: recipients) |
||
331 | logger.info(rec); |
||
332 | logger.info("arguments end"); |
||
872 | jmachado | 333 | Email email = new Email(subject ,recipients,user.getEmail(),template,arguments,Message.RecipientType.BCC); |
334 | email.setFromName(from + " (ESTG)"); |
||
335 | return email; |
||
512 | jmachado | 336 | } |
337 | |||
237 | jmachado | 338 | public Email getCourseUnitAnnouncementEmail(CourseUnitAnnouncement announcement, LangEnum langEnum, 1.5.0/docs/api/java/lang/String.html">String from, List<User> users) |
339 | { |
||
340 | List<String> arguments = new ArrayList<String>(); |
||
341 | arguments.add(announcement.getCourseUnit().getName()); |
||
342 | arguments.add(announcement.getTitle()); |
||
343 | arguments.add(announcement.getText()); |
||
344 | arguments.add(from); |
||
345 | |||
346 | 1.5.0/docs/api/java/lang/String.html">String subjectTxt; |
||
347 | 1.5.0/docs/api/java/lang/String.html">String template; |
||
348 | |||
349 | if(langEnum == LangEnum.PORTUGUESE) |
||
350 | { |
||
351 | template = Globals.EMAIL_COURSEUNIT_ANNOUNCEMENT_TEMPLATE_PT; |
||
352 | subjectTxt = Globals.EMAIL_COURSEUNIT_ANNOUNCEMENT_SUBJECT_PT; |
||
353 | } |
||
354 | else |
||
355 | { |
||
356 | template = Globals.EMAIL_COURSEUNIT_ANNOUNCEMENT_TEMPLATE_EN; |
||
357 | subjectTxt = Globals.EMAIL_COURSEUNIT_ANNOUNCEMENT_SUBJECT_EN; |
||
358 | } |
||
359 | |||
360 | List<String> recipients = new ArrayList<String>(); |
||
361 | for(User u: users) |
||
362 | { |
||
298 | jmachado | 363 | if(Email.validEmail(u.getEmail())) |
364 | recipients.add(u.getEmail()); |
||
419 | jmachado | 365 | if(Email.validEmail(u.getOutEmail())) |
366 | recipients.add(u.getOutEmail()); |
||
237 | jmachado | 367 | } |
368 | |||
369 | 1.5.0/docs/api/java/lang/String.html">String subject = java.text.1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat.format(subjectTxt,new 1.5.0/docs/api/java/lang/String.html">String[]{announcement.getCourseUnit().getNormalizedName(),announcement.getTitle()}); |
||
419 | jmachado | 370 | 1.5.0/docs/api/java/lang/String.html">String fromEmail = Globals.SYSTEM_EMAIL_BOX; |
371 | if(announcement.getOwner() != null && Email.validEmail(announcement.getOwner().getEmail())) |
||
372 | fromEmail = announcement.getOwner().getEmail(); |
||
373 | return new Email(subject ,recipients,fromEmail,template,arguments,Message.RecipientType.BCC); |
||
237 | jmachado | 374 | } |
375 | |||
363 | jmachado | 376 | public Email getCourseUnitBlogPostEmail(BlogPost post, LangEnum langEnum, 1.5.0/docs/api/java/lang/String.html">String from, List<User> users) |
377 | { |
||
378 | List<String> arguments = new ArrayList<String>(); |
||
379 | arguments.add(((CourseUnitBlog)post.getBlog()).getCourseUnit().getName()); |
||
380 | arguments.add(post.getTitle()); |
||
381 | arguments.add(post.getText()); |
||
382 | arguments.add(from); |
||
237 | jmachado | 383 | |
363 | jmachado | 384 | 1.5.0/docs/api/java/lang/String.html">String subjectTxt; |
385 | 1.5.0/docs/api/java/lang/String.html">String template; |
||
386 | |||
387 | if(langEnum == LangEnum.PORTUGUESE) |
||
388 | { |
||
389 | template = Globals.EMAIL_COURSEUNIT_BLOGPOST_TEMPLATE_PT; |
||
390 | subjectTxt = Globals.EMAIL_COURSEUNIT_BLOGPOST_SUBJECT_PT; |
||
391 | } |
||
392 | else |
||
393 | { |
||
394 | template = Globals.EMAIL_COURSEUNIT_BLOGPOST_TEMPLATE_EN; |
||
395 | subjectTxt = Globals.EMAIL_COURSEUNIT_BLOGPOST_SUBJECT_EN; |
||
396 | } |
||
397 | |||
398 | List<String> recipients = new ArrayList<String>(); |
||
399 | for(User u: users) |
||
400 | { |
||
401 | if(Email.validEmail(u.getEmail())) |
||
402 | recipients.add(u.getEmail()); |
||
419 | jmachado | 403 | if(Email.validEmail(u.getOutEmail())) |
404 | recipients.add(u.getOutEmail()); |
||
363 | jmachado | 405 | } |
406 | |||
407 | 1.5.0/docs/api/java/lang/String.html">String subject = java.text.1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat.format(subjectTxt,new 1.5.0/docs/api/java/lang/String.html">String[]{((CourseUnitBlog)post.getBlog()).getCourseUnit().getNormalizedName(),post.getTitle()}); |
||
419 | jmachado | 408 | 1.5.0/docs/api/java/lang/String.html">String fromEmail = Globals.SYSTEM_EMAIL_BOX; |
409 | if(post.getOwner() != null && Email.validEmail(post.getOwner().getEmail())) |
||
410 | fromEmail = post.getOwner().getEmail(); |
||
411 | return new Email(subject ,recipients, fromEmail, template,arguments,Message.RecipientType.BCC); |
||
363 | jmachado | 412 | } |
413 | |||
440 | jmachado | 414 | public Email getCourseUnitWorkEmail(CourseUnitAssignement assignement, LangEnum langEnum, UserSession userSession, List<User> users, boolean newWork) |
419 | jmachado | 415 | { |
416 | List<String> arguments = new ArrayList<String>(); |
||
417 | arguments.add(assignement.getCourseUnit().getName()); |
||
418 | arguments.add(assignement.getTitle()); |
||
419 | arguments.add(assignement.getDescription()); |
||
440 | jmachado | 420 | |
419 | jmachado | 421 | if(userSession != null && userSession.getName() != null) |
422 | arguments.add(userSession.getName()); |
||
423 | else |
||
424 | arguments.add("unknown"); |
||
363 | jmachado | 425 | |
440 | jmachado | 426 | arguments.add( DatesUtils.getStringFromDate(assignement.getDeliverDate())); |
492 | jmachado | 427 | |
419 | jmachado | 428 | 1.5.0/docs/api/java/lang/String.html">String subjectTxt; |
429 | 1.5.0/docs/api/java/lang/String.html">String template; |
||
430 | |||
431 | if(langEnum == LangEnum.PORTUGUESE) |
||
432 | { |
||
440 | jmachado | 433 | if(newWork) |
434 | template = Globals.EMAIL_COURSEUNIT_WORK_TEMPLATE_PT; |
||
435 | else |
||
436 | template = Globals.EMAIL_COURSEUNIT_WORK_CHANGE_TEMPLATE_PT; |
||
419 | jmachado | 437 | subjectTxt = Globals.EMAIL_COURSEUNIT_WORK_SUBJECT_PT; |
438 | } |
||
439 | else |
||
440 | { |
||
440 | jmachado | 441 | if(newWork) |
442 | template = Globals.EMAIL_COURSEUNIT_WORK_TEMPLATE_EN; |
||
443 | else |
||
444 | template = Globals.EMAIL_COURSEUNIT_WORK_CHANGE_TEMPLATE_EN; |
||
419 | jmachado | 445 | subjectTxt = Globals.EMAIL_COURSEUNIT_WORK_SUBJECT_EN; |
446 | } |
||
447 | |||
448 | List<String> recipients = new ArrayList<String>(); |
||
449 | for(User u: users) |
||
450 | { |
||
451 | if(Email.validEmail(u.getEmail())) |
||
452 | recipients.add(u.getEmail()); |
||
453 | if(Email.validEmail(u.getOutEmail())) |
||
454 | recipients.add(u.getOutEmail()); |
||
455 | } |
||
456 | |||
457 | 1.5.0/docs/api/java/lang/String.html">String subject = java.text.1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat.format(subjectTxt,new 1.5.0/docs/api/java/lang/String.html">String[]{assignement.getCourseUnit().getNormalizedName(),assignement.getTitle()}); |
||
458 | |||
459 | 1.5.0/docs/api/java/lang/String.html">String fromEmail = Globals.SYSTEM_EMAIL_BOX; |
||
460 | if(userSession!= null && userSession.getUser() != null && Email.validEmail(userSession.getUser().getEmail())) |
||
461 | fromEmail = userSession.getUser().getEmail(); |
||
462 | return new Email(subject ,recipients,fromEmail,template,arguments,Message.RecipientType.BCC); |
||
463 | } |
||
464 | |||
465 | |||
466 | |||
236 | jmachado | 467 | public Email getNewGradesEmail(List<String[]> grades, LangEnum langEnum, 1.5.0/docs/api/java/lang/String.html">String studentEmail, 1.5.0/docs/api/java/lang/String.html">String studentName) |
468 | { |
||
469 | List<String> arguments = new ArrayList<String>(); |
||
470 | |||
471 | 1.5.0/docs/api/java/lang/String.html">String subject; |
||
472 | 1.5.0/docs/api/java/lang/String.html">String template; |
||
473 | |||
474 | if(langEnum == LangEnum.PORTUGUESE) |
||
475 | { |
||
476 | template = Globals.EMAIL_NEW_GRADES_TEMPLATE_PT; |
||
237 | jmachado | 477 | subject = Globals.EMAIL_NEW_GRADES_SUBJECT_PT; |
236 | jmachado | 478 | } |
479 | else |
||
480 | { |
||
481 | template = Globals.EMAIL_NEW_GRADES_TEMPLATE_EN; |
||
237 | jmachado | 482 | subject = Globals.EMAIL_NEW_GRADES_SUBJECT_EN; |
236 | jmachado | 483 | } |
484 | |||
485 | 1.5.0/docs/api/java/lang/StringBuilder.html">StringBuilder gradesBuilder = new 1.5.0/docs/api/java/lang/StringBuilder.html">StringBuilder(); |
||
486 | for(1.5.0/docs/api/java/lang/String.html">String[] grade: grades) |
||
487 | { |
||
488 | 1.5.0/docs/api/java/lang/String.html">String gradeTxt = TemplateUtils.formatMessageTemplate(gradeUnitTemplate,grade); |
||
489 | gradesBuilder.append(gradeTxt); |
||
490 | } |
||
491 | |||
492 | arguments.add(studentName); |
||
493 | arguments.add(gradesBuilder.toString()); |
||
494 | |||
495 | List<String> recipients = new ArrayList<String>(); |
||
496 | recipients.add(studentEmail); |
||
497 | return new Email(subject ,recipients, Globals.SYSTEM_EMAIL_BOX,template,arguments); |
||
498 | } |
||
499 | |||
358 | jmachado | 500 | public Email getSummaryEmail(List<String> emails, CourseUnitSummary cUS, CourseUnit c, LangEnum langEnum) |
501 | { |
||
502 | List<String> arguments = new ArrayList<String>(); |
||
503 | |||
504 | 1.5.0/docs/api/java/lang/String.html">String data = DatesUtils.getStringFromDate(cUS.getDataAula()); |
||
505 | 1.5.0/docs/api/java/lang/String.html">String subject; |
||
506 | 1.5.0/docs/api/java/lang/String.html">String template; |
||
507 | |||
508 | if(langEnum == LangEnum.PORTUGUESE) |
||
509 | { |
||
510 | template = Globals.EMAIL_SUMMARY_TEMPLATE_PT; |
||
511 | subject = Globals.EMAIL_SUMMARY_SUBJECT_PT; |
||
512 | } |
||
513 | else |
||
514 | { |
||
515 | template = Globals.EMAIL_SUMMARY_TEMPLATE_EN; |
||
516 | subject = Globals.EMAIL_SUMMARY_SUBJECT_EN; |
||
517 | } |
||
518 | subject = TemplateUtils.formatMessageTemplate(subject,new 1.5.0/docs/api/java/lang/String.html">String[]{c.getName()}); |
||
519 | |||
520 | arguments.add(c.getName()); |
||
521 | arguments.add(""+cUS.getNumeroAula()); |
||
522 | arguments.add(data); |
||
523 | arguments.add(cUS.getTitulo()); |
||
524 | arguments.add(cUS.getDescricao()); |
||
525 | |||
526 | List<String> recipients = new ArrayList<String>(); |
||
527 | for(1.5.0/docs/api/java/lang/String.html">String email:emails) |
||
528 | recipients.add(email); |
||
529 | return new Email(subject ,recipients, Globals.SYSTEM_EMAIL_BOX,template,arguments); |
||
530 | } |
||
531 | |||
995 | jmachado | 532 | |
533 | //TODO TESTAR |
||
534 | public Email getSummariesEmail(List<String> emails, List<CourseUnitSummary> cUS, CourseUnit c, LangEnum langEnum) |
||
535 | { |
||
536 | |||
537 | |||
538 | 1.5.0/docs/api/java/lang/String.html">String ultimaData = DatesUtils.getStringFromDate(cUS.get(cUS.size() - 1).getDataAula()); |
||
539 | 1.5.0/docs/api/java/lang/String.html">String subject; |
||
540 | 1.5.0/docs/api/java/lang/String.html">String template; |
||
541 | 1.5.0/docs/api/java/lang/String.html">String templateFragment; |
||
542 | |||
543 | if(langEnum == LangEnum.PORTUGUESE) |
||
544 | { |
||
545 | template = Globals.EMAIL_SUMMARIES_TEMPLATE_PT; |
||
546 | templateFragment = Globals.EMAIL_SUMMARIES_FRAGMENT_TEMPLATE_PT; |
||
547 | subject = Globals.EMAIL_SUMMARY_SUBJECT_PT; |
||
548 | } |
||
549 | else |
||
550 | { |
||
551 | template = Globals.EMAIL_SUMMARIES_TEMPLATE_EN; |
||
552 | templateFragment = Globals.EMAIL_SUMMARIES_FRAGMENT_TEMPLATE_EN; |
||
553 | subject = Globals.EMAIL_SUMMARY_SUBJECT_EN; |
||
554 | } |
||
555 | subject = TemplateUtils.formatMessageTemplate(subject,new 1.5.0/docs/api/java/lang/String.html">String[]{c.getName()}); |
||
556 | |||
557 | 1.5.0/docs/api/java/lang/StringBuilder.html">StringBuilder fragments = new 1.5.0/docs/api/java/lang/StringBuilder.html">StringBuilder(); |
||
558 | |||
559 | List<String> arguments = new ArrayList<String>(); |
||
560 | 1.5.0/docs/api/java/lang/String.html">String fragmentSummaryTemplate = TemplateUtils.getOrLoadTemplate(Globals.TEMPLATE_EMAIL_PATH + "/" + templateFragment); |
||
561 | for(CourseUnitSummary summary: cUS) |
||
562 | { |
||
563 | List<String> argumentsFrag = new ArrayList<String>(); |
||
564 | argumentsFrag.add(summary.getTitulo()); |
||
565 | argumentsFrag.add(DatesUtils.getStringFromDate(summary.getDataAula())); |
||
566 | argumentsFrag.add(summary.getDescricao()); |
||
567 | 1.5.0/docs/api/java/lang/String.html">String fullMessage = 1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat.format(fragmentSummaryTemplate, argumentsFrag.toArray()); |
||
568 | fragments.append("\n").append(fullMessage); |
||
569 | } |
||
570 | |||
571 | arguments.add(c.getName()); |
||
572 | arguments.add(ultimaData); |
||
573 | arguments.add(fragments.toString()); |
||
574 | |||
575 | List<String> recipients = new ArrayList<String>(); |
||
576 | for(1.5.0/docs/api/java/lang/String.html">String email:emails) |
||
577 | recipients.add(email); |
||
578 | return new Email(subject ,recipients, Globals.SYSTEM_EMAIL_BOX,template,arguments); |
||
579 | } |
||
580 | |||
751 | jmachado | 581 | public Email getUnidadeEdicaoEmail(List<String> emails, CourseUnit c, LangEnum langEnum) |
582 | { |
||
583 | List<String> arguments = new ArrayList<String>(); |
||
584 | |||
585 | |||
586 | 1.5.0/docs/api/java/lang/String.html">String subject; |
||
587 | 1.5.0/docs/api/java/lang/String.html">String template; |
||
588 | |||
589 | if(langEnum == LangEnum.PORTUGUESE) |
||
590 | { |
||
591 | template = Globals.EMAIL_UNDADE_EDICAO_TEMPLATE_PT; |
||
592 | subject = Globals.EMAIL_UNDADE_EDICAO_SUBJECT_PT; |
||
593 | } |
||
594 | else |
||
595 | { |
||
596 | template = Globals.EMAIL_UNDADE_EDICAO_TEMPLATE_EN; |
||
597 | subject = Globals.EMAIL_UNDADE_EDICAO_SUBJECT_EN; |
||
598 | } |
||
599 | subject = 1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat.format(ConfigProperties.getProperty(subject),new 1.5.0/docs/api/java/lang/String.html">String[]{c.getName()}); |
||
600 | |||
601 | arguments.add(c.getName()); |
||
602 | |||
603 | List<String> recipients = new ArrayList<String>(); |
||
604 | for(1.5.0/docs/api/java/lang/String.html">String email:emails) |
||
605 | recipients.add(email); |
||
606 | return new Email(subject ,recipients, Globals.SYSTEM_EMAIL_BOX,template,arguments); |
||
607 | } |
||
608 | |||
609 | public Email getUnidadeValidadaEmail(List<String> emails, CourseUnit c, LangEnum langEnum) |
||
610 | { |
||
611 | List<String> arguments = new ArrayList<String>(); |
||
612 | |||
613 | |||
614 | 1.5.0/docs/api/java/lang/String.html">String subject; |
||
615 | 1.5.0/docs/api/java/lang/String.html">String template; |
||
616 | |||
617 | if(langEnum == LangEnum.PORTUGUESE) |
||
618 | { |
||
619 | template = Globals.EMAIL_UNDADE_VALIDADA_TEMPLATE_PT; |
||
620 | subject = Globals.EMAIL_UNDADE_VALIDADA_SUBJECT_PT; |
||
621 | } |
||
622 | else |
||
623 | { |
||
624 | template = Globals.EMAIL_UNDADE_VALIDADA_TEMPLATE_EN; |
||
625 | subject = Globals.EMAIL_UNDADE_VALIDADA_SUBJECT_EN; |
||
626 | } |
||
627 | subject = 1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat.format(ConfigProperties.getProperty(subject),new 1.5.0/docs/api/java/lang/String.html">String[]{c.getName()}); |
||
628 | |||
629 | arguments.add(c.getName()); |
||
630 | |||
631 | List<String> recipients = new ArrayList<String>(); |
||
632 | for(1.5.0/docs/api/java/lang/String.html">String email:emails) |
||
633 | recipients.add(email); |
||
634 | return new Email(subject ,recipients, Globals.SYSTEM_EMAIL_BOX,template,arguments); |
||
635 | } |
||
636 | |||
637 | public Email getUnidadeEditadaEmail(List<String> emails, CourseUnit c, LangEnum langEnum) |
||
638 | { |
||
639 | List<String> arguments = new ArrayList<String>(); |
||
640 | |||
641 | |||
642 | 1.5.0/docs/api/java/lang/String.html">String subject; |
||
643 | 1.5.0/docs/api/java/lang/String.html">String template; |
||
644 | |||
645 | if(langEnum == LangEnum.PORTUGUESE) |
||
646 | { |
||
647 | template = Globals.EMAIL_UNDADE_EDITADA_TEMPLATE_PT; |
||
648 | subject = Globals.EMAIL_UNDADE_EDITADA_SUBJECT_PT; |
||
649 | } |
||
650 | else |
||
651 | { |
||
652 | template = Globals.EMAIL_UNDADE_EDITADA_TEMPLATE_EN; |
||
653 | subject = Globals.EMAIL_UNDADE_EDITADA_SUBJECT_EN; |
||
654 | } |
||
655 | subject = 1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat.format(ConfigProperties.getProperty(subject),new 1.5.0/docs/api/java/lang/String.html">String[]{c.getName()}); |
||
656 | |||
657 | arguments.add(c.getCourseName()); |
||
658 | arguments.add(c.getName()); |
||
659 | |||
660 | List<String> recipients = new ArrayList<String>(); |
||
661 | for(1.5.0/docs/api/java/lang/String.html">String email:emails) |
||
662 | recipients.add(email); |
||
663 | return new Email(subject ,recipients, Globals.SYSTEM_EMAIL_BOX,template,arguments); |
||
664 | } |
||
665 | |||
1033 | jmachado | 666 | public Email getRelatorioAvaliacaoUnidadeEditadaEmail(List<String> emails, CourseUnit c, LangEnum langEnum,1.5.0/docs/api/java/lang/String.html">String estado) |
667 | { |
||
668 | List<String> arguments = new ArrayList<String>(); |
||
669 | |||
670 | |||
671 | 1.5.0/docs/api/java/lang/String.html">String subject; |
||
672 | 1.5.0/docs/api/java/lang/String.html">String template; |
||
673 | |||
674 | if(langEnum == LangEnum.PORTUGUESE) |
||
675 | { |
||
676 | template = Globals.EMAIL_RELATORIO_AVALIACAO_UNDADE_EDITADA_TEMPLATE_PT; |
||
677 | subject = Globals.EMAIL_RELATORIO_AVALIACAO_UNDADE_EDITADA_SUBJECT_PT; |
||
678 | } |
||
679 | else |
||
680 | { |
||
681 | template = Globals.EMAIL_RELATORIO_AVALIACAO_UNDADE_EDITADA_TEMPLATE_EN; |
||
682 | subject = Globals.EMAIL_RELATORIO_AVALIACAO_UNDADE_EDITADA_SUBJECT_EN; |
||
683 | } |
||
684 | subject = 1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat.format(ConfigProperties.getProperty(subject),new 1.5.0/docs/api/java/lang/String.html">String[]{c.getName()}); |
||
685 | |||
686 | arguments.add(c.getCourseName()); |
||
687 | arguments.add(c.getName()); |
||
688 | arguments.add(estado); |
||
689 | |||
690 | List<String> recipients = new ArrayList<String>(); |
||
691 | for(1.5.0/docs/api/java/lang/String.html">String email:emails) |
||
692 | recipients.add(email); |
||
693 | return new Email(subject ,recipients, Globals.SYSTEM_EMAIL_BOX,template,arguments); |
||
694 | } |
||
695 | |||
236 | jmachado | 696 | public boolean sendNewGrades(List<String[]> grades, LangEnum langEnum, 1.5.0/docs/api/java/lang/String.html">String studentEmail, 1.5.0/docs/api/java/lang/String.html">String studentName) |
697 | { |
||
698 | try |
||
699 | { |
||
700 | sendEmail(getNewGradesEmail(grades,langEnum,studentEmail,studentName)); |
||
701 | return true; |
||
702 | } |
||
703 | catch (ServiceException e) |
||
704 | { |
||
705 | return false; |
||
706 | } |
||
707 | |||
708 | } |
||
709 | |||
710 | public static void main(1.5.0/docs/api/java/lang/String.html">String [] args) |
||
711 | { |
||
440 | jmachado | 712 | 1.5.0/docs/api/java/lang/System.html">System.out.println( DatesUtils.getStringFromDate(new 5+0%2Fdocs%2Fapi+Date">Date())); |
713 | // List<String[]> grades = new ArrayList<String[]>(); |
||
714 | // grades.add(new String[]{"Teste","3"}); |
||
715 | // grades.add(new String[]{"Teste2","4"}); |
||
716 | // new SendEmailService().sendNewGrades(grades,LangEnum.PORTUGUESE,"jmachado@estgp.pt","jorge Machado"); |
||
236 | jmachado | 717 | } |
718 | |||
29 | jmachado | 719 | } |