View Javadoc

1   /**
2    * Copyright 2005-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.rice.edl.framework.util;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.exception.RiceRuntimeException;
20  import org.kuali.rice.kew.api.KewApiServiceLocator;
21  import org.kuali.rice.kew.api.WorkflowRuntimeException;
22  import org.kuali.rice.kew.api.document.node.RouteNodeInstance;
23  import org.kuali.rice.kew.api.exception.WorkflowException;
24  import org.kuali.rice.kim.api.group.Group;
25  import org.kuali.rice.kim.api.identity.Person;
26  import org.kuali.rice.krad.UserSession;
27  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
28  import org.kuali.rice.krad.document.authorization.PessimisticLock;
29  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
30  import org.kuali.rice.krad.service.PessimisticLockService;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  
33  import java.util.List;
34  
35  /**
36   * A collection of handy workflow queries to be used from style sheets.
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   *
40   */
41  public class EDLFunctions {
42  
43  
44      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EDLFunctions.class);
45  
46      public static boolean isUserInitiator(String id) throws WorkflowException {
47      	boolean initiator = false;
48      	UserSession userSession = GlobalVariables.getUserSession();
49      	if (userSession != null) {
50      		try {
51      			String documentId = id.trim();
52      			if (userSession.getPrincipalId().equals(KewApiServiceLocator.getWorkflowDocumentService().getDocument(documentId).getInitiatorPrincipalId())) {
53      				initiator = true;
54      			}
55      		} catch (Exception e) {
56      			LOG.debug("Exception encountered trying to determine if user is the document initiator:" + e );
57      		}
58      	}
59      	return initiator;
60      }
61  
62  	public static boolean isUserRouteLogAuthenticated(String documentId) {
63  		boolean authenticated=false;
64  		UserSession userSession=GlobalVariables.getUserSession();
65  		if(userSession!=null){
66  			String principalId = userSession.getPrincipalId();
67  			try {
68  				authenticated = KewApiServiceLocator.getWorkflowDocumentActionsService().isUserInRouteLog(documentId,
69                          principalId, true);
70  			} catch (NumberFormatException e) {
71  				LOG.debug("Invalid format documentId (should be LONG): " + documentId);
72  		    } catch (RiceRuntimeException e) {
73  		    	LOG.error("Runtime Exception checking if user is route log authenticated: userId: " + principalId + ";documentId: " + documentId);
74  
75  		    }
76  		}
77  
78  	    return authenticated;
79  	}
80  
81  	public static boolean isPrincipalIdAuthenticated(String principalId) {
82  		return GlobalVariables.getUserSession().getPrincipalId().equals(principalId);
83  	}
84  	
85  	public static boolean isPrincipalNameAuthenticated(String principalName) {
86  		return GlobalVariables.getUserSession().getPrincipalName().equals(principalName);
87  	}
88  	
89  	public static boolean isEmployeeIdAuthenticated(String employeeId) {
90  		return GlobalVariables.getUserSession().getPerson().getEmployeeId().equals(employeeId);
91  	}
92  
93  	public static Person getAuthenticatedPerson(){
94  		UserSession userSession=GlobalVariables.getUserSession();
95  		Person user = userSession.getPerson();
96  		return user;
97  	}
98  
99  	public static String getUserId() {
100 	        return getAuthenticatedPerson().getPrincipalId();
101 	}
102 
103 	public static String getLastName() {
104 	        return getAuthenticatedPerson().getLastName();
105 	}
106 
107 	public static String getGivenName() {
108 	    return getAuthenticatedPerson().getFirstName();
109 	}
110 
111 	public static String getEmailAddress() {
112 	    return getAuthenticatedPerson().getEmailAddress();
113 	}
114 	
115     public static String getCampus() {
116         return getAuthenticatedPerson().getCampusCode();
117     }
118     
119     public static String getPrimaryDeptCd() {
120         return getAuthenticatedPerson().getPrimaryDepartmentCode();
121     }
122     
123     public static String getEmpTypCd() {
124         return getAuthenticatedPerson().getEmployeeTypeCode();
125     }
126     
127     public static String getEmpPhoneNumber() {
128         return getAuthenticatedPerson().getPhoneNumber();
129     }
130     
131     public static String getCurrentNodeName(String documentId){
132         List<RouteNodeInstance> routeNodeInstances = null;
133 
134         routeNodeInstances = KewApiServiceLocator.getWorkflowDocumentService().getCurrentRouteNodeInstances(documentId);
135         for (RouteNodeInstance currentNode : routeNodeInstances) {
136             return currentNode.getName();
137         }   
138         return null;
139     }
140     
141 	public static boolean isNodeInPreviousNodeList(String nodeName, String id) {
142 		LOG.debug("nodeName came in as: " + nodeName);
143 		LOG.debug("id came in as: " + id);
144 		//get list of previous node names
145 		List<String> previousNodeNames;
146 		try {
147 			previousNodeNames = KewApiServiceLocator.getWorkflowDocumentService().getPreviousRouteNodeNames(id);
148 		} catch (Exception e) {
149 			throw new WorkflowRuntimeException("Problem generating list of previous node names for documentID = " + id, e);
150 		}
151 		//see if node name is in the list of previous node names
152 		for (String previousNodeName : previousNodeNames) {
153 			if (previousNodeName.equals(nodeName)) {
154 				return true;
155 			}
156 		}
157 		return false;
158 	}
159 
160 	public static String escapeJavascript(String value) {
161 		return value.replace("\\", "\\\\").replace("\"", "\\\"");
162 	}
163 
164 	public static boolean isNodeBetween(String firstNodeName, String lastNodeName, String id) {
165 		if (isNodeInPreviousNodeList(firstNodeName, id)) {
166 			if (isNodeInPreviousNodeList(lastNodeName, id)) {
167 				return false;
168 			}else {
169 				return true;
170 			}
171 		} else {
172 			return false;
173 		}
174 	}
175 
176 	public static boolean isAtNode(String documentId, String nodeName) throws Exception {
177 	    List<RouteNodeInstance> activeNodeInstances = KewApiServiceLocator.getWorkflowDocumentService().getActiveRouteNodeInstances(documentId);
178 	    for (RouteNodeInstance nodeInstance : activeNodeInstances) {
179 	        if (nodeInstance.getName().equals(nodeName)) {
180 	            return true;
181 	        }
182 	    }
183 	    return false;
184 	}
185 
186 	public static boolean hasActiveNode(String documentId) throws Exception {
187 	    List<RouteNodeInstance> activeNodeInstances = KewApiServiceLocator.getWorkflowDocumentService().getActiveRouteNodeInstances(documentId);
188 	    if (!activeNodeInstances.isEmpty()) {
189             return true;
190 	    }
191 	    return false;
192 	}
193 
194 	public static String getAuthenticationId() {
195 	    UserSession userSession=GlobalVariables.getUserSession();
196 	    return userSession.getPrincipalName();
197 	
198 	}
199     
200 	public static boolean isUserInGroup(String namespace, String groupName){
201 		boolean isUserInGroup=false;
202 		if(!StringUtils.isEmpty(groupName)){
203 			String principalId = getUserId();
204 			try{
205 				isUserInGroup = isMemberOfGroupWithName(namespace, groupName, principalId);
206 			}catch(Exception e){
207 	    		LOG.error("Exception encountered trying to determine if user is member of a group: userId: " + principalId + ";groupNamespace/Name: " 
208 	    				+ namespace + "/" + groupName + " resulted in error:" + e);
209 			}
210 		}
211 		return isUserInGroup;
212 	}
213 	
214     private static boolean isMemberOfGroupWithName(String namespace, String groupName, String principalId) {
215         for (Group group : KimApiServiceLocator.getGroupService().getGroupsByPrincipalId(principalId)) {
216             if (StringUtils.equals(namespace, group.getNamespaceCode()) && StringUtils.equals(groupName, group.getName())) {
217                 return true;
218             }
219         }
220         return false;
221     }
222 
223     public static String createDocumentLock(String documentId) {
224         PessimisticLockService lockService = KRADServiceLocatorWeb.getPessimisticLockService();
225         PessimisticLock lock = lockService.generateNewLock(documentId);
226         Long lockLong = lock.getId();
227 
228         return lockLong.toString();
229     }
230 
231     public static void removeDocumentLocksByUser(String documentId) {
232         try {
233             PessimisticLockService lockService = KRADServiceLocatorWeb.getPessimisticLockService();
234             List<PessimisticLock> pessimisticLocks =  lockService.getPessimisticLocksForDocument(documentId);
235             lockService.releaseAllLocksForUser(pessimisticLocks, getAuthenticatedPerson());
236         } catch (Exception e) {
237             LOG.error("Exception encountered trying to delete document locks:" + e );
238         }
239     }
240 
241     public static Boolean isDocumentLocked(String documentId) {
242         List<PessimisticLock> pessimisticLocks = KRADServiceLocatorWeb.getPessimisticLockService().getPessimisticLocksForDocument(documentId);
243         if (pessimisticLocks.isEmpty()) {
244             return false;
245         } else {
246             return true;
247         }
248     }
249 
250     public static String getDocumentLockOwner(String documentId) {
251         List<PessimisticLock> pessimisticLocks = KRADServiceLocatorWeb.getPessimisticLockService().getPessimisticLocksForDocument(documentId);
252         if (pessimisticLocks.isEmpty()) {
253             return "NoLockOnDoc";
254         } else {
255             if (pessimisticLocks.size() == (1)) {
256                 PessimisticLock lock = pessimisticLocks.get(0);
257                 return lock.getOwnedByUser().getPrincipalName();
258             } else {
259                 return "MoreThanOneLockOnDoc";
260             }
261         }
262     }
263 }