1 package fr.ifremer.quadrige2.synchro.intercept.data.internal;
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 fr.ifremer.common.synchro.dao.SynchroTableDao;
27 import fr.ifremer.common.synchro.intercept.SynchroInterceptorBase;
28 import fr.ifremer.common.synchro.intercept.SynchroOperationRepository;
29
30 import java.sql.SQLException;
31 import java.util.List;
32 import java.util.Map;
33
34
35
36
37
38
39
40 public class ReplaceColumnValuesInterceptor extends SynchroInterceptorBase {
41
42 private final int columnIndex;
43
44 private final Map<String, Object> valuesMap;
45
46
47
48
49
50
51
52
53
54
55
56 public ReplaceColumnValuesInterceptor(int columnIndex,
57 Map<String, Object> columnValuesMap) {
58 this.columnIndex = columnIndex;
59 this.valuesMap = columnValuesMap;
60 setEnableOnRead(true);
61 setEnableOnWrite(true);
62 }
63
64
65 @Override
66 public SynchroInterceptorBase clone() {
67 return new ReplaceColumnValuesInterceptor(columnIndex, valuesMap);
68 }
69
70
71 @Override
72 protected void doOnRead(Object[] data,
73 SynchroTableDao sourceDao,
74 SynchroTableDao targetDao) throws SQLException {
75
76 Object sourceValue = data[columnIndex];
77 if (sourceValue != null) {
78 Object targetValue = valuesMap.get(sourceValue.toString());
79 if (targetValue != null) {
80 data[columnIndex] = targetValue;
81 }
82 }
83 }
84
85
86 @Override
87 protected void doOnWrite(Object[] data, List<Object> pk, SynchroTableDao sourceDao, SynchroTableDao targetDao, SynchroOperationRepository buffer,
88 boolean insert) throws SQLException {
89
90 Object sourceValue = data[columnIndex];
91 if (sourceValue != null) {
92 Object targetValue = valuesMap.get(sourceValue.toString());
93 if (targetValue != null) {
94 data[columnIndex] = targetValue;
95 }
96 }
97 }
98
99 }