1 package fr.ifremer.quadrige3.synchro.intercept.geometry;
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.vividsolutions.jts.geom.Geometry;
27 import fr.ifremer.common.synchro.dao.SynchroTableDao;
28 import fr.ifremer.common.synchro.intercept.SynchroInterceptorBase;
29 import fr.ifremer.common.synchro.intercept.SynchroOperationRepository;
30 import fr.ifremer.quadrige3.core.dao.technical.Daos;
31 import fr.ifremer.quadrige3.core.dao.technical.Geometries;
32 import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
33 import org.hibernate.spatial.dialect.oracle.DefaultConnectionFinder;
34 import org.hibernate.spatial.dialect.oracle.OracleJDBCTypeFactory;
35 import org.hibernate.spatial.dialect.oracle.SDOGeometryValueBinder;
36
37 import java.lang.reflect.Method;
38 import java.sql.Connection;
39 import java.util.List;
40
41
42
43
44
45
46
47 public class ConvertWkt2SdoGeometryInterceptor extends SynchroInterceptorBase {
48
49 private final static SDOGeometryValueBinder SDO_GEOMETRY_BINDER = new SDOGeometryValueBinder(new OracleJDBCTypeFactory(
50 new DefaultConnectionFinder()));
51
52 private final static Method toNativeMethodSDOGeometryValueBinder = getToNativeMethod();
53
54 private static Method getToNativeMethod() {
55 try {
56 Method toNativeMethod = SDOGeometryValueBinder.class.getDeclaredMethod("toNative", Geometry.class, Connection.class);
57 toNativeMethod.setAccessible(true);
58 return toNativeMethod;
59 } catch (NoSuchMethodException e) {
60 throw new QuadrigeTechnicalException("Could not initialize ConvertWkt2SdoGeometryInterceptor class: " + e.getMessage(), e);
61 }
62 }
63
64 private int columnIndex;
65
66
67
68
69
70
71
72
73
74 public ConvertWkt2SdoGeometryInterceptor(int columnIndex) {
75 super();
76 setEnableOnWrite(true);
77 setEnableOnRead(true);
78 this.columnIndex = columnIndex;
79 }
80
81
82 @Override
83 public SynchroInterceptorBase clone() {
84 return new ConvertWkt2SdoGeometryInterceptor(columnIndex);
85 }
86
87
88 @Override
89 protected void doOnWrite(Object[] data, List<Object> pk, SynchroTableDao sourceDao, SynchroTableDao targetDao, SynchroOperationRepository buffer,
90 boolean insert) {
91 Object position = data[columnIndex];
92 if (position != null && (position instanceof String)) {
93 Geometry geometry = Geometries.getGeometry((String) position);
94 data[columnIndex] = toNative(geometry, targetDao.getConnection());
95 }
96 }
97
98
99 @Override
100 protected void doOnRead(Object[] data, SynchroTableDao sourceDao, SynchroTableDao targetDao) {
101 Object position = data[columnIndex];
102
103 if (position != null && (position instanceof String)) {
104 Geometry geometry = Geometries.getGeometry((String) position);
105
106 data[columnIndex] = toNative(geometry, targetDao.getConnection());
107 }
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 protected Object toNative(Geometry geometry, Connection connection) {
124 if (geometry == null) {
125 return null;
126 }
127
128 try {
129 return toNativeMethodSDOGeometryValueBinder.invoke(
130 SDO_GEOMETRY_BINDER,
131 geometry,
132
133 Daos.unwrapConnection(connection));
134 } catch (Exception e) {
135 throw new QuadrigeTechnicalException("Unable to call SDOGeometryValueBinder.toNative() using reflection: " + e.getMessage(), e);
136 }
137
138 }
139 }