View Javadoc

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