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 org.kuali.workflow.attribute;
017:
018: import java.util.ArrayList;
019: import java.util.Collections;
020: import java.util.HashSet;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Set;
024:
025: import javax.xml.xpath.XPath;
026:
027: import org.apache.commons.lang.StringUtils;
028: import org.apache.commons.lang.builder.HashCodeBuilder;
029: import org.kuali.workflow.KualiWorkflowUtils;
030:
031: import edu.iu.uis.eden.engine.RouteContext;
032: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
033: import edu.iu.uis.eden.lookupable.Row;
034: import edu.iu.uis.eden.plugin.attributes.RoleAttribute;
035: import edu.iu.uis.eden.plugin.attributes.WorkflowAttribute;
036: import edu.iu.uis.eden.routeheader.DocumentContent;
037: import edu.iu.uis.eden.routetemplate.ResolvedQualifiedRole;
038: import edu.iu.uis.eden.routetemplate.Role;
039: import edu.iu.uis.eden.routetemplate.RuleExtension;
040: import edu.iu.uis.eden.routetemplate.RuleExtensionValue;
041: import edu.iu.uis.eden.workgroup.GroupId;
042: import edu.iu.uis.eden.workgroup.GroupNameId;
043:
044: public class KualiVerificationWorkgroupAttribute implements
045: RoleAttribute, WorkflowAttribute {
046:
047: private static final String ROLE_STRING_DELIMITER = "~!~!~";
048:
049: private boolean required = false;
050:
051: private String verificationWorkgoupName;
052:
053: public String getVerificationWorkgoupName() {
054: return verificationWorkgoupName;
055: }
056:
057: public void setVerificationWorkgoupName(
058: String verificationWorkgoupName) {
059: this .verificationWorkgoupName = verificationWorkgoupName;
060: }
061:
062: public List getQualifiedRoleNames(String roleName,
063: DocumentContent docContent)
064: throws EdenUserNotFoundException {
065: Set verificationWorkgroups = new HashSet();
066: XPath xpath = KualiWorkflowUtils.getXPath(docContent
067: .getDocument());
068: String docTypeName = docContent.getRouteContext().getDocument()
069: .getDocumentType().getName();
070: List qualifiedRoleNames = new ArrayList();
071: try {
072: String verificationWorkgroupName = xpath
073: .evaluate(
074: KualiWorkflowUtils
075: .xstreamSafeXPath(KualiWorkflowUtils.XSTREAM_MATCH_ANYWHERE_PREFIX
076: + "cashReceiptHeader/workgroupName"),
077: docContent.getRouteContext()
078: .getDocumentContent()
079: .getDocContent());
080: VerificationWorkgroupRole role = new VerificationWorkgroupRole(
081: roleName);
082: role.verificationWorkgroupName = verificationWorkgroupName;
083:
084: verificationWorkgroups.add(role);
085:
086: qualifiedRoleNames.add(getQualifiedRoleString(role));
087:
088: } catch (Exception e) {
089: throw new RuntimeException(e);
090: }
091: return qualifiedRoleNames;
092: }
093:
094: private String getQualifiedRoleString(VerificationWorkgroupRole role) {
095: return new StringBuffer(getNullSafeValue(role.roleName))
096: .append(ROLE_STRING_DELIMITER)
097: .append(
098: getNullSafeValue(role.verificationWorkgroupName))
099: .toString();
100: }
101:
102: private static String getNullSafeValue(String value) {
103: return (value == null ? "" : value);
104: }
105:
106: public List getRoleNames() {
107: List roles = new ArrayList();
108: roles.add(new Role(this .getClass(),
109: "VERIFICATION_WORGROUP_FROM_DOCUMENT",
110: "Verification Workgroup from Document"));
111:
112: return roles;
113: }
114:
115: public ResolvedQualifiedRole resolveQualifiedRole(
116: RouteContext context, String roleName, String qualifiedRole)
117: throws EdenUserNotFoundException {
118: try {
119: List members = new ArrayList();
120: String annotation = "";
121: VerificationWorkgroupRole role = getUnqualifiedVerificationWorkgroupRole(qualifiedRole);
122: annotation = (role.verificationWorkgroupName == null ? ""
123: : "Routing to workgroup named "
124: + role.verificationWorkgroupName);
125: GroupId verificationWorkgroupId = new GroupNameId(
126: role.verificationWorkgroupName);
127: if (verificationWorkgroupId != null) {
128: members.add(verificationWorkgroupId);
129: }
130: return new ResolvedQualifiedRole(roleName, members,
131: annotation);
132: } catch (Exception e) {
133: throw new RuntimeException(
134: "KualiVerificationWorkgroupAttribute encountered exception while attempting to resolve qualified role",
135: e);
136: }
137: }
138:
139: private static VerificationWorkgroupRole getUnqualifiedVerificationWorkgroupRole(
140: String qualifiedRole) {
141: String[] values = qualifiedRole
142: .split(ROLE_STRING_DELIMITER, -1);
143: if (values.length != 2) {
144: throw new RuntimeException(
145: "Invalid qualifiedRole, expected 2 encoded values: "
146: + qualifiedRole);
147: }
148: VerificationWorkgroupRole role = new VerificationWorkgroupRole(
149: values[0]);
150: role.verificationWorkgroupName = getNullableString(values[1]);
151: return role;
152: }
153:
154: private static String getNullableString(String value) {
155: if (StringUtils.isEmpty(value)) {
156: return null;
157: }
158: return value;
159: }
160:
161: public String getDocContent() {
162:
163: return new StringBuffer(
164: KualiWorkflowUtils.XML_REPORT_DOC_CONTENT_PREFIX)
165: .append("<verificationWorkgroupName>")
166: .append(getVerificationWorkgoupName())
167: .append("</verificationWorkgroupName>")
168: .append(
169: KualiWorkflowUtils.XML_REPORT_DOC_CONTENT_SUFFIX)
170: .toString();
171: }
172:
173: public List<Row> getRoutingDataRows() {
174: // TODO Auto-generated method stub
175: return null;
176: }
177:
178: public List<RuleExtensionValue> getRuleExtensionValues() {
179:
180: return Collections.EMPTY_LIST;
181: }
182:
183: public List<Row> getRuleRows() {
184: return Collections.EMPTY_LIST;
185: }
186:
187: public boolean isMatch(DocumentContent arg0,
188: List<RuleExtension> arg1) {
189: return true;
190: }
191:
192: public boolean isRequired() {
193: return this .required;
194: }
195:
196: public void setRequired(boolean required) {
197: this .required = required;
198:
199: }
200:
201: public List validateRoutingData(Map arg0) {
202: // TODO Auto-generated method stub
203: return null;
204: }
205:
206: public List validateRuleData(Map arg0) {
207: return Collections.EMPTY_LIST;
208: }
209:
210: private static class VerificationWorkgroupRole {
211: public String roleName;
212:
213: public String verificationWorkgroupName;
214:
215: public VerificationWorkgroupRole(String roleName) {
216: this .roleName = roleName;
217: }
218:
219: @Override
220: public boolean equals(Object object) {
221: if (object instanceof VerificationWorkgroupRole) {
222: return true;
223: }
224: return false;
225: }
226:
227: @Override
228: public int hashCode() {
229: return new HashCodeBuilder().append(
230: verificationWorkgroupName).hashCode();
231: }
232:
233: }
234:
235: }
|