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.DateTime;
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.payout.LeavePayout;
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 LeavePayoutWorkflowAttribute extends AbstractRoleAttribute {
047
048        private static final long serialVersionUID = 540529528227045564L;
049        
050        private static final Logger LOG = Logger.getLogger(LeavePayoutWorkflowAttribute.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                LeavePayout leavePayout = null;
063                if (document != null && document.getNewMaintainableObject() != null) {
064                        leavePayout = (LeavePayout) document.getNewMaintainableObject().getDataObject();
065                }
066                
067        if (leavePayout != null) {
068            List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignments(leavePayout.getPrincipalId(), leavePayout.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 header id: " + routeHeaderId);
106                        e.printStackTrace();
107                }
108                
109        LeavePayout leavePayout = null;
110        if (document != null && document.getNewMaintainableObject() != null) {
111                leavePayout = (LeavePayout) document.getNewMaintainableObject().getDataObject();
112        }
113        
114        if (leavePayout != null) {
115                WorkArea workArea = HrServiceLocator.getWorkAreaService().getWorkArea(workAreaNumber, leavePayout.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, new DateTime(), true));
121                        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), workAreaNumber, new DateTime(), 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}