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.lm.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.joda.time.LocalDate;
25  import org.kuali.kpme.core.KPMENamespace;
26  import org.kuali.kpme.core.assignment.Assignment;
27  import org.kuali.kpme.core.role.KPMERole;
28  import org.kuali.kpme.core.service.HrServiceLocator;
29  import org.kuali.kpme.core.workarea.WorkArea;
30  import org.kuali.kpme.tklm.common.TkConstants;
31  import org.kuali.kpme.tklm.leave.transfer.BalanceTransfer;
32  import org.kuali.rice.kew.api.exception.WorkflowException;
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  import org.kuali.rice.kim.api.role.RoleMember;
41  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
42  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
43  
44  @Deprecated
45  public class BalanceTransferWorkflowAttribute extends AbstractRoleAttribute {
46  
47  	private static final long serialVersionUID = -1473150153290419492L;
48  	
49  	private static final Logger LOG = Logger.getLogger(BalanceTransferWorkflowAttribute.class);
50  
51      @Override
52      public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
53          List<String> roles = new ArrayList<String>();
54          String documentNumber = documentContent.getRouteContext().getDocument().getDocumentId();
55          MaintenanceDocument document = null;
56  		try {
57  			document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentNumber);
58  		} catch (WorkflowException e) {
59  			e.printStackTrace();
60  		}
61  		
62  		BalanceTransfer balanceTransfer = null;
63  		if (document != null && document.getNewMaintainableObject() != null) {
64  			balanceTransfer = (BalanceTransfer) document.getNewMaintainableObject().getDataObject();
65  		}
66  		
67          if (balanceTransfer != null) {
68              List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignments(balanceTransfer.getPrincipalId(), balanceTransfer.getEffectiveLocalDate());
69              for (Assignment assignment : assignments) {
70                  String roleStr = roleName + "_" + assignment.getWorkArea();
71                  if (!roles.contains(roleStr)) {
72                      roles.add(roleStr);
73                  }
74              }
75          }
76          
77          return roles;
78      }
79  
80      @Override
81      public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
82          ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
83          Long workAreaNumber = null;
84  
85          try {
86              int pos = qualifiedRole.lastIndexOf("_");
87              if (pos > -1) {
88                  String subs = qualifiedRole.substring(pos+1, qualifiedRole.length());
89                  workAreaNumber = Long.parseLong(subs);
90              }
91          } catch (NumberFormatException nfe) {
92              LOG.error("qualifiedRole did not contain numeric data for work area.");
93          }
94  
95          if (workAreaNumber == null) {
96              throw new RuntimeException("Unable to resolve work area during routing.");
97          }
98  
99          List<Id> principals = new ArrayList<Id>();
100         String routeHeaderId = routeContext.getDocument().getDocumentId();
101         MaintenanceDocument document = null;
102 		try {
103 			document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(routeHeaderId);
104 		} catch (WorkflowException e) {
105 			LOG.error("unable to retrieve the Maintenance Document with route hearder id: " + routeHeaderId);
106 			e.printStackTrace();
107 		}
108         
109         BalanceTransfer balanceTransfer = null;
110         if (document != null && document.getNewMaintainableObject() != null) {
111         	balanceTransfer = (BalanceTransfer) document.getNewMaintainableObject().getDataObject();
112         }
113         
114         if (balanceTransfer != null) {
115 	        WorkArea workArea = HrServiceLocator.getWorkAreaService().getWorkAreaWithoutRoles(workAreaNumber, balanceTransfer.getEffectiveLocalDate());
116 	
117 	        List<RoleMember> roleMembers = new ArrayList<RoleMember>();
118 	        
119 			if (TkConstants.ROLE_TK_APPROVER.equals(roleName)) {
120 		        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), workAreaNumber, LocalDate.now().toDateTimeAtStartOfDay(), true));
121 		        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), workAreaNumber, LocalDate.now().toDateTimeAtStartOfDay(), true));
122 			}
123 	
124 	        for (RoleMember roleMember : roleMembers) {
125 	        	principals.add(new PrincipalId(roleMember.getMemberId()));
126 		    }
127 	
128 	        if (principals.size() == 0)  {
129 	            throw new RuntimeException("No principals to route to. Push to exception routing.");
130             }
131 	        
132 	        rqr.setRecipients(principals);
133 	        rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea());
134 	        
135 	        return rqr;
136         } else {
137         	throw new RuntimeException("no business object could be retreived");
138         }
139     }
140 
141     @Override
142     public List<RoleName> getRoleNames() {
143         return Collections.emptyList();
144     }
145 
146 }