1 package fr.ifremer.quadrige2.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.eventbus.Subscribe;
27 import fr.ifremer.common.synchro.meta.SynchroDatabaseMetadata;
28 import fr.ifremer.common.synchro.meta.SynchroTableMetadata;
29 import fr.ifremer.common.synchro.meta.event.LoadTableEvent;
30 import fr.ifremer.quadrige2.synchro.intercept.data.internal.ConvertSdo2WktGeometryInterceptor;
31 import fr.ifremer.quadrige2.synchro.intercept.data.internal.ConvertWkt2SdoGeometryInterceptor;
32 import fr.ifremer.quadrige2.synchro.service.SynchroDirection;
33 import org.hibernate.tool.hbm2ddl.TableMetadata;
34
35
36
37
38
39
40
41 public class GeometryTablesInterceptor extends AbstractReferentialInterceptor {
42
43
44 @Override
45 public boolean doApply(SynchroDatabaseMetadata meta, TableMetadata table) {
46 String tableName = table.getName().toUpperCase();
47 return tableName.endsWith("_AREA")
48 || tableName.endsWith("_LINE")
49 || tableName.endsWith("_POINT");
50 }
51
52
53
54
55
56
57
58
59
60 @Subscribe
61 public void handleLoadTable(LoadTableEvent e) {
62
63 SynchroTableMetadata table = e.table;
64
65 int columnPositionIndex = getPositionColumnIndex(table);
66 if (columnPositionIndex == -1) {
67 return;
68 }
69
70 SynchroDirection direction = getConfig().getDirection();
71
72
73
74 if (direction == SynchroDirection.IMPORT_NO_TEMP
75 || direction == SynchroDirection.IMPORT_SERVER2TEMP) {
76
77
78 ConvertSdo2WktGeometryInterceptor sdoGeometryInterceptor = new ConvertSdo2WktGeometryInterceptor(columnPositionIndex);
79 table.addInterceptor(sdoGeometryInterceptor);
80 }
81
82
83
84
85 else if (direction == SynchroDirection.EXPORT_NO_TEMP
86 || direction == SynchroDirection.EXPORT_TEMP2SERVER) {
87
88
89 ConvertWkt2SdoGeometryInterceptor sdoGeometryInterceptor = new ConvertWkt2SdoGeometryInterceptor(columnPositionIndex);
90 table.addInterceptor(sdoGeometryInterceptor);
91 }
92 }
93
94
95
96 private int getPositionColumnIndex(SynchroTableMetadata table) {
97 for (String columnName : table.getColumnNames()) {
98 if (columnName.toLowerCase().endsWith("_position")) {
99 return table.getColumnIndex(columnName);
100 }
101 }
102 return -1;
103 }
104 }