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