001: package org.emforge.jbpm.event;
002:
003: import java.util.Map;
004:
005: import org.apache.commons.lang.StringUtils;
006: import org.apache.commons.logging.Log;
007: import org.apache.commons.logging.LogFactory;
008: import org.emforge.jbpm.BpmVariable;
009: import org.emforge.jbpm.TaskTransformer;
010: import org.emforge.jbpm.messages.TaskNotificationEmail;
011: import org.emforge.projectmanager.ProjectService;
012: import org.emforge.projectmanager.base.ProjectDO;
013: import org.emforge.projectmanager.base.ProjectRole;
014: import org.emforge.xfer.PriorityTO;
015: import org.emforge.xfer.StepTO;
016: import org.jbpm.graph.def.ActionHandler;
017: import org.jbpm.graph.exe.ExecutionContext;
018: import org.jbpm.taskmgmt.exe.TaskInstance;
019:
020: import ru.emdev.EmForge.email.EmailSender;
021: import ru.emdev.EmForge.email.EmailServices;
022: import ru.emdev.EmForge.email.EmailerException;
023: import ru.emdev.EmForge.security.EmForgeUserDetails;
024: import ru.emdev.EmForge.security.UserFactory;
025: import ru.emdev.EmForge.security.dao.UserDao;
026:
027: public class TaskAssignEvent implements ActionHandler {
028: private static final long serialVersionUID = -9100407279818763817L;
029: protected final Log logger = LogFactory.getLog(getClass());
030: private UserFactory m_userFactory;
031: private UserDao m_userDao;
032: private ProjectService m_projectService;
033:
034: private EmailServices m_emailServices;
035:
036: public void setUserFactory(UserFactory factory) {
037: m_userFactory = factory;
038: }
039:
040: public void setEmailServices(EmailServices emailServices) {
041: m_emailServices = emailServices;
042: }
043:
044: public void setUserDao(UserDao dao) {
045: m_userDao = dao;
046: }
047:
048: public void setProjectService(ProjectService i_projectService) {
049: m_projectService = i_projectService;
050: }
051:
052: /**
053: * TODO Later here we will able to place emailing,
054: * but I do not know how to get previous task
055: */
056: @SuppressWarnings("unchecked")
057: public void execute(ExecutionContext executionContext)
058: throws Exception {
059: // send email to assigned person
060: // get assigned task
061: TaskInstance task = executionContext.getTaskInstance();
062: assert task != null;
063:
064: String actorId = task.getActorId();
065: assert actorId != null;
066:
067: EmForgeUserDetails assignedUser = null;
068: ProjectRole projectRole = null;
069:
070: try {
071: assignedUser = m_userFactory.getUser(actorId);
072: } catch (Exception ex) {
073: ProjectDO project = null;
074: Map<String, Object> vars = executionContext
075: .getContextInstance().getVariables();
076: if (vars != null
077: && vars.containsKey(BpmVariable.PROJECT
078: .getVariable())
079: && vars.get(BpmVariable.PROJECT.getVariable()) instanceof ProjectDO) {
080: project = (ProjectDO) vars.get(BpmVariable.PROJECT
081: .getVariable());
082: }
083: projectRole = m_userDao.getEmailByRoleNameAndProjectId(
084: actorId, project.getId());
085: }
086:
087: EmForgeUserDetails currentUser = null;
088: try {
089: currentUser = m_userFactory.getCurrentUser();
090: } catch (Exception ex) {
091: }
092:
093: String fromEmail = null;
094: String toEmail = null;
095:
096: if (assignedUser != null) {
097: toEmail = assignedUser.getEmail();
098: } else if (projectRole != null) {
099: toEmail = StringUtils.isEmpty(projectRole.getEmail()) ? null
100: : projectRole.getEmail();
101: }
102:
103: if (currentUser != null) {
104: fromEmail = currentUser.getEmail();
105: }
106:
107: if (fromEmail != null && toEmail != null
108: && !StringUtils.equals(fromEmail, toEmail)) {
109: sendEmail(currentUser, toEmail, task);
110: }
111:
112: }
113:
114: /** send email to assigner
115: *
116: * TODO make it async
117: *
118: * @param i_from
119: * @param i_to
120: * @param i_newTask
121: */
122: private void sendEmail(final EmForgeUserDetails i_from,
123: final String i_to_email, final TaskInstance i_newTask) {
124: assert i_to_email != null;
125: assert i_newTask != null;
126:
127: logger.info("Send notification email from "
128: + i_from.getUsername() + " to " + i_to_email);
129:
130: // create task bean
131: TaskTransformer transformer = new TaskTransformer(
132: m_projectService, m_userFactory);
133: StepTO step = transformer.transform(i_newTask);
134:
135: TaskNotificationEmail message = (TaskNotificationEmail) m_emailServices
136: .getEmailFactory().createEmail("tasknotification");
137:
138: message.setStep(step);
139:
140: // Set up the email
141:
142: // set email priority
143: if (step.getPriority() == PriorityTO.ASAP
144: || step.getPriority() == PriorityTO.HIGH) {
145: message.setPriority(EmailSender.PRIORITY_HIGH);
146: } else if (step.getPriority() == PriorityTO.LOW
147: || step.getPriority() == PriorityTO.LOWEST) {
148: message.setPriority(EmailSender.PRIORITY_ELEVATED);
149: } else {
150: message.setPriority(EmailSender.PRIORITY_NORMAL);
151: }
152:
153: message.setSubject(step.getName() + ": \"" + step.getTitle()
154: + "\"");
155:
156: message.setToName(i_to_email);
157: message.setFromName(i_from.getEmail());
158:
159: //send message
160: try {
161: m_emailServices.getEmailSender().sendMessage(message);
162: } catch (EmailerException ex) {
163: //process error:
164: // print error message
165: logger.error("Cannot send task notification to email "
166: + i_to_email, ex);
167: }
168: }
169:
170: }
|