1 package fr.ifremer.quadrige3.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.collect.ImmutableSet;
27 import fr.ifremer.common.synchro.intercept.SynchroInterceptorBase;
28 import fr.ifremer.common.synchro.meta.SynchroDatabaseMetadata;
29 import fr.ifremer.common.synchro.service.SynchroDatabaseConfiguration;
30 import fr.ifremer.quadrige3.core.dao.technical.Assert;
31 import fr.ifremer.quadrige3.synchro.service.AbstractSynchroDatabaseConfiguration;
32 import fr.ifremer.quadrige3.synchro.service.SynchroDirection;
33 import org.hibernate.tool.hbm2ddl.TableMetadata;
34
35 import java.util.Arrays;
36 import java.util.Set;
37 import java.util.stream.Collectors;
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;
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 Assert.notEmpty(directions);
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 Assert.notEmpty(tableIncludes);
93 Assert.notEmpty(directions);
94
95 this.allowTables = ImmutableSet.copyOf(tableIncludes.stream().map(String::toUpperCase).collect(Collectors.toSet()));
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 Assert.notEmpty(tableIncludes);
111
112 this.allowTables = ImmutableSet.copyOf(tableIncludes.stream().map(String::toUpperCase).collect(Collectors.toSet()));
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 @SuppressWarnings("unchecked")
131 @Override
132 public final boolean apply(SynchroDatabaseConfiguration config) {
133 boolean result = getConfigClass().isInstance(config)
134
135 && super.apply(config);
136
137 if (result) {
138
139 init((T) config);
140 }
141 return result;
142 }
143
144
145
146
147
148
149
150
151 protected abstract Class<T> getConfigClass();
152
153
154 @Override
155 public final boolean apply(SynchroDatabaseMetadata meta, TableMetadata table) {
156 Assert.notNull(meta);
157 if (this.meta != null) {
158 Assert.state(this.meta == meta, "this metadata should be the same as previous");
159 }
160
161
162 else {
163 this.meta = meta;
164
165
166 userId = getConfig().getUserId();
167 }
168
169 return (allowDirections == null || allowDirections.contains(getConfig().getDirection()))
170 && (allowTables == null || allowTables.contains(table.getName().toUpperCase()))
171 && doApply(meta, table);
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185 public abstract boolean doApply(SynchroDatabaseMetadata meta, TableMetadata table);
186
187
188
189
190
191
192
193
194 public int checkAndGetUserId() {
195 Assert.notNull(
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 @SuppressWarnings("unchecked")
214 public T getConfig() {
215 return (T) getDefaultDatabaseConfiguration();
216 }
217
218
219
220
221
222
223
224
225
226
227
228 protected void init(T config) {
229
230 }
231
232
233
234
235
236
237
238
239
240
241 protected boolean isInDirections(SynchroDirection... allowDirections) {
242 return Arrays.asList(allowDirections).contains(getConfig().getDirection());
243 }
244 }