1 package fr.ifremer.quadrige2.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.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 AbstractDataInterceptor {
42
43
44
45
46
47
48 public GeometryTablesInterceptor() {
49
50 super(SynchroDirection.IMPORT_SERVER2TEMP,
51 SynchroDirection.EXPORT_TEMP2SERVER);
52 }
53
54
55 @Override
56 public boolean doApply(SynchroDatabaseMetadata meta, TableMetadata table) {
57 String tableName = table.getName();
58 return tableName.endsWith("_AREA")
59 || tableName.endsWith("_LINE")
60 || tableName.endsWith("_POINT");
61 }
62
63
64
65
66
67
68
69
70
71 @Subscribe
72 public void handleLoadTable(LoadTableEvent e) {
73
74 SynchroTableMetadata table = e.table;
75 SynchroDirection direction = getConfig().getDirection();
76 int columnPositionIndex = getPositionColumnIndex(table);
77 if (columnPositionIndex == -1) {
78 return;
79 }
80
81 if (direction == SynchroDirection.IMPORT_SERVER2TEMP) {
82
83
84 ConvertSdo2WktGeometryInterceptor sdoGeometryInterceptor = new ConvertSdo2WktGeometryInterceptor(columnPositionIndex);
85 table.addInterceptor(sdoGeometryInterceptor);
86 }
87 else if (direction == SynchroDirection.EXPORT_TEMP2SERVER) {
88
89
90 ConvertWkt2SdoGeometryInterceptor sdoGeometryInterceptor = new ConvertWkt2SdoGeometryInterceptor(columnPositionIndex);
91 table.addInterceptor(sdoGeometryInterceptor);
92 }
93 }
94
95
96
97 private int getPositionColumnIndex(SynchroTableMetadata table) {
98 for (String columnName : table.getColumnNames()) {
99 if (columnName.endsWith("_position")) {
100 return table.getColumnIndex(columnName);
101 }
102 }
103 return -1;
104 }
105 }