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