View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package edu.sampleu.travel.workflow;
17  
18  import edu.sampleu.travel.bo.TravelAccount;
19  import org.kuali.rice.kew.api.identity.Id;
20  import org.kuali.rice.kew.api.identity.PrincipalName;
21  import org.kuali.rice.kew.api.rule.RoleName;
22  import org.kuali.rice.kew.engine.RouteContext;
23  import org.kuali.rice.kew.routeheader.DocumentContent;
24  import org.kuali.rice.kew.rule.AbstractRoleAttribute;
25  import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
26  import org.kuali.rice.kns.service.KNSServiceLocator;
27  import org.kuali.rice.krad.service.KRADServiceLocator;
28  import org.kuali.rice.kns.workflow.WorkflowUtils;
29  import org.w3c.dom.Node;
30  import org.w3c.dom.NodeList;
31  
32  import javax.xml.namespace.QName;
33  import javax.xml.xpath.XPath;
34  import javax.xml.xpath.XPathConstants;
35  import javax.xml.xpath.XPathExpressionException;
36  import java.util.ArrayList;
37  import java.util.Collections;
38  import java.util.List;
39  
40  /**
41   * Resolves FO's using the accounts associated with the document XPath xpath =
42   * KualiWorkflowUtils.getXPath(documentContent.getDocument()); List<String> qualifiers = new ArrayList<String>(); NodeList
43   * accountNums = (NodeList)xstreamSafeEval(xpath, "//edu.sampleu.travel.workflow.bo.TravelAccount/number",
44   * documentContent.getDocument(), XPathConstants.NODESET); for (int i = 0; i < accountNums.getLength(); i++) { Node accountNum =
45   * accountNums.item(i); String accuntNumVal = accountNum.getNodeValue(); }
46   */
47  public class AccountAttribute extends AbstractRoleAttribute {
48      private static final RoleName FISCAL_OFFICER_ROLE = new RoleName(AccountAttribute.class.getName(), "FO", "Fiscal Officer");
49      private static final List<RoleName> ROLES;
50      static {
51          List<RoleName> tmp = new ArrayList<RoleName>(1);
52          tmp.add(FISCAL_OFFICER_ROLE);
53          ROLES = Collections.unmodifiableList(tmp);
54      }
55  
56  
57      public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
58          List<String> qualifiedRoleNames = new ArrayList<String>();
59          XPath xpath = WorkflowUtils.getXPath(documentContent.getDocument());
60          // This xpath stream needs updating when the TravelAccount is being updated
61          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);
62          for (int i = 0; i < accountNums.getLength(); i++) {
63              Node accountNum = accountNums.item(i);
64              String accuntNumVal = accountNum.getTextContent();
65              qualifiedRoleNames.add(accuntNumVal);
66          }
67          return qualifiedRoleNames;
68      }
69  
70  
71      public List<RoleName> getRoleNames() {
72          return ROLES;
73      }
74  
75  
76      public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
77          String accountNum = qualifiedRole;
78          TravelAccount account = new TravelAccount();
79          account.setNumber(accountNum);
80          account = (TravelAccount) KNSServiceLocator.getBusinessObjectService().retrieve(account);
81          if (account == null) {
82              throw new RuntimeException("Account " + accountNum + " does not exist!");
83          }
84          ResolvedQualifiedRole qualRole = new ResolvedQualifiedRole();
85          qualRole.setAnnotation("Account " + accountNum + " FO");
86          qualRole.setQualifiedRoleLabel("Fiscal Officer Account " + accountNum);
87          List<Id> ids = new ArrayList<Id>();
88          ids.add(new PrincipalName(account.getFiscalOfficer().getUserName()));
89          qualRole.setRecipients(ids);
90          return qualRole;
91      }
92  
93  
94      /**
95       * This method will do a simple XPath.evaluate, while wrapping your xpathExpression with the xstreamSafe function. It assumes a
96       * String result, and will return such. If an XPathExpressionException is thrown, this will be re-thrown within a
97       * RuntimeException.
98       * 
99       * @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     }