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