001 /** 002 * Copyright 2005-2014 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 */ 016 package edu.sampleu.travel.workflow; 017 018 import edu.sampleu.travel.bo.TravelAccount; 019 import org.kuali.rice.kew.api.identity.Id; 020 import org.kuali.rice.kew.api.identity.PrincipalName; 021 import org.kuali.rice.kew.api.rule.RoleName; 022 import org.kuali.rice.kew.engine.RouteContext; 023 import org.kuali.rice.kew.routeheader.DocumentContent; 024 import org.kuali.rice.kew.rule.AbstractRoleAttribute; 025 import org.kuali.rice.kew.rule.ResolvedQualifiedRole; 026 import org.kuali.rice.kns.service.KNSServiceLocator; 027 import org.kuali.rice.krad.service.KRADServiceLocator; 028 import org.kuali.rice.kns.workflow.WorkflowUtils; 029 import org.w3c.dom.Node; 030 import org.w3c.dom.NodeList; 031 032 import javax.xml.namespace.QName; 033 import javax.xml.xpath.XPath; 034 import javax.xml.xpath.XPathConstants; 035 import javax.xml.xpath.XPathExpressionException; 036 import java.util.ArrayList; 037 import java.util.Collections; 038 import java.util.List; 039 040 /** 041 * Resolves FO's using the accounts associated with the document XPath xpath = 042 * KualiWorkflowUtils.getXPath(documentContent.getDocument()); List<String> qualifiers = new ArrayList<String>(); NodeList 043 * accountNums = (NodeList)xstreamSafeEval(xpath, "//edu.sampleu.travel.workflow.bo.TravelAccount/number", 044 * documentContent.getDocument(), XPathConstants.NODESET); for (int i = 0; i < accountNums.getLength(); i++) { Node accountNum = 045 * accountNums.item(i); String accuntNumVal = accountNum.getNodeValue(); } 046 */ 047 public class AccountAttribute extends AbstractRoleAttribute { 048 private static final RoleName FISCAL_OFFICER_ROLE = new RoleName(AccountAttribute.class.getName(), "FO", "Fiscal Officer"); 049 private static final List<RoleName> ROLES; 050 static { 051 List<RoleName> tmp = new ArrayList<RoleName>(1); 052 tmp.add(FISCAL_OFFICER_ROLE); 053 ROLES = Collections.unmodifiableList(tmp); 054 } 055 056 057 public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) { 058 List<String> qualifiedRoleNames = new ArrayList<String>(); 059 XPath xpath = WorkflowUtils.getXPath(documentContent.getDocument()); 060 // This xpath stream needs updating when the TravelAccount is being updated 061 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); 062 for (int i = 0; i < accountNums.getLength(); i++) { 063 Node accountNum = accountNums.item(i); 064 String accuntNumVal = accountNum.getTextContent(); 065 qualifiedRoleNames.add(accuntNumVal); 066 } 067 return qualifiedRoleNames; 068 } 069 070 071 public List<RoleName> getRoleNames() { 072 return ROLES; 073 } 074 075 076 public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) { 077 String accountNum = qualifiedRole; 078 TravelAccount account = new TravelAccount(); 079 account.setNumber(accountNum); 080 account = (TravelAccount) KNSServiceLocator.getBusinessObjectService().retrieve(account); 081 if (account == null) { 082 throw new RuntimeException("Account " + accountNum + " does not exist!"); 083 } 084 ResolvedQualifiedRole qualRole = new ResolvedQualifiedRole(); 085 qualRole.setAnnotation("Account " + accountNum + " FO"); 086 qualRole.setQualifiedRoleLabel("Fiscal Officer Account " + accountNum); 087 List<Id> ids = new ArrayList<Id>(); 088 ids.add(new PrincipalName(account.getFiscalOfficer().getUserName())); 089 qualRole.setRecipients(ids); 090 return qualRole; 091 } 092 093 094 /** 095 * This method will do a simple XPath.evaluate, while wrapping your xpathExpression with the xstreamSafe function. It assumes a 096 * String result, and will return such. If an XPathExpressionException is thrown, this will be re-thrown within a 097 * RuntimeException. 098 * 099 * @param xpath A correctly initialized XPath instance. 100 * @param xpathExpression Your XPath Expression that needs to be wrapped in an xstreamSafe wrapper and run. 101 * @param item The document contents you will be searching within. 102 * @return The string value of the xpath.evaluate(). 103 */ 104 public static final Object xstreamSafeEval(XPath xpath, String xpathExpression, Object item, QName returnType) { 105 String xstreamSafeXPath = new StringBuilder(WorkflowUtils.XSTREAM_SAFE_PREFIX).append(xpathExpression).append(WorkflowUtils.XSTREAM_SAFE_SUFFIX).toString(); 106 try { 107 return xpath.evaluate(xstreamSafeXPath, item, returnType); 108 } 109 catch (XPathExpressionException e) { 110 throw new RuntimeException("XPathExpressionException occurred on xpath: " + xstreamSafeXPath, e); 111 } 112 } 113 }