View Javadoc
1   package fr.ifremer.quadrige2.synchro.intercept.data.internal;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 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.quadrige2.core.dao.technical.Geometries;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.hibernate.spatial.GeometryJavaTypeDescriptor;
34  import org.hibernate.spatial.dialect.oracle.DefaultConnectionFinder;
35  import org.hibernate.spatial.dialect.oracle.OracleJDBCTypeFactory;
36  import org.hibernate.spatial.dialect.oracle.SDOGeometryTypeDescriptor;
37  import org.hibernate.spatial.dialect.oracle.SDOGeometryValueExtractor;
38  
39  import java.sql.SQLException;
40  import java.util.List;
41  
42  /**
43   * <p>
44   * ConvertSdo2WktGeometryInterceptor class.
45   * </p>
46   * 
47   */
48  public class ConvertSdo2WktGeometryInterceptor extends SynchroInterceptorBase {
49  
50  	private final static SDOGeometryValueExtractor SDO_GEOMETRY_EXTRACTOR = new SDOGeometryValueExtractor(GeometryJavaTypeDescriptor.INSTANCE,
51  			new SDOGeometryTypeDescriptor(new OracleJDBCTypeFactory(new DefaultConnectionFinder())));
52  
53  	private static final Log LOG = LogFactory.getLog(ConvertSdo2WktGeometryInterceptor.class);
54  
55  	private int columnIndex = -1;
56  
57  	/**
58  	 * <p>
59  	 * Constructor for ConvertSdo2WktGeometryInterceptor.
60  	 * </p>
61  	 * 
62  	 * @param columnIndex
63  	 *            a int.
64  	 */
65  	public ConvertSdo2WktGeometryInterceptor(int columnIndex) {
66  		super();
67  		setEnableOnWrite(true);
68  		setEnableOnRead(true);
69  		this.columnIndex = columnIndex;
70  	}
71  
72  	/** {@inheritDoc} */
73  	@Override
74  	public SynchroInterceptorBase clone() {
75  		return new ConvertSdo2WktGeometryInterceptor(columnIndex);
76  	}
77  
78  	/** {@inheritDoc} */
79  	@Override
80  	protected void doOnWrite(Object[] data, List<Object> pk, SynchroTableDao sourceDao, SynchroTableDao targetDao, SynchroOperationRepository buffer,
81  			boolean insert) throws SQLException {
82  		Object position = data[columnIndex];
83  		if (position != null && !(position instanceof String)) {
84  			Geometry geometry = null;
85  			try {
86  				geometry = SDO_GEOMETRY_EXTRACTOR.toJTS(position);
87  			} catch (IllegalArgumentException iae) {
88  				LOG.warn("the position is unreadable : " + iae.getLocalizedMessage());
89  			}
90  			if (geometry != null) {
91  				String wtkGeometry = Geometries.getWKTString(geometry);
92  				data[columnIndex] = wtkGeometry.replaceAll(", ", ",");
93  			} else {
94  				data[columnIndex] = null;
95  			}
96  		}
97  	}
98  
99  	/** {@inheritDoc} */
100 	@Override
101 	protected void doOnRead(Object[] data, SynchroTableDao sourceDao, SynchroTableDao targetDao) throws SQLException {
102 		Object position = data[columnIndex];
103 
104 		if (position != null && !(position instanceof String)) {
105 			Geometry geometry = SDO_GEOMETRY_EXTRACTOR.toJTS(position);
106 			String wtkGeometry = Geometries.getWKTString(geometry);
107 
108 			data[columnIndex] = wtkGeometry.replaceAll(", ", ",");
109 		}
110 	}
111 
112 	/* -- Internal methods -- */
113 
114 }