Commit fc4ad847c984cdf85c292938bcfb0dc65dff1302

Authored by carlos.alberto
1 parent 852587c5
Exists in master

Melhoria no controle de exceção do workflow

cit-esi-api/src/main/java/br/com/centralit/esi/api/execution/model/ProcessInstance.java
... ... @@ -259,6 +259,7 @@ public class ProcessInstance extends PersistentObject implements Serializable {
259 259 if (runtimeEnvironment != null) {
260 260 this.setRuntimeEnvironment(runtimeEnvironment);
261 261 this.setRuntimeId(runtimeEnvironment.getRuntimeId());
  262 + runtimeEnvironment.addProcessInstance(this);
262 263 }
263 264 }
264 265  
... ...
cit-esi-api/src/main/java/br/com/centralit/esi/api/execution/service/ProcessInstanceServiceBase.java
... ... @@ -22,8 +22,6 @@ public interface ProcessInstanceServiceBase extends GenericService<ProcessInstan
22 22 public void executeWorkItem(RuntimeEnvironment runtimeEnvironment, WorkItem workItem);
23 23  
24 24 public void completeProcessInstance(RuntimeEnvironment runtimeEnvironment, ProcessInstance processInstance);
25   -
26   - public boolean isPersistent(ProcessInstance processInstance);
27 25  
28 26 public List<ProcessInstance> findByOriginWorkItem(WorkItem workItem);
29 27  
... ... @@ -49,7 +47,7 @@ public interface ProcessInstanceServiceBase extends GenericService&lt;ProcessInstan
49 47  
50 48 public WorkItem getWorkItem(long workItemId);
51 49  
52   - public ProcessInstance saveOrUpdate(RuntimeEnvironment runtimeEnvironment, ProcessInstance processInstance, boolean force);
  50 + public ProcessInstance saveOrUpdate(RuntimeEnvironment runtimeEnvironment, ProcessInstance processInstance);
53 51  
54 52 public void addMessage(RuntimeEnvironment runtimeEnvironment, ProcessInstance processInstance, NotificationTemplate notificationTemplate, User[] users, Group[] groups, List<File> files);
55 53  
... ...
cit-esi-api/src/main/java/br/com/centralit/esi/api/execution/service/impl/ProcessInstanceServiceBaseImpl.java
... ... @@ -53,8 +53,6 @@ import br.com.centralit.esi.api.security.model.User;
53 53 import br.com.centralit.esi.api.security.service.SecurityService;
54 54 import br.com.centralit.esi.api.subscriber.model.Subscriber;
55 55 import br.com.centralit.esi.api.util.EsiAppUtils;
56   -import br.com.centralit.esi.exception.EsiControlledException;
57   -import br.com.centralit.esi.exception.EsiExecutionException;
58 56 import br.com.centralit.framework.exception.BusinessException;
59 57 import br.com.centralit.framework.mail.HTMLMail;
60 58 import br.com.centralit.framework.service.arquitetura.GenericServiceImpl;
... ... @@ -144,7 +142,7 @@ public abstract class ProcessInstanceServiceBaseImpl extends GenericServiceImpl&lt;
144 142  
145 143 this.verifyOriginWorkItem(runtimeEnvironment, processInstance);
146 144  
147   - if (processInstance.isSendCompletionNotification()) {
  145 + if (!processInstance.hasError() && processInstance.isSendCompletionNotification()) {
148 146 this.addMessage(runtimeEnvironment, processInstance, processInstance.getCompletionTemplate(), this.getSubscribersUsers(processInstance), this.getSubscribersGroups(processInstance), null);
149 147 }
150 148 }
... ... @@ -216,51 +214,7 @@ public abstract class ProcessInstanceServiceBaseImpl extends GenericServiceImpl&lt;
216 214 return new ArrayList<FlowElement>();
217 215 }
218 216 }
219   -
220   - @Override
221   - public boolean isPersistent(ProcessInstance processInstance) {
222   - boolean result = false;
223   - if (!processInstance.hasOwner()) {
224   - result = processInstance.hasError() || this.hasPersistentChild(processInstance);
225   - if (!result) {
226   - result = processInstance.getFlowVersion().getPersistentExecution() != null && processInstance.getFlowVersion().getPersistentExecution();
227   - }
228   - }
229   - return result;
230   - }
231   -
232   - private boolean hasPersistentChild(ProcessInstance processInstance) {
233   - boolean result = false;
234   - if (processInstance.hasChildren()) {
235   - for (ProcessInstance child : processInstance.getChildren()) {
236   - if (this.isPersistent(child)) {
237   - result = true;
238   - break;
239   - }
240   - }
241   - }
242   - return result;
243   - }
244 217  
245   - protected void solveException(ProcessInstance processInstance) {
246   - if (processInstance.hasException() && processInstance.isStopOnException()) {
247   - Throwable e = processInstance.getException();
248   - if (processInstance.hasOwner()) {
249   - if (e instanceof EsiControlledException) {
250   - throw (EsiControlledException) e;
251   - }else{
252   - throw new EsiControlledException(e);
253   - }
254   - }else{
255   - if (e instanceof EsiExecutionException) {
256   - throw (EsiExecutionException) e;
257   - }else{
258   - throw new EsiExecutionException(e);
259   - }
260   - }
261   - }
262   - }
263   -
264 218 protected void updateVariables(ProcessInstance processInstance, boolean force) {
265 219 if (processInstance.getFlowVersion().getVariables() != null) {
266 220 RuntimeEnvironment runtimeEnvironment = processInstance.getRuntimeEnvironment();
... ... @@ -280,7 +234,7 @@ public abstract class ProcessInstanceServiceBaseImpl extends GenericServiceImpl&lt;
280 234 instanceVariable.setProcessInstance(processInstance);
281 235 instanceVariable.setFlowVariable(flowVariable);
282 236 instanceVariable.buildValues(obj);
283   - if ((force || this.isPersistent(processInstance)) && flowVariable.getPersistentVariable() != null && flowVariable.getPersistentVariable()) {
  237 + if ((force || processInstance.isPersistentExecution()) && flowVariable.getPersistentVariable() != null && flowVariable.getPersistentVariable()) {
284 238 instanceVariable = processInstanceVariableService.save(instanceVariable);
285 239 }
286 240 processInstance.addVariable(instanceVariable);
... ... @@ -404,51 +358,11 @@ public abstract class ProcessInstanceServiceBaseImpl extends GenericServiceImpl&lt;
404 358 }
405 359 }
406 360  
407   - @SuppressWarnings({ "rawtypes", "unchecked" })
408   - @Override
409   - public ProcessInstance saveOrUpdate(RuntimeEnvironment runtimeEnvironment, ProcessInstance processInstance, boolean force) {
410   - if (this.isPersistent(processInstance) || force) {
411   - List<WorkItem> newWorkItems = processInstance.getNewWorkItems();
412   -
413   - this.initializeTimeManagementStatus(processInstance);
414   - if (processInstance.isNew()) {
415   - processInstance = this.save(processInstance);
416   - }else{
417   - processInstance = this.merge(processInstance);
418   - }
419   -
420   - while (newWorkItems.size() > 0) {
421   - WorkItem workItem = newWorkItems.get(0);
422   - WorkItemService workItemService = runtimeContext.getWorkItemService(workItem.getFlowElement());
423   - workItem = (WorkItem) workItemService.save(workItem);
424   - workItemService.afterRegistry(processInstance.getRuntimeEnvironment(), workItem);
425   - this.solveTimeManagementStatus(processInstance, workItem);
426   - newWorkItems.remove(0);
427   - }
428   -
429   - this.updateVariables(processInstance, force);
430   -
431   - if (processInstance.hasChildren()) {
432   - for (ProcessInstance child : processInstance.getChildren()) {
433   - this.saveOrUpdate(runtimeEnvironment, child, true);
434   - }
435   - }
436   -
437   - this.sendMessages(runtimeEnvironment, processInstance);
438   - }
439   - return processInstance;
440   - }
441 361  
442 362 protected ProcessInstance getProcessInstance(WorkItem workItem) {
443 363 return workItem.getProcessInstance();
444 364 }
445   -
446   - protected ProcessInstance finalize(RuntimeEnvironment runtimeEnvironment, ProcessInstance processInstance) {
447   - this.solveException(processInstance);
448 365  
449   - return processInstance;
450   - }
451   -
452 366 @Override
453 367 public void sendMessages(RuntimeEnvironment runtimeEnvironment, ProcessInstance processInstance) {
454 368 if ((EsiAppUtils.BPE_SEND_EMAIL || EsiAppUtils.BPE_SEND_NOTIFICATION) && processInstance.getMessages() != null) {
... ... @@ -556,7 +470,7 @@ public abstract class ProcessInstanceServiceBaseImpl extends GenericServiceImpl&lt;
556 470 throw new BusinessException("Elemento de início do fluxo '"+runtimeEnvironment.getFlowName()+"' não existe.");
557 471  
558 472 ProcessInstance processInstance = this.createInstance(runtimeEnvironment, originWorkItem, flowVersion);
559   - if (this.isPersistent(processInstance)) {
  473 + if (processInstance.isPersistentExecution()) {
560 474 processInstance = super.save(processInstance);
561 475 }
562 476  
... ... @@ -568,13 +482,11 @@ public abstract class ProcessInstanceServiceBaseImpl extends GenericServiceImpl&lt;
568 482  
569 483 this.go(runtimeEnvironment, processInstance, elementoInicio);
570 484  
571   - if (processInstance.isSendCreationNotification()) {
  485 + if (!processInstance.hasError() && processInstance.isSendCreationNotification()) {
572 486 this.addMessage(runtimeEnvironment, processInstance, processInstance.getCreationTemplate(), this.getSubscribersUsers(processInstance), this.getSubscribersGroups(processInstance), null);
573 487 }
574 488  
575   - this.saveOrUpdate(runtimeEnvironment, processInstance, false);
576   -
577   - return finalize(runtimeEnvironment, processInstance);
  489 + return processInstance;
578 490 }
579 491  
580 492 @Override
... ... @@ -590,8 +502,6 @@ public abstract class ProcessInstanceServiceBaseImpl extends GenericServiceImpl&lt;
590 502 this.initialize(runtimeEnvironment, processInstance);
591 503  
592 504 userTaskItem = workItemUserTaskService.capture(runtimeEnvironment, userTaskItem);
593   -
594   - this.saveOrUpdate(runtimeEnvironment, processInstance, true);
595 505 }
596 506  
597 507 protected void validateExecute(RuntimeEnvironment runtimeEnvironment, ProcessInstance processInstance, WorkItem workItem) {
... ... @@ -609,7 +519,7 @@ public abstract class ProcessInstanceServiceBaseImpl extends GenericServiceImpl&lt;
609 519 }
610 520  
611 521 @SuppressWarnings({ "rawtypes" })
612   - public void execute(RuntimeEnvironment runtimeEnvironment, WorkItem workItem) {
  522 + protected void execute(RuntimeEnvironment runtimeEnvironment, WorkItem workItem) {
613 523 if (workItem.getStatus().equals(WorkItemStatusEnum.COMPLETED)) {
614 524 return;
615 525 }
... ... @@ -632,9 +542,6 @@ public abstract class ProcessInstanceServiceBaseImpl extends GenericServiceImpl&lt;
632 542 this.go(runtimeEnvironment, processInstance, target);
633 543 }
634 544 }
635   -
636   - this.saveOrUpdate(runtimeEnvironment, processInstance, processInstance.isPersistentExecution());
637   - this.finalize(runtimeEnvironment, processInstance);
638 545 }
639 546  
640 547 @Override
... ... @@ -670,8 +577,6 @@ public abstract class ProcessInstanceServiceBaseImpl extends GenericServiceImpl&lt;
670 577 this.initialize(runtimeEnvironment, processInstance);
671 578  
672 579 workItemUserTaskService.delegate(runtimeEnvironment, userTaskItem, userTaskItem.getGroups(), userTaskItem.getUsers());
673   -
674   - this.finalize(runtimeEnvironment, processInstance);
675 580 }
676 581  
677 582 @Override
... ... @@ -689,8 +594,6 @@ public abstract class ProcessInstanceServiceBaseImpl extends GenericServiceImpl&lt;
689 594 this.initialize(runtimeEnvironment, processInstance);
690 595  
691 596 workItemUserTaskService.removeDelegation(runtimeEnvironment, userTaskItem, userTaskItem.getGroups(), userTaskItem.getUsers());
692   -
693   - this.finalize(runtimeEnvironment, processInstance);
694 597 }
695 598  
696 599 @Override
... ... @@ -708,8 +611,6 @@ public abstract class ProcessInstanceServiceBaseImpl extends GenericServiceImpl&lt;
708 611 this.initialize(runtimeEnvironment, processInstance);
709 612  
710 613 userTaskItem = workItemUserTaskService.capture(runtimeEnvironment, userTaskItem);
711   -
712   - this.saveOrUpdate(runtimeEnvironment, processInstance, true);
713 614 }
714 615  
715 616 @Override
... ... @@ -774,10 +675,6 @@ public abstract class ProcessInstanceServiceBaseImpl extends GenericServiceImpl&lt;
774 675 this.initialize(runtimeEnvironment, processInstance);
775 676  
776 677 this.go(runtimeEnvironment, processInstance, elementoInicio);
777   -
778   - this.saveOrUpdate(runtimeEnvironment, processInstance, true);
779   -
780   - finalize(runtimeEnvironment, processInstance);
781 678 }
782 679  
783 680 @Override
... ... @@ -802,4 +699,31 @@ public abstract class ProcessInstanceServiceBaseImpl extends GenericServiceImpl&lt;
802 699 return processInstanceDao.findByOwner(owner);
803 700 }
804 701  
  702 + @SuppressWarnings({ "unchecked", "rawtypes" })
  703 + @Override
  704 + public ProcessInstance saveOrUpdate(RuntimeEnvironment runtimeEnvironment, ProcessInstance processInstance) {
  705 + List<WorkItem> newWorkItems = processInstance.getNewWorkItems();
  706 +
  707 + this.initializeTimeManagementStatus(processInstance);
  708 + if (processInstance.isNew()) {
  709 + processInstance = this.save(processInstance);
  710 + }else{
  711 + processInstance = this.merge(processInstance);
  712 + }
  713 +
  714 + while (newWorkItems.size() > 0) {
  715 + WorkItem workItem = newWorkItems.get(0);
  716 + WorkItemService workItemService = runtimeContext.getWorkItemService(workItem.getFlowElement());
  717 + workItem = (WorkItem) workItemService.save(workItem);
  718 + workItemService.afterRegistry(processInstance.getRuntimeEnvironment(), workItem);
  719 + this.solveTimeManagementStatus(processInstance, workItem);
  720 + newWorkItems.remove(0);
  721 + }
  722 +
  723 + this.updateVariables(processInstance, true);
  724 +
  725 + this.sendMessages(runtimeEnvironment, processInstance);
  726 +
  727 + return processInstance;
  728 + }
805 729 }
806 730 \ No newline at end of file
... ...
cit-esi-api/src/main/java/br/com/centralit/esi/api/execution/service/impl/WorkItemServiceImpl.java
... ... @@ -187,26 +187,24 @@ public class WorkItemServiceImpl&lt;E extends FlowElement&gt; extends GenericServiceIm
187 187 }
188 188  
189 189 protected WorkItem solveException(Exception e, RuntimeEnvironment runtimeEnvironment, ProcessInstance processInstance, FlowElement flowElement, WorkItem workItem) {
190   - if (workItem == null) {
191   - workItem = this.register(runtimeEnvironment, processInstance,flowElement);
  190 + EsiControlledException controlledException = null;
  191 + if (e instanceof EsiControlledException) {
  192 + controlledException = (EsiControlledException) e;
  193 + }else{
  194 + controlledException = new EsiControlledException(e);
192 195 }
193   - workItem = this.retrieveErrorEvents(runtimeEnvironment, processInstance, workItem);
  196 +
  197 + ErrorLog errorLog = errorLogService.create(controlledException);
  198 + processInstance.setErrorLog(errorLog);
194 199  
195   - ErrorLog errorLog = processInstance.getErrorLog();
196   - if (errorLog == null) {
197   - errorLog = errorLogService.create(e);
198   - processInstance.setErrorLog(errorLog);
  200 + if (workItem != null) {
  201 + workItem = this.retrieveErrorEvents(runtimeEnvironment, processInstance, workItem);
  202 +
  203 + workItem.setErrorLog(errorLog);
  204 +
  205 + processInstance.setStopOnException(!processInstance.isStopOnException() && (workItem.getTargets() == null || workItem.getTargets().size() == 0));
199 206 }
200   - workItem.setErrorLog(errorLog);
201 207  
202   - if (workItem.getTargets() == null) {
203   - processInstance.setStopOnException(true);
204   - if (e instanceof EsiControlledException) {
205   - throw (EsiControlledException) e;
206   - }else{
207   - throw new EsiControlledException(e);
208   - }
209   - }
210 208 return workItem;
211 209 }
212 210  
... ...
cit-esi-api/src/main/java/br/com/centralit/esi/api/runtime/RuntimeEnvironment.java
... ... @@ -183,5 +183,15 @@ public interface RuntimeEnvironment {
183 183 * @return <code>List<BusinessRule></code>
184 184 */
185 185 public List<BusinessRule> getBusinessRules();
  186 +
  187 + /**
  188 + * Retorna as instâncias do processo do ambiente de execução
  189 + */
  190 + public List<ProcessInstance> getInstances();
  191 +
  192 + /**
  193 + * Adiciona uma instância de processo à lista de instância
  194 + */
  195 + public void addProcessInstance(ProcessInstance processInstance);
186 196  
187 197 }
... ...
cit-esi-api/src/main/java/br/com/centralit/esi/api/runtime/RuntimeEnvironmentImpl.java
... ... @@ -47,6 +47,9 @@ public class RuntimeEnvironmentImpl implements RuntimeEnvironment {
47 47  
48 48 @JsonIgnore
49 49 protected List<BusinessRule> businessRules;
  50 +
  51 + @JsonIgnore
  52 + protected List<ProcessInstance> instances;
50 53  
51 54 @Override
52 55 public RuntimeEnvironmentInput getInput() {
... ... @@ -213,6 +216,7 @@ public class RuntimeEnvironmentImpl implements RuntimeEnvironment {
213 216 * @param flowName
214 217 */
215 218 private void initialize(RuntimeEnvironmentInput input, FlowVersion flowVersion) {
  219 + this.instances = new ArrayList<ProcessInstance>();
216 220 this.objects = new MapObjects();
217 221 this.initializeAttributes(input, flowVersion, null);
218 222 this.assignInputVariables(flowVersion, true);
... ... @@ -226,6 +230,7 @@ public class RuntimeEnvironmentImpl implements RuntimeEnvironment {
226 230 * @param processInstance
227 231 */
228 232 private void initialize(ProcessInstance processInstance, RuntimeEnvironmentInput input) {
  233 + this.instances = new ArrayList<ProcessInstance>();
229 234 this.objects = new MapObjects();
230 235 this.initializeAttributes(input, processInstance.getFlowVersion(), processInstance.getRuntimeId());
231 236 this.assignInitialValues(processInstance.getFlowVersion(), true);
... ... @@ -238,6 +243,7 @@ public class RuntimeEnvironmentImpl implements RuntimeEnvironment {
238 243 * @param ownerEnvironment
239 244 */
240 245 private void initialize(RuntimeEnvironment ownerEnvironment, FlowVersion flowVersion) {
  246 + this.instances = ownerEnvironment.getInstances();
241 247 this.objects = ownerEnvironment.getObjects();
242 248 this.user = ownerEnvironment.getUser();
243 249  
... ... @@ -461,4 +467,13 @@ public class RuntimeEnvironmentImpl implements RuntimeEnvironment {
461 467 }
462 468 this.addOrUpdateObject(businessRule.getIdentifier(), businessRule);
463 469 }
  470 +
  471 + @Override
  472 + public void addProcessInstance(ProcessInstance processInstance) {
  473 + this.instances.add(processInstance);
  474 + }
  475 +
  476 + public List<ProcessInstance> getInstances() {
  477 + return instances;
  478 + }
464 479 }
... ...
cit-esi-api/src/main/java/br/com/centralit/esi/api/runtime/service/impl/RuntimeManagerBaseImpl.java
... ... @@ -92,7 +92,51 @@ public abstract class RuntimeManagerBaseImpl implements RuntimeManagerBase {
92 92 return runtimeEnvironment;
93 93 }
94 94  
  95 + protected void solveException(RuntimeActionEnum action, Throwable exception, EnvironmentInput runtimeEnvironmentInput, FlowVersion flowVersion, ProcessInstance processInstance) {
  96 + exception.printStackTrace();
  97 + if (!(exception instanceof EsiControlledException) || processInstance == null) {
  98 + runtimeLogService.create(action, exception, securityService.getAuthenticatedUser(), runtimeEnvironmentInput, flowVersion, processInstance);
  99 + if (exception instanceof EsiControlledException) {
  100 + throw (EsiControlledException) exception;
  101 + }else{
  102 + throw new EsiControlledException(exception);
  103 + }
  104 + }else if (exception instanceof RuntimeException) {
  105 + throw (RuntimeException) exception;
  106 + }else{
  107 + throw new EsiControlledException(exception);
  108 + }
  109 + }
  110 +
  111 + protected void verifyException(RuntimeEnvironment runtimeEnvironment) {
  112 + if (runtimeEnvironment.getInstances() != null) {
  113 + for (ProcessInstance processInstance : runtimeEnvironment.getInstances()) {
  114 + if (processInstance.hasException() && processInstance.isStopOnException()) {
  115 + Throwable e = processInstance.getException();
  116 + if (e instanceof EsiControlledException) {
  117 + throw (EsiControlledException) e;
  118 + }else{
  119 + throw new EsiControlledException(e);
  120 + }
  121 + }
  122 + }
  123 + }
  124 + }
  125 +
  126 + protected void saveOrUpdateProcessInstances(RuntimeEnvironment runtimeEnvironment) {
  127 + if (runtimeEnvironment.getInstances() != null) {
  128 + for (ProcessInstance processInstance : runtimeEnvironment.getInstances()) {
  129 + if (processInstance.hasError() || processInstance.isPersistentExecution()) {
  130 + this.getProcessInstanceService().saveOrUpdate(runtimeEnvironment, processInstance);
  131 + }
  132 + }
  133 + }
  134 + }
  135 +
95 136 protected RuntimeEnvironmentOutput finalize(ProcessInstance processInstance, RuntimeEnvironment runtimeEnvironment) {
  137 + this.verifyException(runtimeEnvironment);
  138 + this.saveOrUpdateProcessInstances(runtimeEnvironment);
  139 +
96 140 return new RuntimeEnvironmentOutput(runtimeEnvironment, processInstance);
97 141 }
98 142  
... ... @@ -118,7 +162,7 @@ public abstract class RuntimeManagerBaseImpl implements RuntimeManagerBase {
118 162 ProcessInstance processInstance = this.getProcessInstanceService().start(runtimeEnvironment, originWorkItem, flowVersion);
119 163 RuntimeEnvironmentOutput output = finalize(processInstance, runtimeEnvironment);
120 164  
121   - if (createLog && this.getProcessInstanceService().isPersistent(processInstance)) {
  165 + if (createLog && (processInstance.isPersistentExecution() || processInstance.hasError())) {
122 166 runtimeLogService.create( runtimeEnvironment.getInput().isSignal() ? RuntimeActionEnum.SIGNAL_EVENT
123 167 : flowVersion.isBusinessRule() ? RuntimeActionEnum.BUSINESS_RULE
124 168 : RuntimeActionEnum.START, null,
... ... @@ -195,22 +239,6 @@ public abstract class RuntimeManagerBaseImpl implements RuntimeManagerBase {
195 239 }
196 240 }
197 241  
198   - protected void solveException(RuntimeActionEnum action, Throwable exception, EnvironmentInput runtimeEnvironmentInput, FlowVersion flowVersion, ProcessInstance processInstance) {
199   - exception.printStackTrace();
200   - if (!(exception instanceof EsiControlledException) || processInstance == null) {
201   - runtimeLogService.create(action, exception, securityService.getAuthenticatedUser(), runtimeEnvironmentInput, flowVersion, processInstance);
202   - if (exception instanceof EsiControlledException) {
203   - throw (EsiControlledException) exception;
204   - }else{
205   - throw new EsiControlledException(exception);
206   - }
207   - }else if (exception instanceof RuntimeException) {
208   - throw (RuntimeException) exception;
209   - }else{
210   - throw new EsiControlledException(exception);
211   - }
212   - }
213   -
214 242 @Override
215 243 public RuntimeEnvironmentOutput start(RuntimeEnvironment ownerEnvironment, WorkItem originWorkItem, FlowVersion flowVersion) {
216 244 RuntimeEnvironmentOutput output = null;
... ...