1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.bam.processor; |
18 |
|
|
19 |
|
import org.apache.camel.Exchange; |
20 |
|
import org.apache.camel.Expression; |
21 |
|
import org.apache.camel.Processor; |
22 |
|
import org.apache.camel.bam.model.ProcessDefinition; |
23 |
|
import org.apache.camel.bam.rules.ActivityRules; |
24 |
|
import org.apache.camel.util.IntrospectionSupport; |
25 |
|
import org.springframework.orm.jpa.JpaTemplate; |
26 |
|
import org.springframework.transaction.support.TransactionTemplate; |
27 |
|
|
28 |
|
import java.util.List; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
public class JpaBamProcessorSupport<T> extends BamProcessorSupport<T> { |
37 |
|
private ActivityRules activityRules; |
38 |
|
private JpaTemplate template; |
39 |
|
private String findByKeyQuery; |
40 |
3 |
private String keyPropertyName = "correlationKey"; |
41 |
|
|
42 |
|
public JpaBamProcessorSupport(TransactionTemplate transactionTemplate, JpaTemplate template, Expression<Exchange> correlationKeyExpression, ActivityRules activityRules, Class<T> entitytype) { |
43 |
3 |
super(transactionTemplate, correlationKeyExpression, entitytype); |
44 |
3 |
this.activityRules = activityRules; |
45 |
3 |
this.template = template; |
46 |
3 |
} |
47 |
|
|
48 |
|
public JpaBamProcessorSupport(TransactionTemplate transactionTemplate, JpaTemplate template, Expression<Exchange> correlationKeyExpression, ActivityRules activityRules) { |
49 |
0 |
super(transactionTemplate, correlationKeyExpression); |
50 |
0 |
this.activityRules = activityRules; |
51 |
0 |
this.template = template; |
52 |
0 |
} |
53 |
|
|
54 |
|
public String getFindByKeyQuery() { |
55 |
1 |
if (findByKeyQuery == null) { |
56 |
1 |
findByKeyQuery = createFindByKeyQuery(); |
57 |
|
} |
58 |
1 |
return findByKeyQuery; |
59 |
|
} |
60 |
|
|
61 |
|
public void setFindByKeyQuery(String findByKeyQuery) { |
62 |
0 |
this.findByKeyQuery = findByKeyQuery; |
63 |
0 |
} |
64 |
|
|
65 |
|
public ActivityRules getActivityRules() { |
66 |
2 |
return activityRules; |
67 |
|
} |
68 |
|
|
69 |
|
public void setActivityRules(ActivityRules activityRules) { |
70 |
0 |
this.activityRules = activityRules; |
71 |
0 |
} |
72 |
|
|
73 |
|
public String getKeyPropertyName() { |
74 |
2 |
return keyPropertyName; |
75 |
|
} |
76 |
|
|
77 |
|
public void setKeyPropertyName(String keyPropertyName) { |
78 |
0 |
this.keyPropertyName = keyPropertyName; |
79 |
0 |
} |
80 |
|
|
81 |
|
public JpaTemplate getTemplate() { |
82 |
0 |
return template; |
83 |
|
} |
84 |
|
|
85 |
|
public void setTemplate(JpaTemplate template) { |
86 |
0 |
this.template = template; |
87 |
0 |
} |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
protected T loadEntity(Exchange exchange, Object key) { |
92 |
1 |
List<T> list = template.find(getFindByKeyQuery(), key); |
93 |
1 |
T entity = null; |
94 |
1 |
if (!list.isEmpty()) { |
95 |
0 |
entity = list.get(0); |
96 |
|
} |
97 |
1 |
if (entity == null) { |
98 |
1 |
entity = createEntity(exchange, key); |
99 |
1 |
setKeyProperty(entity, key); |
100 |
1 |
ProcessDefinition definition = ProcessDefinition.getRefreshedProcessDefinition(template, getActivityRules().getProcessRules().getProcessDefinition()); |
101 |
1 |
setProcessDefinitionProperty(entity, definition); |
102 |
1 |
template.persist(entity); |
103 |
|
} |
104 |
1 |
return entity; |
105 |
|
} |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
protected void setKeyProperty(T entity, Object key) { |
111 |
1 |
IntrospectionSupport.setProperty(entity, getKeyPropertyName(), key); |
112 |
1 |
} |
113 |
|
|
114 |
|
protected void setProcessDefinitionProperty(T entity, ProcessDefinition processDefinition) { |
115 |
1 |
IntrospectionSupport.setProperty(entity, "processDefinition", processDefinition); |
116 |
1 |
} |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
protected T createEntity(Exchange exchange, Object key) { |
122 |
1 |
return (T) exchange.getContext().getInjector().newInstance(getEntityType()); |
123 |
|
} |
124 |
|
|
125 |
|
protected void processEntity(Exchange exchange, T entity) throws Exception { |
126 |
0 |
if (entity instanceof Processor) { |
127 |
0 |
Processor processor = (Processor) entity; |
128 |
0 |
processor.process(exchange); |
129 |
0 |
} |
130 |
|
else { |
131 |
|
|
132 |
0 |
throw new IllegalArgumentException("No processor defined for this route"); |
133 |
|
} |
134 |
0 |
} |
135 |
|
|
136 |
|
protected String createFindByKeyQuery() { |
137 |
1 |
return "select x from " + getEntityType().getName() + " x where x." + getKeyPropertyName() + " = ?1"; |
138 |
|
} |
139 |
|
} |