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