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.ArrayList;
26  import java.util.List;
27  import java.util.UUID;
28  
29  import org.efaps.admin.event.Parameter;
30  import org.efaps.admin.event.Parameter.ParameterValues;
31  import org.efaps.admin.event.Return;
32  import org.efaps.admin.event.Return.ReturnValues;
33  import org.efaps.admin.program.esjp.EFapsClassLoader;
34  import org.efaps.admin.user.AbstractUserObject;
35  import org.efaps.admin.user.Person;
36  import org.efaps.admin.user.Role;
37  import org.efaps.db.Context;
38  import org.efaps.util.EFapsException;
39  import org.jbpm.services.task.impl.model.GroupImpl;
40  import org.jbpm.services.task.impl.model.UserImpl;
41  import org.jbpm.services.task.wih.LocalHTWorkItemHandler;
42  import org.jbpm.services.task.wih.util.PeopleAssignmentHelper;
43  import org.kie.api.runtime.KieSession;
44  import org.kie.api.runtime.process.WorkItem;
45  import org.kie.api.task.model.Group;
46  import org.kie.api.task.model.OrganizationalEntity;
47  import org.kie.api.task.model.Task;
48  import org.kie.api.task.model.User;
49  import org.kie.internal.task.api.TaskModelProvider;
50  import org.kie.internal.task.api.model.InternalOrganizationalEntity;
51  import org.kie.internal.task.api.model.InternalPeopleAssignments;
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  /**
56   * TODO comment!
57   *
58   * @author The eFaps Team
59   * @version $Id$
60   */
61  public class HumanTaskWorkItemHandler
62      extends LocalHTWorkItemHandler
63  {
64  
65      /**
66       * Classname of the class to be executed.
67       */
68      public static final String POTOWNERCLASS = "org.efaps.esjp.bpm.listener.PotentialOwnerListener";
69  
70      /**
71       * Logger for this Instance.
72       */
73      private static final Logger LOG = LoggerFactory.getLogger(HumanTaskWorkItemHandler.class);
74  
75      @Override
76      protected Task createTaskBasedOnWorkItemParams(final KieSession _ksession,
77                                                     final WorkItem _workItem)
78      {
79          final Task ret = super.createTaskBasedOnWorkItemParams(_ksession, _workItem);
80          try {
81              final Person person = Context.getThreadContext().getPerson();
82              if (person != null) {
83                  final User user = new UserImpl(person.getUUID().toString());
84                  ((InternalPeopleAssignments) ret.getPeopleAssignments()).setTaskInitiator(user);
85              }
86              // the original implementation does not allow to add groups as bussinessadministrator
87              final String baIds = (String)_workItem.getParameter(PeopleAssignmentHelper.BUSINESSADMINISTRATOR_ID);
88              if (baIds != null && !baIds.isEmpty()) {
89                  final List<OrganizationalEntity> baList = ret.getPeopleAssignments().getBusinessAdministrators();
90                  final String separator = System.getProperty("org.jbpm.ht.user.separator", ",");
91                  for (final String baId : baIds.split(separator)) {
92                      final Role role = Role.get(UUID.fromString(baId));
93                      if (role != null) {
94                          final Group group = TaskModelProvider.getFactory().newGroup();
95                          ((InternalOrganizationalEntity) group).setId(baId);
96                          // this works only due to the reason that the Group "equal" implementation uses only the id
97                          if (baList.contains(group)) {
98                              baList.remove(group);
99                          }
100                         baList.add(group);
101                     }
102                 }
103                 ((InternalPeopleAssignments) ret.getPeopleAssignments()).setBusinessAdministrators(baList);
104             }
105 
106             final List<OrganizationalEntity> potOwners = ret.getPeopleAssignments().getPotentialOwners();
107 
108             final List<AbstractUserObject> users = new ArrayList<AbstractUserObject>();
109             for (final OrganizationalEntity potOwner : potOwners) {
110                 users.add(AbstractUserObject.getUserObject(UUID.fromString(potOwner.getId())));
111             }
112             List<?> newUser = null;
113             try {
114                 final Parameter parameter = new Parameter();
115                 parameter.put(ParameterValues.BPM_TASK, _workItem.getName());
116                 parameter.put(ParameterValues.BPM_VALUES, users);
117                 parameter.put(ParameterValues.PARAMETERS, _workItem.getParameters());
118 
119                 final Class<?> transformer = Class.forName(HumanTaskWorkItemHandler.POTOWNERCLASS, true,
120                                 EFapsClassLoader.getInstance());
121                 final Method method = transformer.getMethod("execute", new Class[] { Parameter.class });
122                 final Return ret2 = (Return) method.invoke(transformer.newInstance(), parameter);
123                 if (ret2 != null) {
124                     newUser = (List<?>) ret2.get(ReturnValues.VALUES);
125                 }
126             } catch (final ClassNotFoundException e) {
127                 HumanTaskWorkItemHandler.LOG.error("Class could not be found.", e);
128             } catch (final InstantiationException e) {
129                 HumanTaskWorkItemHandler.LOG.error("Class could not be instantiation.", e);
130             } catch (final IllegalAccessException e) {
131                 HumanTaskWorkItemHandler.LOG.error("Class could not be accessed.", e);
132             } catch (final IllegalArgumentException e) {
133                 HumanTaskWorkItemHandler.LOG.error("Illegal Argument.", e);
134             } catch (final InvocationTargetException e) {
135                 HumanTaskWorkItemHandler.LOG.error("Invocation Target.", e);
136             } catch (final SecurityException e) {
137                 HumanTaskWorkItemHandler.LOG.error("Class could not be found.", e);
138             } catch (final NoSuchMethodException e) {
139                 HumanTaskWorkItemHandler.LOG.error("Class could not be found.", e);
140             }
141 
142             if (newUser != null) {
143                 final List<OrganizationalEntity> newPotOwners = new ArrayList<OrganizationalEntity>();
144                 for (final Object user : newUser) {
145                     if (user instanceof Role) {
146                         final Group group = new GroupImpl(((Role) user).getUUID().toString());
147                         newPotOwners.add(group);
148                     }
149                 }
150                 ((InternalPeopleAssignments) ret.getPeopleAssignments()).setPotentialOwners(newPotOwners);
151             }
152         } catch (final EFapsException e) {
153             HumanTaskWorkItemHandler.LOG.error("Catched error on creation of task", e);
154         }
155         return ret;
156     }
157 }