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.eventbus.Subscribe;
27 import fr.ifremer.common.synchro.intercept.SynchroInterceptor;
28 import fr.ifremer.common.synchro.meta.SynchroDatabaseMetadata;
29 import fr.ifremer.common.synchro.meta.SynchroTableMetadata;
30 import fr.ifremer.common.synchro.meta.event.LoadTableEvent;
31 import fr.ifremer.quadrige3.core.dao.technical.Daos;
32 import fr.ifremer.quadrige3.synchro.intercept.geometry.ConvertPostgis2WktGeometryInterceptor;
33 import fr.ifremer.quadrige3.synchro.intercept.geometry.ConvertSdo2WktGeometryInterceptor;
34 import fr.ifremer.quadrige3.synchro.intercept.geometry.ConvertWkt2PostgisGeometryInterceptor;
35 import fr.ifremer.quadrige3.synchro.intercept.geometry.ConvertWkt2SdoGeometryInterceptor;
36 import fr.ifremer.quadrige3.synchro.service.SynchroDirection;
37 import org.hibernate.tool.hbm2ddl.TableMetadata;
38
39
40
41
42
43
44
45 public class GeometryTablesInterceptor extends AbstractReferentialInterceptor {
46
47
48 @Override
49 public boolean doApply(SynchroDatabaseMetadata meta, TableMetadata table) {
50 String tableName = table.getName().toUpperCase();
51 return tableName.endsWith("_AREA")
52 || tableName.endsWith("_LINE")
53 || tableName.endsWith("_POINT");
54 }
55
56
57
58
59
60
61
62
63
64 @Subscribe
65 public void handleLoadTable(LoadTableEvent e) {
66
67 SynchroTableMetadata table = e.table;
68
69 int columnPositionIndex = getPositionColumnIndex(table);
70 if (columnPositionIndex == -1) {
71 return;
72 }
73
74 SynchroDirection direction = getConfig().getDirection();
75 SynchroInterceptor geometryInterceptor = null;
76
77
78
79 if (direction == SynchroDirection.IMPORT_NO_TEMP
80 || direction == SynchroDirection.IMPORT_SERVER2TEMP) {
81
82 if (Daos.isOracleDatabase(getSourceJdbcUrl())) {
83
84 geometryInterceptor = new ConvertSdo2WktGeometryInterceptor(columnPositionIndex);
85 } else if (Daos.isPostgresqlDatabase(getSourceJdbcUrl())) {
86
87 geometryInterceptor = new ConvertPostgis2WktGeometryInterceptor(columnPositionIndex);
88 }
89 }
90
91
92
93
94 else if (direction == SynchroDirection.EXPORT_NO_TEMP
95 || direction == SynchroDirection.EXPORT_TEMP2SERVER) {
96
97 if (Daos.isOracleDatabase(getTargetJdbcUrl())) {
98
99 geometryInterceptor = new ConvertWkt2SdoGeometryInterceptor(columnPositionIndex);
100 } else if (Daos.isPostgresqlDatabase(getTargetJdbcUrl())) {
101
102 geometryInterceptor = new ConvertWkt2PostgisGeometryInterceptor(columnPositionIndex);
103 }
104 }
105
106 if (geometryInterceptor != null) {
107 table.addInterceptor(geometryInterceptor);
108 }
109 }
110
111
112
113 private int getPositionColumnIndex(SynchroTableMetadata table) {
114 for (String columnName : table.getColumnNames()) {
115 if (columnName.toUpperCase().endsWith("_POSITION")) {
116 return table.getColumnIndex(columnName);
117 }
118 }
119 return -1;
120 }
121
122 private String getSourceJdbcUrl() {
123 return getConfig().getSource() != null ? getConfig().getSource().getJdbcUrl() : getConfig().getJdbcUrl();
124 }
125
126 private String getTargetJdbcUrl() {
127 return getConfig().getTarget() != null ? getConfig().getTarget().getJdbcUrl() : getConfig().getJdbcUrl();
128 }
129 }