View Javadoc

1   package migratool.datasources.geometric;
2   
3   import igis.datasources.Registry;
4   
5   import java.sql.PreparedStatement;
6   import java.sql.SQLException;
7   import java.util.ArrayList;
8   import java.util.Iterator;
9   import java.io.File;
10  
11  import migratool.access.Connector;
12  import migratool.datasources.AbstractDestinationDataSource;
13  import migratool.datasources.MigDestinationDataSourceException;
14  import migratool.model.BDdescription;
15  import migratool.ora2pgsql.OracleGeometryParser;
16  import migratool.ora2pgsql.SRIDConverter;
17  import oracle.sdoapi.geom.Geometry;
18  
19  import org.apache.log4j.Logger;
20  
21  /**
22   * 
23   * MIGRATOOL Program to migrate spatial databsets.
24   * Copyright (C) 2007 Fábio Luiz Leite Júnior
25   * Universidade Federal de Campina Grande
26   * contact: fabioleite@gmail.com
27   *
28   * This program is free software; you can redistribute it and/or
29   * modify it under the terms of the GNU General Public License
30   * as published by the Free Software Foundation; either version 2
31   * of the License, or (at your option) any later version.
32   *
33   * This program is distributed in the hope that it will be useful,
34   * but WITHOUT ANY WARRANTY; without even the implied warranty of
35   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36   * GNU General Public License for more details.
37   *
38   * You should have received a copy of the GNU General Public License
39   * along with this program; if not, write to the Free Software
40   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
41   *
42   * @author fabio
43   *
44   */
45  
46  public class GeometricDestinationDataSource	extends AbstractDestinationDataSource {
47  
48  	static Logger logger = Logger.getLogger(GeometricDestinationDataSource.class.getName());	
49  	private OracleGeometryParser ora2pg = new OracleGeometryParser();
50  
51  	/**
52  	 * @param destinationDescrition
53  	 */
54  	public GeometricDestinationDataSource(BDdescription destinationDescrition) {
55  		this.destinationDescrition = destinationDescrition;
56  		dbAccess = Connector.getInstance();
57  		dbConnection = dbAccess.getConnection(destinationDescrition);
58  	}
59  
60  	public void addData(ArrayList data) throws MigDestinationDataSourceException {
61  		
62  		try {
63  			
64  			int quantFields = ((Registry) data.get(0)).getFieldsNames().size();
65  			logger.debug("quantFields - " + quantFields);
66  			String variables = generateVariables(quantFields);
67  			PreparedStatement pst = null;
68  			String consulta =
69  				"INSERT INTO "
70  					+ destinationDescrition.getTableName()
71  					+ " VALUES "
72  					+ variables;
73  			pst = dbConnection.prepareStatement(consulta);
74  			Iterator itLines = data.iterator();
75  			int line = 1;
76  			Object insertValue = null;
77  			StringBuffer pgGeometry = null;
78  			while (itLines.hasNext()) {
79  				int srid = -1;
80  				Registry currentLine = (Registry) itLines.next();
81  				for (int parameter = 0;	parameter < currentLine.getFieldsNames().size(); parameter++) {
82  					insertValue = currentLine.getValue(parameter+1);
83  	
84  					if (insertValue == null) {
85  						insertValue = "null";
86  						logger.debug("addData() - valor insertValue eh null");
87  					}
88  					logger.debug("addData() - parameter: " 
89  																	+ parameter + " valor: " + (insertValue));
90  					if (destinationDescrition.getPositionGeometryField() == (parameter+1)){
91  						logger.info("addData() - o q vai ser inserido: " + ((Geometry)insertValue).toString());
92  						//Obtendo o SRID oracle
93  						srid = ((Geometry)insertValue).getSpatialReference().getID();
94  						//Convertendo o SRID para o Postgis
95  						srid = SRIDConverter.getSRID(SRIDConverter.ORACLE, SRIDConverter.POSTGIS, srid);
96  						logger.info("SRID da geometria " + srid);
97  						pgGeometry = ora2pg.parse((Geometry)insertValue);
98  						logger.info("addData() - Convertido para: " + pgGeometry.toString());
99  						logger.debug("Parametro  " + parameter);
100 						pst.setObject(parameter+1, pgGeometry.toString());
101 					}else{
102 						logger.debug("ParametroNG  " + parameter);
103 						pst.setObject(parameter+1, insertValue);
104 					}
105 					
106 				}
107 				logger.debug("addData() - consulta: " + pst.toString());
108 				String consultaFinal = convertQuery(pst.toString(), srid);
109 				logger.debug("addData() - consultaFinal: " + consultaFinal);
110 				pst.executeUpdate(consultaFinal);
111 				logger.info("addData() - adicionado linha " + line);
112 				pst = dbConnection.prepareStatement(consulta);
113 				line++;
114 			}
115 	
116 			logger.info("addData() - consulta: " + consulta);
117 			dbConnection.commit();
118 		} catch (SQLException excep) {
119 			throw new MigDestinationDataSourceException(excep);
120 		}
121 
122 	}
123 	
124 	/**
125 	 * @param string
126 	 * @return
127 	 */
128 	private String convertQuery(String string, int srid ) {
129 		if ( srid == -1){
130 			return string;
131 		}
132 		String sridText = srid + ""; 
133 		string = string.replaceAll(File.separator+File.separator,"");
134 		
135 		string = string.replaceAll("'GeometryFromText","GeometryFromText");
136 		if ( string.endsWith(")')")){
137 			string = string.substring(0, string.length()-3) +"))";	
138 		}
139 		int index =string.indexOf(sridText+")'"); 
140 		if (  index != -1 ){
141 			String before = string.substring(0,index);
142 			String after = string.substring(index+7,string.length());
143 			string = before + sridText +")" + after;
144 		}
145 		return string;
146 	}
147 
148 	public String toString(){
149 		return "GeometricDestinationDataSource";
150 	}
151 }