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