View Javadoc

1   /**
2    * Copyright 2004-2012 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.cxf.common.util.StringUtils;
23  import org.apache.log4j.Logger;
24  import org.kuali.hr.job.Job;
25  import org.kuali.hr.time.assignment.Assignment;
26  import org.kuali.hr.time.roles.TkRole;
27  import org.kuali.hr.time.roles.service.TkRoleService;
28  import org.kuali.hr.time.service.base.TkServiceLocator;
29  import org.kuali.hr.time.timesheet.TimesheetDocument;
30  import org.kuali.hr.time.util.TKUtils;
31  import org.kuali.hr.time.util.TkConstants;
32  import org.kuali.hr.time.workarea.WorkArea;
33  import org.kuali.rice.kew.api.identity.Id;
34  import org.kuali.rice.kew.api.identity.PrincipalId;
35  import org.kuali.rice.kew.api.rule.RoleName;
36  import org.kuali.rice.kew.engine.RouteContext;
37  import org.kuali.rice.kew.routeheader.DocumentContent;
38  import org.kuali.rice.kew.rule.AbstractRoleAttribute;
39  import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
40  
41  public class TkWorkflowTimesheetAttribute extends AbstractRoleAttribute {
42  
43      private static final Logger LOG = Logger.getLogger(TkWorkflowTimesheetAttribute.class);
44  
45  	@Override
46  	public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
47  		List<String> roles = new ArrayList<String>();
48  		Long routeHeaderId = new Long(documentContent.getRouteContext().getDocument().getDocumentId());
49  		TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(routeHeaderId.toString());
50  
51  		if (timesheetDocument != null) {
52  			List<Assignment> assignments = timesheetDocument.getAssignments();
53  			for (Assignment assignment : assignments) {
54  				String roleStr = roleName + "_" +assignment.getWorkArea();
55  				if(!roles.contains(roleStr)){
56  					roles.add(roleStr);
57  				}
58  			}
59  		}
60  		return roles;
61  	}
62  
63  	/**
64  	 * Role name is passed in in the routing rule.
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  		TkRoleService roleService = TkServiceLocator.getTkRoleService();
88  		TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(routeHeaderId.toString());
89  		WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(workAreaNumber, timesheetDocument.getAsOfDate());
90  
91          // KPME-1071
92          List<TkRole> approvers = roleService.getWorkAreaRoles(workAreaNumber, roleName, TKUtils.getCurrentDate());
93          List<TkRole> approverDelegates = roleService.getWorkAreaRoles(workAreaNumber, TkConstants.ROLE_TK_APPROVER_DELEGATE, TKUtils.getCurrentDate());
94  		List<TkRole> roles = new ArrayList<TkRole>();
95          roles.addAll(approvers);
96          roles.addAll(approverDelegates);
97  
98  		for (TkRole role : roles) {
99  			//Position routing
100 			if(StringUtils.isEmpty(role.getPrincipalId())){
101 				String positionNumber = role.getPositionNumber();
102 				List<Job> lstJobsForPosition = TkServiceLocator.getJobService().getActiveJobsForPosition(positionNumber, timesheetDocument.getPayCalendarEntry().getEndPeriodDateTime());
103 				for(Job job : lstJobsForPosition){
104 					PrincipalId pid = new PrincipalId(job.getPrincipalId());
105 					if (!principals.contains(pid)) {
106 						principals.add(pid);
107 					}
108 				}
109 			} else {
110 				PrincipalId pid = new PrincipalId(role.getPrincipalId());
111 					if (!principals.contains(pid)) {
112 						principals.add(pid);
113 					}
114 			}
115 		}
116 
117 		if (principals.size() == 0)
118 			throw new RuntimeException("No principals to route to. Push to exception routing.");
119 
120 		rqr.setRecipients(principals);
121 		rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea());
122 
123 		return rqr;
124 	}
125 
126 	@Override
127 	public List<RoleName> getRoleNames() {
128         return Collections.EMPTY_LIST;
129 	}
130 
131 }