| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kns.workflow.service.impl; |
| 17 | |
|
| 18 | |
import java.text.MessageFormat; |
| 19 | |
import java.util.ArrayList; |
| 20 | |
import java.util.List; |
| 21 | |
|
| 22 | |
import org.apache.commons.lang.StringUtils; |
| 23 | |
import org.apache.commons.lang.time.StopWatch; |
| 24 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
| 25 | |
import org.kuali.rice.core.util.RiceKeyConstants; |
| 26 | |
import org.kuali.rice.kew.dto.RouteNodeInstanceDTO; |
| 27 | |
import org.kuali.rice.kew.exception.DocumentTypeNotFoundException; |
| 28 | |
import org.kuali.rice.kew.exception.InvalidActionTakenException; |
| 29 | |
import org.kuali.rice.kew.exception.WorkflowException; |
| 30 | |
import org.kuali.rice.kew.service.WorkflowInfo; |
| 31 | |
import org.kuali.rice.kew.util.KEWConstants; |
| 32 | |
import org.kuali.rice.kim.bo.Group; |
| 33 | |
import org.kuali.rice.kim.bo.Person; |
| 34 | |
import org.kuali.rice.kim.bo.entity.KimPrincipal; |
| 35 | |
import org.kuali.rice.kim.service.KIMServiceLocator; |
| 36 | |
import org.kuali.rice.kns.bo.AdHocRouteRecipient; |
| 37 | |
import org.kuali.rice.kns.exception.UnknownDocumentIdException; |
| 38 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
| 39 | |
import org.kuali.rice.kns.util.GlobalVariables; |
| 40 | |
import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument; |
| 41 | |
import org.kuali.rice.kns.workflow.service.KualiWorkflowInfo; |
| 42 | |
import org.kuali.rice.kns.workflow.service.WorkflowDocumentService; |
| 43 | |
import org.springframework.transaction.annotation.Transactional; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
@Transactional |
| 50 | 0 | public class WorkflowDocumentServiceImpl implements WorkflowDocumentService { |
| 51 | 0 | private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(WorkflowDocumentServiceImpl.class); |
| 52 | |
|
| 53 | |
private KualiWorkflowInfo workflowInfoService; |
| 54 | |
|
| 55 | |
public boolean workflowDocumentExists(String documentHeaderId) { |
| 56 | 0 | boolean exists = false; |
| 57 | |
|
| 58 | 0 | if (StringUtils.isBlank(documentHeaderId)) { |
| 59 | 0 | throw new IllegalArgumentException("invalid (blank) documentHeaderId"); |
| 60 | |
} |
| 61 | |
|
| 62 | 0 | Long routeHeaderId = null; |
| 63 | |
try { |
| 64 | 0 | routeHeaderId = new Long(documentHeaderId); |
| 65 | |
} |
| 66 | 0 | catch (NumberFormatException e) { |
| 67 | 0 | throw new IllegalArgumentException("cannot convert '" + documentHeaderId + "' into a Long"); |
| 68 | 0 | } |
| 69 | |
|
| 70 | 0 | exists = workflowInfoService.routeHeaderExists(routeHeaderId); |
| 71 | |
|
| 72 | 0 | return exists; |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public KualiWorkflowDocument createWorkflowDocument(String documentTypeId, Person person) throws WorkflowException { |
| 80 | 0 | String watchName = "createWorkflowDocument"; |
| 81 | 0 | StopWatch watch = new StopWatch(); |
| 82 | 0 | watch.start(); |
| 83 | 0 | if (LOG.isDebugEnabled()) { |
| 84 | 0 | LOG.debug(watchName + ": started"); |
| 85 | |
} |
| 86 | 0 | if (StringUtils.isBlank(documentTypeId)) { |
| 87 | 0 | throw new IllegalArgumentException("invalid (blank) documentTypeId"); |
| 88 | |
} |
| 89 | 0 | if (person == null) { |
| 90 | 0 | throw new IllegalArgumentException("invalid (null) person"); |
| 91 | |
} |
| 92 | |
|
| 93 | 0 | if (StringUtils.isBlank(person.getPrincipalName())) { |
| 94 | 0 | throw new IllegalArgumentException("invalid (empty) PrincipalName"); |
| 95 | |
} |
| 96 | |
|
| 97 | 0 | if (LOG.isDebugEnabled()) { |
| 98 | 0 | LOG.debug("creating workflowDoc(" + documentTypeId + "," + person.getPrincipalName() + ")"); |
| 99 | |
} |
| 100 | |
|
| 101 | 0 | KualiWorkflowDocument document = new KualiWorkflowDocumentImpl(person.getPrincipalId(), documentTypeId); |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
try { |
| 109 | 0 | document.getRouteHeaderId(); |
| 110 | |
} |
| 111 | 0 | catch (WorkflowException e) { |
| 112 | 0 | if (e.getMessage().contains("Could not locate the given document type name")) { |
| 113 | 0 | throw new DocumentTypeNotFoundException("unknown document type '" + documentTypeId + "'"); |
| 114 | |
} |
| 115 | 0 | throw e; |
| 116 | 0 | } |
| 117 | |
|
| 118 | 0 | watch.stop(); |
| 119 | 0 | if (LOG.isDebugEnabled()) { |
| 120 | 0 | LOG.debug(watchName + ": " + watch.toString()); |
| 121 | |
} |
| 122 | |
|
| 123 | 0 | return document; |
| 124 | |
} |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
public KualiWorkflowDocument createWorkflowDocument(Long documentHeaderId, Person user) throws WorkflowException { |
| 131 | 0 | if (documentHeaderId == null) { |
| 132 | 0 | throw new IllegalArgumentException("invalid (null) documentHeaderId"); |
| 133 | |
} |
| 134 | 0 | if (user == null) { |
| 135 | 0 | throw new IllegalArgumentException("invalid (null) workflowUser"); |
| 136 | |
} |
| 137 | 0 | else if (StringUtils.isEmpty(user.getPrincipalName())) { |
| 138 | 0 | throw new IllegalArgumentException("invalid (empty) workflowUser"); |
| 139 | |
} |
| 140 | |
|
| 141 | 0 | if (LOG.isDebugEnabled()) { |
| 142 | 0 | LOG.debug("retrieving document(" + documentHeaderId + "," + user.getPrincipalName() + ")"); |
| 143 | |
} |
| 144 | |
|
| 145 | 0 | KualiWorkflowDocument document = new KualiWorkflowDocumentImpl(user.getPrincipalId(), documentHeaderId); |
| 146 | 0 | if (document.getRouteHeader() == null) { |
| 147 | 0 | throw new UnknownDocumentIdException("unable to locate document with documentHeaderId '" + documentHeaderId + "'"); |
| 148 | |
} |
| 149 | 0 | return document; |
| 150 | |
} |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public void acknowledge(KualiWorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException { |
| 156 | 0 | if (LOG.isDebugEnabled()) { |
| 157 | 0 | LOG.debug("acknowleding document(" + workflowDocument.getRouteHeaderId() + ",'" + annotation + "')"); |
| 158 | |
} |
| 159 | |
|
| 160 | 0 | handleAdHocRouteRequests(workflowDocument, annotation, filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ })); |
| 161 | 0 | workflowDocument.acknowledge(annotation); |
| 162 | 0 | } |
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
public void approve(KualiWorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException { |
| 168 | 0 | if (LOG.isDebugEnabled()) { |
| 169 | 0 | LOG.debug("approving document(" + workflowDocument.getRouteHeaderId() + ",'" + annotation + "')"); |
| 170 | |
} |
| 171 | |
|
| 172 | 0 | handleAdHocRouteRequests(workflowDocument, annotation, filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ, KEWConstants.ACTION_REQUEST_APPROVE_REQ })); |
| 173 | 0 | workflowDocument.approve(annotation); |
| 174 | 0 | } |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
public void superUserApprove(KualiWorkflowDocument workflowDocument, String annotation) throws WorkflowException { |
| 182 | 0 | if ( LOG.isInfoEnabled() ) { |
| 183 | 0 | LOG.info("super user approve document(" + workflowDocument.getRouteHeaderId() + ",'" + annotation + "')"); |
| 184 | |
} |
| 185 | 0 | workflowDocument.superUserApprove(annotation); |
| 186 | 0 | } |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
public void superUserCancel(KualiWorkflowDocument workflowDocument, String annotation) throws WorkflowException { |
| 193 | 0 | LOG.info("super user cancel document(" + workflowDocument.getRouteHeaderId() + ",'" + annotation + "')"); |
| 194 | 0 | workflowDocument.superUserCancel(annotation); |
| 195 | 0 | } |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
public void superUserDisapprove(KualiWorkflowDocument workflowDocument, String annotation) throws WorkflowException { |
| 202 | 0 | if ( LOG.isInfoEnabled() ) { |
| 203 | 0 | LOG.info("super user disapprove document(" + workflowDocument.getRouteHeaderId() + ",'" + annotation + "')"); |
| 204 | |
} |
| 205 | 0 | workflowDocument.superUserDisapprove(annotation); |
| 206 | 0 | } |
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
public void blanketApprove(KualiWorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException { |
| 212 | 0 | if (LOG.isDebugEnabled()) { |
| 213 | 0 | LOG.debug("blanket approving document(" + workflowDocument.getRouteHeaderId() + ",'" + annotation + "')"); |
| 214 | |
} |
| 215 | |
|
| 216 | 0 | handleAdHocRouteRequests(workflowDocument, annotation, filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ })); |
| 217 | 0 | workflowDocument.blanketApprove(annotation); |
| 218 | 0 | } |
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
public void cancel(KualiWorkflowDocument workflowDocument, String annotation) throws WorkflowException { |
| 224 | 0 | if (LOG.isDebugEnabled()) { |
| 225 | 0 | LOG.debug("canceling document(" + workflowDocument.getRouteHeaderId() + ",'" + annotation + "')"); |
| 226 | |
} |
| 227 | |
|
| 228 | 0 | workflowDocument.cancel(annotation); |
| 229 | 0 | } |
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
public void clearFyi(KualiWorkflowDocument workflowDocument, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException { |
| 235 | 0 | if (LOG.isDebugEnabled()) { |
| 236 | 0 | LOG.debug("clearing FYI for document(" + workflowDocument.getRouteHeaderId() + ")"); |
| 237 | |
} |
| 238 | |
|
| 239 | 0 | handleAdHocRouteRequests(workflowDocument, "", filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_FYI_REQ })); |
| 240 | 0 | workflowDocument.fyi(); |
| 241 | 0 | } |
| 242 | |
|
| 243 | |
public void sendWorkflowNotification(KualiWorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException { |
| 244 | 0 | sendWorkflowNotification(workflowDocument, annotation, adHocRecipients, null); |
| 245 | 0 | } |
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
public void sendWorkflowNotification(KualiWorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients, String notificationLabel) throws WorkflowException { |
| 251 | 0 | if (LOG.isDebugEnabled()) { |
| 252 | 0 | LOG.debug("sending FYI for document(" + workflowDocument.getRouteHeaderId() + ")"); |
| 253 | |
} |
| 254 | |
|
| 255 | 0 | handleAdHocRouteRequests(workflowDocument, annotation, adHocRecipients, notificationLabel); |
| 256 | 0 | } |
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
public void disapprove(KualiWorkflowDocument workflowDocument, String annotation) throws WorkflowException { |
| 262 | 0 | if (LOG.isDebugEnabled()) { |
| 263 | 0 | LOG.debug("disapproving document(" + workflowDocument.getRouteHeaderId() + ",'" + annotation + "')"); |
| 264 | |
} |
| 265 | |
|
| 266 | 0 | workflowDocument.disapprove(annotation); |
| 267 | 0 | } |
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
public void route(KualiWorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException { |
| 273 | 0 | if (LOG.isDebugEnabled()) { |
| 274 | 0 | LOG.debug("routing document(" + workflowDocument.getRouteHeaderId() + ",'" + annotation + "')"); |
| 275 | |
} |
| 276 | |
|
| 277 | 0 | handleAdHocRouteRequests(workflowDocument, annotation, filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ, KEWConstants.ACTION_REQUEST_APPROVE_REQ })); |
| 278 | 0 | workflowDocument.routeDocument(annotation); |
| 279 | 0 | } |
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
public void save(KualiWorkflowDocument workflowDocument, String annotation) throws WorkflowException { |
| 285 | 0 | if (workflowDocument.isStandardSaveAllowed()) { |
| 286 | 0 | if (LOG.isDebugEnabled()) { |
| 287 | 0 | LOG.debug("saving document(" + workflowDocument.getRouteHeaderId() + ",'" + annotation + "')"); |
| 288 | |
} |
| 289 | |
|
| 290 | 0 | workflowDocument.saveDocument(annotation); |
| 291 | |
} |
| 292 | |
else { |
| 293 | 0 | this.saveRoutingData(workflowDocument); |
| 294 | |
} |
| 295 | 0 | } |
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
public void saveRoutingData(KualiWorkflowDocument workflowDocument) throws WorkflowException { |
| 301 | 0 | if (LOG.isDebugEnabled()) { |
| 302 | 0 | LOG.debug("saving document(" + workflowDocument.getRouteHeaderId() + ")"); |
| 303 | |
} |
| 304 | |
|
| 305 | 0 | workflowDocument.saveRoutingData(); |
| 306 | 0 | } |
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
public String getCurrentRouteLevelName(KualiWorkflowDocument workflowDocument) throws WorkflowException { |
| 312 | 0 | if (LOG.isDebugEnabled()) { |
| 313 | 0 | LOG.debug("getting current route level name for document(" + workflowDocument.getRouteHeaderId()); |
| 314 | |
} |
| 315 | |
|
| 316 | 0 | KualiWorkflowDocument freshCopyWorkflowDoc = createWorkflowDocument(workflowDocument.getRouteHeaderId(), GlobalVariables.getUserSession().getPerson()); |
| 317 | 0 | return freshCopyWorkflowDoc.getCurrentRouteNodeNames(); |
| 318 | |
} |
| 319 | |
|
| 320 | |
private void handleAdHocRouteRequests(KualiWorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException { |
| 321 | 0 | handleAdHocRouteRequests(workflowDocument, annotation, adHocRecipients, null); |
| 322 | 0 | } |
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
private void handleAdHocRouteRequests(KualiWorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients, String notificationLabel) throws WorkflowException { |
| 335 | |
|
| 336 | 0 | if (adHocRecipients != null && adHocRecipients.size() > 0) { |
| 337 | 0 | String currentNode = null; |
| 338 | 0 | String[] currentNodes = workflowDocument.getNodeNames(); |
| 339 | 0 | if (currentNodes.length == 0) { |
| 340 | 0 | WorkflowInfo workflowInfo = new WorkflowInfo(); |
| 341 | 0 | RouteNodeInstanceDTO[] nodes = workflowInfo.getTerminalNodeInstances(workflowDocument.getRouteHeaderId()); |
| 342 | 0 | currentNodes = new String[nodes.length]; |
| 343 | 0 | for (int nodeIndex = 0; nodeIndex < nodes.length; nodeIndex++) { |
| 344 | 0 | currentNodes[nodeIndex] = nodes[nodeIndex].getName(); |
| 345 | |
} |
| 346 | |
} |
| 347 | |
|
| 348 | 0 | for (int i = 0; i < currentNodes.length; i++) { |
| 349 | 0 | currentNode = currentNodes[i]; |
| 350 | |
} |
| 351 | |
|
| 352 | |
|
| 353 | 0 | for (AdHocRouteRecipient recipient : adHocRecipients) { |
| 354 | 0 | if (StringUtils.isNotEmpty(recipient.getId())) { |
| 355 | 0 | String newAnnotation = annotation; |
| 356 | 0 | if ( StringUtils.isBlank( annotation ) ) { |
| 357 | |
try { |
| 358 | 0 | String message = KNSServiceLocator.getKualiConfigurationService().getPropertyString(RiceKeyConstants.MESSAGE_ADHOC_ANNOTATION); |
| 359 | 0 | newAnnotation = MessageFormat.format(message, GlobalVariables.getUserSession().getPrincipalName() ); |
| 360 | 0 | } catch ( Exception ex ) { |
| 361 | 0 | LOG.warn("Unable to set annotation", ex ); |
| 362 | 0 | } |
| 363 | |
} |
| 364 | 0 | if (AdHocRouteRecipient.PERSON_TYPE.equals(recipient.getType())) { |
| 365 | |
|
| 366 | 0 | KimPrincipal principal = KIMServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(recipient.getId()); |
| 367 | 0 | if (principal == null) { |
| 368 | 0 | throw new RiceRuntimeException("Could not locate principal with name '" + recipient.getId() + "'"); |
| 369 | |
} |
| 370 | 0 | workflowDocument.adHocRouteDocumentToPrincipal(recipient.getActionRequested(), currentNode, newAnnotation, principal.getPrincipalId(), "", true, notificationLabel); |
| 371 | 0 | } |
| 372 | |
else { |
| 373 | 0 | Group group = KIMServiceLocator.getIdentityManagementService().getGroup(recipient.getId()); |
| 374 | 0 | if (group == null) { |
| 375 | 0 | throw new RiceRuntimeException("Could not locate group with id '" + recipient.getId() + "'"); |
| 376 | |
} |
| 377 | 0 | workflowDocument.adHocRouteDocumentToGroup(recipient.getActionRequested(), currentNode, newAnnotation, group.getGroupId() , "", true, notificationLabel); |
| 378 | |
} |
| 379 | 0 | } |
| 380 | |
} |
| 381 | |
} |
| 382 | 0 | } |
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
private List<AdHocRouteRecipient> filterAdHocRecipients(List<AdHocRouteRecipient> adHocRecipients, String[] validTypes) { |
| 391 | |
|
| 392 | 0 | List<AdHocRouteRecipient> realAdHocRecipients = new ArrayList<AdHocRouteRecipient>(); |
| 393 | 0 | if (adHocRecipients != null) { |
| 394 | 0 | for (AdHocRouteRecipient proposedRecipient : adHocRecipients) { |
| 395 | 0 | if (StringUtils.isNotBlank(proposedRecipient.getActionRequested())) { |
| 396 | 0 | for (int i = 0; i < validTypes.length; i++) { |
| 397 | 0 | if (validTypes[i].equals(proposedRecipient.getActionRequested())) { |
| 398 | 0 | realAdHocRecipients.add(proposedRecipient); |
| 399 | |
} |
| 400 | |
} |
| 401 | |
} |
| 402 | |
} |
| 403 | |
} |
| 404 | 0 | return realAdHocRecipients; |
| 405 | |
} |
| 406 | |
|
| 407 | |
|
| 408 | |
public void setWorkflowInfoService(KualiWorkflowInfo workflowInfoService) { |
| 409 | 0 | this.workflowInfoService = workflowInfoService; |
| 410 | 0 | } |
| 411 | |
|
| 412 | |
public KualiWorkflowInfo getWorkflowInfoService() { |
| 413 | 0 | return workflowInfoService; |
| 414 | |
} |
| 415 | |
} |