1 package fr.ifremer.quadrige3.synchro.intercept.referential;
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.meta.SynchroDatabaseMetadata;
28 import fr.ifremer.common.synchro.meta.SynchroMetadataUtils;
29 import fr.ifremer.common.synchro.meta.SynchroTableMetadata;
30 import fr.ifremer.quadrige3.synchro.intercept.AbstractSynchroInterceptor;
31 import fr.ifremer.quadrige3.synchro.service.SynchroDirection;
32 import fr.ifremer.quadrige3.synchro.service.referential.ReferentialSynchroDatabaseConfiguration;
33 import org.apache.commons.collections4.CollectionUtils;
34 import org.hibernate.tool.hbm2ddl.TableMetadata;
35
36 import java.util.Collection;
37 import java.util.Set;
38
39
40
41
42
43
44
45 public abstract class AbstractReferentialInterceptor extends AbstractSynchroInterceptor<ReferentialSynchroDatabaseConfiguration> {
46
47
48
49
50
51
52 public AbstractReferentialInterceptor() {
53 super();
54 }
55
56
57
58
59
60
61
62
63
64 public AbstractReferentialInterceptor(SynchroDirection... directions) {
65 super(directions);
66 }
67
68
69
70
71
72
73
74
75
76
77
78 public AbstractReferentialInterceptor(Set<String> tables, SynchroDirection... directions) {
79 super(tables, directions);
80 }
81
82
83
84
85
86
87
88
89
90 public AbstractReferentialInterceptor(Set<String> tables) {
91 super(tables);
92 }
93
94
95
96
97
98
99
100
101
102
103
104 public AbstractReferentialInterceptor(String tableName, SynchroDirection... directions) {
105 this(ImmutableSet.of(tableName), directions);
106 }
107
108
109
110
111
112
113
114
115
116 public AbstractReferentialInterceptor(String tableName) {
117 this(ImmutableSet.of(tableName));
118 }
119
120
121 @Override
122 protected Class<ReferentialSynchroDatabaseConfiguration> getConfigClass() {
123 return ReferentialSynchroDatabaseConfiguration.class;
124 }
125
126
127 @Override
128 public boolean doApply(SynchroDatabaseMetadata meta, TableMetadata table) {
129 return true;
130 }
131
132
133
134
135
136
137
138
139
140
141 protected boolean hasStatusCodesIncludes() {
142 return CollectionUtils.isNotEmpty(getConfig().getStatusCodeIncludes());
143 }
144
145
146
147
148
149
150
151
152
153
154 protected String createPkFilter(SynchroTableMetadata table) {
155 if (getConfig().getPkIncludes() == null
156 || getConfig().getPkIncludes().isEmpty()) {
157 return null;
158 }
159
160
161 if (table.getPkNames().size() != 1) {
162 return null;
163 }
164
165 String pkName = table.getPkNames().iterator().next();
166
167 Collection<String> pkStrs = getConfig()
168 .getPkIncludes()
169 .get(table.getName().toUpperCase());
170
171 if (org.apache.commons.collections.CollectionUtils.isEmpty(pkStrs)) {
172
173
174 return "1=2";
175 }
176
177 boolean isNumericPk = SynchroMetadataUtils.isNumericType(table.getColumnMetadata(pkName));
178
179 StringBuilder inBuilder = new StringBuilder();
180 if (isNumericPk) {
181 for (String pkStr : pkStrs) {
182 inBuilder.append(',').append(pkStr);
183 }
184 return String.format("t.%s IN (%s)",
185 pkName,
186 inBuilder.substring(1)
187 );
188 }
189
190
191 else {
192 for (String pkStr : pkStrs) {
193 inBuilder.append("','").append(pkStr);
194 }
195 inBuilder.append("'");
196
197 return String.format("t.%s IN (%s)",
198 pkName,
199 inBuilder.substring(2)
200 );
201 }
202 }
203 }