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