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 java.util.ArrayList;
019    import java.util.Collections;
020    import java.util.List;
021    
022    import org.joda.time.DateTime;
023    import org.kuali.kpme.core.KPMENamespace;
024    import org.kuali.kpme.core.assignment.Assignment;
025    import org.kuali.kpme.core.assignment.AssignmentDescriptionKey;
026    import org.kuali.kpme.core.role.KPMERole;
027    import org.kuali.kpme.core.service.HrServiceLocator;
028    import org.kuali.kpme.tklm.common.TkConstants;
029    import org.kuali.kpme.tklm.time.missedpunch.MissedPunch;
030    import org.kuali.kpme.tklm.time.missedpunch.MissedPunchDocument;
031    import org.kuali.kpme.tklm.time.service.TkServiceLocator;
032    import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
033    import org.kuali.rice.kew.api.exception.WorkflowException;
034    import org.kuali.rice.kew.api.identity.Id;
035    import org.kuali.rice.kew.api.identity.PrincipalId;
036    import org.kuali.rice.kew.api.rule.RoleName;
037    import org.kuali.rice.kew.engine.RouteContext;
038    import org.kuali.rice.kew.routeheader.DocumentContent;
039    import org.kuali.rice.kew.rule.AbstractRoleAttribute;
040    import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
041    import org.kuali.rice.kim.api.role.RoleMember;
042    import org.kuali.rice.krad.document.Document;
043    import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
044    
045    @Deprecated
046    public class TkWorkflowMissedPunchAttribute extends AbstractRoleAttribute {
047    
048            private static final long serialVersionUID = 8994254411764426802L;
049    
050            @Override
051            public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
052                    List<String> roles = new ArrayList<String>();
053                    roles.add(roleName);
054                    return roles;
055            }
056    
057            @Override
058            public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
059                    ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
060                    List<Id> principals = new ArrayList<Id>();
061    
062                    try {
063                            String documentId = routeContext.getDocument().getDocumentId();
064                            Document document = (MissedPunchDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderIdSessionless(documentId);
065                    MissedPunchDocument missedPunchDocument = (MissedPunchDocument) document;
066                    MissedPunch missedPunch = missedPunchDocument.getMissedPunch();
067            
068                    AssignmentDescriptionKey assignKey = AssignmentDescriptionKey.get(missedPunch.getAssignmentKey());
069                    String tsDocIdString = missedPunch.getTimesheetDocumentId();
070            
071                    if (tsDocIdString != null && assignKey != null) {
072                        TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().getTimesheetDocument(tsDocIdString);
073                        if (tdoc != null) {
074                            Assignment assignment = tdoc.getAssignment(assignKey);
075                            if (assignment != null) {
076                                    List<RoleMember> roleMembers = new ArrayList<RoleMember>();
077                                    
078                                    if (TkConstants.ROLE_TK_APPROVER.equals(roleName)) {
079                                    roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), assignment.getWorkArea(), new DateTime(), true));
080                                    roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), assignment.getWorkArea(), new DateTime(), true));
081                                    }
082                            
083                                    for (RoleMember roleMember : roleMembers) {
084                                            principals.add(new PrincipalId(roleMember.getMemberId()));
085                                        }
086                            } else {
087                                throw new RuntimeException("Could not obtain Assignment.");
088                            }
089                        } else {
090                            throw new RuntimeException("Could not obtain TimesheetDocument.");
091                        }
092                    } else {
093                        throw new RuntimeException("Could not obtain Timesheet Document ID or Assignment ID");
094                    }
095                    } catch (WorkflowException we) {
096                            we.printStackTrace();
097            }
098    
099                    if (principals.size() == 0) {
100                            throw new RuntimeException("No principals to route to. Push to exception routing.");
101                    }
102                    
103                    rqr.setRecipients(principals);
104                    
105                    return rqr;
106            }
107    
108            @Override
109            public List<RoleName> getRoleNames() {
110            return Collections.emptyList();
111            }
112            
113    }