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