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.time.workflow;
017    
018    import org.apache.cxf.common.util.StringUtils;
019    import org.apache.log4j.Logger;
020    import org.kuali.hr.job.Job;
021    import org.kuali.hr.lm.leavecalendar.LeaveCalendarDocument;
022    import org.kuali.hr.time.assignment.Assignment;
023    import org.kuali.hr.time.roles.TkRole;
024    import org.kuali.hr.time.roles.service.TkRoleService;
025    import org.kuali.hr.time.service.base.TkServiceLocator;
026    import org.kuali.hr.time.util.TKUtils;
027    import org.kuali.hr.time.util.TkConstants;
028    import org.kuali.hr.time.workarea.WorkArea;
029    import org.kuali.rice.kew.api.identity.Id;
030    import org.kuali.rice.kew.api.identity.PrincipalId;
031    import org.kuali.rice.kew.api.rule.RoleName;
032    import org.kuali.rice.kew.engine.RouteContext;
033    import org.kuali.rice.kew.routeheader.DocumentContent;
034    import org.kuali.rice.kew.rule.AbstractRoleAttribute;
035    import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
036    
037    import java.util.ArrayList;
038    import java.util.Collections;
039    import java.util.List;
040    
041    public class TkWorkflowLeaveCalendarAttribute extends AbstractRoleAttribute {
042    
043        private static final Logger LOG = Logger.getLogger(TkWorkflowLeaveCalendarAttribute.class);
044    
045            @Override
046            public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
047                    List<String> roles = new ArrayList<String>();
048                    String routeHeaderId = documentContent.getRouteContext().getDocument().getDocumentId();
049                    LeaveCalendarDocument leaveDocument = TkServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(routeHeaderId);
050    
051                    if (leaveDocument != null) {
052                            List<Assignment> assignments = leaveDocument.getAssignments();
053                            for (Assignment assignment : assignments) {
054                                    String roleStr = roleName + "_" +assignment.getWorkArea();
055                                    if(!roles.contains(roleStr)){
056                                            roles.add(roleStr);
057                                    }
058                            }
059                    }
060                    return roles;
061            }
062    
063            /**
064             * Role name is passed in in the routing rule.
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                    TkRoleService roleService = TkServiceLocator.getTkRoleService();
088                    LeaveCalendarDocument leaveCalendarDocument = TkServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(routeHeaderId);
089                    WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(workAreaNumber, leaveCalendarDocument.getAsOfDate());
090    
091            // KPME-1071
092            List<TkRole> approvers = roleService.getWorkAreaRoles(workAreaNumber, roleName, TKUtils.getCurrentDate());
093            List<TkRole> approverDelegates = roleService.getWorkAreaRoles(workAreaNumber, TkConstants.ROLE_TK_APPROVER_DELEGATE, TKUtils.getCurrentDate());
094                    List<TkRole> roles = new ArrayList<TkRole>();
095            roles.addAll(approvers);
096            roles.addAll(approverDelegates);
097    
098                    for (TkRole role : roles) {
099                            //Position routing
100                            if(StringUtils.isEmpty(role.getPrincipalId())){
101                                    String positionNumber = role.getPositionNumber();
102                                    List<Job> lstJobsForPosition = TkServiceLocator.getJobService().getActiveJobsForPosition(positionNumber, leaveCalendarDocument.getCalendarEntry().getEndPeriodDateTime());
103                                    for(Job job : lstJobsForPosition){
104                                            PrincipalId pid = new PrincipalId(job.getPrincipalId());
105                                            if (!principals.contains(pid)) {
106                                                    principals.add(pid);
107                                            }
108                                    }
109                            } else {
110                                    PrincipalId pid = new PrincipalId(role.getPrincipalId());
111                                            if (!principals.contains(pid)) {
112                                                    principals.add(pid);
113                                            }
114                            }
115                    }
116    
117                    if (principals.size() == 0)
118                            throw new RuntimeException("No principals to route to. Push to exception routing.");
119    
120                    rqr.setRecipients(principals);
121                    rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea());
122    
123                    return rqr;
124            }
125    
126            @Override
127            public List<RoleName> getRoleNames() {
128            return Collections.EMPTY_LIST;
129            }
130    
131    }