View Javadoc
1   package fr.ifremer.quadrige2.synchro.intercept.data;
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.google.common.eventbus.Subscribe;
27  import fr.ifremer.common.synchro.meta.SynchroDatabaseMetadata;
28  import fr.ifremer.common.synchro.meta.SynchroTableMetadata;
29  import fr.ifremer.common.synchro.meta.event.LoadTableEvent;
30  import fr.ifremer.quadrige2.synchro.intercept.data.internal.ConvertSdo2WktGeometryInterceptor;
31  import fr.ifremer.quadrige2.synchro.intercept.data.internal.ConvertWkt2SdoGeometryInterceptor;
32  import fr.ifremer.quadrige2.synchro.service.SynchroDirection;
33  import org.hibernate.tool.hbm2ddl.TableMetadata;
34  
35  /**
36   * Manage geometry columns rewriting
37   * 
38   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
39   * @since 1.0
40   */
41  public class GeometryTablesInterceptor extends AbstractDataInterceptor {
42  
43  	/**
44  	 * <p>
45  	 * Constructor for GeometryTablesInterceptor.
46  	 * </p>
47  	 */
48  	public GeometryTablesInterceptor() {
49  		// Enable only when using a server (e.g. Oracle)
50  		super(SynchroDirection.IMPORT_SERVER2TEMP,
51  				SynchroDirection.EXPORT_TEMP2SERVER);
52  	}
53  
54  	/** {@inheritDoc} */
55  	@Override
56  	public boolean doApply(SynchroDatabaseMetadata meta, TableMetadata table) {
57  		String tableName = table.getName();
58  		return tableName.endsWith("_AREA")
59  				|| tableName.endsWith("_LINE")
60  				|| tableName.endsWith("_POINT");
61  	}
62  
63  	/**
64  	 * <p>
65  	 * handleLoadTable.
66  	 * </p>
67  	 * 
68  	 * @param e
69  	 *            a {@link fr.ifremer.common.synchro.meta.event.LoadTableEvent} object.
70  	 */
71  	@Subscribe
72  	public void handleLoadTable(LoadTableEvent e) {
73  
74  		SynchroTableMetadata table = e.table;
75  		SynchroDirection direction = getConfig().getDirection();
76  		int columnPositionIndex = getPositionColumnIndex(table);
77  		if (columnPositionIndex == -1) {
78  			return;
79  		}
80  
81  		if (direction == SynchroDirection.IMPORT_SERVER2TEMP) {
82  
83  			// Add a converter (from SDO_GEOMETRY to WKT string)
84  			ConvertSdo2WktGeometryInterceptor sdoGeometryInterceptor = new ConvertSdo2WktGeometryInterceptor(columnPositionIndex);
85  			table.addInterceptor(sdoGeometryInterceptor);
86  		}
87  		else if (direction == SynchroDirection.EXPORT_TEMP2SERVER) {
88  
89  			// Add a converter (from WKT to SDO_GEOMETRY)
90  			ConvertWkt2SdoGeometryInterceptor sdoGeometryInterceptor = new ConvertWkt2SdoGeometryInterceptor(columnPositionIndex);
91  			table.addInterceptor(sdoGeometryInterceptor);
92  		}
93  	}
94  
95  	/* -- Internal methods -- */
96  
97  	private int getPositionColumnIndex(SynchroTableMetadata table) {
98  		for (String columnName : table.getColumnNames()) {
99  			if (columnName.endsWith("_position")) {
100 				return table.getColumnIndex(columnName);
101 			}
102 		}
103 		return -1;
104 	}
105 }