View Javadoc
1   package fr.ifremer.quadrige3.synchro.intercept.referential;
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.google.common.eventbus.Subscribe;
27  import fr.ifremer.common.synchro.intercept.SynchroInterceptor;
28  import fr.ifremer.common.synchro.meta.SynchroDatabaseMetadata;
29  import fr.ifremer.common.synchro.meta.SynchroTableMetadata;
30  import fr.ifremer.common.synchro.meta.event.LoadTableEvent;
31  import fr.ifremer.quadrige3.core.dao.technical.Daos;
32  import fr.ifremer.quadrige3.synchro.intercept.geometry.ConvertPostgis2WktGeometryInterceptor;
33  import fr.ifremer.quadrige3.synchro.intercept.geometry.ConvertSdo2WktGeometryInterceptor;
34  import fr.ifremer.quadrige3.synchro.intercept.geometry.ConvertWkt2PostgisGeometryInterceptor;
35  import fr.ifremer.quadrige3.synchro.intercept.geometry.ConvertWkt2SdoGeometryInterceptor;
36  import fr.ifremer.quadrige3.synchro.service.SynchroDirection;
37  import org.hibernate.tool.hbm2ddl.TableMetadata;
38  
39  /**
40   * <p>
41   * GeometryTablesInterceptor class.
42   * </p>
43   * 
44   */
45  public class GeometryTablesInterceptor extends AbstractReferentialInterceptor {
46  
47  	/** {@inheritDoc} */
48  	@Override
49  	public boolean doApply(SynchroDatabaseMetadata meta, TableMetadata table) {
50  		String tableName = table.getName().toUpperCase();
51  		return tableName.endsWith("_AREA")
52  				|| tableName.endsWith("_LINE")
53  				|| tableName.endsWith("_POINT");
54  	}
55  
56  	/**
57  	 * <p>
58  	 * handleLoadTable.
59  	 * </p>
60  	 * 
61  	 * @param e
62  	 *            a {@link fr.ifremer.common.synchro.meta.event.LoadTableEvent} object.
63  	 */
64  	@Subscribe
65  	public void handleLoadTable(LoadTableEvent e) {
66  
67  		SynchroTableMetadata table = e.table;
68  
69  		int columnPositionIndex = getPositionColumnIndex(table);
70  		if (columnPositionIndex == -1) {
71  			return;
72  		}
73  
74  		SynchroDirection direction = getConfig().getDirection();
75  		SynchroInterceptor geometryInterceptor = null;
76  
77  		// Import: Server DB -> Temp DB
78  		// Import: Server DB -> Local DB (e.g. from command line, or when running without synchronization server)
79  		if (direction == SynchroDirection.IMPORT_NO_TEMP
80  				|| direction == SynchroDirection.IMPORT_SERVER2TEMP) {
81  
82  			if (Daos.isOracleDatabase(getSourceJdbcUrl())) {
83  				// Add a interceptor SDO -> WKT
84  				geometryInterceptor = new ConvertSdo2WktGeometryInterceptor(columnPositionIndex);
85  			} else if (Daos.isPostgresqlDatabase(getSourceJdbcUrl())) {
86  				// Add a interceptor Postgis -> WKT
87  				geometryInterceptor = new ConvertPostgis2WktGeometryInterceptor(columnPositionIndex);
88  			}
89  		}
90  
91  		// Export: Temp DB -> Server DB
92  		// Export: Local DB -> Server DB (=Direct export - e.g. from command line, or when running without
93  		// synchronization server)
94  		else if (direction == SynchroDirection.EXPORT_NO_TEMP
95  				|| direction == SynchroDirection.EXPORT_TEMP2SERVER) {
96  
97  			if (Daos.isOracleDatabase(getTargetJdbcUrl())) {
98  				// Add a interceptor WKT -> SDO
99  				geometryInterceptor = new ConvertWkt2SdoGeometryInterceptor(columnPositionIndex);
100 			} else if (Daos.isPostgresqlDatabase(getTargetJdbcUrl())) {
101 				// Add a interceptor WKT -> Postgis
102 				geometryInterceptor = new ConvertWkt2PostgisGeometryInterceptor(columnPositionIndex);
103 			}
104 		}
105 
106 		if (geometryInterceptor != null) {
107 			table.addInterceptor(geometryInterceptor);
108 		}
109 	}
110 
111 	/* -- Internal methods -- */
112 
113 	private int getPositionColumnIndex(SynchroTableMetadata table) {
114 		for (String columnName : table.getColumnNames()) {
115 			if (columnName.toUpperCase().endsWith("_POSITION")) {
116 				return table.getColumnIndex(columnName);
117 			}
118 		}
119 		return -1;
120 	}
121 
122 	private String getSourceJdbcUrl() {
123 		return getConfig().getSource() != null ? getConfig().getSource().getJdbcUrl() : getConfig().getJdbcUrl();
124 	}
125 
126 	private String getTargetJdbcUrl() {
127 		return getConfig().getTarget() != null ? getConfig().getTarget().getJdbcUrl() : getConfig().getJdbcUrl();
128 	}
129 }