001/** 002 * Copyright 2004-2014 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 */ 016package org.kuali.hr.lm.workflow; 017 018import org.apache.log4j.Logger; 019import org.joda.time.DateTime; 020import org.kuali.kpme.core.api.assignment.Assignment; 021import org.kuali.kpme.core.api.calendar.entry.CalendarEntry; 022import org.kuali.kpme.core.api.namespace.KPMENamespace; 023import org.kuali.kpme.core.api.workarea.WorkArea; 024import org.kuali.kpme.core.calendar.entry.CalendarEntryBo; 025import org.kuali.kpme.core.role.KPMERole; 026import org.kuali.kpme.core.service.HrServiceLocator; 027import org.kuali.kpme.tklm.api.common.TkConstants; 028import org.kuali.kpme.tklm.api.leave.block.LeaveBlock; 029import org.kuali.kpme.tklm.leave.service.LmServiceLocator; 030import org.kuali.kpme.tklm.leave.workflow.LeaveRequestDocument; 031import org.kuali.rice.kew.api.identity.Id; 032import org.kuali.rice.kew.api.identity.PrincipalId; 033import org.kuali.rice.kew.api.rule.RoleName; 034import org.kuali.rice.kew.engine.RouteContext; 035import org.kuali.rice.kew.routeheader.DocumentContent; 036import org.kuali.rice.kew.rule.AbstractRoleAttribute; 037import org.kuali.rice.kew.rule.ResolvedQualifiedRole; 038import org.kuali.rice.kim.api.role.RoleMember; 039 040import java.util.ArrayList; 041import java.util.Collections; 042import java.util.List; 043 044@Deprecated 045public class LeaveRequestWorkflowAttribute extends AbstractRoleAttribute { 046 047 private static final long serialVersionUID = -6939277052363491806L; 048 049 private static final Logger LOG = Logger.getLogger(LeaveRequestWorkflowAttribute.class); 050 051 @Override 052 public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) { 053 List<String> roles = new ArrayList<String>(); 054 String documentNumber = documentContent.getRouteContext().getDocument().getDocumentId(); 055 LeaveRequestDocument leaveRequestDocument = LmServiceLocator.getLeaveRequestDocumentService().getLeaveRequestDocument(documentNumber); 056 057 if (leaveRequestDocument != null) { 058 LeaveBlock leaveBlock = leaveRequestDocument.getLeaveBlock(); 059 CalendarEntry ce = getCalendarEntry(leaveBlock); 060 List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAllAssignmentsByCalEntryForLeaveCalendar(leaveBlock.getPrincipalId(), ce); 061 for (Assignment assignment : assignments) { 062 String roleStr = roleName + "_" + assignment.getWorkArea(); 063 if (!roles.contains(roleStr)) { 064 roles.add(roleStr); 065 } 066 } 067 } 068 069 return roles; 070 } 071 072 @Override 073 public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) { 074 ResolvedQualifiedRole rqr = new ResolvedQualifiedRole(); 075 Long workAreaNumber = null; 076 077 try { 078 int pos = qualifiedRole.lastIndexOf("_"); 079 if (pos > -1) { 080 String subs = qualifiedRole.substring(pos+1, qualifiedRole.length()); 081 workAreaNumber = Long.parseLong(subs); 082 } 083 } catch (NumberFormatException nfe) { 084 LOG.error("qualifiedRole did not contain numeric data for work area."); 085 } 086 087 if (workAreaNumber == null) { 088 throw new RuntimeException("Unable to resolve work area during routing."); 089 } 090 091 List<Id> principals = new ArrayList<Id>(); 092 String routeHeaderId = routeContext.getDocument().getDocumentId(); 093 LeaveRequestDocument leaveRequestDocument = LmServiceLocator.getLeaveRequestDocumentService().getLeaveRequestDocument(routeHeaderId); 094 LeaveBlock leaveBlock = LmServiceLocator.getLeaveBlockService().getLeaveBlock(leaveRequestDocument.getLmLeaveBlockId()); 095 WorkArea workArea = HrServiceLocator.getWorkAreaService().getWorkArea(workAreaNumber, leaveBlock.getLeaveLocalDate()); 096 097 List<RoleMember> roleMembers = new ArrayList<RoleMember>(); 098 099 if (TkConstants.ROLE_TK_APPROVER.equals(roleName)) { 100 roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), workAreaNumber, new DateTime(), true)); 101 roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), workAreaNumber, new DateTime(), true)); 102 } 103 104 for (RoleMember roleMember : roleMembers) { 105 principals.add(new PrincipalId(roleMember.getMemberId())); 106 } 107 108 if (principals.size() == 0) { 109 throw new RuntimeException("No principals to route to. Push to exception routing."); 110 } 111 112 rqr.setRecipients(principals); 113 rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea()); 114 115 return rqr; 116 } 117 118 @Override 119 public List<RoleName> getRoleNames() { 120 return Collections.emptyList(); 121 } 122 123 private CalendarEntry getCalendarEntry(LeaveBlock leaveBlock) { 124 return HrServiceLocator.getCalendarEntryService().getCalendarEntry(leaveBlock.getCalendarId()); 125 } 126}