View Javadoc
1   /**
2    * Copyright 2004-2015 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.kpme.tklm.time.workflow;
17  
18  import java.util.ArrayList;
19  import java.util.HashSet;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.commons.lang.math.NumberUtils;
26  import org.apache.log4j.Logger;
27  import org.joda.time.DateTime;
28  import org.kuali.kpme.core.api.namespace.KPMENamespace;
29  import org.kuali.kpme.core.role.KPMERole;
30  import org.kuali.kpme.core.service.HrServiceLocator;
31  import org.kuali.kpme.tklm.time.missedpunch.MissedPunchBo;
32  import org.kuali.kpme.tklm.time.missedpunch.MissedPunchDocument;
33  import org.kuali.rice.kew.api.exception.WorkflowException;
34  import org.kuali.rice.kew.api.identity.Id;
35  import org.kuali.rice.kew.api.identity.PrincipalId;
36  import org.kuali.rice.kew.api.rule.RoleName;
37  import org.kuali.rice.kew.engine.RouteContext;
38  import org.kuali.rice.kew.routeheader.DocumentContent;
39  import org.kuali.rice.kew.rule.GenericRoleAttribute;
40  import org.kuali.rice.kew.rule.QualifiedRoleName;
41  import org.kuali.rice.kim.api.role.RoleMember;
42  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
43  
44  @SuppressWarnings("unchecked")
45  public class MissedPunchRoleAttribute extends GenericRoleAttribute {
46  
47  	private static final long serialVersionUID = -7188190168685823002L;
48  	
49  	private static final Logger LOG = Logger.getLogger(MissedPunchRoleAttribute.class);
50  
51  	@Override
52  	public List<RoleName> getRoleNames() {
53  		List<RoleName> roleNames = new ArrayList<RoleName>();
54  		
55  		roleNames.add(RoleName.Builder.create(getClass().getName(), KPMERole.APPROVER.getRoleName(), KPMERole.APPROVER.getRoleName()).build());
56  		roleNames.add(RoleName.Builder.create(getClass().getName(), KPMERole.APPROVER_DELEGATE.getRoleName(), KPMERole.APPROVER_DELEGATE.getRoleName()).build());
57  
58  		return roleNames;
59  	}
60  	
61  	@Override
62      protected List<String> getRoleNameQualifiers(String roleName, DocumentContent documentContent) {
63  		Set<String> roleNameQualifiers = new HashSet<String>();
64  		
65  		try {
66  			String documentId = documentContent.getRouteContext().getDocument().getDocumentId();
67  			MissedPunchDocument missedPunchDocument = (MissedPunchDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentId);
68  			MissedPunchBo missedPunch = missedPunchDocument.getMissedPunch();
69  	        roleNameQualifiers.add(String.valueOf(missedPunch.getWorkArea()));
70  		} catch (WorkflowException we) {
71  			we.printStackTrace();
72  		}
73  
74  		return new ArrayList<String>(roleNameQualifiers);
75      }
76  	
77  	@Override
78      protected List<Id> resolveRecipients(RouteContext routeContext, QualifiedRoleName qualifiedRoleName) {
79  		List<Id> recipients = new ArrayList<Id>();
80  		
81  		String roleName = qualifiedRoleName.getBaseRoleName();
82  		String qualifier = qualifiedRoleName.getQualifier();
83  		
84  		if (StringUtils.isNotBlank(roleName) && NumberUtils.isNumber(qualifier)) {
85  			Long workArea = Long.valueOf(qualifier);
86  	
87  			List<RoleMember> roleMembers = HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), roleName, workArea, new DateTime(), true);
88  			
89  	        for (RoleMember roleMember : roleMembers) {
90  	        	recipients.add(new PrincipalId(roleMember.getMemberId()));
91  		    }
92  		} else {
93  			LOG.error("Could not route to role " + roleName + " with work area " + qualifier);
94  		}
95  		
96  		return recipients;
97      }
98  
99  	@Override
100 	public Map<String, String> getProperties() {
101 		return null;
102 	}
103 
104 }