指派任務元件用於將任務指派給工作流程參與者。 當任務指派給使用者或群組時,會傳送電子郵件通知給已定義的使用者或群組成員。
此電子郵件通知通常包含與任務相關的動態資料。 此動態資料是使用產生的系統擷取 中繼資料屬性.
若要在電子郵件通知中包含來自已提交表單資料的值,我們需要建立自訂中繼資料屬性,然後在電子郵件範本中使用這些自訂中繼資料屬性
建議的方法是建立實作的getUserMetadata方法的OSGI元件 WorkitemUserMetadataService
下列程式碼會建立4個中繼資料屬性(名字,姓氏,原因 和 amountRequested)並從提交的資料中設定其值。 例如中繼資料屬性 名字的數值會根據提交的資料,設定為名為firstName的元素的值。 下列程式碼假設最適化表單提交的資料為xml格式。 以JSON結構描述或表單資料模型為基礎的最適化Forms會產生JSON格式的資料。
package com.aemforms.workitemuserservice.core;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Session;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.*;
import com.adobe.fd.workspace.service.external.WorkitemUserMetadataService;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.metadata.MetaDataMap;
@Component(property={Constants.SERVICE_DESCRIPTION+"=A sample implementation of a user metadata service.",
Constants.SERVICE_VENDOR+"=Adobe Systems",
"process.label"+"=Sample Custom Metadata Service"})
public class WorkItemUserServiceImpl implements WorkitemUserMetadataService {
private static final Logger log = LoggerFactory.getLogger(WorkItemUserServiceImpl.class);
@Override
public Map<String, String> getUserMetadata(WorkItem workItem, WorkflowSession workflowSession,MetaDataMap metadataMap)
{
HashMap<String, String> customMetadataMap = new HashMap<String, String>();
String payloadPath = workItem.getWorkflowData().getPayload().toString();
String dataFilePath = payloadPath + "/Data.xml/jcr:content";
Session session = workflowSession.adaptTo(Session.class);
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
Document xmlDocument = null;
javax.jcr.Node xmlDataNode = null;
try
{
xmlDataNode = session.getNode(dataFilePath);
InputStream xmlDataStream = xmlDataNode.getProperty("jcr:data").getBinary().getStream();
XPath xPath = javax.xml.xpath.XPathFactory.newInstance().newXPath();
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
xmlDocument = builder.parse(xmlDataStream);
Node firstNameNode = (org.w3c.dom.Node) xPath.compile("afData/afUnboundData/data/firstName")
.evaluate(xmlDocument, javax.xml.xpath.XPathConstants.NODE);
log.debug("The value of first name element is " + firstNameNode.getTextContent());
Node lastNameNode = (org.w3c.dom.Node) xPath.compile("afData/afUnboundData/data/lastName")
.evaluate(xmlDocument, javax.xml.xpath.XPathConstants.NODE);
Node amountRequested = (org.w3c.dom.Node) xPath
.compile("afData/afUnboundData/data/amountRequested")
.evaluate(xmlDocument, javax.xml.xpath.XPathConstants.NODE);
Node reason = (org.w3c.dom.Node) xPath.compile("afData/afUnboundData/data/reason")
.evaluate(xmlDocument, javax.xml.xpath.XPathConstants.NODE);
customMetadataMap.put("firstName", firstNameNode.getTextContent());
customMetadataMap.put("lastName", lastNameNode.getTextContent());
customMetadataMap.put("amountRequested", amountRequested.getTextContent());
customMetadataMap.put("reason", reason.getTextContent());
log.debug("Created " + customMetadataMap.size() + " metadata properties");
}
catch (Exception e)
{
log.debug(e.getMessage());
}
return customMetadataMap;
}
}
在電子郵件範本中,您可以使用以下語法來包含中繼資料屬性,其中amountRequested是中繼資料屬性 ${amountRequested}
在OSGi元件建立並部署到AEM伺服器後,請如下所示設定「指派工作」元件以使用自訂中繼資料屬性。
在表單提交時,任務指派通知會傳送到與管理員使用者相關聯的電子郵件ID。 下列熒幕擷圖顯示範例任務指派通知
指派任務通知的電子郵件範本需要以下列格式。
subject=任務已指派 — ${workitem_title}
message=字串,代表您的電子郵件範本,不含任何新行字元。
在某些情況下,您可能會想要在後續的任務通知中包含前一個任務擁有者的註解。 擷取任務最後評論的程式碼如下:
package samples.aemforms.taskcomments.core;
import org.osgi.service.component.annotations.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jcr.Session;
import org.osgi.framework.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.HistoryItem;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.adobe.fd.workspace.service.external.WorkitemUserMetadataService;
@Component(property = {
Constants.SERVICE_DESCRIPTION + "=A sample implementation of a user metadata service.",
Constants.SERVICE_VENDOR + "=Adobe Systems",
"process.label" + "=Capture Workflow Comments"
})
public class CaptureTaskComments implements WorkitemUserMetadataService {
private static final Logger log = LoggerFactory.getLogger(CaptureTaskComments.class);
@Override
public Map <String, String> getUserMetadata(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metadataMap) {
HashMap < String, String > customMetadataMap = new HashMap < String, String > ();
workflowSession.adaptTo(Session.class);
try {
List <HistoryItem> workItemsHistory = workflowSession.getHistory(workItem.getWorkflow());
int listSize = workItemsHistory.size();
HistoryItem lastItem = workItemsHistory.get(listSize - 1);
String reviewerComments = (String) lastItem.getWorkItem().getMetaDataMap().get("workitemComment");
log.debug("####The comment I got was ...." + reviewerComments);
customMetadataMap.put("comments", reviewerComments);
log.debug("Created " + customMetadataMap.size() + " metadata properties");
} catch (Exception e) {
log.debug(e.getMessage());
}
return customMetadataMap;
}
}
具有上述程式碼的套件組合可以是 已從此處下載