View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.workflow;
17  
18  import org.joda.time.DateTime;
19  import org.kuali.kpme.core.api.assignment.Assignment;
20  import org.kuali.kpme.core.api.assignment.AssignmentDescriptionKey;
21  import org.kuali.kpme.core.api.namespace.KPMENamespace;
22  import org.kuali.kpme.core.role.KPMERole;
23  import org.kuali.kpme.core.service.HrServiceLocator;
24  import org.kuali.kpme.tklm.api.common.TkConstants;
25  import org.kuali.kpme.tklm.time.missedpunch.MissedPunchBo;
26  import org.kuali.kpme.tklm.time.missedpunch.MissedPunchDocument;
27  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
28  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
29  import org.kuali.rice.kew.api.exception.WorkflowException;
30  import org.kuali.rice.kew.api.identity.Id;
31  import org.kuali.rice.kew.api.identity.PrincipalId;
32  import org.kuali.rice.kew.api.rule.RoleName;
33  import org.kuali.rice.kew.engine.RouteContext;
34  import org.kuali.rice.kew.routeheader.DocumentContent;
35  import org.kuali.rice.kew.rule.AbstractRoleAttribute;
36  import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
37  import org.kuali.rice.kim.api.role.RoleMember;
38  import org.kuali.rice.krad.document.Document;
39  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
40  
41  import java.util.ArrayList;
42  import java.util.Collections;
43  import java.util.List;
44  
45  @Deprecated
46  public class TkWorkflowMissedPunchAttribute extends AbstractRoleAttribute {
47  
48  	private static final long serialVersionUID = 8994254411764426802L;
49  
50  	@Override
51  	public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
52  		List<String> roles = new ArrayList<String>();
53  		roles.add(roleName);
54  		return roles;
55  	}
56  
57  	@Override
58  	public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
59  		ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
60  		List<Id> principals = new ArrayList<Id>();
61  
62  		try {
63  			String documentId = routeContext.getDocument().getDocumentId();
64  			Document document = (MissedPunchDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderIdSessionless(documentId);
65  	        MissedPunchDocument missedPunchDocument = (MissedPunchDocument) document;
66  	        MissedPunchBo missedPunch = missedPunchDocument.getMissedPunch();
67  	
68  	        AssignmentDescriptionKey assignKey = AssignmentDescriptionKey.get(missedPunch.getAssignmentKey());
69  	        String tsDocIdString = missedPunch.getTimesheetDocumentId();
70  	
71  	        if (tsDocIdString != null && assignKey != null) {
72  	            TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().getTimesheetDocument(tsDocIdString);
73  	            if (tdoc != null) {
74                      Assignment assignment = tdoc.getAssignment(assignKey, missedPunch.getActionLocalDate());
75  	                if (assignment != null) {
76  	            		List<RoleMember> roleMembers = new ArrayList<RoleMember>();
77  	            		
78  	            		if (TkConstants.ROLE_TK_APPROVER.equals(roleName)) {
79  	            	        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), assignment.getWorkArea(), new DateTime(), true));
80  	            	        roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), assignment.getWorkArea(), new DateTime(), true));
81  	            		}
82  	        	
83  	        	        for (RoleMember roleMember : roleMembers) {
84  	        	        	principals.add(new PrincipalId(roleMember.getMemberId()));
85  	        		    }
86  	                } else {
87  	                    throw new RuntimeException("Could not obtain Assignment.");
88  	                }
89  	            } else {
90  	                throw new RuntimeException("Could not obtain TimesheetDocument.");
91  	            }
92  	        } else {
93  	            throw new RuntimeException("Could not obtain Timesheet Document ID or Assignment ID");
94  	        }
95  		} catch (WorkflowException we) {
96  			we.printStackTrace();
97          }
98  
99  		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 }