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 java.util.ArrayList;
019import java.util.Collections;
020import java.util.List;
021
022import org.apache.log4j.Logger;
023import org.joda.time.DateTime;
024import org.kuali.kpme.core.KPMENamespace;
025import org.kuali.kpme.core.assignment.Assignment;
026import org.kuali.kpme.core.role.KPMERole;
027import org.kuali.kpme.core.service.HrServiceLocator;
028import org.kuali.kpme.core.workarea.WorkArea;
029import org.kuali.kpme.tklm.common.TkConstants;
030import org.kuali.kpme.tklm.leave.payout.LeavePayout;
031import org.kuali.rice.kew.api.exception.WorkflowException;
032import org.kuali.rice.kew.api.identity.Id;
033import org.kuali.rice.kew.api.identity.PrincipalId;
034import org.kuali.rice.kew.api.rule.RoleName;
035import org.kuali.rice.kew.engine.RouteContext;
036import org.kuali.rice.kew.routeheader.DocumentContent;
037import org.kuali.rice.kew.rule.AbstractRoleAttribute;
038import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
039import org.kuali.rice.kim.api.role.RoleMember;
040import org.kuali.rice.krad.maintenance.MaintenanceDocument;
041import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
042
043@Deprecated
044public class LeavePayoutWorkflowAttribute extends AbstractRoleAttribute {
045
046        private static final long serialVersionUID = 540529528227045564L;
047        
048        private static final Logger LOG = Logger.getLogger(LeavePayoutWorkflowAttribute.class);
049
050    @Override
051    public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
052        List<String> roles = new ArrayList<String>();
053        String documentNumber = documentContent.getRouteContext().getDocument().getDocumentId();
054        MaintenanceDocument document = null;
055                try {
056                        document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentNumber);
057                } catch (WorkflowException e) {
058                        e.printStackTrace();
059                }
060                LeavePayout leavePayout = null;
061                if (document != null && document.getNewMaintainableObject() != null) {
062                        leavePayout = (LeavePayout) document.getNewMaintainableObject().getDataObject();
063                }
064                
065        if (leavePayout != null) {
066            List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignments(leavePayout.getPrincipalId(), leavePayout.getEffectiveLocalDate());
067            for (Assignment assignment : assignments) {
068                String roleStr = roleName + "_" + assignment.getWorkArea();
069                if (!roles.contains(roleStr)) {
070                        roles.add(roleStr);
071                }
072            }
073        }
074        
075        return roles;
076    }
077
078    @Override
079    public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
080        ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
081        Long workAreaNumber = null;
082
083        try {
084            int pos = qualifiedRole.lastIndexOf("_");
085            if (pos > -1) {
086                String subs = qualifiedRole.substring(pos+1, qualifiedRole.length());
087                workAreaNumber = Long.parseLong(subs);
088            }
089        } catch (NumberFormatException nfe) {
090            LOG.error("qualifiedRole did not contain numeric data for work area.");
091        }
092
093        if (workAreaNumber == null) {
094            throw new RuntimeException("Unable to resolve work area during routing.");
095        }
096
097        List<Id> principals = new ArrayList<Id>();
098        String routeHeaderId = routeContext.getDocument().getDocumentId();
099        MaintenanceDocument document = null;
100                try {
101                        document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(routeHeaderId);
102                } catch (WorkflowException e) {
103                        LOG.error("unable to retrieve the Maintenance Document with route header id: " + routeHeaderId);
104                        e.printStackTrace();
105                }
106                
107        LeavePayout leavePayout = null;
108        if (document != null && document.getNewMaintainableObject() != null) {
109                leavePayout = (LeavePayout) document.getNewMaintainableObject().getDataObject();
110        }
111        
112        if (leavePayout != null) {
113                WorkArea workArea = HrServiceLocator.getWorkAreaService().getWorkAreaWithoutRoles(workAreaNumber, leavePayout.getEffectiveLocalDate());
114        
115                List<RoleMember> roleMembers = new ArrayList<RoleMember>();
116                
117                if (TkConstants.ROLE_TK_APPROVER.equals(roleName)) {
118                        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), workAreaNumber, new DateTime(), true));
119                        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), workAreaNumber, new DateTime(), true));
120                        }
121                        
122                for (RoleMember roleMember : roleMembers) {
123                        principals.add(new PrincipalId(roleMember.getMemberId()));
124                    }
125        
126                if (principals.size() == 0)  {
127                    throw new RuntimeException("No principals to route to. Push to exception routing.");
128            }
129                
130                rqr.setRecipients(principals);
131                rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea());
132                
133                return rqr;
134        } else {
135                throw new RuntimeException("no business object could be retreived");
136        }
137    }
138
139    @Override
140    public List<RoleName> getRoleNames() {
141        return Collections.emptyList();
142    }
143
144}