1 package org.smartcomps.twister.engine.priv.messaging.impl;
2
3 import org.smartcomps.twister.common.persistence.DBSessionException;
4 import org.smartcomps.twister.common.transaction.TransactionManager;
5 import org.smartcomps.twister.common.transaction.TransactionException;
6 import org.smartcomps.twister.engine.priv.core.definition.ActivityFactory;
7 import org.smartcomps.twister.engine.priv.core.definition.Pick;
8 import org.smartcomps.twister.engine.priv.core.definition.Activity;
9 import org.smartcomps.twister.engine.priv.core.definition.MessageEvent;
10 import org.smartcomps.twister.engine.priv.core.definition.impl.MessageEventImpl;
11 import org.smartcomps.twister.engine.priv.core.dynamic.*;
12 import org.smartcomps.twister.common.persistence.FinderException;
13
14 import java.util.List;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Iterator;
18
19 /***
20 * Factory class used by the MessageController implementation to get activities
21 * and execution contexts from the engine core. This factory includes methods
22 * to find picks and receives and wraps the picks to behave as a receive using
23 * the wrapping class. It basically calls factory methods of the engine core and
24 * creates correct wrappers for picks.
25 * @see PickAsReceive
26 * @see PickECAsReceiveEC
27 * @see org.smartcomps.twister.engine.priv.core.definition.ActivityFactory
28 * @see org.smartcomps.twister.engine.priv.core.dynamic.ExecutionContextFactory
29 */
30 public class CoreWrappingFactory {
31
32 /***
33 * Find execution context by its activity and an instance. A special case is
34 * when the provided activity is our wrapper PickAsReceive. In this particular case
35 * we replace the wrapper with the Pick behind it in the finder and return the PickEC
36 * wrapped around PickECAsReceiveEC.
37 * @param activity
38 * @return
39 */
40 public static ExecutionContext findECsByActivityAndInstance(ProcessInstance instance, Activity activity) throws FinderException {
41 if (activity instanceof PickAsReceive) {
42 PickAsReceive pickAsReceive = (PickAsReceive)activity;
43 PickEC pickEC = (PickEC) ExecutionContextFactory.findECForActivityInInstance(instance, pickAsReceive.getPick());
44 return new PickECAsReceiveEC(pickEC, pickAsReceive.getEventPos());
45 } else {
46 return ExecutionContextFactory.findECForActivityInInstance(instance, activity);
47 }
48 }
49
50 /***
51 * Find receives as well as picks by invoker and wraps the picks using the
52 * class PickAsReceive, returning a List only containing Receive implementations.
53 * @param partnerLink
54 * @param portType
55 * @param operation
56 * @return List of Receive implementations, either a really Receive or a pick wrapped as a Receive
57 * @throws DBSessionException
58 */
59 public static List findReceivesByInvoker(String partnerLink, String portType, String operation) throws DBSessionException {
60 List result = ActivityFactory.findReceivesByInvoker(partnerLink, portType, operation);
61 List pickEvents = ActivityFactory.findPickEventsByInvoker(partnerLink, portType, operation);
62 result.addAll(wrapPicksAsReceives(pickEvents));
63 return result;
64 }
65
66 public static ReceiveEC reload(ReceiveEC receiveEC) throws DBSessionException, FinderException {
67 if (receiveEC instanceof PickECAsReceiveEC) {
68 PickEC pickEC = (PickEC) ExecutionContextFactory.findECById(receiveEC.getId());
69 return new PickECAsReceiveEC(pickEC, ((PickECAsReceiveEC)receiveEC).getEventPos());
70 } else {
71 return (ReceiveEC) ExecutionContextFactory.findECById(receiveEC.getId());
72 }
73 }
74
75 private static List wrapPicksAsReceives(List pickEvents) {
76 List result = new ArrayList(pickEvents.size());
77 Pick pick;
78 int eventPos;
79 for (int m = 0; m < pickEvents.size(); m++) {
80 Object[] objects = (Object[]) pickEvents.get(m);
81 pick = (Pick)objects[0];
82 eventPos = pick.getMessageEvents().indexOf(objects[1]);
83 System.out.println("Adding event at pos : " + eventPos + " with pick " + pick.getName());
84 System.out.println("Found as second object " + objects[1].getClass() + " with id " + ((MessageEventImpl)objects[1]).getId());
85 PickAsReceive pickAsReceive = new PickAsReceive(pick, eventPos);
86 result.add(pickAsReceive);
87 }
88 return result;
89 }
90
91 private static List wrapPickECsAsReceiveECs(PickAsReceive pickAsReceive, Collection pickECs) {
92 ArrayList result = new ArrayList(pickECs.size());
93 for (Iterator pickIter = pickECs.iterator(); pickIter.hasNext();) {
94 PickEC pickEC = (PickEC) pickIter.next();
95 result.add(new PickECAsReceiveEC(pickEC, pickAsReceive.getEventPos()));
96 }
97 return result;
98
99 }
100
101 }
This page was automatically generated by Maven