001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package edu.sampleu.travel.workflow; 017 018import edu.sampleu.travel.bo.TravelAccount; 019import org.kuali.rice.kew.api.identity.Id; 020import org.kuali.rice.kew.api.identity.PrincipalName; 021import org.kuali.rice.kew.api.rule.RoleName; 022import org.kuali.rice.kew.engine.RouteContext; 023import org.kuali.rice.kew.routeheader.DocumentContent; 024import org.kuali.rice.kew.rule.AbstractRoleAttribute; 025import org.kuali.rice.kew.rule.ResolvedQualifiedRole; 026import org.kuali.rice.krad.service.KRADServiceLocator; 027import org.kuali.rice.krad.workflow.WorkflowUtils; 028import org.w3c.dom.Node; 029import org.w3c.dom.NodeList; 030 031import javax.xml.namespace.QName; 032import javax.xml.xpath.XPath; 033import javax.xml.xpath.XPathConstants; 034import javax.xml.xpath.XPathExpressionException; 035import java.util.ArrayList; 036import java.util.Collections; 037import java.util.List; 038 039/** 040 * Resolves FO's using the accounts associated with the document XPath xpath = 041 * KualiWorkflowUtils.getXPath(documentContent.getDocument()); List<String> qualifiers = new ArrayList<String>(); NodeList 042 * accountNums = (NodeList)xstreamSafeEval(xpath, "//edu.sampleu.travel.workflow.bo.TravelAccount/number", 043 * documentContent.getDocument(), XPathConstants.NODESET); for (int i = 0; i < accountNums.getLength(); i++) { Node accountNum = 044 * accountNums.item(i); String accuntNumVal = accountNum.getNodeValue(); } 045 */ 046public class AccountAttribute extends AbstractRoleAttribute { 047 private static final RoleName FISCAL_OFFICER_ROLE = new RoleName(AccountAttribute.class.getName(), "FO", "Fiscal Officer"); 048 private static final List<RoleName> ROLES; 049 static { 050 List<RoleName> tmp = new ArrayList<RoleName>(1); 051 tmp.add(FISCAL_OFFICER_ROLE); 052 ROLES = Collections.unmodifiableList(tmp); 053 } 054 055 056 public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) { 057 List<String> qualifiedRoleNames = new ArrayList<String>(); 058 XPath xpath = WorkflowUtils.getXPath(documentContent.getDocument()); 059 // This xpath stream needs updating when the TravelAccount is being updated 060 NodeList accountNums = (NodeList) xstreamSafeEval(xpath, "/documentContent/applicationContent/org.kuali.rice.krad.workflow.KualiDocumentXmlMaterializer/document/travelAccounts/vector/default/elementData/edu.sampleu.travel.bo.TravelAccount/number", documentContent.getDocument(), XPathConstants.NODESET); 061 for (int i = 0; i < accountNums.getLength(); i++) { 062 Node accountNum = accountNums.item(i); 063 String accuntNumVal = accountNum.getTextContent(); 064 qualifiedRoleNames.add(accuntNumVal); 065 } 066 return qualifiedRoleNames; 067 } 068 069 070 public List<RoleName> getRoleNames() { 071 return ROLES; 072 } 073 074 075 public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) { 076 String accountNum = qualifiedRole; 077 TravelAccount account = new TravelAccount(); 078 account.setNumber(accountNum); 079 account = (TravelAccount) KRADServiceLocator.getBusinessObjectService().retrieve(account); 080 if (account == null) { 081 throw new RuntimeException("Account " + accountNum + " does not exist!"); 082 } 083 ResolvedQualifiedRole qualRole = new ResolvedQualifiedRole(); 084 qualRole.setAnnotation("Account " + accountNum + " FO"); 085 qualRole.setQualifiedRoleLabel("Fiscal Officer Account " + accountNum); 086 List<Id> ids = new ArrayList<Id>(); 087 ids.add(new PrincipalName(account.getFiscalOfficer().getUserName())); 088 qualRole.setRecipients(ids); 089 return qualRole; 090 } 091 092 093 /** 094 * This method will do a simple XPath.evaluate, while wrapping your xpathExpression with the xstreamSafe function. It assumes a 095 * String result, and will return such. If an XPathExpressionException is thrown, this will be re-thrown within a 096 * RuntimeException. 097 * 098 * @param xpath A correctly initialized XPath instance. 099 * @param xpathExpression Your XPath Expression that needs to be wrapped in an xstreamSafe wrapper and run. 100 * @param item The document contents you will be searching within. 101 * @return The string value of the xpath.evaluate(). 102 */ 103 public static final Object xstreamSafeEval(XPath xpath, String xpathExpression, Object item, QName returnType) { 104 String xstreamSafeXPath = new StringBuilder(WorkflowUtils.XSTREAM_SAFE_PREFIX).append(xpathExpression).append(WorkflowUtils.XSTREAM_SAFE_SUFFIX).toString(); 105 try { 106 return xpath.evaluate(xstreamSafeXPath, item, returnType); 107 } 108 catch (XPathExpressionException e) { 109 throw new RuntimeException("XPathExpressionException occurred on xpath: " + xstreamSafeXPath, e); 110 } 111 } 112 }