1 25 package org.ofbiz.workflow.client; 26 27 import java.sql.Timestamp ; 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.HashMap ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import org.ofbiz.base.util.Debug; 35 import org.ofbiz.base.util.UtilDateTime; 36 import org.ofbiz.base.util.UtilMisc; 37 import org.ofbiz.entity.GenericDelegator; 38 import org.ofbiz.entity.GenericEntityException; 39 import org.ofbiz.entity.GenericValue; 40 import org.ofbiz.entity.condition.EntityExpr; 41 import org.ofbiz.entity.condition.EntityOperator; 42 import org.ofbiz.security.Security; 43 import org.ofbiz.service.DispatchContext; 44 import org.ofbiz.service.GenericServiceException; 45 import org.ofbiz.service.LocalDispatcher; 46 import org.ofbiz.service.ModelService; 47 import org.ofbiz.workflow.WfException; 48 import org.ofbiz.workflow.WfFactory; 49 import org.ofbiz.workflow.WfProcess; 50 51 58 public class WorkflowServices { 59 60 public static final String module = WorkflowServices.class.getName(); 61 62 66 67 public static Map cancelWorkflow(DispatchContext ctx, Map context) { 68 Map result = new HashMap (); 69 GenericDelegator delegator = ctx.getDelegator(); 70 Security security = ctx.getSecurity(); 71 String workEffortId = (String ) context.get("workEffortId"); 72 73 try { 75 GenericValue testObject = delegator.findByPrimaryKey("WorkEffort", UtilMisc.toMap("workEffortId", workEffortId)); 76 if (testObject == null) { 77 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 78 result.put(ModelService.ERROR_MESSAGE, "Not a valid workflow runtime identifier"); 79 return result; 80 } else if (testObject.get("workEffortTypeId") != null && testObject.getString("workEffortTypeId").equals("WORK_FLOW")) { 81 } else if (testObject.get("workEffortTypeId") != null && testObject.getString("workEffortTypeId").equals("ACTIVITY")) { 83 workEffortId = testObject.getString("workEffortParentId"); 85 } else { 86 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 87 result.put(ModelService.ERROR_MESSAGE, "Not a valid workflow runtime identifier"); 88 return result; 89 } 90 } catch (GenericEntityException e) { 91 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 92 result.put(ModelService.ERROR_MESSAGE, "Problems looking up runtime object; invalid id"); 93 return result; 94 } 95 96 GenericValue userLogin = (GenericValue) context.get("userLogin"); 97 98 if (!hasPermission(security, workEffortId, userLogin)) { 99 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 100 result.put(ModelService.ERROR_MESSAGE, "You do not have permission to access this workflow"); 101 return result; 102 } 103 try { 104 WfProcess process = WfFactory.getWfProcess(delegator, workEffortId); 105 process.abort(); 106 } catch (WfException we) { 107 we.printStackTrace(); 108 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 109 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 110 } 111 return result; 112 } 113 114 115 public static Map suspendActivity(DispatchContext ctx, Map context) { 116 Map result = new HashMap (); 117 GenericDelegator delegator = ctx.getDelegator(); 118 Security security = ctx.getSecurity(); 119 String workEffortId = (String ) context.get("workEffortId"); 120 121 GenericValue userLogin = (GenericValue) context.get("userLogin"); 122 123 if (!hasPermission(security, workEffortId, userLogin)) { 124 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 125 result.put(ModelService.ERROR_MESSAGE, "You do not have permission to access this activity"); 126 return result; 127 } 128 try { 129 WorkflowClient client = WfFactory.getClient(ctx); 130 client.suspend(workEffortId); 131 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 132 } catch (WfException we) { 133 we.printStackTrace(); 134 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 135 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 136 } 137 return result; 138 } 139 140 141 public static Map resumeActivity(DispatchContext ctx, Map context) { 142 Map result = new HashMap (); 143 GenericDelegator delegator = ctx.getDelegator(); 144 Security security = ctx.getSecurity(); 145 String workEffortId = (String ) context.get("workEffortId"); 146 147 GenericValue userLogin = (GenericValue) context.get("userLogin"); 148 149 if (!hasPermission(security, workEffortId, userLogin)) { 150 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 151 result.put(ModelService.ERROR_MESSAGE, "You do not have permission to access this activity"); 152 return result; 153 } 154 try { 155 WorkflowClient client = WfFactory.getClient(ctx); 156 client.resume(workEffortId); 157 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 158 } catch (WfException we) { 159 we.printStackTrace(); 160 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 161 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 162 } 163 return result; 164 } 165 166 167 public static Map changeActivityState(DispatchContext ctx, Map context) { 168 Map result = new HashMap (); 169 GenericDelegator delegator = ctx.getDelegator(); 170 Security security = ctx.getSecurity(); 171 String workEffortId = (String ) context.get("workEffortId"); 172 String newState = (String ) context.get("newState"); 173 174 GenericValue userLogin = (GenericValue) context.get("userLogin"); 175 176 if (!hasPermission(security, workEffortId, userLogin)) { 177 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 178 result.put(ModelService.ERROR_MESSAGE, "You do not have permission to access this activity"); 179 return result; 180 } 181 try { 182 WorkflowClient client = WfFactory.getClient(ctx); 183 client.setState(workEffortId, newState); 184 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 185 } catch (WfException we) { 186 we.printStackTrace(); 187 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 188 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 189 } 190 return result; 191 } 192 193 194 public static Map checkActivityState(DispatchContext ctx, Map context) { 195 Map result = new HashMap (); 196 GenericDelegator delegator = ctx.getDelegator(); 197 Security security = ctx.getSecurity(); 198 String workEffortId = (String ) context.get("workEffortId"); 199 200 try { 201 WorkflowClient client = WfFactory.getClient(ctx); 202 result.put("activityState", client.getState(workEffortId)); 203 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 204 } catch (WfException we) { 205 we.printStackTrace(); 206 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 207 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 208 } 209 return result; 210 } 211 212 213 public static Map getActivityContext(DispatchContext ctx, Map context) { 214 Map result = new HashMap (); 215 GenericDelegator delegator = ctx.getDelegator(); 216 Security security = ctx.getSecurity(); 217 String workEffortId = (String ) context.get("workEffortId"); 218 219 try { 220 WorkflowClient client = WfFactory.getClient(ctx); 221 result.put("activityContext", client.getContext(workEffortId)); 222 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 223 } catch (WfException we) { 224 we.printStackTrace(); 225 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 226 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 227 } 228 return result; 229 } 230 231 232 public static Map appendActivityContext(DispatchContext ctx, Map context) { 233 Map result = new HashMap (); 234 GenericDelegator delegator = ctx.getDelegator(); 235 Security security = ctx.getSecurity(); 236 String workEffortId = (String ) context.get("workEffortId"); 237 Map appendContext = (Map ) context.get("currentContext"); 238 239 if (appendContext == null || appendContext.size() == 0) { 240 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 241 result.put(ModelService.ERROR_MESSAGE, "The passed context is empty"); 242 } 243 244 GenericValue userLogin = (GenericValue) context.get("userLogin"); 245 246 if (!hasPermission(security, workEffortId, userLogin)) { 247 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 248 result.put(ModelService.ERROR_MESSAGE, "You do not have permission to access this activity"); 249 return result; 250 } 251 try { 252 WorkflowClient client = WfFactory.getClient(ctx); 253 client.appendContext(workEffortId, appendContext); 254 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 255 } catch (WfException we) { 256 we.printStackTrace(); 257 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 258 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 259 } 260 return result; 261 } 262 263 264 public static Map assignActivity(DispatchContext ctx, Map context) { 265 Map result = new HashMap (); 266 GenericDelegator delegator = ctx.getDelegator(); 267 Security security = ctx.getSecurity(); 268 String workEffortId = (String ) context.get("workEffortId"); 269 String partyId = (String ) context.get("partyId"); 270 String roleType = (String ) context.get("roleTypeId"); 271 boolean removeOldAssign = false; 272 273 if (context.containsKey("removeOldAssignments")) { 274 removeOldAssign = ((String ) context.get("removeOldAssignments")).equals("true") ? true : false; 275 } 276 277 GenericValue userLogin = (GenericValue) context.get("userLogin"); 278 279 if (!hasPermission(security, workEffortId, userLogin)) { 280 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 281 result.put(ModelService.ERROR_MESSAGE, "You do not have permission to access this activity"); 282 return result; 283 } 284 try { 285 WorkflowClient client = WfFactory.getClient(ctx); 286 client.assign(workEffortId, partyId, roleType, null, removeOldAssign ? false : true); 287 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 288 } catch (WfException we) { 289 we.printStackTrace(); 290 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 291 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 292 } 293 return result; 294 } 295 296 297 public static Map acceptAssignment(DispatchContext ctx, Map context) { 298 Map result = new HashMap (); 299 String workEffortId = (String ) context.get("workEffortId"); 300 String partyId = (String ) context.get("partyId"); 301 String roleType = (String ) context.get("roleTypeId"); 302 Timestamp fromDate = (Timestamp ) context.get("fromDate"); 303 304 try { 305 WorkflowClient client = WfFactory.getClient(ctx); 306 client.acceptAndStart(workEffortId, partyId, roleType, fromDate); 307 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 308 } catch (WfException we) { 309 we.printStackTrace(); 310 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 311 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 312 } 313 return result; 314 315 } 316 317 318 public static Map delegateAssignment(DispatchContext ctx, Map context) { 319 Map result = new HashMap (); 320 String workEffortId = (String ) context.get("workEffortId"); 321 String fromParty = (String ) context.get("fromPartyId"); 322 String fromRole = (String ) context.get("fromRoleTypeId"); 323 Timestamp fromFromDate = (Timestamp ) context.get("fromFromDate"); 324 String toParty = (String ) context.get("toPartyId"); 325 String toRole = (String ) context.get("toRoleTypeId"); 326 Timestamp toFromDate = (Timestamp ) context.get("toFromDate"); 327 328 if (toFromDate == null) 330 toFromDate = UtilDateTime.nowTimestamp(); 331 332 try { 333 WorkflowClient client = new WorkflowClient(ctx); 334 client.delegate(workEffortId, fromParty, fromRole, fromFromDate, toParty, toRole, toFromDate); 335 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 336 } catch (WfException we) { 337 we.printStackTrace(); 338 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 339 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 340 } 341 return result; 342 } 343 344 345 public static Map delegateAcceptAssignment(DispatchContext ctx, Map context) { 346 Map result = new HashMap (); 347 String workEffortId = (String ) context.get("workEffortId"); 348 String fromParty = (String ) context.get("fromPartyId"); 349 String fromRole = (String ) context.get("fromRoleTypeId"); 350 Timestamp fromFromDate = (Timestamp ) context.get("fromFromDate"); 351 String toParty = (String ) context.get("toPartyId"); 352 String toRole = (String ) context.get("toRoleTypeId"); 353 Timestamp toFromDate = (Timestamp ) context.get("toFromDate"); 354 Boolean startObj = (Boolean ) context.get("startActivity"); 355 356 boolean start = false; 358 if (startObj != null) 359 start = startObj.booleanValue(); 360 361 if (toFromDate == null) 363 toFromDate = UtilDateTime.nowTimestamp(); 364 365 try { 366 WorkflowClient client = new WorkflowClient(ctx); 367 client.delegateAndAccept(workEffortId, fromParty, fromRole, fromFromDate, toParty, toRole, toFromDate, start); 368 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 369 } catch (WfException we) { 370 we.printStackTrace(); 371 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 372 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 373 } 374 return result; 375 } 376 377 378 public static Map acceptRoleAssignment(DispatchContext ctx, Map context) { 379 Map result = new HashMap (); 380 String workEffortId = (String ) context.get("workEffortId"); 381 String partyId = (String ) context.get("partyId"); 382 String roleType = (String ) context.get("roleTypeId"); 383 Timestamp fromDate = (Timestamp ) context.get("fromDate"); 384 385 try { 386 WorkflowClient client = new WorkflowClient(ctx); 387 client.delegateAndAccept(workEffortId, "_NA_", roleType, fromDate, partyId, roleType, fromDate, true); 388 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 389 } catch (WfException we) { 390 we.printStackTrace(); 391 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 392 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 393 } 394 return result; 395 } 396 397 398 public static Map completeAssignment(DispatchContext ctx, Map context) { 399 Map result = new HashMap (); 400 GenericDelegator delegator = ctx.getDelegator(); 401 Security security = ctx.getSecurity(); 402 String workEffortId = (String ) context.get("workEffortId"); 403 String partyId = (String ) context.get("partyId"); 404 String roleType = (String ) context.get("roleTypeId"); 405 Timestamp fromDate = (Timestamp ) context.get("fromDate"); 406 Map actResults = (Map ) context.get("result"); 407 408 GenericValue userLogin = (GenericValue) context.get("userLogin"); 409 410 if (!hasPermission(security, workEffortId, userLogin)) { 411 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 412 result.put(ModelService.ERROR_MESSAGE, "You do not have permission to access this assignment"); 413 return result; 414 } 415 416 try { 417 WorkflowClient client = WfFactory.getClient(ctx); 418 client.complete(workEffortId, partyId, roleType, fromDate, actResults); 419 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 420 } catch (WfException we) { 421 we.printStackTrace(); 422 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 423 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 424 } 425 return result; 426 } 427 428 public static Map limitInvoker(DispatchContext ctx, Map context) { 429 Map result = new HashMap (); 430 GenericDelegator delegator = ctx.getDelegator(); 431 LocalDispatcher dispatcher = ctx.getDispatcher(); 432 String workEffortId = (String ) context.get("workEffortId"); 433 String limitService = (String ) context.get("serviceName"); 434 Map limitContext = (Map ) context.get("serviceContext"); 435 436 try { 437 WorkflowClient client = WfFactory.getClient(ctx); 438 String state = client.getState(workEffortId); 439 440 if (state.startsWith("open")) { 441 dispatcher.runSync(limitService, limitContext); 442 } 443 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 444 } catch (WfException we) { 445 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 446 result.put(ModelService.ERROR_MESSAGE, we.getMessage()); 447 } catch (GenericServiceException se) { 448 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 449 result.put(ModelService.ERROR_MESSAGE, se.getMessage()); 450 } 451 return result; 452 } 453 454 458 461 public static boolean hasPermission(Security security, String workEffortId, GenericValue userLogin) { 462 if (userLogin == null || workEffortId == null) { 463 Debug.logWarning("No UserLogin object or no Workeffort ID was passed.", module); 464 return false; 465 } 466 if (security.hasPermission("WORKFLOW_MAINT", userLogin)) { 467 return true; 468 } else { 469 String partyId = userLogin.getString("partyId"); 470 List expr = new ArrayList (); 471 472 expr.add(new EntityExpr("partyId", EntityOperator.EQUALS, partyId)); 473 expr.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_DECLINED")); 474 expr.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_DELEGATED")); 475 expr.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_COMPLETED")); 476 expr.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_CANCELLED")); 477 expr.add(new EntityExpr("workEffortId", EntityOperator.EQUALS, workEffortId)); 478 expr.add(new EntityExpr("fromDate", EntityOperator.LESS_THAN_EQUAL_TO, UtilDateTime.nowTimestamp())); 479 480 Collection c = null; 481 482 try { 483 c = userLogin.getDelegator().findByAnd("WorkEffortAndPartyAssign", expr); 484 } catch (GenericEntityException e) { 486 Debug.logWarning(e, module); 487 return false; 488 } 489 if (c.size() == 0) { 490 expr = new ArrayList (); 491 expr.add(new EntityExpr("partyId", EntityOperator.EQUALS, partyId)); 492 expr.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_DECLINED")); 493 expr.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_DELEGATED")); 494 expr.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_COMPLETED")); 495 expr.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_CANCELLED")); 496 expr.add(new EntityExpr("workEffortParentId", EntityOperator.EQUALS, workEffortId)); 497 expr.add(new EntityExpr("fromDate", EntityOperator.LESS_THAN_EQUAL_TO, UtilDateTime.nowTimestamp())); 498 try { 499 c = userLogin.getDelegator().findByAnd("WorkEffortAndPartyAssign", expr); 500 } catch (GenericEntityException e) { 502 Debug.logWarning(e, module); 503 return false; 504 } 505 } 506 507 if (c.size() > 0) { 508 return true; 509 } 510 } 511 return false; 512 } 513 514 517 public static GenericValue getOwner(GenericDelegator delegator, String workEffortId) { 518 try { 519 GenericValue we = delegator.findByPrimaryKey("WorkEffort", UtilMisc.toMap("workEffortId", workEffortId)); 520 521 if (we != null && we.getString("workEffortParentId") == null) { 522 Collection c = delegator.findByAnd("WorkEffortPartyAssignment", 523 UtilMisc.toMap("workEffortId", workEffortId, "roleTypeId", "WF_OWNER")); 524 525 return (GenericValue) c.iterator().next(); 526 } else { 527 return getOwner(delegator, we.getString("workEffortParentId")); 528 } 529 } catch (GenericEntityException e) { 530 Debug.logWarning(e, module); 531 } 532 return null; 533 } 534 535 } 536 537 | Popular Tags |