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.leave.workflow;
17  
18  import java.util.ArrayList;
19  import java.util.HashSet;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.commons.lang.math.NumberUtils;
26  import org.apache.log4j.Logger;
27  import org.joda.time.DateTime;
28  import org.kuali.kpme.core.KPMENamespace;
29  import org.kuali.kpme.core.assignment.Assignment;
30  import org.kuali.kpme.core.role.KPMERole;
31  import org.kuali.kpme.core.service.HrServiceLocator;
32  import org.kuali.kpme.tklm.leave.payout.LeavePayout;
33  import org.kuali.rice.kew.api.exception.WorkflowException;
34  import org.kuali.rice.kew.api.identity.Id;
35  import org.kuali.rice.kew.api.identity.PrincipalId;
36  import org.kuali.rice.kew.api.rule.RoleName;
37  import org.kuali.rice.kew.engine.RouteContext;
38  import org.kuali.rice.kew.routeheader.DocumentContent;
39  import org.kuali.rice.kew.rule.GenericRoleAttribute;
40  import org.kuali.rice.kew.rule.QualifiedRoleName;
41  import org.kuali.rice.kim.api.role.RoleMember;
42  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
43  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
44  
45  @SuppressWarnings("unchecked")
46  public class LeavePayoutRoleAttribute extends GenericRoleAttribute {
47  
48  	private static final long serialVersionUID = -7366724230970121521L;
49  	
50  	private static final Logger LOG = Logger.getLogger(LeavePayoutRoleAttribute.class);
51  
52  	@Override
53  	public List<RoleName> getRoleNames() {
54  		List<RoleName> roleNames = new ArrayList<RoleName>();
55  		
56  		roleNames.add(RoleName.Builder.create(getClass().getName(), KPMERole.APPROVER.getRoleName(), KPMERole.APPROVER.getRoleName()).build());
57  		roleNames.add(RoleName.Builder.create(getClass().getName(), KPMERole.APPROVER_DELEGATE.getRoleName(), KPMERole.APPROVER_DELEGATE.getRoleName()).build());
58  
59  		return roleNames;
60  	}
61  	
62  	@Override
63      protected List<String> getRoleNameQualifiers(String roleName, DocumentContent documentContent) {
64  		Set<String> roleNameQualifiers = new HashSet<String>();
65  		
66  		Long routeHeaderId = new Long(documentContent.getRouteContext().getDocument().getDocumentId());
67          MaintenanceDocument document = null;
68  		try {
69  			document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(routeHeaderId.toString());
70  		} catch (WorkflowException e) {
71  			e.printStackTrace();
72  		}
73  		
74  		LeavePayout leavePayout = null;
75  		if (document != null && document.getNewMaintainableObject() != null) {
76  			leavePayout = (LeavePayout) document.getNewMaintainableObject().getDataObject();
77  		}
78  		
79          if (leavePayout != null) {
80              List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignments(leavePayout.getPrincipalId(), leavePayout.getEffectiveLocalDate());
81              for (Assignment assignment : assignments) {
82              	roleNameQualifiers.add(String.valueOf(assignment.getWorkArea()));
83              }
84          }
85          
86  		return new ArrayList<String>(roleNameQualifiers);
87      }
88  	
89  	@Override
90      protected List<Id> resolveRecipients(RouteContext routeContext, QualifiedRoleName qualifiedRoleName) {
91  		List<Id> recipients = new ArrayList<Id>();
92  		
93  		String roleName = qualifiedRoleName.getBaseRoleName();
94  		String qualifier = qualifiedRoleName.getQualifier();
95  		
96  		if (StringUtils.isNotBlank(roleName) && NumberUtils.isNumber(qualifier)) {
97  			Long workArea = Long.valueOf(qualifier);
98  	
99  			List<RoleMember> roleMembers = HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), roleName, workArea, new DateTime(), true);
100 			
101 	        for (RoleMember roleMember : roleMembers) {
102 	        	recipients.add(new PrincipalId(roleMember.getMemberId()));
103 		    }
104 		} else {
105 			LOG.error("Could not route to role " + roleName + " with work area " + qualifier);
106 		}
107 		
108 		return recipients;
109     }
110 	
111 	@Override
112 	public Map<String, String> getProperties() {
113 		return null;
114 	}
115 
116 }