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 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.time.service.TkServiceLocator;
31  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
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 TkWorkflowTimesheetAttribute extends AbstractRoleAttribute {
43  
44  	private static final long serialVersionUID = -1745965455586658502L;
45  	
46  	private static final Logger LOG = Logger.getLogger(TkWorkflowTimesheetAttribute.class);
47  
48  	@Override
49  	public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
50  		List<String> roles = new ArrayList<String>();
51  		Long routeHeaderId = new Long(documentContent.getRouteContext().getDocument().getDocumentId());
52  		TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(routeHeaderId.toString());
53  
54  		if (timesheetDocument != null) {
55  			List<Assignment> assignments = timesheetDocument.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  		TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(routeHeaderId.toString());
89  		WorkArea workArea = HrServiceLocator.getWorkAreaService().getWorkAreaWithoutRoles(workAreaNumber, timesheetDocument.getAsOfDate());
90  
91  		List<RoleMember> roleMembers = new ArrayList<RoleMember>();
92  		
93  		if (TkConstants.ROLE_TK_APPROVER.equals(roleName)) {
94  	        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), workAreaNumber, new DateTime(), true));
95  	        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), workAreaNumber, new DateTime(), true));
96  		}
97  		
98          for (RoleMember roleMember : roleMembers) {
99          	principals.add(new PrincipalId(roleMember.getMemberId()));
100 	    }
101 
102 		if (principals.size() == 0) {
103 			throw new RuntimeException("No principals to route to. Push to exception routing.");
104 		}
105 		
106 		rqr.setRecipients(principals);
107 		rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea());
108 
109 		return rqr;
110 	}
111 
112 	@Override
113 	public List<RoleName> getRoleNames() {
114         return Collections.emptyList();
115 	}
116 
117 }