Coverage Report - edu.sampleu.travel.workflow.EmployeeAttribute
 
Classes in this File Line Coverage Branch Coverage Complexity
EmployeeAttribute
0%
0/65
0%
0/22
2.167
 
 1  
 /*
 2  
  * Copyright 2007 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 org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.core.util.ConcreteKeyValue;
 20  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 21  
 import org.kuali.rice.kew.engine.RouteContext;
 22  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
 23  
 import org.kuali.rice.kew.identity.Id;
 24  
 import org.kuali.rice.kew.identity.PrincipalName;
 25  
 import org.kuali.rice.kew.routeheader.DocumentContent;
 26  
 import org.kuali.rice.kew.rule.GenericRoleAttribute;
 27  
 import org.kuali.rice.kew.rule.QualifiedRoleName;
 28  
 import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
 29  
 import org.kuali.rice.kew.rule.Role;
 30  
 import org.kuali.rice.kew.user.UserId;
 31  
 import org.kuali.rice.kew.user.WorkflowUserId;
 32  
 import org.kuali.rice.kim.api.identity.principal.Principal;
 33  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 34  
 import org.kuali.rice.kns.web.ui.Field;
 35  
 import org.kuali.rice.kns.web.ui.Row;
 36  
 
 37  
 import java.util.ArrayList;
 38  
 import java.util.Collections;
 39  
 import java.util.HashMap;
 40  
 import java.util.List;
 41  
 import java.util.Map;
 42  
 
 43  
 /**
 44  
  * An attribute implementation that can resolve organizational roles
 45  
  */
 46  
 public class EmployeeAttribute extends GenericRoleAttribute {
 47  0
     private static final Role EMPLOYEE_ROLE = new Role(EmployeeAttribute.class, "employee", "Employee");
 48  0
     private static final Role SUPERVISOR_ROLE = new Role(EmployeeAttribute.class, "supervisr", "Supervisor");
 49  0
     private static final Role DIRECTOR_ROLE = new Role(EmployeeAttribute.class, "director", "Dean/Director");
 50  
     private static final List<Role> ROLES;
 51  
     static {
 52  0
         List<Role> tmp = new ArrayList<Role>(1);
 53  0
         tmp.add(EMPLOYEE_ROLE);
 54  0
         tmp.add(SUPERVISOR_ROLE);
 55  0
         tmp.add(DIRECTOR_ROLE);
 56  0
         ROLES = Collections.unmodifiableList(tmp);
 57  
     }
 58  
 
 59  0
         private static String USERID_FORM_FIELDNAME = "userid";
 60  
 
 61  
     /**
 62  
      * Traveler to be set by client application so that doc content can be generated appropriately
 63  
      */
 64  
         private String traveler;
 65  
 
 66  
         //private AttributeParser _attributeParser = new AttributeParser(ATTRIBUTE_TAGNAME);
 67  
 
 68  
         public EmployeeAttribute() {
 69  0
         super("employee");
 70  0
         }
 71  
 
 72  
         public EmployeeAttribute(String traveler) {
 73  0
         super("employee");
 74  0
                 this.traveler = traveler;
 75  0
         }
 76  
 
 77  
     /** for edoclite?? */
 78  
     public void setTraveler(String traveler) {
 79  0
         this.traveler = traveler;
 80  0
     }
 81  
 
 82  
         /* RoleAttribute methods */
 83  
         public List<Role> getRoleNames() {
 84  0
         return ROLES;
 85  
         }
 86  
 
 87  
     protected boolean isValidRole(String roleName) {
 88  0
         for (Role role: ROLES) {
 89  0
             if (role.getBaseName().equals(roleName)) {
 90  0
                 return true;
 91  
             }
 92  
         }
 93  0
         return false;
 94  
     }
 95  
 
 96  
 
 97  
         @Override
 98  
     protected List<String> getRoleNameQualifiers(String roleName, DocumentContent documentContent) {
 99  0
         if (!isValidRole(roleName)) {
 100  0
             throw new WorkflowRuntimeException("Invalid role: " + roleName);
 101  
         }
 102  
 
 103  0
         List<String> qualifiers = new ArrayList<String>();
 104  0
         qualifiers.add(roleName);
 105  
         // find all traveller inputs in incoming doc
 106  
 //        List<Map<String, String>> attrs;
 107  
 //        try {
 108  
 //            attrs = content.parseContent(documentContent.getAttributeContent());
 109  
 //        } catch (XPathExpressionException xpee) {
 110  
 //            throw new WorkflowRuntimeException("Error parsing attribute content: " + XmlJotter.jotNode(documentContent.getAttributeContent()));
 111  
 //        }
 112  
 //        for (Map<String, String> props: attrs) {
 113  
 //            String attrTraveler = props.get("traveler");
 114  
 //            if (attrTraveler != null) {
 115  
 //                qualifiers.add(attrTraveler);
 116  
 //            }
 117  
 //        }
 118  0
         return qualifiers;
 119  
     }
 120  
 
 121  
         @Override
 122  
         protected ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, QualifiedRoleName qualifiedRoleName) {
 123  0
         List<Id> recipients = resolveRecipients(routeContext, qualifiedRoleName);
 124  0
         ResolvedQualifiedRole rqr = new ResolvedQualifiedRole(getLabelForQualifiedRoleName(qualifiedRoleName),
 125  
                                                               recipients,
 126  
                                                               qualifiedRoleName.getBaseRoleName()); // default to no annotation...
 127  0
         return rqr;
 128  
     }
 129  
         
 130  
         @Override
 131  
     protected List<Id> resolveRecipients(RouteContext routeContext, QualifiedRoleName qualifiedRoleName) {
 132  0
         List<Id> members = new ArrayList<Id>();
 133  0
         UserId roleUserId = null;
 134  0
         String roleName = qualifiedRoleName.getBaseRoleName();
 135  0
         String roleTraveler = qualifiedRoleName.getQualifier();
 136  
 
 137  
         /* EMPLOYEE role routes to traveler */
 138  0
         if (StringUtils.equals(EMPLOYEE_ROLE.getBaseName(), roleName)) {
 139  0
             roleUserId = new WorkflowUserId(roleTraveler);
 140  
 
 141  
         /* SUPERVISOR role routes to... supervisor */
 142  0
         } else if (StringUtils.equals(SUPERVISOR_ROLE.getBaseName(), roleName)) {
 143  
             // HACK: need to create an organizational-hierarchy service which
 144  
             // has methods like
 145  
             // getSupervisor( user ), getDirector( user ), getSupervised( user
 146  
             // ), etc.
 147  
             // using q.uhuuid() as input
 148  0
             roleUserId = new PrincipalName("supervisr");
 149  
 
 150  
         /* SUPERVISOR role routes to... director */
 151  0
         } else if (StringUtils.equals(DIRECTOR_ROLE.getBaseName(), roleName)) {
 152  
             // HACK: need to create an organizational-hierarchy service which
 153  
             // has methods like
 154  
             // getSupervisor( user ), getDirector( user ), getSupervised( user
 155  
             // ), etc.
 156  
             // using q.uhuuid() as input
 157  0
             roleUserId = new PrincipalName("director");
 158  
         } else {
 159  
             // throw an exception if you get an unrecognized roleName
 160  0
             throw new WorkflowRuntimeException("unable to process unknown role '" + roleName + "'");
 161  
         }
 162  0
         members.add(roleUserId);
 163  
 
 164  0
         return members;
 165  
     }
 166  
 
 167  
     public Map<String, String> getProperties() {
 168  0
         Map<String, String> properties = new HashMap<String, String>();
 169  0
         properties.put("traveler", traveler);
 170  0
         return properties;
 171  
     }
 172  
 
 173  
         /**
 174  
          * Required to support flex routing report
 175  
          *
 176  
          * @see org.kuali.rice.kew.rule.WorkflowAttribute#getFieldConversions()
 177  
          */
 178  
         public List getFieldConversions() {
 179  0
                 List conversionFields = new ArrayList();
 180  0
                 conversionFields.add(new ConcreteKeyValue("userid", USERID_FORM_FIELDNAME));
 181  0
                 return conversionFields;
 182  
         }
 183  
 
 184  
         public List<Row> getRoutingDataRows() {
 185  0
                 List<Row> rows = new ArrayList<Row>();
 186  
 
 187  0
                 List<Field> fields = new ArrayList<Field>();
 188  0
                 fields.add(new Field("Traveler username", "", Field.TEXT, false, USERID_FORM_FIELDNAME, "", false, false, null, null));
 189  0
                 rows.add(new Row(fields));
 190  
 
 191  0
                 return rows;
 192  
         }
 193  
 
 194  
         public List validateRoutingData(Map paramMap) {
 195  0
                 List errors = new ArrayList();
 196  
 
 197  0
                 String userid = StringUtils.trim((String) paramMap.get(USERID_FORM_FIELDNAME));
 198  0
                 if (isRequired() && StringUtils.isBlank(userid)) {
 199  0
                         errors.add(new WorkflowServiceErrorImpl("userid is required", "uh.accountattribute.userid.required"));
 200  
                 }
 201  
 
 202  0
                 Principal principal = null;
 203  0
                 if (!StringUtils.isBlank(userid)) {
 204  0
                         principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(userid);
 205  
                 }
 206  0
                 if (principal == null) {
 207  0
                         errors.add(new WorkflowServiceErrorImpl("unable to retrieve user for userid '" + userid + "'", "uh.accountattribute.userid.invalid"));
 208  
                 }
 209  
         
 210  0
                 if (errors.size() == 0) {
 211  0
                         traveler = principal.getPrincipalId();
 212  
                 }
 213  
 
 214  0
                 return errors;
 215  
         }
 216  
 }