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.HashMap;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.apache.commons.lang.StringUtils;
025:
026: import edu.iu.uis.eden.Id;
027: import edu.iu.uis.eden.KEWServiceLocator;
028: import edu.iu.uis.eden.WorkflowServiceErrorImpl;
029: import edu.iu.uis.eden.engine.RouteContext;
030: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
031: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
032: import edu.iu.uis.eden.lookupable.Field;
033: import edu.iu.uis.eden.lookupable.Row;
034: import edu.iu.uis.eden.routeheader.DocumentContent;
035: import edu.iu.uis.eden.routetemplate.GenericRoleAttribute;
036: import edu.iu.uis.eden.routetemplate.QualifiedRoleName;
037: import edu.iu.uis.eden.routetemplate.Role;
038: import edu.iu.uis.eden.user.AuthenticationUserId;
039: import edu.iu.uis.eden.user.EmplId;
040: import edu.iu.uis.eden.user.UserId;
041: import edu.iu.uis.eden.user.WorkflowUser;
042: import edu.iu.uis.eden.user.WorkflowUserId;
043: import edu.iu.uis.eden.util.KeyLabelPair;
044:
045: /**
046: * An attribute implementation that can resolve organizational roles
047: */
048: public class EmployeeAttribute extends GenericRoleAttribute {
049: private static final Role EMPLOYEE_ROLE = new Role(
050: EmployeeAttribute.class, "employee", "Employee");
051: private static final Role SUPERVISOR_ROLE = new Role(
052: EmployeeAttribute.class, "supervisr", "Supervisor");
053: private static final Role DIRECTOR_ROLE = new Role(
054: EmployeeAttribute.class, "director", "Dean/Director");
055: private static final List<Role> ROLES;
056: static {
057: List<Role> tmp = new ArrayList<Role>(1);
058: tmp.add(EMPLOYEE_ROLE);
059: tmp.add(SUPERVISOR_ROLE);
060: tmp.add(DIRECTOR_ROLE);
061: ROLES = Collections.unmodifiableList(tmp);
062: }
063:
064: private static String USERID_FORM_FIELDNAME = "userid";
065:
066: /**
067: * Traveler to be set by client application so that doc content can be generated appropriately
068: */
069: private String traveler;
070:
071: //private AttributeParser _attributeParser = new AttributeParser(ATTRIBUTE_TAGNAME);
072:
073: public EmployeeAttribute() {
074: super ("employee");
075: }
076:
077: public EmployeeAttribute(String traveler) {
078: super ("employee");
079: this .traveler = traveler;
080: }
081:
082: /** for edoclite?? */
083: public void setTraveler(String traveler) {
084: this .traveler = traveler;
085: }
086:
087: /* RoleAttribute methods */
088: public List<Role> getRoleNames() {
089: return ROLES;
090: }
091:
092: protected boolean isValidRole(String roleName) {
093: for (Role role : ROLES) {
094: if (role.getBaseName().equals(roleName)) {
095: return true;
096: }
097: }
098: return false;
099: }
100:
101: @Override
102: protected List<String> getRoleNameQualifiers(String roleName,
103: DocumentContent documentContent)
104: throws EdenUserNotFoundException {
105: if (!isValidRole(roleName)) {
106: throw new WorkflowRuntimeException("Invalid role: "
107: + roleName);
108: }
109:
110: List<String> qualifiers = new ArrayList<String>();
111: qualifiers.add(roleName);
112: // find all traveller inputs in incoming doc
113: // List<Map<String, String>> attrs;
114: // try {
115: // attrs = content.parseContent(documentContent.getAttributeContent());
116: // } catch (XPathExpressionException xpee) {
117: // throw new WorkflowRuntimeException("Error parsing attribute content: " + XmlHelper.jotNode(documentContent.getAttributeContent()));
118: // }
119: // for (Map<String, String> props: attrs) {
120: // String attrTraveler = props.get("traveler");
121: // if (attrTraveler != null) {
122: // qualifiers.add(attrTraveler);
123: // }
124: // }
125: return qualifiers;
126: }
127:
128: @Override
129: protected List<Id> resolveRecipients(RouteContext routeContext,
130: QualifiedRoleName qualifiedRoleName)
131: throws EdenUserNotFoundException {
132: List<Id> members = new ArrayList<Id>();
133: UserId roleUserId = null;
134: String roleName = qualifiedRoleName.getBaseRoleName();
135: String roleTraveler = qualifiedRoleName.getQualifier();
136:
137: /* EMPLOYEE role routes to traveler */
138: if (StringUtils.equals(EMPLOYEE_ROLE.getBaseName(), roleName)) {
139: roleUserId = new WorkflowUserId(roleTraveler);
140:
141: /* SUPERVISOR role routes to... supervisor */
142: } else if (StringUtils.equals(SUPERVISOR_ROLE.getBaseName(),
143: roleName)) {
144: // HACK: need to create an organizational-hierarchy service which
145: // has methods like
146: // getSupervisor( user ), getDirector( user ), getSupervised( user
147: // ), etc.
148: // using q.uhuuid() as input
149: roleUserId = new EmplId("supervisr");
150:
151: /* SUPERVISOR role routes to... director */
152: } else if (StringUtils.equals(DIRECTOR_ROLE.getBaseName(),
153: roleName)) {
154: // HACK: need to create an organizational-hierarchy service which
155: // has methods like
156: // getSupervisor( user ), getDirector( user ), getSupervised( user
157: // ), etc.
158: // using q.uhuuid() as input
159: roleUserId = new EmplId("director");
160: } else {
161: // throw an exception if you get an unrecognized roleName
162: throw new WorkflowRuntimeException(
163: "unable to process unknown role '" + roleName + "'");
164: }
165: members.add(roleUserId);
166:
167: return members;
168: }
169:
170: public Map<String, String> getProperties() {
171: Map<String, String> properties = new HashMap<String, String>();
172: properties.put("traveler", traveler);
173: return properties;
174: }
175:
176: /**
177: * Required to support flex routing report
178: *
179: * @see edu.iu.uis.eden.routetemplate.WorkflowAttribute#getFieldConversions()
180: */
181: public List getFieldConversions() {
182: List conversionFields = new ArrayList();
183: conversionFields.add(new KeyLabelPair("userid",
184: USERID_FORM_FIELDNAME));
185: return conversionFields;
186: }
187:
188: public List<Row> getRoutingDataRows() {
189: List<Row> rows = new ArrayList<Row>();
190:
191: List<Field> fields = new ArrayList<Field>();
192: fields.add(new Field("Traveler username", "", Field.TEXT,
193: false, USERID_FORM_FIELDNAME, "", null, null));
194: rows.add(new Row(fields));
195:
196: return rows;
197: }
198:
199: public List validateRoutingData(Map paramMap) {
200: List errors = new ArrayList();
201:
202: String userid = StringUtils.trim((String) paramMap
203: .get(USERID_FORM_FIELDNAME));
204: if (isRequired() && StringUtils.isBlank(userid)) {
205: errors.add(new WorkflowServiceErrorImpl(
206: "userid is required",
207: "uh.accountattribute.userid.required"));
208: }
209:
210: WorkflowUser user = null;
211: if (!StringUtils.isBlank(userid)) {
212: try {
213: user = KEWServiceLocator.getUserService()
214: .getWorkflowUser(
215: new AuthenticationUserId(userid));
216: } catch (EdenUserNotFoundException e) {
217: errors.add(new WorkflowServiceErrorImpl(
218: "unable to retrieve user for userid '" + userid
219: + "'",
220: "uh.accountattribute.userid.invalid"));
221: }
222: }
223: if (errors.size() == 0) {
224: traveler = user.getUuId().getUuId();
225: }
226:
227: return errors;
228: }
229: }
|