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.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   * <p>
43   * ConvertWkt2SdoGeometryInterceptor class.
44   * </p>
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  	 * <p>
68  	 * Constructor for ConvertWkt2SdoGeometryInterceptor.
69  	 * </p>
70  	 * 
71  	 * @param columnIndex
72  	 *            a int.
73  	 */
74  	public ConvertWkt2SdoGeometryInterceptor(int columnIndex) {
75  		super();
76  		setEnableOnWrite(true);
77  		setEnableOnRead(true);
78  		this.columnIndex = columnIndex;
79  	}
80  
81  	/** {@inheritDoc} */
82  	@Override
83  	public SynchroInterceptorBase clone() {
84  		return new ConvertWkt2SdoGeometryInterceptor(columnIndex);
85  	}
86  
87  	/** {@inheritDoc} */
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  	/** {@inheritDoc} */
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 	/* -- Internal methods -- */
111 
112 	/**
113 	 * <p>
114 	 * toNative.
115 	 * </p>
116 	 * 
117 	 * @param geometry
118 	 *            a {@link com.vividsolutions.jts.geom.Geometry} object.
119 	 * @param connection
120 	 *            a {@link java.sql.Connection} object.
121 	 * @return a {@link java.lang.Object} object.
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 					// unwrap connection (Mantis #46477)
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 }