1 package fr.ifremer.quadrige2.synchro.intercept;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import com.google.common.base.Preconditions;
27 import com.google.common.collect.ImmutableSet;
28 import com.google.common.collect.Sets;
29 import fr.ifremer.common.synchro.intercept.SynchroInterceptorBase;
30 import fr.ifremer.common.synchro.meta.SynchroDatabaseMetadata;
31 import fr.ifremer.common.synchro.service.SynchroDatabaseConfiguration;
32 import fr.ifremer.quadrige2.synchro.service.AbstractSynchroDatabaseConfiguration;
33 import fr.ifremer.quadrige2.synchro.service.SynchroDirection;
34 import org.hibernate.tool.hbm2ddl.TableMetadata;
35
36 import java.util.Arrays;
37 import java.util.Set;
38
39
40
41
42
43
44
45 public abstract class AbstractSynchroInterceptor<T extends AbstractSynchroDatabaseConfiguration> extends SynchroInterceptorBase {
46
47 private SynchroDatabaseMetadata meta;
48 private Integer userId;
49 private Set<SynchroDirection> allowDirections = Sets.newHashSet();
50 private Set<String> allowTables;
51
52
53
54
55
56
57 public AbstractSynchroInterceptor() {
58
59 this.allowDirections = null;
60
61 this.allowTables = null;
62 }
63
64
65
66
67
68
69
70
71
72 public AbstractSynchroInterceptor(SynchroDirection... directions) {
73 Preconditions.checkArgument(directions != null && directions.length > 0);
74
75
76 this.allowDirections = ImmutableSet.copyOf(directions);
77
78 this.allowTables = null;
79 }
80
81
82
83
84
85
86
87
88
89
90
91 public AbstractSynchroInterceptor(Set<String> tableIncludes, SynchroDirection... directions) {
92 Preconditions.checkArgument(tableIncludes != null && tableIncludes.size() > 0);
93 Preconditions.checkArgument(directions != null && directions.length > 0);
94
95 this.allowTables = ImmutableSet.copyOf(tableIncludes);
96
97
98 this.allowDirections = ImmutableSet.copyOf(directions);
99 }
100
101
102
103
104
105
106
107
108
109 public AbstractSynchroInterceptor(Set<String> tableIncludes) {
110 Preconditions.checkArgument(tableIncludes != null && tableIncludes.size() > 0);
111
112 this.allowTables = ImmutableSet.copyOf(tableIncludes);
113
114
115 this.allowDirections = null;
116 }
117
118
119 @Override
120 public SynchroInterceptorBase clone() {
121 AbstractSynchroInterceptor result = (AbstractSynchroInterceptor) super.clone();
122 result.meta = this.meta;
123 result.userId = this.userId;
124 result.allowDirections = allowDirections;
125 result.allowTables = allowTables;
126 return result;
127 }
128
129
130 @Override
131 public final boolean apply(SynchroDatabaseConfiguration config) {
132 boolean result = getConfigClass().isInstance(config)
133
134 && super.apply(config);
135
136 if (result) {
137
138 init((T) config);
139 }
140 return result;
141 }
142
143
144
145
146
147
148
149
150 protected abstract Class<T> getConfigClass();
151
152
153 @Override
154 public final boolean apply(SynchroDatabaseMetadata meta, TableMetadata table) {
155 Preconditions.checkNotNull(meta);
156 if (this.meta != null) {
157 Preconditions.checkState(this.meta == meta);
158 }
159
160
161 else {
162 this.meta = meta;
163
164
165 userId = getConfig().getUserId();
166 }
167
168 return (allowDirections == null || allowDirections.contains(getConfig().getDirection()))
169 && (allowTables == null || allowTables.contains(table.getName()))
170 && doApply(meta, table);
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184 public abstract boolean doApply(SynchroDatabaseMetadata meta, TableMetadata table);
185
186
187
188
189
190
191
192
193 public int checkAndGetUserId() {
194 Preconditions
195 .checkNotNull(
196 userId,
197 String.format(
198 "Could not retrieve user id (in database configuration). %s need a not null userId.",
199 getClass().getSimpleName()
200 ));
201 return userId;
202 }
203
204
205
206
207
208
209
210
211
212
213 public T getConfig() {
214 return (T) getDefaultDatabaseConfiguration();
215 }
216
217
218
219
220
221
222
223
224
225
226
227 protected void init(T config) {
228
229 }
230
231
232
233
234
235
236
237
238
239
240 protected boolean isInDirections(SynchroDirection... allowDirections) {
241 return Arrays.asList(allowDirections).contains(getConfig().getDirection());
242 }
243 }