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