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.calendar.entry.CalendarEntry;
027import org.kuali.kpme.core.role.KPMERole;
028import org.kuali.kpme.core.service.HrServiceLocator;
029import org.kuali.kpme.core.workarea.WorkArea;
030import org.kuali.kpme.tklm.common.TkConstants;
031import org.kuali.kpme.tklm.leave.block.LeaveBlock;
032import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
033import org.kuali.kpme.tklm.leave.workflow.LeaveRequestDocument;
034import org.kuali.rice.kew.api.identity.Id;
035import org.kuali.rice.kew.api.identity.PrincipalId;
036import org.kuali.rice.kew.api.rule.RoleName;
037import org.kuali.rice.kew.engine.RouteContext;
038import org.kuali.rice.kew.routeheader.DocumentContent;
039import org.kuali.rice.kew.rule.AbstractRoleAttribute;
040import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
041import org.kuali.rice.kim.api.role.RoleMember;
042
043@Deprecated
044public class LeaveRequestWorkflowAttribute extends AbstractRoleAttribute {
045
046        private static final long serialVersionUID = -6939277052363491806L;
047        
048        private static final Logger LOG = Logger.getLogger(LeaveRequestWorkflowAttribute.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        LeaveRequestDocument leaveRequestDocument = LmServiceLocator.getLeaveRequestDocumentService().getLeaveRequestDocument(documentNumber);
055
056        if (leaveRequestDocument != null) {
057            LeaveBlock leaveBlock = leaveRequestDocument.getLeaveBlock();
058            CalendarEntry ce = getCalendarEntry(leaveBlock);
059            List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignmentsByCalEntryForLeaveCalendar(leaveBlock.getPrincipalId(), ce);
060            for (Assignment assignment : assignments) {
061                String roleStr = roleName + "_" + assignment.getWorkArea();
062                if (!roles.contains(roleStr)) {
063                    roles.add(roleStr);
064                }
065            }
066        }
067        
068        return roles;
069    }
070
071    @Override
072    public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
073        ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
074        Long workAreaNumber = null;
075
076        try {
077            int pos = qualifiedRole.lastIndexOf("_");
078            if (pos > -1) {
079                String subs = qualifiedRole.substring(pos+1, qualifiedRole.length());
080                workAreaNumber = Long.parseLong(subs);
081            }
082        } catch (NumberFormatException nfe) {
083            LOG.error("qualifiedRole did not contain numeric data for work area.");
084        }
085
086        if (workAreaNumber == null) {
087            throw new RuntimeException("Unable to resolve work area during routing.");
088        }
089
090        List<Id> principals = new ArrayList<Id>();
091        String routeHeaderId = routeContext.getDocument().getDocumentId();
092        LeaveRequestDocument leaveRequestDocument = LmServiceLocator.getLeaveRequestDocumentService().getLeaveRequestDocument(routeHeaderId);
093        LeaveBlock leaveBlock = LmServiceLocator.getLeaveBlockService().getLeaveBlock(leaveRequestDocument.getLmLeaveBlockId());
094        WorkArea workArea = HrServiceLocator.getWorkAreaService().getWorkAreaWithoutRoles(workAreaNumber, leaveBlock.getLeaveLocalDate());
095
096        List<RoleMember> roleMembers = new ArrayList<RoleMember>();
097        
098                if (TkConstants.ROLE_TK_APPROVER.equals(roleName)) {
099                roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), workAreaNumber, new DateTime(), true));
100                roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), workAreaNumber, new DateTime(), true));
101                }
102                
103        for (RoleMember roleMember : roleMembers) {
104                principals.add(new PrincipalId(roleMember.getMemberId()));
105            }
106
107        if (principals.size() == 0) {
108            throw new RuntimeException("No principals to route to. Push to exception routing.");
109        }
110        
111        rqr.setRecipients(principals);
112        rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea());
113
114        return rqr;
115    }
116
117    @Override
118    public List<RoleName> getRoleNames() {
119        return Collections.emptyList();
120    }
121
122    private CalendarEntry getCalendarEntry(LeaveBlock leaveBlock) {
123        return HrServiceLocator.getCalendarEntryService().getCalendarEntry(leaveBlock.getCalendarId());
124    }
125}