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