001 /** 002 * Copyright 2004-2013 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 */ 016 package org.kuali.hr.lm.workflow; 017 018 import java.util.ArrayList; 019 import java.util.Collections; 020 import java.util.List; 021 022 import org.apache.log4j.Logger; 023 import org.joda.time.DateTime; 024 import org.joda.time.LocalDate; 025 import org.kuali.kpme.core.KPMENamespace; 026 import org.kuali.kpme.core.assignment.Assignment; 027 import org.kuali.kpme.core.role.KPMERole; 028 import org.kuali.kpme.core.service.HrServiceLocator; 029 import org.kuali.kpme.core.workarea.WorkArea; 030 import org.kuali.kpme.tklm.common.TkConstants; 031 import org.kuali.kpme.tklm.leave.transfer.BalanceTransfer; 032 import org.kuali.rice.kew.api.exception.WorkflowException; 033 import org.kuali.rice.kew.api.identity.Id; 034 import org.kuali.rice.kew.api.identity.PrincipalId; 035 import org.kuali.rice.kew.api.rule.RoleName; 036 import org.kuali.rice.kew.engine.RouteContext; 037 import org.kuali.rice.kew.routeheader.DocumentContent; 038 import org.kuali.rice.kew.rule.AbstractRoleAttribute; 039 import org.kuali.rice.kew.rule.ResolvedQualifiedRole; 040 import org.kuali.rice.kim.api.role.RoleMember; 041 import org.kuali.rice.krad.maintenance.MaintenanceDocument; 042 import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 043 044 @Deprecated 045 public class BalanceTransferWorkflowAttribute extends AbstractRoleAttribute { 046 047 private static final long serialVersionUID = -1473150153290419492L; 048 049 private static final Logger LOG = Logger.getLogger(BalanceTransferWorkflowAttribute.class); 050 051 @Override 052 public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) { 053 List<String> roles = new ArrayList<String>(); 054 String documentNumber = documentContent.getRouteContext().getDocument().getDocumentId(); 055 MaintenanceDocument document = null; 056 try { 057 document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentNumber); 058 } catch (WorkflowException e) { 059 e.printStackTrace(); 060 } 061 062 BalanceTransfer balanceTransfer = null; 063 if (document != null && document.getNewMaintainableObject() != null) { 064 balanceTransfer = (BalanceTransfer) document.getNewMaintainableObject().getDataObject(); 065 } 066 067 if (balanceTransfer != null) { 068 List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignments(balanceTransfer.getPrincipalId(), balanceTransfer.getEffectiveLocalDate()); 069 for (Assignment assignment : assignments) { 070 String roleStr = roleName + "_" + assignment.getWorkArea(); 071 if (!roles.contains(roleStr)) { 072 roles.add(roleStr); 073 } 074 } 075 } 076 077 return roles; 078 } 079 080 @Override 081 public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) { 082 ResolvedQualifiedRole rqr = new ResolvedQualifiedRole(); 083 Long workAreaNumber = null; 084 085 try { 086 int pos = qualifiedRole.lastIndexOf("_"); 087 if (pos > -1) { 088 String subs = qualifiedRole.substring(pos+1, qualifiedRole.length()); 089 workAreaNumber = Long.parseLong(subs); 090 } 091 } catch (NumberFormatException nfe) { 092 LOG.error("qualifiedRole did not contain numeric data for work area."); 093 } 094 095 if (workAreaNumber == null) { 096 throw new RuntimeException("Unable to resolve work area during routing."); 097 } 098 099 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 }