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.joda.time.DateTime;
019import org.kuali.kpme.core.api.assignment.Assignment;
020import org.kuali.kpme.core.api.assignment.AssignmentDescriptionKey;
021import org.kuali.kpme.core.api.namespace.KPMENamespace;
022import org.kuali.kpme.core.role.KPMERole;
023import org.kuali.kpme.core.service.HrServiceLocator;
024import org.kuali.kpme.tklm.api.common.TkConstants;
025import org.kuali.kpme.tklm.time.missedpunch.MissedPunchBo;
026import org.kuali.kpme.tklm.time.missedpunch.MissedPunchDocument;
027import org.kuali.kpme.tklm.time.service.TkServiceLocator;
028import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
029import org.kuali.rice.kew.api.exception.WorkflowException;
030import org.kuali.rice.kew.api.identity.Id;
031import org.kuali.rice.kew.api.identity.PrincipalId;
032import org.kuali.rice.kew.api.rule.RoleName;
033import org.kuali.rice.kew.engine.RouteContext;
034import org.kuali.rice.kew.routeheader.DocumentContent;
035import org.kuali.rice.kew.rule.AbstractRoleAttribute;
036import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
037import org.kuali.rice.kim.api.role.RoleMember;
038import org.kuali.rice.krad.document.Document;
039import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
040
041import java.util.ArrayList;
042import java.util.Collections;
043import java.util.List;
044
045@Deprecated
046public 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                MissedPunchBo 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, missedPunch.getActionLocalDate());
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}