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.kpme.tklm.time.workflow;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.commons.lang.math.NumberUtils;
20  import org.apache.log4j.Logger;
21  import org.joda.time.DateTime;
22  import org.kuali.kpme.core.api.assignment.Assignment;
23  import org.kuali.kpme.core.api.namespace.KPMENamespace;
24  import org.kuali.kpme.core.role.KPMERole;
25  import org.kuali.kpme.core.service.HrServiceLocator;
26  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
27  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
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.GenericRoleAttribute;
34  import org.kuali.rice.kew.rule.QualifiedRoleName;
35  import org.kuali.rice.kim.api.role.RoleMember;
36  
37  import java.util.*;
38  
39  @SuppressWarnings("unchecked")
40  public class TimesheetRoleAttribute extends GenericRoleAttribute {
41  
42  	private static final long serialVersionUID = 6133675090144279606L;
43  	
44  	private static final Logger LOG = Logger.getLogger(TimesheetRoleAttribute.class);
45  
46  	@Override
47  	public List<RoleName> getRoleNames() {
48  		List<RoleName> roleNames = new ArrayList<RoleName>();
49  		
50  		roleNames.add(RoleName.Builder.create(getClass().getName(), KPMERole.APPROVER.getRoleName(), KPMERole.APPROVER.getRoleName()).build());
51  		roleNames.add(RoleName.Builder.create(getClass().getName(), KPMERole.APPROVER_DELEGATE.getRoleName(), KPMERole.APPROVER_DELEGATE.getRoleName()).build());
52  
53  		return roleNames;
54  	}
55  	
56  	@Override
57      protected List<String> getRoleNameQualifiers(String roleName, DocumentContent documentContent) {
58  		Set<String> roleNameQualifiers = new HashSet<String>();
59  		
60  		Long routeHeaderId = new Long(documentContent.getRouteContext().getDocument().getDocumentId());
61  		TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(routeHeaderId.toString());
62  
63  		if (timesheetDocument != null) {
64  			List<Assignment> assignments = timesheetDocument.getAllAssignments();
65  			for (Assignment assignment : assignments) {
66  				roleNameQualifiers.add(String.valueOf(assignment.getWorkArea()));
67  			}
68  		}
69  		
70  		return new ArrayList<String>(roleNameQualifiers);
71      }
72  	
73  	@Override
74      protected List<Id> resolveRecipients(RouteContext routeContext, QualifiedRoleName qualifiedRoleName) {
75  		List<Id> recipients = new ArrayList<Id>();
76  		
77  		String roleName = qualifiedRoleName.getBaseRoleName();
78  		String qualifier = qualifiedRoleName.getQualifier();
79  		
80  		if (StringUtils.isNotBlank(roleName) && NumberUtils.isNumber(qualifier)) {
81  			Long workArea = Long.valueOf(qualifier);
82  	
83  			List<RoleMember> roleMembers = HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), roleName, workArea, new DateTime(), true);
84  			
85  	        for (RoleMember roleMember : roleMembers) {
86  	        	recipients.add(new PrincipalId(roleMember.getMemberId()));
87  		    }
88  		} else {
89  			LOG.error("Could not route to role " + roleName + " with work area " + qualifier);
90  		}
91  		
92  		return recipients;
93      }
94  
95  	@Override
96  	public Map<String, String> getProperties() {
97  		return null;
98  	}
99  
100 }