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