View Javadoc
1   package fr.ifremer.quadrige3.synchro.intercept.geometry;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Synchro Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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   * <p>
41   * ConvertWkt2PostgisGeometryInterceptor class.
42   * </p>
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  	 * <p>
65  	 * Constructor for ConvertWkt2PostgisGeometryInterceptor.
66  	 * </p>
67  	 *
68  	 * @param columnIndex
69  	 *            a int.
70  	 */
71  	public ConvertWkt2PostgisGeometryInterceptor(int columnIndex) {
72  		super();
73  		setEnableOnWrite(true);
74  		setEnableOnRead(true);
75  		this.columnIndex = columnIndex;
76  	}
77  
78  	/** {@inheritDoc} */
79  	@Override
80  	public SynchroInterceptorBase clone() {
81  		return new ConvertWkt2PostgisGeometryInterceptor(columnIndex);
82  	}
83  
84  	/** {@inheritDoc} */
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  	/** {@inheritDoc} */
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 	/* -- Internal methods -- */
109 
110 	/**
111 	 * <p>
112 	 * toNative.
113 	 * </p>
114 	 *
115 	 * @param geometry
116 	 *            a {@link Geometry} object.
117 	 * @param connection
118 	 *            a {@link Connection} object.
119 	 * @return a {@link Object} object.
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 					// unwrap connection (Mantis #46477)
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 }