1 package org.smartcomps.twister.engine.priv.core.dynamic.impl;
2
3 import org.smartcomps.twister.engine.priv.core.dynamic.AssignEC;
4 import org.smartcomps.twister.engine.priv.core.dynamic.impl.xao.VariableXAO;
5 import org.smartcomps.twister.engine.priv.core.definition.Assign;
6 import org.smartcomps.twister.engine.priv.core.definition.Assignment;
7 import org.smartcomps.twister.engine.priv.core.definition.Property;
8 import org.smartcomps.twister.engine.priv.expression.ExpressionProcessorFactory;
9 import org.smartcomps.twister.engine.priv.util.CorrelationExtractor;
10 import org.smartcomps.twister.common.persistence.FinderException;
11 import org.smartcomps.twister.engine.exception.*;
12 import org.smartcomps.twister.common.persistence.XMLSessionException;
13 import org.dom4j.DocumentHelper;
14 import org.dom4j.Document;
15
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.ArrayList;
19
20 /***
21 * Persistent implementation of the AssignEC interface.
22 */
23 public class AssignECImpl extends BasicECImpl implements AssignEC {
24
25 public void execute() {
26 this.setStatus(ACTIVE);
27 notifyExecutionToContainer();
28
29 Assign assign = (Assign) getInitialActivity();
30 for (Iterator assignmentIter = assign.getAssignments().iterator(); assignmentIter.hasNext();) {
31 Assignment assignment = (Assignment) assignmentIter.next();
32 List elmts = getFromAssignment(assign, assignment);
33 setToAssignment(assign, assignment, elmts);
34 }
35
36 notifyTerminationToContainer();
37 this.setStatus(COMPLETED);
38 }
39
40 private List getFromAssignment(Assign assign, Assignment assignment) {
41 List result = null;
42 if (assignment.getFromType() == Assignment.VARIABLE_PART) {
43 String fromPart = assignment.getFromSecondValue();
44 String fromQuery = assignment.getFromQuery();
45 try {
46 result = VariableXAO.queryVariable(assign.fetchProcess().getName(), assignment.getFromFirstValue(),
47 fetchInstance().getId(), fromPart, fromQuery);
48 } catch (XMLSessionException e) {
49 throw new ExecutionException("Could not copy a variable part to another variable part in an " +
50 "Assign execution context execution with id " + this.getId(), e);
51 } catch (FinderException e) {
52 throw new SelectionFailureException(e);
53 }
54 } else if (assignment.getFromType() == Assignment.VARIABLE_PROPERTY) {
55 Document variable = null;
56 try {
57 variable = VariableXAO.getVariable(assign.fetchProcess().getName(),
58 assignment.getFromFirstValue(), fetchInstance().getId());
59 } catch (XMLSessionException e) {
60 throw new ExecutionException("Could not get a variable in an " +
61 "Assign execution context execution with id " + this.getId(), e);
62 } catch (FinderException e) {
63 throw new SelectionFailureException(e);
64 }
65 String propValue = null;
66 try {
67 propValue = CorrelationExtractor.extractPropertyValue(assign.fetchProcess(),
68 assignment.getFromSecondValue(), variable);
69 } catch (CorrelationViolationException e) {
70 throw new EngineRuntimeException(e);
71 }
72 result = new ArrayList(1);
73 result.add(DocumentHelper.createText(propValue));
74 } else if (assignment.getFromType() == Assignment.PARTNER_REFERENCE) {
75 //TODO implement me with partner links
76 throw new UnsupportedOperationException("The partner reference assignment will be implemented with partner links");
77 } else if (assignment.getFromType() == Assignment.LITERAL) {
78 result = new ArrayList(1);
79 result.add(DocumentHelper.createText(assignment.getFromFirstValue()));
80 } else if (assignment.getFromType() == Assignment.EXPRESSION) {
81 try {
82 result = new ArrayList(1);
83 result.add(DocumentHelper.createText(ExpressionProcessorFactory.getBPELExpressionProcessor()
84 .evaluateAsString(fetchInstance(), assignment.getFromFirstValue())));
85 } catch (XPathEvaluationException e) {
86 throw new SelectionFailureException(e);
87 }
88 }
89 return result;
90 }
91
92 private void setToAssignment(Assign assign, Assignment assignment, List elmts) {
93 if (assignment.getToType() == Assignment.VARIABLE_PART) {
94 String toPart = assignment.getToSecondValue();
95 if (toPart == null || toPart.length() == 0) {
96 toPart = "";
97 }
98 String toQuery = assignment.getToQuery();
99 if (toQuery == null || toQuery.length() == 0) {
100 toQuery = "";
101 }
102 try {
103 VariableXAO.setVariableElements(assign.fetchProcess().getName(), assignment.getToFirstValue(),
104 fetchInstance().getId(), toPart, toQuery, elmts);
105 } catch (XMLSessionException e) {
106 throw new ExecutionException("Could not set elements " + elmts + " in a variable " +
107 assignment.getToFirstValue() + " in part " + toPart + " and query" + toQuery);
108 } catch (FinderException e) {
109 throw new SelectionFailureException(e);
110 }
111 } else if (assignment.getFromType() == Assignment.VARIABLE_PROPERTY) {
112 Property prop = assign.fetchProcess().getProperty(assignment.getToSecondValue());
113 if (prop == null) {
114 throw new ExecutionException("Could not find property " + assignment.getToSecondValue() +
115 " in process " + assign.fetchProcess().getName());
116 }
117 try {
118 VariableXAO.setVariableElements(assign.fetchProcess().getName(), assignment.getToFirstValue(),
119 fetchInstance().getId(), prop.getAlias().getPart(), prop.getAlias().getQuery(), elmts);
120 } catch (XMLSessionException e) {
121 throw new ExecutionException("Could not set elements " + elmts + " in a variable " +
122 assignment.getToFirstValue() + " with property " + assignment.getToSecondValue());
123 } catch (FinderException e) {
124 throw new SelectionFailureException(e);
125 }
126 } else if (assignment.getFromType() == Assignment.PARTNER_REFERENCE) {
127 //TODO implement me with partner links
128 throw new UnsupportedOperationException("The partner reference assignment will be implemented with partner links");
129 }
130 }
131 }
This page was automatically generated by Maven