use of org.kie.internal.task.api.model.EmailNotification in project jbpm by kiegroup.
the class UserTaskAdminServiceImpl method buildEmailNotification.
@Override
public EmailNotification buildEmailNotification(String subjectStr, List<OrganizationalEntity> recipients, String bodyStr, String fromStr, String replyToStr) {
EmailNotification emailNotification = TaskModelProvider.getFactory().newEmialNotification();
Map<Language, EmailNotificationHeader> emailHeaders = new HashMap<Language, EmailNotificationHeader>();
List<I18NText> subjects = new ArrayList<I18NText>();
List<I18NText> names = new ArrayList<I18NText>();
String locale = "en-UK";
EmailNotificationHeader emailHeader = TaskModelProvider.getFactory().newEmailNotificationHeader();
emailHeader.setBody(bodyStr);
emailHeader.setFrom(fromStr);
emailHeader.setReplyTo(replyToStr);
emailHeader.setLanguage(locale);
emailHeader.setSubject(subjectStr);
Language lang = TaskModelProvider.getFactory().newLanguage();
lang.setMapkey(locale);
emailHeaders.put(lang, emailHeader);
I18NText subject = TaskModelProvider.getFactory().newI18NText();
((InternalI18NText) subject).setLanguage(locale);
((InternalI18NText) subject).setText(emailHeader.getSubject());
;
subjects.add(subject);
names.add(subject);
emailNotification.setEmailHeaders(emailHeaders);
emailNotification.setNames(names);
emailNotification.setRecipients(new ArrayList<>(recipients));
emailNotification.setSubjects(subjects);
return emailNotification;
}
use of org.kie.internal.task.api.model.EmailNotification in project jbpm by kiegroup.
the class EmailNotificationListener method onNotification.
@Override
public void onNotification(NotificationEvent event, UserInfo userInfo) {
if (userInfo == null || mailSession == null) {
logger.info("Missing mail session or userinfo - skipping email notification listener processing");
return;
}
if (event.getNotification() instanceof EmailNotification) {
EmailNotification notification = (EmailNotification) event.getNotification();
Task task = event.getTask();
// group users into languages
Map<String, List<User>> users = new HashMap<String, List<User>>();
for (OrganizationalEntity entity : notification.getBusinessAdministrators()) {
if (entity instanceof Group) {
buildMapByLanguage(users, (Group) entity, userInfo);
} else {
buildMapByLanguage(users, (User) entity, userInfo);
}
}
for (OrganizationalEntity entity : notification.getRecipients()) {
if (entity instanceof Group) {
buildMapByLanguage(users, (Group) entity, userInfo);
} else {
buildMapByLanguage(users, (User) entity, userInfo);
}
}
Map<String, Object> variables = event.getContent();
Map<? extends Language, ? extends EmailNotificationHeader> headers = notification.getEmailHeaders();
for (Iterator<Map.Entry<String, List<User>>> it = users.entrySet().iterator(); it.hasNext(); ) {
try {
Map.Entry<String, List<User>> entry = it.next();
Language lang = TaskModelProvider.getFactory().newLanguage();
lang.setMapkey(entry.getKey());
EmailNotificationHeader header = headers.get(lang);
Message msg = new MimeMessage(mailSession);
Set<String> toAddresses = new HashSet<String>();
for (User user : entry.getValue()) {
String emailAddress = userInfo.getEmailForEntity(user);
if (emailAddress != null && !toAddresses.contains(emailAddress)) {
msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(emailAddress, false));
toAddresses.add(emailAddress);
} else {
logger.warn("Email address not found for user {}", user.getId());
}
}
if (header.getFrom() != null && header.getFrom().trim().length() > 0) {
User user = TaskModelProvider.getFactory().newUser();
((InternalOrganizationalEntity) user).setId(header.getFrom());
msg.setFrom(new InternetAddress(userInfo.getEmailForEntity(user)));
} else {
msg.setFrom(new InternetAddress(mailSession.getProperty("mail.from")));
}
if (header.getReplyTo() != null && header.getReplyTo().trim().length() > 0) {
User user = TaskModelProvider.getFactory().newUser();
((InternalOrganizationalEntity) user).setId(header.getReplyTo());
msg.setReplyTo(new InternetAddress[] { new InternetAddress(userInfo.getEmailForEntity(user)) });
} else if (mailSession.getProperty("mail.replyto") != null) {
msg.setReplyTo(new InternetAddress[] { new InternetAddress(mailSession.getProperty("mail.replyto")) });
}
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("doc", variables);
// add internal items to be able to reference them in templates
vars.put("processInstanceId", task.getTaskData().getProcessInstanceId());
vars.put("processSessionId", task.getTaskData().getProcessSessionId());
vars.put("workItemId", task.getTaskData().getWorkItemId());
vars.put("expirationTime", task.getTaskData().getExpirationTime());
vars.put("taskId", task.getId());
if (task.getPeopleAssignments() != null) {
vars.put("owners", task.getPeopleAssignments().getPotentialOwners());
}
String subject = (String) TemplateRuntime.eval(header.getSubject(), vars);
String body = (String) TemplateRuntime.eval(header.getBody(), vars);
if (variables.containsKey("attachments")) {
Multipart multipart = new MimeMultipart();
// prepare body as first mime body part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(body, "text/html")));
multipart.addBodyPart(messageBodyPart);
List<String> attachments = getAttachements(variables.get("attachments"));
for (String attachment : attachments) {
MimeBodyPart attachementBodyPart = new MimeBodyPart();
URL attachmentUrl = getAttachemntURL(attachment);
String contentType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(attachmentUrl.getFile());
attachementBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(attachmentUrl.openStream(), contentType)));
String fileName = new File(attachmentUrl.getFile()).getName();
attachementBodyPart.setFileName(fileName);
attachementBodyPart.setContentID("<" + fileName + ">");
multipart.addBodyPart(attachementBodyPart);
}
// Put parts in message
msg.setContent(multipart);
} else {
msg.setDataHandler(new DataHandler(new ByteArrayDataSource(body, "text/html")));
}
msg.setSubject(subject);
msg.setHeader("X-Mailer", "jbpm human task service");
msg.setSentDate(new Date());
Transport.send(msg);
} catch (Exception e) {
logger.error("Unable to send email notification due to {}", e.getMessage());
logger.debug("Stacktrace:", e);
}
}
}
}
use of org.kie.internal.task.api.model.EmailNotification in project jbpm by kiegroup.
the class ExecuteReminderCommand method buildDefaultNotification.
private Notification buildDefaultNotification(TaskData taskData, Task task) {
EmailNotification emailNotificationImpl = TaskModelProvider.getFactory().newEmialNotification();
Map<Language, EmailNotificationHeader> map = new HashMap<Language, EmailNotificationHeader>();
EmailNotificationHeader emailNotificationHeaderImpl = TaskModelProvider.getFactory().newEmailNotificationHeader();
emailNotificationHeaderImpl.setBody(buildDefafultEmailBody(taskData, task));
emailNotificationHeaderImpl.setFrom(fromUser);
emailNotificationHeaderImpl.setReplyTo(fromUser);
emailNotificationHeaderImpl.setLanguage("en-UK");
emailNotificationHeaderImpl.setSubject(buildDefafultEmailSubject(taskData, task));
Language language = TaskModelProvider.getFactory().newLanguage();
language.setMapkey("en-UK");
map.put(language, emailNotificationHeaderImpl);
emailNotificationImpl.setEmailHeaders(map);
List<OrganizationalEntity> recipients = new ArrayList<OrganizationalEntity>();
recipients.add(taskData.getActualOwner());
emailNotificationImpl.setRecipients(recipients);
return emailNotificationImpl;
}
use of org.kie.internal.task.api.model.EmailNotification in project jbpm by kiegroup.
the class UserTaskAdminServiceImplTest method testNotifyNotCompleted.
@Test(timeout = 10000)
public void testNotifyNotCompleted() throws Exception {
processInstanceId = processService.startProcess(deploymentUnit.getIdentifier(), "org.jbpm.writedocument");
Assertions.assertThat(processInstanceId).isNotNull();
List<TaskSummary> tasks = runtimeDataService.getTasksAssignedAsPotentialOwner("salaboy", new QueryFilter());
Assertions.assertThat(tasks).hasSize(1);
TaskSummary task = tasks.get(0);
Collection<TaskNotification> notifications = userTaskAdminService.getTaskNotifications(task.getId(), false);
Assertions.assertThat(notifications).isNotNull();
Assertions.assertThat(notifications).hasSize(0);
userTaskService.start(task.getId(), "salaboy");
List<OrganizationalEntity> recipients = new ArrayList<>();
recipients.add(factory.newUser("john"));
EmailNotification emailNotification = userTaskAdminService.buildEmailNotification("test", recipients, "Simple body", "Administrator", "");
userTaskAdminService.notifyWhenNotCompleted(task.getId(), "2s", emailNotification);
notifications = userTaskAdminService.getTaskNotifications(task.getId(), false);
Assertions.assertThat(notifications).isNotNull();
Assertions.assertThat(notifications).hasSize(1);
CountDownListenerFactory.getExistingTask("userTaskAdminService").waitTillCompleted();
tasks = runtimeDataService.getTasksAssignedAsPotentialOwner("salaboy", new QueryFilter());
Assertions.assertThat(tasks).hasSize(1);
notifications = userTaskAdminService.getTaskNotifications(task.getId(), true);
Assertions.assertThat(notifications).isNotNull();
Assertions.assertThat(notifications).hasSize(0);
notifications = userTaskAdminService.getTaskNotifications(task.getId(), false);
Assertions.assertThat(notifications).isNotNull();
Assertions.assertThat(notifications).hasSize(1);
}
use of org.kie.internal.task.api.model.EmailNotification in project jbpm by kiegroup.
the class UserTaskAdminServiceImplTest method testNotifyNotStartedAndCancel.
@Test(timeout = 10000)
public void testNotifyNotStartedAndCancel() throws Exception {
processInstanceId = processService.startProcess(deploymentUnit.getIdentifier(), "org.jbpm.writedocument");
Assertions.assertThat(processInstanceId).isNotNull();
List<TaskSummary> tasks = runtimeDataService.getTasksAssignedAsPotentialOwner("salaboy", new QueryFilter());
Assertions.assertThat(tasks).hasSize(1);
TaskSummary task = tasks.get(0);
Collection<TaskNotification> notifications = userTaskAdminService.getTaskNotifications(task.getId(), false);
Assertions.assertThat(notifications).isNotNull();
Assertions.assertThat(notifications).hasSize(0);
List<OrganizationalEntity> recipients = new ArrayList<>();
recipients.add(factory.newUser("john"));
EmailNotification emailNotification = userTaskAdminService.buildEmailNotification("test", recipients, "Simple body", "Administrator", "");
long notificationId = userTaskAdminService.notifyWhenNotStarted(task.getId(), "2s", emailNotification);
notifications = userTaskAdminService.getTaskNotifications(task.getId(), true);
Assertions.assertThat(notifications).isNotNull();
Assertions.assertThat(notifications).hasSize(1);
userTaskAdminService.cancelNotification(task.getId(), notificationId);
notifications = userTaskAdminService.getTaskNotifications(task.getId(), true);
Assertions.assertThat(notifications).isNotNull();
Assertions.assertThat(notifications).hasSize(0);
}
Aggregations