1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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.util.GlobalVariables; |
29 | |
|
30 | |
import java.util.List; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | public class EDLFunctions { |
39 | |
|
40 | |
|
41 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EDLFunctions.class); |
42 | |
|
43 | |
public static boolean isUserInitiator(String id) throws WorkflowException { |
44 | 0 | boolean initiator = false; |
45 | 0 | UserSession userSession = GlobalVariables.getUserSession(); |
46 | 0 | if (userSession != null) { |
47 | |
try { |
48 | 0 | String documentId = id.trim(); |
49 | 0 | if (userSession.getPrincipalId().equals(KewApiServiceLocator.getWorkflowDocumentService().getDocument(documentId).getInitiatorPrincipalId())) { |
50 | 0 | initiator = true; |
51 | |
} |
52 | 0 | } catch (Exception e) { |
53 | 0 | LOG.debug("Exception encountered trying to determine if user is the document initiator:" + e ); |
54 | 0 | } |
55 | |
} |
56 | 0 | return initiator; |
57 | |
} |
58 | |
|
59 | |
public static boolean isUserRouteLogAuthenticated(String documentId) { |
60 | 0 | boolean authenticated=false; |
61 | 0 | UserSession userSession=GlobalVariables.getUserSession(); |
62 | 0 | if(userSession!=null){ |
63 | 0 | String principalId = userSession.getPrincipalId(); |
64 | |
try { |
65 | 0 | authenticated = KewApiServiceLocator.getWorkflowDocumentActionsService().isUserInRouteLog(documentId, |
66 | |
principalId, true); |
67 | 0 | } catch (NumberFormatException e) { |
68 | 0 | LOG.debug("Invalid format documentId (should be LONG): " + documentId); |
69 | 0 | } catch (RiceRuntimeException e) { |
70 | 0 | LOG.error("Runtime Exception checking if user is route log authenticated: userId: " + principalId + ";documentId: " + documentId); |
71 | |
|
72 | 0 | } |
73 | |
} |
74 | |
|
75 | 0 | return authenticated; |
76 | |
} |
77 | |
|
78 | |
public static boolean isPrincipalIdAuthenticated(String principalId) { |
79 | 0 | return GlobalVariables.getUserSession().getPrincipalId().equals(principalId); |
80 | |
} |
81 | |
|
82 | |
public static boolean isPrincipalNameAuthenticated(String principalName) { |
83 | 0 | return GlobalVariables.getUserSession().getPrincipalName().equals(principalName); |
84 | |
} |
85 | |
|
86 | |
public static boolean isEmployeeIdAuthenticated(String employeeId) { |
87 | 0 | return GlobalVariables.getUserSession().getPerson().getEmployeeId().equals(employeeId); |
88 | |
} |
89 | |
|
90 | |
public static Person getAuthenticatedPerson(){ |
91 | 0 | UserSession userSession=GlobalVariables.getUserSession(); |
92 | 0 | Person user = userSession.getPerson(); |
93 | 0 | return user; |
94 | |
} |
95 | |
|
96 | |
public static String getUserId() { |
97 | 0 | return getAuthenticatedPerson().getPrincipalId(); |
98 | |
} |
99 | |
|
100 | |
public static String getLastName() { |
101 | 0 | return getAuthenticatedPerson().getLastName(); |
102 | |
} |
103 | |
|
104 | |
public static String getGivenName() { |
105 | 0 | return getAuthenticatedPerson().getFirstName(); |
106 | |
} |
107 | |
|
108 | |
public static String getEmailAddress() { |
109 | 0 | return getAuthenticatedPerson().getEmailAddress(); |
110 | |
} |
111 | |
|
112 | |
public static String getCampus() { |
113 | 0 | return getAuthenticatedPerson().getCampusCode(); |
114 | |
} |
115 | |
|
116 | |
public static String getPrimaryDeptCd() { |
117 | 0 | return getAuthenticatedPerson().getPrimaryDepartmentCode(); |
118 | |
} |
119 | |
|
120 | |
public static String getEmpTypCd() { |
121 | 0 | return getAuthenticatedPerson().getEmployeeTypeCode(); |
122 | |
} |
123 | |
|
124 | |
public static String getEmpPhoneNumber() { |
125 | 0 | return getAuthenticatedPerson().getPhoneNumber(); |
126 | |
} |
127 | |
|
128 | |
public static String getCurrentNodeName(String documentId){ |
129 | 0 | List<RouteNodeInstance> routeNodeInstances = null; |
130 | |
|
131 | 0 | routeNodeInstances = KewApiServiceLocator.getWorkflowDocumentService().getCurrentRouteNodeInstances(documentId); |
132 | 0 | for (RouteNodeInstance currentNode : routeNodeInstances) { |
133 | 0 | return currentNode.getName(); |
134 | |
} |
135 | 0 | return null; |
136 | |
} |
137 | |
|
138 | |
public static boolean isNodeInPreviousNodeList(String nodeName, String id) { |
139 | 0 | LOG.debug("nodeName came in as: " + nodeName); |
140 | 0 | LOG.debug("id came in as: " + id); |
141 | |
|
142 | |
List<String> previousNodeNames; |
143 | |
try { |
144 | 0 | previousNodeNames = KewApiServiceLocator.getWorkflowDocumentService().getPreviousRouteNodeNames(id); |
145 | 0 | } catch (Exception e) { |
146 | 0 | throw new WorkflowRuntimeException("Problem generating list of previous node names for documentID = " + id, e); |
147 | 0 | } |
148 | |
|
149 | 0 | for (String previousNodeName : previousNodeNames) { |
150 | 0 | if (previousNodeName.equals(nodeName)) { |
151 | 0 | return true; |
152 | |
} |
153 | |
} |
154 | 0 | return false; |
155 | |
} |
156 | |
|
157 | |
public static String escapeJavascript(String value) { |
158 | 0 | return value.replace("\\", "\\\\").replace("\"", "\\\""); |
159 | |
} |
160 | |
|
161 | |
public static boolean isNodeBetween(String firstNodeName, String lastNodeName, String id) { |
162 | 0 | if (isNodeInPreviousNodeList(firstNodeName, id)) { |
163 | 0 | if (isNodeInPreviousNodeList(lastNodeName, id)) { |
164 | 0 | return false; |
165 | |
}else { |
166 | 0 | return true; |
167 | |
} |
168 | |
} else { |
169 | 0 | return false; |
170 | |
} |
171 | |
} |
172 | |
|
173 | |
public static boolean isAtNode(String documentId, String nodeName) throws Exception { |
174 | 0 | List<RouteNodeInstance> activeNodeInstances = KewApiServiceLocator.getWorkflowDocumentService().getActiveRouteNodeInstances(documentId); |
175 | 0 | for (RouteNodeInstance nodeInstance : activeNodeInstances) { |
176 | 0 | if (nodeInstance.getName().equals(nodeName)) { |
177 | 0 | return true; |
178 | |
} |
179 | |
} |
180 | 0 | return false; |
181 | |
} |
182 | |
|
183 | |
public static boolean hasActiveNode(String documentId) throws Exception { |
184 | 0 | List<RouteNodeInstance> activeNodeInstances = KewApiServiceLocator.getWorkflowDocumentService().getActiveRouteNodeInstances(documentId); |
185 | 0 | if (!activeNodeInstances.isEmpty()) { |
186 | 0 | return true; |
187 | |
} |
188 | 0 | return false; |
189 | |
} |
190 | |
|
191 | |
public static String getAuthenticationId() { |
192 | 0 | UserSession userSession=GlobalVariables.getUserSession(); |
193 | 0 | return userSession.getPrincipalName(); |
194 | |
|
195 | |
} |
196 | |
|
197 | |
public static boolean isUserInGroup(String namespace, String groupName){ |
198 | 0 | boolean isUserInGroup=false; |
199 | 0 | if(!StringUtils.isEmpty(groupName)){ |
200 | 0 | String principalId = getUserId(); |
201 | |
try{ |
202 | 0 | isUserInGroup = isMemberOfGroupWithName(namespace, groupName, principalId); |
203 | 0 | }catch(Exception e){ |
204 | 0 | LOG.error("Exception encountered trying to determine if user is member of a group: userId: " + principalId + ";groupNamespace/Name: " |
205 | |
+ namespace + "/" + groupName + " resulted in error:" + e); |
206 | 0 | } |
207 | |
} |
208 | 0 | return isUserInGroup; |
209 | |
} |
210 | |
|
211 | |
private static boolean isMemberOfGroupWithName(String namespace, String groupName, String principalId) { |
212 | 0 | for (Group group : KimApiServiceLocator.getGroupService().getGroupsByPrincipalId(principalId)) { |
213 | 0 | if (StringUtils.equals(namespace, group.getNamespaceCode()) && StringUtils.equals(groupName, group.getName())) { |
214 | 0 | return true; |
215 | |
} |
216 | |
} |
217 | 0 | return false; |
218 | |
} |
219 | |
} |