View Javadoc

1   /**
2    * Copyright 2004-2013 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.apache.log4j.Logger;
19  import org.kuali.hr.time.assignment.Assignment;
20  import org.kuali.hr.time.missedpunch.MissedPunchDocument;
21  import org.kuali.hr.time.roles.TkRole;
22  import org.kuali.hr.time.roles.service.TkRoleService;
23  import org.kuali.hr.time.service.base.TkServiceLocator;
24  import org.kuali.hr.time.timesheet.TimesheetDocument;
25  import org.kuali.hr.time.util.TKUtils;
26  import org.kuali.hr.time.util.TkConstants;
27  import org.kuali.rice.kew.api.identity.Id;
28  import org.kuali.rice.kew.api.identity.PrincipalId;
29  import org.kuali.rice.kew.api.rule.RoleName;
30  import org.kuali.rice.kew.engine.RouteContext;
31  import org.kuali.rice.kew.routeheader.DocumentContent;
32  import org.kuali.rice.kew.rule.AbstractRoleAttribute;
33  import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
34  
35  import java.util.*;
36  
37  public class TkWorkflowMissedPunchAttribute extends AbstractRoleAttribute {
38  
39      private static final Logger LOG = Logger.getLogger(TkWorkflowMissedPunchAttribute.class);
40  
41      // Root of the XPath expression needed to retrieve relevant data
42  
43      public static final String XP_BO_ROOT = "/documentContent/applicationContent/org.kuali.rice.kns.workflow.KualiDocumentXmlMaterializer/document/newMaintainableObject/businessObject";
44      // Attributes on the MissedPunch object we require to determine route recipients.
45      public static final String XP_MD_A_ASSIGN = "/assignment/text()";
46      public static final String XP_MD_A_TDOCID = "/timesheetDocumentId/text()";
47  
48  	@Override
49  	public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
50  		List<String> roles = new ArrayList<String>();
51  		roles.add(roleName);
52  		return roles;
53  	}
54  
55  	/**
56  	 * Role name is passed in in the routing rule.
57  	 */
58  	@Override
59  	public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
60  		ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
61  		List<Id> principals = new ArrayList<Id>();
62  		Long routeHeaderId = new Long(routeContext.getDocument().getDocumentId());
63  
64  		TkRoleService roleService = TkServiceLocator.getTkRoleService();
65          MissedPunchDocument missedPunch = TkServiceLocator.getMissedPunchService().getMissedPunchByRouteHeader(routeHeaderId.toString());
66  
67          String assign_string = missedPunch.getAssignment();
68          String tsDocIdString = missedPunch.getTimesheetDocumentId();
69  
70          if (tsDocIdString != null && assign_string != null) {
71              TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().getTimesheetDocument(tsDocIdString);
72              if (tdoc != null) {
73                  Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(tdoc, assign_string);
74                  if (assignment != null) {
75                      List<String> users = roleService.getResponsibleParties(assignment, roleName, tdoc.getAsOfDate());
76                     
77                      // add approver delegates to users
78                      Long workAreaNumber = assignment.getWorkArea();
79                      List<TkRole> approvers = roleService.getWorkAreaRoles(workAreaNumber, roleName, TKUtils.getCurrentDate());
80                      List<TkRole> approverDelegates = roleService.getWorkAreaRoles(workAreaNumber, TkConstants.ROLE_TK_APPROVER_DELEGATE, TKUtils.getCurrentDate());
81                      Set<TkRole> roles = new HashSet<TkRole>();
82                      roles.addAll(approvers);
83                      roles.addAll(approverDelegates);
84                      for(TkRole aRole : roles) {
85                      	users.add(aRole.getPrincipalId());
86                      }
87                      
88                      if(users.isEmpty()){
89                      	throw new RuntimeException("No responsible people for work area" + assignment.getWorkArea());
90                      }
91                      for (String user : users) {
92                      	if(user != null) {
93  	                        PrincipalId pid = new PrincipalId(user);
94  	                        if (!principals.contains(pid)) {
95  	                            principals.add(pid);
96  	                        }
97                      	}
98                      }
99                  } else {
100                     throw new RuntimeException("Could not obtain Assignment.");
101                 }
102             } else {
103                 throw new RuntimeException("Could not obtain TimesheetDocument.");
104             }
105         } else {
106             throw new RuntimeException("Could not obtain Timesheet Document ID or Assignment ID");
107         }
108 
109 
110 		if (principals.size() == 0)
111 			throw new RuntimeException("No principals to route to. Push to exception routing.");
112 
113 		rqr.setRecipients(principals);
114 		return rqr;
115 	}
116 
117 	@Override
118 	public List<RoleName> getRoleNames() {
119         return Collections.EMPTY_LIST;
120 	}
121 }