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.lm.workflow;
017    
018    
019    import org.apache.cxf.common.util.StringUtils;
020    import org.apache.log4j.Logger;
021    import org.kuali.hr.job.Job;
022    import org.kuali.hr.lm.leavepayout.LeavePayout;
023    import org.kuali.hr.lm.leaveblock.LeaveBlock;
024    import org.kuali.hr.time.assignment.Assignment;
025    import org.kuali.hr.time.calendar.CalendarEntries;
026    import org.kuali.hr.time.roles.TkRole;
027    import org.kuali.hr.time.roles.service.TkRoleService;
028    import org.kuali.hr.time.service.base.TkServiceLocator;
029    import org.kuali.hr.time.util.TKUtils;
030    import org.kuali.hr.time.util.TkConstants;
031    import org.kuali.hr.time.workarea.WorkArea;
032    import org.kuali.rice.kew.api.exception.WorkflowException;
033    import org.kuali.rice.kew.api.identity.Id;
034    import org.kuali.rice.kew.api.identity.PrincipalId;
035    import org.kuali.rice.kew.api.rule.RoleName;
036    import org.kuali.rice.kew.engine.RouteContext;
037    import org.kuali.rice.kew.routeheader.DocumentContent;
038    import org.kuali.rice.kew.rule.AbstractRoleAttribute;
039    import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
040    import org.kuali.rice.krad.maintenance.MaintenanceDocument;
041    import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
042    import org.kuali.rice.krad.util.ObjectUtils;
043    
044    import java.util.ArrayList;
045    import java.util.Collections;
046    import java.util.List;
047    
048    public class LeavePayoutWorkflowAttribute extends AbstractRoleAttribute {
049        private static final Logger LOG = Logger.getLogger(LeavePayoutWorkflowAttribute.class);
050    
051        @Override
052        public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
053            List<String> roles = new ArrayList<String>();
054            String documentNumber = documentContent.getRouteContext().getDocument().getDocumentId();
055            MaintenanceDocument document = null;
056                    try {
057                            document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentNumber);
058                    } catch (WorkflowException e) {
059                            // TODO Auto-generated catch block
060                            e.printStackTrace();
061                    }
062                    LeavePayout leavePayout = null;
063                    if(document != null
064                    && document.getNewMaintainableObject() != null)
065                            leavePayout = (LeavePayout) document.getNewMaintainableObject().getDataObject();
066    
067            if (leavePayout != null) {
068                List<Assignment> assignments = TkServiceLocator.getAssignmentService().getAssignments(leavePayout.getPrincipalId(), leavePayout.getEffectiveDate());
069                    for(Assignment assignment : assignments) {
070                    String roleStr = roleName+"_"+assignment.getWorkArea();
071                            if(!roles.contains(roleStr))
072                                    roles.add(roleStr);
073                    }
074    
075                            }
076            return roles;
077        }
078    
079        /**
080         * Role name is passed in in the routing rule.
081         */
082        @Override
083        public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
084            ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
085            Long workAreaNumber = null;
086    
087            try {
088                int pos = qualifiedRole.lastIndexOf("_");
089                if (pos > -1) {
090                    String subs = qualifiedRole.substring(pos+1, qualifiedRole.length());
091                    workAreaNumber = Long.parseLong(subs);
092                }
093            } catch (NumberFormatException nfe) {
094                LOG.error("qualifiedRole did not contain numeric data for work area.");
095            }
096    
097            if (workAreaNumber == null) {
098                throw new RuntimeException("Unable to resolve work area during routing.");
099            }
100    
101            List<Id> principals = new ArrayList<Id>();
102            String routeHeaderId = routeContext.getDocument().getDocumentId();
103            MaintenanceDocument document = null;
104                    try {
105                            document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(routeHeaderId);
106                    } catch (WorkflowException e) {
107                            LOG.error("unable to retrieve the Maintenance Document with route header id: " + routeHeaderId);
108                            e.printStackTrace();
109                    }
110            TkRoleService roleService = TkServiceLocator.getTkRoleService();
111            LeavePayout leavePayout = null;
112            if(ObjectUtils.isNotNull(document))
113                    leavePayout = (LeavePayout) document.getNewMaintainableObject().getDataObject();
114            if(ObjectUtils.isNotNull(leavePayout)) {
115                    WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(workAreaNumber, leavePayout.getEffectiveDate());
116            
117                    // KPME-1071
118                    List<TkRole> approvers = roleService.getWorkAreaRoles(workAreaNumber, roleName, TKUtils.getCurrentDate());
119                    List<TkRole> approverDelegates = roleService.getWorkAreaRoles(workAreaNumber, TkConstants.ROLE_TK_APPROVER_DELEGATE, TKUtils.getCurrentDate());
120                    List<TkRole> roles = new ArrayList<TkRole>();
121                    roles.addAll(approvers);
122                    roles.addAll(approverDelegates);
123            
124                    for (TkRole role : roles) {
125                        //Position routing
126                        if(StringUtils.isEmpty(role.getPrincipalId())){
127                            String positionNumber = role.getPositionNumber();
128                            List<Job> lstJobsForPosition = TkServiceLocator.getJobService().getActiveJobsForPosition(positionNumber, leavePayout.getEffectiveDate());
129                            for(Job job : lstJobsForPosition){
130                                PrincipalId pid = new PrincipalId(job.getPrincipalId());
131                                if (!principals.contains(pid)) {
132                                    principals.add(pid);
133                                }
134                            }
135                        } else {
136                            PrincipalId pid = new PrincipalId(role.getPrincipalId());
137                            if (!principals.contains(pid)) {
138                                principals.add(pid);
139                            }
140                        }
141                    }
142            
143                    if (principals.size() == 0)  {
144                        throw new RuntimeException("No principals to route to. Push to exception routing.");
145                }
146                    rqr.setRecipients(principals);
147                    rqr.setAnnotation("Dept: "+ workArea.getDept()+", Work Area: "+workArea.getWorkArea());
148                    return rqr;
149            }
150            else {
151                    throw new RuntimeException("no business object could be retreived");
152            }
153        }
154    
155        @Override
156        public List<RoleName> getRoleNames() {
157            return Collections.EMPTY_LIST;
158        }
159    
160        private CalendarEntries getCalendarEntry(LeaveBlock leaveBlock) {
161            return TkServiceLocator.getCalendarEntriesService().getCalendarEntries(leaveBlock.getCalendarId());
162        }
163    }