View Javadoc

1   /**
2    * Copyright 2004-2013 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 org.kuali.hr.lm.workflow;
17  
18  
19  import org.apache.cxf.common.util.StringUtils;
20  import org.apache.log4j.Logger;
21  import org.kuali.hr.job.Job;
22  import org.kuali.hr.lm.leaveblock.LeaveBlock;
23  import org.kuali.hr.time.assignment.Assignment;
24  import org.kuali.hr.time.calendar.CalendarEntries;
25  import org.kuali.hr.time.roles.TkRole;
26  import org.kuali.hr.time.roles.service.TkRoleService;
27  import org.kuali.hr.time.service.base.TkServiceLocator;
28  import org.kuali.hr.time.util.TKUtils;
29  import org.kuali.hr.time.util.TkConstants;
30  import org.kuali.hr.time.workarea.WorkArea;
31  import org.kuali.rice.kew.api.identity.Id;
32  import org.kuali.rice.kew.api.identity.PrincipalId;
33  import org.kuali.rice.kew.api.rule.RoleName;
34  import org.kuali.rice.kew.engine.RouteContext;
35  import org.kuali.rice.kew.routeheader.DocumentContent;
36  import org.kuali.rice.kew.rule.AbstractRoleAttribute;
37  import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
38  
39  import java.util.ArrayList;
40  import java.util.Collections;
41  import java.util.List;
42  
43  public class LeaveRequestWorkflowAttribute extends AbstractRoleAttribute {
44      private static final Logger LOG = Logger.getLogger(LeaveRequestWorkflowAttribute.class);
45  
46      @Override
47      public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
48          List<String> roles = new ArrayList<String>();
49          String documentNumber = documentContent.getRouteContext().getDocument().getDocumentId();
50          LeaveRequestDocument leaveRequestDocument = TkServiceLocator.getLeaveRequestDocumentService().getLeaveRequestDocument(documentNumber);
51  
52          if (leaveRequestDocument != null) {
53              LeaveBlock leaveBlock = leaveRequestDocument.getLeaveBlock();
54              CalendarEntries ce = getCalendarEntry(leaveBlock);
55              List<Assignment> assignments = TkServiceLocator.getAssignmentService().getAssignmentsByCalEntryForLeaveCalendar(leaveBlock.getPrincipalId(), ce);
56              for (Assignment assignment : assignments) {
57                  String roleStr = roleName + "_" +assignment.getWorkArea();
58                  if(!roles.contains(roleStr)){
59                      roles.add(roleStr);
60                  }
61              }
62          }
63          return roles;
64      }
65  
66      /**
67       * Role name is passed in in the routing rule.
68       */
69      @Override
70      public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
71          ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
72          Long workAreaNumber = null;
73  
74          try {
75              int pos = qualifiedRole.lastIndexOf("_");
76              if (pos > -1) {
77                  String subs = qualifiedRole.substring(pos+1, qualifiedRole.length());
78                  workAreaNumber = Long.parseLong(subs);
79              }
80          } catch (NumberFormatException nfe) {
81              LOG.error("qualifiedRole did not contain numeric data for work area.");
82          }
83  
84          if (workAreaNumber == null) {
85              throw new RuntimeException("Unable to resolve work area during routing.");
86          }
87  
88          List<Id> principals = new ArrayList<Id>();
89          String routeHeaderId = routeContext.getDocument().getDocumentId();
90          TkRoleService roleService = TkServiceLocator.getTkRoleService();
91          LeaveRequestDocument leaveRequestDocument = TkServiceLocator.getLeaveRequestDocumentService().getLeaveRequestDocument(routeHeaderId);
92          LeaveBlock leaveBlock = TkServiceLocator.getLeaveBlockService().getLeaveBlock(leaveRequestDocument.getLmLeaveBlockId());
93          WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(workAreaNumber, leaveBlock.getLeaveDate());
94  
95          // KPME-1071
96          List<TkRole> approvers = roleService.getWorkAreaRoles(workAreaNumber, roleName, TKUtils.getCurrentDate());
97          List<TkRole> approverDelegates = roleService.getWorkAreaRoles(workAreaNumber, TkConstants.ROLE_TK_APPROVER_DELEGATE, TKUtils.getCurrentDate());
98          List<TkRole> roles = new ArrayList<TkRole>();
99          roles.addAll(approvers);
100         roles.addAll(approverDelegates);
101 
102         for (TkRole role : roles) {
103             //Position routing
104             if(StringUtils.isEmpty(role.getPrincipalId())){
105                 String positionNumber = role.getPositionNumber();
106                 List<Job> lstJobsForPosition = TkServiceLocator.getJobService().getActiveJobsForPosition(positionNumber, getCalendarEntry(leaveBlock).getEndPeriodDateTime());
107                 for(Job job : lstJobsForPosition){
108                     PrincipalId pid = new PrincipalId(job.getPrincipalId());
109                     if (!principals.contains(pid)) {
110                         principals.add(pid);
111                     }
112                 }
113             } else {
114                 PrincipalId pid = new PrincipalId(role.getPrincipalId());
115                 if (!principals.contains(pid)) {
116                     principals.add(pid);
117                 }
118             }
119         }
120 
121         if (principals.size() == 0)
122             throw new RuntimeException("No principals to route to. Push to exception routing.");
123 
124         rqr.setRecipients(principals);
125         rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea());
126 
127         return rqr;
128     }
129 
130     @Override
131     public List<RoleName> getRoleNames() {
132         return Collections.EMPTY_LIST;
133     }
134 
135     private CalendarEntries getCalendarEntry(LeaveBlock leaveBlock) {
136         return TkServiceLocator.getCalendarEntriesService().getCalendarEntries(leaveBlock.getCalendarId());
137     }
138 }