View Javadoc
1   /**
2    * Copyright 2004-2014 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  import org.apache.log4j.Logger;
19  import org.joda.time.DateTime;
20  import org.kuali.kpme.core.api.assignment.Assignment;
21  import org.kuali.kpme.core.api.calendar.entry.CalendarEntry;
22  import org.kuali.kpme.core.api.namespace.KPMENamespace;
23  import org.kuali.kpme.core.api.workarea.WorkArea;
24  import org.kuali.kpme.core.calendar.entry.CalendarEntryBo;
25  import org.kuali.kpme.core.role.KPMERole;
26  import org.kuali.kpme.core.service.HrServiceLocator;
27  import org.kuali.kpme.tklm.api.common.TkConstants;
28  import org.kuali.kpme.tklm.api.leave.block.LeaveBlock;
29  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
30  import org.kuali.kpme.tklm.leave.workflow.LeaveRequestDocument;
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  import org.kuali.rice.kim.api.role.RoleMember;
39  
40  import java.util.ArrayList;
41  import java.util.Collections;
42  import java.util.List;
43  
44  @Deprecated
45  public class LeaveRequestWorkflowAttribute extends AbstractRoleAttribute {
46  
47  	private static final long serialVersionUID = -6939277052363491806L;
48  	
49  	private static final Logger LOG = Logger.getLogger(LeaveRequestWorkflowAttribute.class);
50  
51      @Override
52      public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
53          List<String> roles = new ArrayList<String>();
54          String documentNumber = documentContent.getRouteContext().getDocument().getDocumentId();
55          LeaveRequestDocument leaveRequestDocument = LmServiceLocator.getLeaveRequestDocumentService().getLeaveRequestDocument(documentNumber);
56  
57          if (leaveRequestDocument != null) {
58              LeaveBlock leaveBlock = leaveRequestDocument.getLeaveBlock();
59              CalendarEntry ce = getCalendarEntry(leaveBlock);
60              List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAllAssignmentsByCalEntryForLeaveCalendar(leaveBlock.getPrincipalId(), ce);
61              for (Assignment assignment : assignments) {
62                  String roleStr = roleName + "_" + assignment.getWorkArea();
63                  if (!roles.contains(roleStr)) {
64                      roles.add(roleStr);
65                  }
66              }
67          }
68          
69          return roles;
70      }
71  
72      @Override
73      public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
74          ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
75          Long workAreaNumber = null;
76  
77          try {
78              int pos = qualifiedRole.lastIndexOf("_");
79              if (pos > -1) {
80                  String subs = qualifiedRole.substring(pos+1, qualifiedRole.length());
81                  workAreaNumber = Long.parseLong(subs);
82              }
83          } catch (NumberFormatException nfe) {
84              LOG.error("qualifiedRole did not contain numeric data for work area.");
85          }
86  
87          if (workAreaNumber == null) {
88              throw new RuntimeException("Unable to resolve work area during routing.");
89          }
90  
91          List<Id> principals = new ArrayList<Id>();
92          String routeHeaderId = routeContext.getDocument().getDocumentId();
93          LeaveRequestDocument leaveRequestDocument = LmServiceLocator.getLeaveRequestDocumentService().getLeaveRequestDocument(routeHeaderId);
94          LeaveBlock leaveBlock = LmServiceLocator.getLeaveBlockService().getLeaveBlock(leaveRequestDocument.getLmLeaveBlockId());
95          WorkArea workArea = HrServiceLocator.getWorkAreaService().getWorkArea(workAreaNumber, leaveBlock.getLeaveLocalDate());
96  
97          List<RoleMember> roleMembers = new ArrayList<RoleMember>();
98          
99  		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 }