001/** 002 * Copyright 2004-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.hr.lm.workflow; 017 018import org.apache.log4j.Logger; 019import org.joda.time.LocalDate; 020import org.kuali.kpme.core.api.assignment.Assignment; 021import org.kuali.kpme.core.api.namespace.KPMENamespace; 022import org.kuali.kpme.core.api.workarea.WorkArea; 023import org.kuali.kpme.core.assignment.AssignmentBo; 024import org.kuali.kpme.core.assignment.AssignmentBo; 025import org.kuali.kpme.core.role.KPMERole; 026import org.kuali.kpme.core.service.HrServiceLocator; 027import org.kuali.kpme.tklm.api.common.TkConstants; 028import org.kuali.kpme.tklm.leave.transfer.BalanceTransfer; 029import org.kuali.rice.kew.api.exception.WorkflowException; 030import org.kuali.rice.kew.api.identity.Id; 031import org.kuali.rice.kew.api.identity.PrincipalId; 032import org.kuali.rice.kew.api.rule.RoleName; 033import org.kuali.rice.kew.engine.RouteContext; 034import org.kuali.rice.kew.routeheader.DocumentContent; 035import org.kuali.rice.kew.rule.AbstractRoleAttribute; 036import org.kuali.rice.kew.rule.ResolvedQualifiedRole; 037import org.kuali.rice.kim.api.role.RoleMember; 038import org.kuali.rice.krad.maintenance.MaintenanceDocument; 039import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 040 041import java.util.ArrayList; 042import java.util.Collections; 043import java.util.List; 044 045@Deprecated 046public class BalanceTransferWorkflowAttribute extends AbstractRoleAttribute { 047 048 private static final long serialVersionUID = -1473150153290419492L; 049 050 private static final Logger LOG = Logger.getLogger(BalanceTransferWorkflowAttribute.class); 051 052 @Override 053 public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) { 054 List<String> roles = new ArrayList<String>(); 055 String documentNumber = documentContent.getRouteContext().getDocument().getDocumentId(); 056 MaintenanceDocument document = null; 057 try { 058 document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentNumber); 059 } catch (WorkflowException e) { 060 e.printStackTrace(); 061 } 062 063 BalanceTransfer balanceTransfer = null; 064 if (document != null && document.getNewMaintainableObject() != null) { 065 balanceTransfer = (BalanceTransfer) document.getNewMaintainableObject().getDataObject(); 066 } 067 068 if (balanceTransfer != null) { 069 List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignments(balanceTransfer.getPrincipalId(), balanceTransfer.getEffectiveLocalDate()); 070 for (Assignment assignment : assignments) { 071 String roleStr = roleName + "_" + assignment.getWorkArea(); 072 if (!roles.contains(roleStr)) { 073 roles.add(roleStr); 074 } 075 } 076 } 077 078 return roles; 079 } 080 081 @Override 082 public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) { 083 ResolvedQualifiedRole rqr = new ResolvedQualifiedRole(); 084 Long workAreaNumber = null; 085 086 try { 087 int pos = qualifiedRole.lastIndexOf("_"); 088 if (pos > -1) { 089 String subs = qualifiedRole.substring(pos+1, qualifiedRole.length()); 090 workAreaNumber = Long.parseLong(subs); 091 } 092 } catch (NumberFormatException nfe) { 093 LOG.error("qualifiedRole did not contain numeric data for work area."); 094 } 095 096 if (workAreaNumber == null) { 097 throw new RuntimeException("Unable to resolve work area during routing."); 098 } 099 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 110 BalanceTransfer balanceTransfer = null; 111 if (document != null && document.getNewMaintainableObject() != null) { 112 balanceTransfer = (BalanceTransfer) document.getNewMaintainableObject().getDataObject(); 113 } 114 115 if (balanceTransfer != null) { 116 WorkArea workArea = HrServiceLocator.getWorkAreaService().getWorkArea(workAreaNumber, balanceTransfer.getEffectiveLocalDate()); 117 118 List<RoleMember> roleMembers = new ArrayList<RoleMember>(); 119 120 if (TkConstants.ROLE_TK_APPROVER.equals(roleName)) { 121 roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), workAreaNumber, LocalDate.now().toDateTimeAtStartOfDay(), true)); 122 roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), workAreaNumber, LocalDate.now().toDateTimeAtStartOfDay(), true)); 123 } 124 125 for (RoleMember roleMember : roleMembers) { 126 principals.add(new PrincipalId(roleMember.getMemberId())); 127 } 128 129 if (principals.size() == 0) { 130 throw new RuntimeException("No principals to route to. Push to exception routing."); 131 } 132 133 rqr.setRecipients(principals); 134 rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea()); 135 136 return rqr; 137 } else { 138 throw new RuntimeException("no business object could be retreived"); 139 } 140 } 141 142 @Override 143 public List<RoleName> getRoleNames() { 144 return Collections.emptyList(); 145 } 146 147}