1 package org.smartcomps.twister.engine.priv.core.dynamic;
2
3 import org.smartcomps.twister.common.persistence.DBSessionException;
4 import org.smartcomps.twister.common.persistence.XMLSessionException;
5 import org.smartcomps.twister.engine.priv.core.definition.TwisterProcess;
6 import org.smartcomps.twister.engine.priv.core.definition.CorrelationSet;
7 import org.smartcomps.twister.engine.priv.core.dynamic.impl.ProcessInstanceImpl;
8 import org.smartcomps.twister.engine.priv.core.dynamic.impl.ValuedPropertyImpl;
9 import org.smartcomps.twister.engine.priv.core.dynamic.impl.xao.CorrelationXAO;
10 import org.smartcomps.twister.engine.priv.core.dynamic.impl.dao.ProcessInstanceDAO;
11 import org.smartcomps.twister.common.persistence.FinderException;
12 import org.smartcomps.twister.common.persistence.CreationException;
13 import org.smartcomps.twister.engine.exception.CorrelationViolationException;
14
15 import java.util.Map;
16 import java.util.Iterator;
17
18 /***
19 * Factory class for constructing and retrieveng persistnt implementations of
20 * a ProcessInstance.
21 * TODO manage events
22 */
23 public class ProcessInstanceFactory {
24
25 /***
26 * Creates a persistent implementation of a ProcessInstance for its original
27 * TwisterProcess.
28 * @param process original process
29 * @return ProcessInstance the persistent implmentation of this interface
30 * @throws DBSessionException
31 */
32 public static ProcessInstance createProcessInstance(TwisterProcess process, String correlationSetName, Map propertyValues)
33 throws DBSessionException, CreationException, XMLSessionException, CorrelationViolationException {
34
35 // Checking that such an instance doesn't exist already
36 if (correlationSetName != null && correlationSetName.length() > 0) {
37 try {
38 ProcessInstanceDAO.findInstanceByCorrelation(correlationSetName, propertyValues);
39 throw new CreationException("A process instance correlated with set " + correlationSetName + " and having "
40 + "property values " + propertyValues + " already exists.");
41 } catch (FinderException e) { }
42 }
43
44 // Setting the process originating this instance
45 ProcessInstanceImpl instance = new ProcessInstanceImpl();
46 instance.setProcess(process);
47 instance.setStatus(ProcessInstance.OPEN);
48
49 if (correlationSetName != null && correlationSetName.length() > 0) {
50 attachCorrelation(instance, correlationSetName, propertyValues);
51 }
52
53 // Inserting the instance
54 ProcessInstanceDAO.create(instance);
55
56 // Creating the correlation set in the XML database too with the id of the created instance
57 if (correlationSetName != null && correlationSetName.length() > 0) {
58 CorrelationXAO.createCorrelation(process.getName(), correlationSetName, instance.getId(), propertyValues);
59 }
60 return instance;
61 }
62
63 public static void addCorrelation(ProcessInstance instance, String correlationSetName, Map propertyValues)
64 throws DBSessionException, XMLSessionException, CorrelationViolationException {
65 ProcessInstanceImpl instanceImpl = (ProcessInstanceImpl)instance;
66 attachCorrelation(instanceImpl, correlationSetName, propertyValues);
67 ProcessInstanceDAO.update(instance);
68 // Creating the correlation set in the XML database too with the id of the created instance
69 CorrelationXAO.createCorrelation(instance.getProcess().getName(), correlationSetName, instanceImpl.getId(), propertyValues);
70 }
71
72 public static ValuedProperty createValuedProperty(ProcessInstance processInstance, String name, String value)
73 throws DBSessionException {
74
75 ValuedPropertyImpl property = new ValuedPropertyImpl();
76 property.setName(name);
77 property.setValue(value);
78 ((ProcessInstanceImpl)processInstance).addProperty(property);
79
80 ProcessInstanceDAO.create(property);
81 return property;
82 }
83
84 public static ProcessInstance findInstanceByCorrelation(String correlationSetName, Map propertyValues)
85 throws DBSessionException, FinderException {
86
87 return ProcessInstanceDAO.findInstanceByCorrelation(correlationSetName, propertyValues);
88 }
89
90 private static void attachCorrelation(ProcessInstanceImpl instance, String correlationSetName, Map propertyValues)
91 throws DBSessionException, CorrelationViolationException {
92 // If a process instance can be found with the same correlation there's a correlation error
93 boolean correlationExists = false;
94 try {
95 findInstanceByCorrelation(correlationSetName, propertyValues);
96 correlationExists = true;
97 } catch (FinderException e) { }
98 if (correlationExists) throw new CorrelationViolationException("Correlation " + correlationSetName +
99 " has already been set for instance " + instance);
100
101 // Checking that the correlation set exists and that properties are consistent
102 CorrelationSet correlationSet = instance.getProcess().getCorrelationSet(correlationSetName);
103 if (correlationSet != null) {
104 for (Iterator corrPropsIter = correlationSet.getProperties().iterator(); corrPropsIter.hasNext();) {
105 String propName = (String) corrPropsIter.next();
106 String propValue = (String) propertyValues.get(propName);
107 // TODO check the type of the property value and validate it matches with Property definition
108 if (propValue != null) {
109 ValuedPropertyImpl valuedProperty = new ValuedPropertyImpl();
110 valuedProperty.setName(propName);
111 valuedProperty.setValue(propValue);
112 instance.addProperty(valuedProperty);
113 ProcessInstanceDAO.create(valuedProperty);
114 } else {
115 throw new IllegalArgumentException("The property " + propName + " included in the correlation set "
116 + correlationSetName + " has not been provided in the property map for process " +
117 "instance creation.");
118 }
119 }
120 } else {
121 throw new IllegalArgumentException("The correlation set " + correlationSetName + " could not be found " +
122 "in process " + instance.getProcess());
123 }
124 }
125 }
This page was automatically generated by Maven