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