001    /*
002     * Copyright 2007-2008 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.engine.RouteContext;
020    import org.kuali.rice.kew.identity.Id;
021    import org.kuali.rice.kew.routeheader.DocumentContent;
022    import org.kuali.rice.kew.rule.AbstractRoleAttribute;
023    import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
024    import org.kuali.rice.kew.rule.Role;
025    import org.kuali.rice.kew.identity.PrincipalName;
026    import org.kuali.rice.krad.service.KRADServiceLocator;
027    import org.kuali.rice.krad.workflow.WorkflowUtils;
028    import org.w3c.dom.Node;
029    import org.w3c.dom.NodeList;
030    
031    import javax.xml.namespace.QName;
032    import javax.xml.xpath.XPath;
033    import javax.xml.xpath.XPathConstants;
034    import javax.xml.xpath.XPathExpressionException;
035    import java.util.ArrayList;
036    import java.util.Collections;
037    import 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     */
046    public class AccountAttribute extends AbstractRoleAttribute {
047        private static final Role FISCAL_OFFICER_ROLE = new Role(AccountAttribute.class, "FO", "Fiscal Officer");
048        private static final List<Role> ROLES;
049        static {
050            List<Role> tmp = new ArrayList<Role>(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<Role> 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        }