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 java.lang.reflect.InvocationTargetException;
24  import java.lang.reflect.Method;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.efaps.admin.event.Parameter;
29  import org.efaps.admin.event.Parameter.ParameterValues;
30  import org.efaps.admin.event.Return;
31  import org.efaps.admin.event.Return.ReturnValues;
32  import org.efaps.admin.program.esjp.EFapsClassLoader;
33  import org.kie.api.runtime.process.WorkItem;
34  import org.kie.api.runtime.process.WorkItemHandler;
35  import org.kie.api.runtime.process.WorkItemManager;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  /**
40   * TODO comment!
41   *
42   * @author The eFaps Team
43   * @version $Id$
44   */
45  public class EsjpWorkItemHandler
46      implements WorkItemHandler
47  {
48      /**
49       * Name for the parameter containing the classname.
50       */
51      public static final String PARAMETERNAME_ESJP = "esjp";
52  
53      /**
54       * Name for the parameter containing the methodname.
55       */
56      public static final String PARAMETERNAME_METHOD = "method";
57  
58      /**
59       * Name for the parameter containing the error signal.
60       */
61      public static final String PARAMETERNAME_ERRORSIGNAL = "errorSignal";
62  
63      /**
64       * Logging instance used in this class.
65       */
66      private static final Logger LOG = LoggerFactory.getLogger(EsjpWorkItemHandler.class);
67  
68      /**
69       * The given work item should be executed.
70       *
71       * @param _workItem the work item that should be executed
72       * @param _manager the manager that requested the work item to be executed
73       */
74      @SuppressWarnings("unchecked")
75      @Override
76      public void executeWorkItem(final WorkItem _workItem,
77                                  final WorkItemManager _manager)
78  
79      {
80          final Map<String, Object> results = new HashMap<String, Object>();
81          final String esjpName = (String) _workItem.getParameter(EsjpWorkItemHandler.PARAMETERNAME_ESJP);
82          if (esjpName != null) {
83              final String methodName = (String) _workItem.getParameter(EsjpWorkItemHandler.PARAMETERNAME_METHOD);
84              final Parameter parameter = new Parameter();
85              parameter.put(ParameterValues.BPM_VALUES, _workItem.getParameters());
86              try {
87                  final Class<?> esjp = Class.forName(esjpName.trim(), true,
88                                  EFapsClassLoader.getInstance());
89                  final Method method = esjp.getMethod(methodName.trim(), new Class[] { Parameter.class });
90                  final Return ret = (Return) method.invoke(esjp.newInstance(), parameter);
91                  if (ret != null) {
92                      final Object values = ret.get(ReturnValues.VALUES);
93                      if (values instanceof Map) {
94                          results.putAll((Map<? extends String, ? extends Object>) values);
95                      }
96                  }
97              } catch (final ClassNotFoundException e) {
98                  EsjpWorkItemHandler.LOG.error("Class could not be found.", e);
99              } catch (final InstantiationException e) {
100                 EsjpWorkItemHandler.LOG.error("Class could not be instantiation.", e);
101             } catch (final IllegalAccessException e) {
102                 EsjpWorkItemHandler.LOG.error("Class could not be accessed.", e);
103             } catch (final IllegalArgumentException e) {
104                 EsjpWorkItemHandler.LOG.error("Illegal Argument.", e);
105             } catch (final InvocationTargetException e) {
106                 EsjpWorkItemHandler.LOG.error("Invocation Target.", e);
107                 if (e.getCause() instanceof WorkItemException) {
108                     throw (WorkItemException) e.getCause();
109                 }
110             } catch (final SecurityException e) {
111                 EsjpWorkItemHandler.LOG.error("Security.", e);
112             } catch (final NoSuchMethodException e) {
113                 EsjpWorkItemHandler.LOG.error("Method could not be found.", e);
114             }
115         }
116         _manager.completeWorkItem(_workItem.getId(), results);
117     }
118 
119     /**
120      * The given work item should be aborted.
121      *
122      * @param _workItem the work item that should be aborted
123      * @param _manager the manager that requested the work item to be aborted
124      */
125     @Override
126     public void abortWorkItem(final WorkItem _workItem,
127                               final WorkItemManager _manager)
128     {
129         _manager.abortWorkItem(_workItem.getId());
130     }
131 
132 }