001 /**
002 * Copyright 2005-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.edl.framework.util;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.kuali.rice.core.api.exception.RiceRuntimeException;
020 import org.kuali.rice.kew.api.KewApiServiceLocator;
021 import org.kuali.rice.kew.api.WorkflowRuntimeException;
022 import org.kuali.rice.kew.api.document.node.RouteNodeInstance;
023 import org.kuali.rice.kew.api.exception.WorkflowException;
024 import org.kuali.rice.kim.api.group.Group;
025 import org.kuali.rice.kim.api.identity.Person;
026 import org.kuali.rice.krad.UserSession;
027 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
028 import org.kuali.rice.krad.document.authorization.PessimisticLock;
029 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
030 import org.kuali.rice.krad.service.PessimisticLockService;
031 import org.kuali.rice.krad.util.GlobalVariables;
032
033 import java.util.List;
034
035 /**
036 * A collection of handy workflow queries to be used from style sheets.
037 *
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 *
040 */
041 public class EDLFunctions {
042
043
044 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EDLFunctions.class);
045
046 public static boolean isUserInitiator(String id) throws WorkflowException {
047 boolean initiator = false;
048 UserSession userSession = GlobalVariables.getUserSession();
049 if (userSession != null) {
050 try {
051 String documentId = id.trim();
052 if (userSession.getPrincipalId().equals(KewApiServiceLocator.getWorkflowDocumentService().getDocument(documentId).getInitiatorPrincipalId())) {
053 initiator = true;
054 }
055 } catch (Exception e) {
056 LOG.debug("Exception encountered trying to determine if user is the document initiator:" + e );
057 }
058 }
059 return initiator;
060 }
061
062 public static boolean isUserRouteLogAuthenticated(String documentId) {
063 boolean authenticated=false;
064 UserSession userSession=GlobalVariables.getUserSession();
065 if(userSession!=null){
066 String principalId = userSession.getPrincipalId();
067 try {
068 authenticated = KewApiServiceLocator.getWorkflowDocumentActionsService().isUserInRouteLog(documentId,
069 principalId, true);
070 } catch (NumberFormatException e) {
071 LOG.debug("Invalid format documentId (should be LONG): " + documentId);
072 } catch (RiceRuntimeException e) {
073 LOG.error("Runtime Exception checking if user is route log authenticated: userId: " + principalId + ";documentId: " + documentId);
074
075 }
076 }
077
078 return authenticated;
079 }
080
081 public static boolean isPrincipalIdAuthenticated(String principalId) {
082 return GlobalVariables.getUserSession().getPrincipalId().equals(principalId);
083 }
084
085 public static boolean isPrincipalNameAuthenticated(String principalName) {
086 return GlobalVariables.getUserSession().getPrincipalName().equals(principalName);
087 }
088
089 public static boolean isEmployeeIdAuthenticated(String employeeId) {
090 return GlobalVariables.getUserSession().getPerson().getEmployeeId().equals(employeeId);
091 }
092
093 public static Person getAuthenticatedPerson(){
094 UserSession userSession=GlobalVariables.getUserSession();
095 Person user = userSession.getPerson();
096 return user;
097 }
098
099 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 }