如何在AEM工作流程中向工作流程启动器发送批准或拒绝电子邮件?

时间:2016-05-20 08:29:57

标签: workflow aem

我想创建一个工作流程,通过审批者评论向工作流程启动器发送批准或拒绝电子邮件。

1 个答案:

答案 0 :(得分:0)

您可以为电子邮件通知编写自定义工作流程过程。在该类中,您可以从工作流实例模型中读取工作流启动器。此数据保存在userid属性中的元数据节点下。

import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail.HtmlEmail;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.osgi.PropertiesUtil;

import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.mailer.MessageGateway;
import com.day.cq.mailer.MessageGatewayService;

@Service
@Component(immediate = true)
public final class NotificationWorkflowProcess implements WorkflowProcess {

    @Reference
    private MessageGatewayService messageService;

    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
        ResourceResolver resolver = workflowSession.adaptTo(ResourceResolver.class); // or get admin resolver here
        UserManager userManager = resolver.adaptTo(UserManager.class);

        MessageGateway<HtmlEmail> messageGateway = messageService.getGateway(HtmlEmail.class);
        try {
            String initiator = workItem.getWorkflow().getInitiator();
            Authorizable authorizable = userManager.getAuthorizable(initiator);
            String userEmail = PropertiesUtil.toString(authorizable.getProperty("profile/email"), "");
            if(StringUtils.isBlank(userEmail)) {
                return;
            }
            HtmlEmail email = new HtmlEmail();
            email.setCharset(CharEncoding.UTF_8);
            email.addTo(userEmail);
            email.setSubject("test email subject");
            email.setMsg("text email body");
            email.setHtmlMsg("<!DOCTYPE html><html><head></head><body><p>html email body</p></body></html>");
            messageGateway.send(email);
        } catch(Exception e) {
            // cannot send email. print some error
            e.printStackTrace();
        }
    }
}