001 /** 002 * Copyright 2004-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 org.kuali.hr.time.workflow; 017 018 import org.apache.cxf.common.util.StringUtils; 019 import org.apache.log4j.Logger; 020 import org.kuali.hr.job.Job; 021 import org.kuali.hr.lm.leavecalendar.LeaveCalendarDocument; 022 import org.kuali.hr.time.assignment.Assignment; 023 import org.kuali.hr.time.roles.TkRole; 024 import org.kuali.hr.time.roles.service.TkRoleService; 025 import org.kuali.hr.time.service.base.TkServiceLocator; 026 import org.kuali.hr.time.util.TKUtils; 027 import org.kuali.hr.time.util.TkConstants; 028 import org.kuali.hr.time.workarea.WorkArea; 029 import org.kuali.rice.kew.api.identity.Id; 030 import org.kuali.rice.kew.api.identity.PrincipalId; 031 import org.kuali.rice.kew.api.rule.RoleName; 032 import org.kuali.rice.kew.engine.RouteContext; 033 import org.kuali.rice.kew.routeheader.DocumentContent; 034 import org.kuali.rice.kew.rule.AbstractRoleAttribute; 035 import org.kuali.rice.kew.rule.ResolvedQualifiedRole; 036 037 import java.util.ArrayList; 038 import java.util.Collections; 039 import java.util.List; 040 041 public class TkWorkflowLeaveCalendarAttribute extends AbstractRoleAttribute { 042 043 private static final Logger LOG = Logger.getLogger(TkWorkflowLeaveCalendarAttribute.class); 044 045 @Override 046 public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) { 047 List<String> roles = new ArrayList<String>(); 048 String routeHeaderId = documentContent.getRouteContext().getDocument().getDocumentId(); 049 LeaveCalendarDocument leaveDocument = TkServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(routeHeaderId); 050 051 if (leaveDocument != null) { 052 List<Assignment> assignments = leaveDocument.getAssignments(); 053 for (Assignment assignment : assignments) { 054 String roleStr = roleName + "_" +assignment.getWorkArea(); 055 if(!roles.contains(roleStr)){ 056 roles.add(roleStr); 057 } 058 } 059 } 060 return roles; 061 } 062 063 /** 064 * Role name is passed in in the routing rule. 065 */ 066 @Override 067 public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) { 068 ResolvedQualifiedRole rqr = new ResolvedQualifiedRole(); 069 Long workAreaNumber = null; 070 071 try { 072 int pos = qualifiedRole.lastIndexOf("_"); 073 if (pos > -1) { 074 String subs = qualifiedRole.substring(pos+1, qualifiedRole.length()); 075 workAreaNumber = Long.parseLong(subs); 076 } 077 } catch (NumberFormatException nfe) { 078 LOG.error("qualifiedRole did not contain numeric data for work area."); 079 } 080 081 if (workAreaNumber == null) { 082 throw new RuntimeException("Unable to resolve work area during routing."); 083 } 084 085 List<Id> principals = new ArrayList<Id>(); 086 String routeHeaderId = routeContext.getDocument().getDocumentId(); 087 TkRoleService roleService = TkServiceLocator.getTkRoleService(); 088 LeaveCalendarDocument leaveCalendarDocument = TkServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(routeHeaderId); 089 WorkArea workArea = null; 090 if (leaveCalendarDocument != null) { 091 workArea = TkServiceLocator.getWorkAreaService().getWorkArea(workAreaNumber, leaveCalendarDocument.getAsOfDate()); 092 } 093 094 // KPME-1071 095 List<TkRole> approvers = roleService.getWorkAreaRoles(workAreaNumber, roleName, TKUtils.getCurrentDate()); 096 List<TkRole> approverDelegates = roleService.getWorkAreaRoles(workAreaNumber, TkConstants.ROLE_TK_APPROVER_DELEGATE, TKUtils.getCurrentDate()); 097 List<TkRole> roles = new ArrayList<TkRole>(); 098 roles.addAll(approvers); 099 roles.addAll(approverDelegates); 100 101 for (TkRole role : roles) { 102 //Position routing 103 if(StringUtils.isEmpty(role.getPrincipalId())){ 104 String positionNumber = role.getPositionNumber(); 105 List<Job> lstJobsForPosition = TkServiceLocator.getJobService().getActiveJobsForPosition(positionNumber, leaveCalendarDocument.getCalendarEntry().getEndPeriodDateTime()); 106 for(Job job : lstJobsForPosition){ 107 PrincipalId pid = new PrincipalId(job.getPrincipalId()); 108 if (!principals.contains(pid)) { 109 principals.add(pid); 110 } 111 } 112 } else { 113 PrincipalId pid = new PrincipalId(role.getPrincipalId()); 114 if (!principals.contains(pid)) { 115 principals.add(pid); 116 } 117 } 118 } 119 120 if (principals.size() == 0) 121 throw new RuntimeException("No principals to route to. Push to exception routing."); 122 123 rqr.setRecipients(principals); 124 rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea()); 125 126 return rqr; 127 } 128 129 @Override 130 public List<RoleName> getRoleNames() { 131 return Collections.EMPTY_LIST; 132 } 133 134 }