Author: ilgrosso Date: Mon Jan 7 17:06:35 2013 New Revision: 1429899 URL: http://svn.apache.org/viewvc?rev=1429899&view=rev Log: [SYNCOPE-263] recipients and events added to Velocity model + update sample mail template
Modified: syncope/branches/1_0_X/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java syncope/branches/1_0_X/core/src/main/java/org/apache/syncope/core/scheduling/NotificationJob.java syncope/branches/1_0_X/core/src/main/resources/mailTemplates/optin.html.vm syncope/branches/1_0_X/core/src/main/resources/mailTemplates/optin.txt.vm Modified: syncope/branches/1_0_X/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java URL: http://svn.apache.org/viewvc/syncope/branches/1_0_X/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java?rev=1429899&r1=1429898&r2=1429899&view=diff ============================================================================== --- syncope/branches/1_0_X/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java (original) +++ syncope/branches/1_0_X/core/src/main/java/org/apache/syncope/core/notification/NotificationManager.java Mon Jan 7 17:06:35 2013 @@ -126,18 +126,16 @@ public class NotificationManager { * * @param notification notification to take as model * @param user the user this task is about - * @param emailSchema name of user schema containing e-mail address * @return notification task, fully populated */ private NotificationTask getNotificationTask(final Notification notification, final SyncopeUser user) { - connObjectUtil.retrieveVirAttrValues(user); final List<SyncopeUser> recipients = new ArrayList<SyncopeUser>(); if (notification.getRecipients() != null) { - recipients.addAll(searchDAO.search(EntitlementUtil.getRoleIds( - entitlementDAO.findAll()), notification.getRecipients())); + recipients.addAll(searchDAO.search(EntitlementUtil.getRoleIds(entitlementDAO.findAll()), + notification.getRecipients())); } if (notification.isSelfAsRecipient()) { @@ -147,7 +145,6 @@ public class NotificationManager { Set<String> recipientEmails = new HashSet<String>(); for (SyncopeUser recipient : recipients) { - connObjectUtil.retrieveVirAttrValues(recipient); String email = getRecipientEmail( @@ -169,6 +166,8 @@ public class NotificationManager { final Map<String, Object> model = new HashMap<String, Object>(); model.put("user", userDataBinder.getUserTO(user)); model.put("syncopeConf", this.findAllSyncopeConfs()); + model.put("recipients", recipientEmails); + model.put("events", notification.getEvents()); String htmlBody; String textBody; @@ -208,11 +207,11 @@ public class NotificationManager { Set<String> events = new HashSet<String>(notification.getEvents()); events.retainAll(wfResult.getPerformedTasks()); - if (!events.isEmpty()) { + if (events.isEmpty()) { + LOG.debug("No events found about {}", user); + } else { LOG.debug("Creating notification task for events {} about {}", events, user); taskDAO.save(getNotificationTask(notification, user)); - } else { - LOG.debug("No events found about {}", user); } } } @@ -231,18 +230,22 @@ public class NotificationManager { case Username: email = user.getUsername(); break; + case UserSchema: UAttr attr = user.getAttribute(recipientAttrName); email = attr == null || attr.getValuesAsStrings().isEmpty() ? null : attr.getValuesAsStrings().get(0); break; + case UserVirtualSchema: UVirAttr virAttr = user.getVirtualAttribute(recipientAttrName); email = virAttr == null || virAttr.getValues().isEmpty() ? null : virAttr.getValues().get(0); break; + case UserDerivedSchema: UDerAttr derAttr = user.getDerivedAttribute(recipientAttrName); email = derAttr == null ? null : derAttr.getValue(user.getAttributes()); break; + default: email = null; } @@ -267,7 +270,7 @@ public class NotificationManager { /** * Mark NotificationTask with provided id as executed. - * + * * @param taskId task to be updated */ public void setTaskExecuted(final Long taskId) { Modified: syncope/branches/1_0_X/core/src/main/java/org/apache/syncope/core/scheduling/NotificationJob.java URL: http://svn.apache.org/viewvc/syncope/branches/1_0_X/core/src/main/java/org/apache/syncope/core/scheduling/NotificationJob.java?rev=1429899&r1=1429898&r2=1429899&view=diff ============================================================================== --- syncope/branches/1_0_X/core/src/main/java/org/apache/syncope/core/scheduling/NotificationJob.java (original) +++ syncope/branches/1_0_X/core/src/main/java/org/apache/syncope/core/scheduling/NotificationJob.java Mon Jan 7 17:06:35 2013 @@ -48,22 +48,22 @@ import org.springframework.mail.javamail * @see NotificationTask */ public class NotificationJob implements StatefulJob { - + enum Status { - + SENT, NOT_SENT - + } /** * Logger. */ private static final Logger LOG = LoggerFactory.getLogger(NotificationJob.class); - + @Autowired private AuditManager auditManager; - + @Autowired private NotificationManager notificationManager; @@ -78,15 +78,15 @@ public class NotificationJob implements */ @Autowired private ConfDAO confDAO; - + private String smtpHost; - + private int smtpPort; - + private String smtpUsername; - + private String smtpPassword; - + private void init() { smtpHost = confDAO.find("smtp.host", "").getValue(); smtpPort = 25; @@ -97,40 +97,43 @@ public class NotificationJob implements } smtpUsername = confDAO.find("smtp.username", "").getValue(); smtpPassword = confDAO.find("smtp.password", "").getValue(); - + LOG.debug("SMTP details fetched: {}:{} / {}:[PASSWORD_NOT_SHOWN]", new Object[]{smtpHost, smtpPort, smtpUsername}); } - + public TaskExec executeSingle(final NotificationTask task) { init(); - + TaskExec execution = new TaskExec(); execution.setTask(task); execution.setStartDate(new Date()); - + if (StringUtils.isBlank(smtpHost) || StringUtils.isBlank(task.getSender()) || StringUtils.isBlank(task.getSubject()) || task.getRecipients().isEmpty() || StringUtils.isBlank(task.getHtmlBody()) || StringUtils.isBlank(task.getTextBody())) { - + String message = "Could not fetch all required information for " + "sending e-mails:\n" + smtpHost + ":" + smtpPort + "\n" + task.getRecipients() + "\n" + task.getSender() + "\n" + task.getSubject() + "\n" + task.getHtmlBody() + "\n" + task.getTextBody(); LOG.error(message); - + execution.setStatus(Status.NOT_SENT.name()); - + if (task.getTraceLevel().ordinal() >= TraceLevel.FAILURES.ordinal()) { - execution.setMessage(message); } } else { if (LOG.isDebugEnabled()) { - LOG.debug("About to send e-mails:\n" + smtpHost + ":" + smtpPort + "\n" + task.getRecipients() + "\n" - + task.getSender() + "\n" + task.getSubject() + "\n" + task.getHtmlBody() + "\n" + LOG.debug("About to send e-mails:\n" + + smtpHost + ":" + smtpPort + "\n" + + task.getRecipients() + "\n" + + task.getSender() + "\n" + + task.getSubject() + "\n" + + task.getHtmlBody() + "\n" + task.getTextBody() + "\n"); } - + for (String to : task.getRecipients()) { try { JavaMailSenderImpl sender = new JavaMailSenderImpl(); @@ -143,18 +146,18 @@ public class NotificationJob implements if (StringUtils.isNotBlank(smtpPassword)) { sender.setPassword(smtpPassword); } - + MimeMessage message = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(to); helper.setFrom(task.getSender()); helper.setSubject(task.getSubject()); helper.setText(task.getTextBody(), task.getHtmlBody()); - + sender.send(message); - + execution.setStatus(Status.SENT.name()); - + StringBuilder report = new StringBuilder(); switch (task.getTraceLevel()) { case ALL: @@ -164,11 +167,11 @@ public class NotificationJob implements append(task.getTextBody()).append('\n').append('\n'). append(task.getHtmlBody()).append('\n'); break; - + case SUMMARY: report.append("E-mail sent to ").append(to).append('\n'); break; - + case FAILURES: case NONE: default: @@ -176,53 +179,53 @@ public class NotificationJob implements if (report.length() > 0) { execution.setMessage(report.toString()); } - + auditManager.audit(Category.notification, NotificationSubCategory.sent, Result.success, "Successfully sent notification to " + to); } catch (Exception e) { LOG.error("Could not send e-mail", e); - + execution.setStatus(Status.NOT_SENT.name()); StringWriter exceptionWriter = new StringWriter(); exceptionWriter.write(e.getMessage() + "\n\n"); e.printStackTrace(new PrintWriter(exceptionWriter)); - + if (task.getTraceLevel().ordinal() >= TraceLevel.FAILURES.ordinal()) { execution.setMessage(exceptionWriter.toString()); } - + auditManager.audit(Category.notification, NotificationSubCategory.sent, Result.failure, "Could not send notification to " + to, e); } - + execution.setEndDate(new Date()); } } - + if (hasToBeRegistered(execution)) { execution = notificationManager.storeExec(execution); } else { notificationManager.setTaskExecuted(execution.getTask().getId()); } - + return execution; } - + @Override public void execute(final JobExecutionContext context) throws JobExecutionException { - + LOG.debug("Waking up..."); - + for (NotificationTask task : taskDAO.findToExec(NotificationTask.class)) { LOG.debug("Found notification task {} to be executed: starting...", task); executeSingle(task); LOG.debug("Notification task {} executed", task); } - + LOG.debug("Sleeping again..."); } - + private boolean hasToBeRegistered(final TaskExec execution) { NotificationTask task = (NotificationTask) execution.getTask(); Modified: syncope/branches/1_0_X/core/src/main/resources/mailTemplates/optin.html.vm URL: http://svn.apache.org/viewvc/syncope/branches/1_0_X/core/src/main/resources/mailTemplates/optin.html.vm?rev=1429899&r1=1429898&r2=1429899&view=diff ============================================================================== --- syncope/branches/1_0_X/core/src/main/resources/mailTemplates/optin.html.vm (original) +++ syncope/branches/1_0_X/core/src/main/resources/mailTemplates/optin.html.vm Mon Jan 7 17:06:35 2013 @@ -26,6 +26,22 @@ under the License. Your email address is $user.getAttributeMap().get("email").getValues().get(0). </p> +<p> + This message was sent to the following recipients: +<ul> +#foreach($recipient in $recipients) + <li>$recipient</i> +#end +</ul> + +becase one of the following events occurred: +<ul> +#foreach($event in $events) + <li>$event</i> +#end +</ul> +</p> + #if(!$user.getMemberships().isEmpty()) You have been provided with the following roles: <ul> Modified: syncope/branches/1_0_X/core/src/main/resources/mailTemplates/optin.txt.vm URL: http://svn.apache.org/viewvc/syncope/branches/1_0_X/core/src/main/resources/mailTemplates/optin.txt.vm?rev=1429899&r1=1429898&r2=1429899&view=diff ============================================================================== --- syncope/branches/1_0_X/core/src/main/resources/mailTemplates/optin.txt.vm (original) +++ syncope/branches/1_0_X/core/src/main/resources/mailTemplates/optin.txt.vm Mon Jan 7 17:06:35 2013 @@ -14,6 +14,16 @@ Hi $user.getAttributeMap().get("firstnam Your username is $user.getUsername(). Your email address is $user.getAttributeMap().get("email").getValues().get(0). +This message was sent to the following recipients: +#foreach($recipient in $recipients) + * $recipient +#end + +becase one of the following events occurred: +#foreach($event in $events) + * $event +#end + #if(!$user.getMemberships().isEmpty()) You have been provided with the following roles: #foreach($membership in $user.getMemberships())