1 package fr.ifremer.quadrige3.synchro.intercept.data;
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.eventbus.Subscribe;
27 import fr.ifremer.common.synchro.intercept.SynchroInterceptorBase;
28 import fr.ifremer.common.synchro.meta.SynchroDatabaseMetadata;
29 import fr.ifremer.common.synchro.meta.event.LoadTableEvent;
30 import fr.ifremer.common.synchro.service.SynchroDatabaseConfiguration;
31 import fr.ifremer.quadrige3.core.dao.technical.Assert;
32 import fr.ifremer.quadrige3.synchro.intercept.data.internal.ExportUpdateDateInterceptor;
33 import fr.ifremer.quadrige3.synchro.meta.data.DataSynchroTables;
34 import fr.ifremer.quadrige3.synchro.service.SynchroDirection;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.hibernate.tool.hbm2ddl.TableMetadata;
38
39 import java.sql.Timestamp;
40 import java.util.ArrayList;
41 import java.util.List;
42
43
44
45
46
47
48
49
50
51 public class DataTableWithUpdateDateInterceptor extends AbstractDataInterceptor {
52
53 private static final Log log = LogFactory.getLog(DataTableWithUpdateDateInterceptor.class);
54
55 private Timestamp systimestamp = null;
56 private static final List<String> excludedTableNames;
57
58 static {
59 excludedTableNames = initExcludedTableNames();
60 }
61
62
63
64
65
66
67 public DataTableWithUpdateDateInterceptor() {
68 super(SynchroDirection.EXPORT_TEMP2SERVER);
69 }
70
71
72 @Override
73 public SynchroInterceptorBase clone() {
74 DataTableWithUpdateDateInterceptor result = (DataTableWithUpdateDateInterceptor) super.clone();
75 result.systimestamp = this.systimestamp;
76 return result;
77 }
78
79
80 public boolean doApply(SynchroDatabaseMetadata dbMeta, TableMetadata table) {
81
82 return hasColumns(table, getConfig().getColumnUpdateDate())
83
84 && !hasColumns(table, getConfig().getColumnRemoteId())
85
86 && isIncludedTable(table);
87 }
88
89 private boolean isIncludedTable(TableMetadata table) {
90 return !excludedTableNames.contains(table.getName().toUpperCase());
91 }
92
93
94
95
96
97
98
99
100
101 @Subscribe
102 public void handleTableLoad(LoadTableEvent e) {
103 if (systimestamp == null) {
104 systimestamp = checkAndGetSystemTimestamp(getConfig());
105 }
106
107 int updateDateColumnIndex = e.table.getSelectColumnIndex(getConfig().getColumnUpdateDate());
108 if (updateDateColumnIndex == -1) {
109 log.warn(String.format("[%s] Unable to find %s column in the select query. %s will be ignore.",
110 e.table.getName(),
111 getConfig().getColumnUpdateDate(),
112 getClass().getSimpleName()));
113 }
114 else {
115 ExportUpdateDateInterceptor setUpdateDateInterceptor =
116 new ExportUpdateDateInterceptor(updateDateColumnIndex, systimestamp);
117
118 e.table.addInterceptor(setUpdateDateInterceptor);
119 }
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133 protected Timestamp checkAndGetSystemTimestamp(SynchroDatabaseConfiguration configuration) {
134 Timestamp systimestamp = configuration.getSystemTimestamp();
135 Assert.notNull(systimestamp,
136 String.format("Could not found system timestamp in database configuration. This is need for %s", getClass().getSimpleName()));
137 return systimestamp;
138 }
139
140 private static List<String> initExcludedTableNames() {
141 List<String> result = new ArrayList<>();
142
143 result.add(DataSynchroTables.VALIDATION_HISTORY.name());
144 result.add(DataSynchroTables.QUALIFICATION_HISTORY.name());
145
146 return result;
147 }
148 }