View Javadoc
1   package fr.ifremer.quadrige2.synchro.intercept.referential;
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   * <p>
37   * GeometryTablesInterceptor class.
38   * </p>
39   * 
40   */
41  public class GeometryTablesInterceptor extends AbstractReferentialInterceptor {
42  
43  	/** {@inheritDoc} */
44  	@Override
45  	public boolean doApply(SynchroDatabaseMetadata meta, TableMetadata table) {
46  		String tableName = table.getName().toUpperCase();
47  		return tableName.endsWith("_AREA")
48  				|| tableName.endsWith("_LINE")
49  				|| tableName.endsWith("_POINT");
50  	}
51  
52  	/**
53  	 * <p>
54  	 * handleLoadTable.
55  	 * </p>
56  	 * 
57  	 * @param e
58  	 *            a {@link fr.ifremer.common.synchro.meta.event.LoadTableEvent} object.
59  	 */
60  	@Subscribe
61  	public void handleLoadTable(LoadTableEvent e) {
62  
63  		SynchroTableMetadata table = e.table;
64  
65  		int columnPositionIndex = getPositionColumnIndex(table);
66  		if (columnPositionIndex == -1) {
67  			return;
68  		}
69  
70  		SynchroDirection direction = getConfig().getDirection();
71  
72  		// Import: Server DB -> Temp DB
73  		// Import: Server DB -> Local DB (e.g. from command line, or when running without synchronization server)
74  		if (direction == SynchroDirection.IMPORT_NO_TEMP
75  				|| direction == SynchroDirection.IMPORT_SERVER2TEMP) {
76  
77  			// Add a interceptor SDO -> WKT
78  			ConvertSdo2WktGeometryInterceptor sdoGeometryInterceptor = new ConvertSdo2WktGeometryInterceptor(columnPositionIndex);
79  			table.addInterceptor(sdoGeometryInterceptor);
80  		}
81  
82  		// Export: Temp DB -> Server DB
83  		// Export: Local DB -> Server DB (=Direct export - e.g. from command line, or when running without
84  		// synchronization server)
85  		else if (direction == SynchroDirection.EXPORT_NO_TEMP
86  				|| direction == SynchroDirection.EXPORT_TEMP2SERVER) {
87  
88  			// Add a interceptor WKT -> SDO
89  			ConvertWkt2SdoGeometryInterceptor sdoGeometryInterceptor = new ConvertWkt2SdoGeometryInterceptor(columnPositionIndex);
90  			table.addInterceptor(sdoGeometryInterceptor);
91  		}
92  	}
93  
94  	/* -- Internal methods -- */
95  
96  	private int getPositionColumnIndex(SynchroTableMetadata table) {
97  		for (String columnName : table.getColumnNames()) {
98  			if (columnName.toLowerCase().endsWith("_position")) {
99  				return table.getColumnIndex(columnName);
100 			}
101 		}
102 		return -1;
103 	}
104 }