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