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.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.postgis.PGGeometryTypeDescriptor;
35  import org.hibernate.spatial.dialect.postgis.PGGeometryValueExtractor;
36  
37  import java.util.List;
38  
39  /**
40   * <p>
41   * ConvertPostgis2WktGeometryInterceptor class.
42   * </p>
43   * 
44   */
45  public class ConvertPostgis2WktGeometryInterceptor extends SynchroInterceptorBase {
46  
47  	private final static PGGeometryValueExtractor<Geometry> PG_GEOMETRY_EXTRACTOR = new PGGeometryValueExtractor<>(GeometryJavaTypeDescriptor.INSTANCE, PGGeometryTypeDescriptor.INSTANCE);
48  
49  	private static final Log LOG = LogFactory.getLog(ConvertPostgis2WktGeometryInterceptor.class);
50  
51  	private int columnIndex;
52  
53  	/**
54  	 * <p>
55  	 * Constructor for ConvertPostgis2WktGeometryInterceptor.
56  	 * </p>
57  	 *
58  	 * @param columnIndex
59  	 *            a int.
60  	 */
61  	public ConvertPostgis2WktGeometryInterceptor(int columnIndex) {
62  		super();
63  		setEnableOnWrite(true);
64  		setEnableOnRead(true);
65  		this.columnIndex = columnIndex;
66  	}
67  
68  	/** {@inheritDoc} */
69  	@Override
70  	public SynchroInterceptorBase clone() {
71  		return new ConvertPostgis2WktGeometryInterceptor(columnIndex);
72  	}
73  
74  	/** {@inheritDoc} */
75  	@Override
76  	protected void doOnWrite(Object[] data, List<Object> pk, SynchroTableDao sourceDao, SynchroTableDao targetDao, SynchroOperationRepository buffer,
77  			boolean insert) {
78  		Object position = data[columnIndex];
79  		if (position != null && !(position instanceof String)) {
80  			Geometry geometry = null;
81  			try {
82  				geometry = PG_GEOMETRY_EXTRACTOR.toJTS(position);
83  			} catch (IllegalArgumentException iae) {
84  				LOG.warn("the position is unreadable : " + iae.getLocalizedMessage());
85  			}
86  			if (geometry != null) {
87  				String wtkGeometry = Geometries.getWKTString(geometry);
88  				data[columnIndex] = wtkGeometry.replaceAll(", ", ",");
89  			} else {
90  				data[columnIndex] = null;
91  			}
92  		}
93  	}
94  
95  	/** {@inheritDoc} */
96  	@Override
97  	protected void doOnRead(Object[] data, SynchroTableDao sourceDao, SynchroTableDao targetDao) {
98  		Object position = data[columnIndex];
99  
100 		if (position != null && !(position instanceof String)) {
101 			Geometry geometry = PG_GEOMETRY_EXTRACTOR.toJTS(position);
102 			String wtkGeometry = Geometries.getWKTString(geometry);
103 			data[columnIndex] = wtkGeometry.replaceAll(", ", ",");
104 		}
105 	}
106 
107 	/* -- Internal methods -- */
108 
109 }