1   /*
2    * Copyright 2003 - 2013 The eFaps Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  package org.efaps.bpm.workitem;
22  
23  import org.kie.api.runtime.process.WorkItem;
24  import org.kie.api.runtime.process.WorkItemHandler;
25  import org.kie.api.runtime.process.WorkItemManager;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  /**
30   * TODO comment!
31   *
32   * @author The eFaps Team
33   * @version $Id$
34   */
35  public class SignallingHandlerWrapper
36      extends AbstractExceptionWorkItemHandler
37  {
38      /**
39       * Logging instance used in this class.
40       */
41      private static final Logger LOG = LoggerFactory.getLogger(SignallingHandlerWrapper.class);
42  
43      /**
44       * Event type key.
45       */
46      private String eventType;
47  
48      /**
49       * Key to the exception returned to the Process.
50       */
51      private String workItemExceptionParameterName = "org.efaps.bpm.workitem.exception";
52  
53      /**
54       * @param _originalTaskHandler  task handler that will be wrapped in
55       */
56      public SignallingHandlerWrapper(final WorkItemHandler _originalTaskHandler)
57      {
58          super(_originalTaskHandler);
59          this.eventType = null;
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public void handleExecuteException(final Throwable _cause,
67                                         final WorkItem _workItem,
68                                         final WorkItemManager _manager)
69      {
70          _workItem.getParameters().put(this.workItemExceptionParameterName, _cause);
71          final String eventTypeTmp = getEventType(_cause, _workItem);
72          SignallingHandlerWrapper.LOG.debug("Signaling event with eventType: {}", eventTypeTmp);
73          ((org.drools.core.process.instance.WorkItemManager) _manager).signalEvent(eventTypeTmp, _workItem,
74                          _workItem.getProcessInstanceId());
75      }
76  
77      /**
78       * @param _cause    cause the ventype is wanted for
79       * @param _workItem workitem the couse belongs to
80       * @return eventype
81       */
82      public String getEventType(final Throwable _cause,
83                                 final WorkItem _workItem)
84      {
85          // check if manually set
86          if (this.eventType == null) {
87              final Object signal = _workItem.getParameter(EsjpWorkItemHandler.PARAMETERNAME_ERRORSIGNAL);
88              if (signal != null) {
89                  this.eventType = "Error-" + signal;
90              }
91              if (_cause instanceof WorkItemException) {
92                  this.eventType = this.eventType + "-" + _cause.getMessage();
93              }
94          }
95          return this.eventType;
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public void handleAbortException(final Throwable _cause,
103                                      final WorkItem _workItem,
104                                      final WorkItemManager _manager)
105     {
106         _workItem.getParameters().put(this.workItemExceptionParameterName, _cause);
107         final String eventTypeTmp = getEventType(_cause, _workItem);
108         SignallingHandlerWrapper.LOG.debug("Signaling event with eventType: {}", eventTypeTmp);
109         ((org.drools.core.process.instance.WorkItemManager) _manager).signalEvent(eventTypeTmp, _workItem,
110                         _workItem.getProcessInstanceId());
111     }
112 
113     /**
114      * Getter method for the instance variable {@link #eventType}.
115      *
116      * @return value of instance variable {@link #eventType}
117      */
118     public String getEventType()
119     {
120         return this.eventType;
121     }
122 
123     /**
124      * Setter method for instance variable {@link #eventType}.
125      *
126      * @param _eventType value for instance variable {@link #eventType}
127      */
128     public void setEventType(final String _eventType)
129     {
130         this.eventType = _eventType;
131     }
132 
133     /**
134      * Getter method for the instance variable {@link #workItemExceptionParameterName}.
135      *
136      * @return value of instance variable {@link #workItemExceptionParameterName}
137      */
138     public String getWorkItemExceptionParameterName()
139     {
140         return this.workItemExceptionParameterName;
141     }
142 
143     /**
144      * Setter method for instance variable {@link #workItemExceptionParameterName}.
145      *
146      * @param _workItemExceptionParameterName value for instance variable {@link #workItemExceptionParameterName}
147      */
148     public void setWorkItemExceptionParameterName(final String _workItemExceptionParameterName)
149     {
150         this.workItemExceptionParameterName = _workItemExceptionParameterName;
151     }
152 }