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 java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.apache.log4j.Logger;
23  import org.joda.time.DateTime;
24  import org.kuali.kpme.core.KPMENamespace;
25  import org.kuali.kpme.core.assignment.Assignment;
26  import org.kuali.kpme.core.calendar.entry.CalendarEntry;
27  import org.kuali.kpme.core.role.KPMERole;
28  import org.kuali.kpme.core.service.HrServiceLocator;
29  import org.kuali.kpme.core.workarea.WorkArea;
30  import org.kuali.kpme.tklm.common.TkConstants;
31  import org.kuali.kpme.tklm.leave.block.LeaveBlock;
32  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
33  import org.kuali.kpme.tklm.leave.workflow.LeaveRequestDocument;
34  import org.kuali.rice.kew.api.identity.Id;
35  import org.kuali.rice.kew.api.identity.PrincipalId;
36  import org.kuali.rice.kew.api.rule.RoleName;
37  import org.kuali.rice.kew.engine.RouteContext;
38  import org.kuali.rice.kew.routeheader.DocumentContent;
39  import org.kuali.rice.kew.rule.AbstractRoleAttribute;
40  import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
41  import org.kuali.rice.kim.api.role.RoleMember;
42  
43  @Deprecated
44  public class LeaveRequestWorkflowAttribute extends AbstractRoleAttribute {
45  
46  	private static final long serialVersionUID = -6939277052363491806L;
47  	
48  	private static final Logger LOG = Logger.getLogger(LeaveRequestWorkflowAttribute.class);
49  
50      @Override
51      public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
52          List<String> roles = new ArrayList<String>();
53          String documentNumber = documentContent.getRouteContext().getDocument().getDocumentId();
54          LeaveRequestDocument leaveRequestDocument = LmServiceLocator.getLeaveRequestDocumentService().getLeaveRequestDocument(documentNumber);
55  
56          if (leaveRequestDocument != null) {
57              LeaveBlock leaveBlock = leaveRequestDocument.getLeaveBlock();
58              CalendarEntry ce = getCalendarEntry(leaveBlock);
59              List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignmentsByCalEntryForLeaveCalendar(leaveBlock.getPrincipalId(), ce);
60              for (Assignment assignment : assignments) {
61                  String roleStr = roleName + "_" + assignment.getWorkArea();
62                  if (!roles.contains(roleStr)) {
63                      roles.add(roleStr);
64                  }
65              }
66          }
67          
68          return roles;
69      }
70  
71      @Override
72      public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
73          ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
74          Long workAreaNumber = null;
75  
76          try {
77              int pos = qualifiedRole.lastIndexOf("_");
78              if (pos > -1) {
79                  String subs = qualifiedRole.substring(pos+1, qualifiedRole.length());
80                  workAreaNumber = Long.parseLong(subs);
81              }
82          } catch (NumberFormatException nfe) {
83              LOG.error("qualifiedRole did not contain numeric data for work area.");
84          }
85  
86          if (workAreaNumber == null) {
87              throw new RuntimeException("Unable to resolve work area during routing.");
88          }
89  
90          List<Id> principals = new ArrayList<Id>();
91          String routeHeaderId = routeContext.getDocument().getDocumentId();
92          LeaveRequestDocument leaveRequestDocument = LmServiceLocator.getLeaveRequestDocumentService().getLeaveRequestDocument(routeHeaderId);
93          LeaveBlock leaveBlock = LmServiceLocator.getLeaveBlockService().getLeaveBlock(leaveRequestDocument.getLmLeaveBlockId());
94          WorkArea workArea = HrServiceLocator.getWorkAreaService().getWorkAreaWithoutRoles(workAreaNumber, leaveBlock.getLeaveLocalDate());
95  
96          List<RoleMember> roleMembers = new ArrayList<RoleMember>();
97          
98  		if (TkConstants.ROLE_TK_APPROVER.equals(roleName)) {
99  	        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), workAreaNumber, new DateTime(), true));
100 	        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), workAreaNumber, new DateTime(), true));
101 		}
102 		
103         for (RoleMember roleMember : roleMembers) {
104         	principals.add(new PrincipalId(roleMember.getMemberId()));
105 	    }
106 
107         if (principals.size() == 0) {
108             throw new RuntimeException("No principals to route to. Push to exception routing.");
109         }
110         
111         rqr.setRecipients(principals);
112         rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea());
113 
114         return rqr;
115     }
116 
117     @Override
118     public List<RoleName> getRoleNames() {
119         return Collections.emptyList();
120     }
121 
122     private CalendarEntry getCalendarEntry(LeaveBlock leaveBlock) {
123         return HrServiceLocator.getCalendarEntryService().getCalendarEntry(leaveBlock.getCalendarId());
124     }
125 }