View Javadoc

1   /**
2    * Copyright 2004-2013 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  
19  import org.apache.cxf.common.util.StringUtils;
20  import org.apache.log4j.Logger;
21  import org.kuali.hr.job.Job;
22  import org.kuali.hr.lm.balancetransfer.BalanceTransfer;
23  import org.kuali.hr.lm.leaveblock.LeaveBlock;
24  import org.kuali.hr.time.assignment.Assignment;
25  import org.kuali.hr.time.calendar.CalendarEntries;
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.util.TKUtils;
30  import org.kuali.hr.time.util.TkConstants;
31  import org.kuali.hr.time.workarea.WorkArea;
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.krad.maintenance.MaintenanceDocument;
41  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
42  import org.kuali.rice.krad.util.ObjectUtils;
43  
44  import java.util.ArrayList;
45  import java.util.Collections;
46  import java.util.List;
47  
48  public class BalanceTransferWorkflowAttribute extends AbstractRoleAttribute {
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  			// TODO Auto-generated catch block
60  			e.printStackTrace();
61  		}
62  		BalanceTransfer balanceTransfer = null;
63  		if(ObjectUtils.isNotNull(document))
64  			balanceTransfer = (BalanceTransfer) document.getNewMaintainableObject().getDataObject();
65  
66          if (balanceTransfer != null) {
67              List<Assignment> assignments = TkServiceLocator.getAssignmentService().getAssignments(balanceTransfer.getPrincipalId(), balanceTransfer.getEffectiveDate());
68              for(Assignment assignment : assignments) {
69                  String roleStr = roleName+"_"+assignment.getWorkArea();
70                  if(!roles.contains(roleStr))
71                      roles.add(roleStr);
72              }
73  
74          }
75          return roles;
76      }
77  
78      /**
79       * Role name is passed in in the routing rule.
80       */
81      @Override
82      public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
83          ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
84          Long workAreaNumber = null;
85  
86          try {
87              int pos = qualifiedRole.lastIndexOf("_");
88              if (pos > -1) {
89                  String subs = qualifiedRole.substring(pos+1, qualifiedRole.length());
90                  workAreaNumber = Long.parseLong(subs);
91              }
92          } catch (NumberFormatException nfe) {
93              LOG.error("qualifiedRole did not contain numeric data for work area.");
94          }
95  
96          if (workAreaNumber == null) {
97              throw new RuntimeException("Unable to resolve work area during routing.");
98          }
99  
100         List<Id> principals = new ArrayList<Id>();
101         String routeHeaderId = routeContext.getDocument().getDocumentId();
102         MaintenanceDocument document = null;
103 		try {
104 			document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(routeHeaderId);
105 		} catch (WorkflowException e) {
106 			LOG.error("unable to retrieve the Maintenance Document with route hearder id: " + routeHeaderId);
107 			e.printStackTrace();
108 		}
109         TkRoleService roleService = TkServiceLocator.getTkRoleService();
110         BalanceTransfer balanceTransfer = null;
111         if(ObjectUtils.isNotNull(document)) {
112         	balanceTransfer = (BalanceTransfer) document.getNewMaintainableObject().getDataObject();
113         }
114         if(ObjectUtils.isNotNull(balanceTransfer)) {
115 	        WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(workAreaNumber, balanceTransfer.getEffectiveDate());
116 	
117 	        // KPME-1071
118 	        List<TkRole> approvers = roleService.getWorkAreaRoles(workAreaNumber, roleName, TKUtils.getCurrentDate());
119 	        List<TkRole> approverDelegates = roleService.getWorkAreaRoles(workAreaNumber, TkConstants.ROLE_TK_APPROVER_DELEGATE, TKUtils.getCurrentDate());
120 	        List<TkRole> roles = new ArrayList<TkRole>();
121 	        roles.addAll(approvers);
122 	        roles.addAll(approverDelegates);
123 	
124 	        for (TkRole role : roles) {
125 	            //Position routing
126 	            if(StringUtils.isEmpty(role.getPrincipalId())){
127 	                String positionNumber = role.getPositionNumber();
128 	                List<Job> lstJobsForPosition = TkServiceLocator.getJobService().getActiveJobsForPosition(positionNumber, balanceTransfer.getEffectiveDate());
129 	                for(Job job : lstJobsForPosition){
130 	                    PrincipalId pid = new PrincipalId(job.getPrincipalId());
131 	                    if (!principals.contains(pid)) {
132 	                        principals.add(pid);
133 	                    }
134 	                }
135 	            } else {
136 	                PrincipalId pid = new PrincipalId(role.getPrincipalId());
137 	                if (!principals.contains(pid)) {
138 	                    principals.add(pid);
139 	                }
140 	            }
141 	        }
142 	
143 	        if (principals.size() == 0)  {
144 	            throw new RuntimeException("No principals to route to. Push to exception routing.");
145             }
146 	        rqr.setRecipients(principals);
147 	        rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea());
148 	        return rqr;
149         }
150         else {
151         	throw new RuntimeException("no business object could be retreived");
152         }
153     }
154 
155     @Override
156     public List<RoleName> getRoleNames() {
157         return Collections.EMPTY_LIST;
158     }
159 
160     private CalendarEntries getCalendarEntry(LeaveBlock leaveBlock) {
161         return TkServiceLocator.getCalendarEntriesService().getCalendarEntries(leaveBlock.getCalendarId());
162     }
163 }