View Javadoc

1   package migratool.datasources.geometric;
2   
3   
4   import igis.datasources.Field;
5   import igis.datasources.InvalidFieldNumberException;
6   import igis.datasources.Registry;
7   import igis.datasources.Unit;
8   
9   import java.sql.ResultSet;
10  import java.sql.ResultSetMetaData;
11  import java.sql.SQLException;
12  import java.text.SimpleDateFormat;
13  import java.util.Collection;
14  import java.util.Iterator;
15  import java.util.Vector;
16  
17  import migratool.datasources.AbstractOriginDataSource;
18  import migratool.datasources.DataSourceException;
19  import migratool.datasources.LogicTable;
20  import migratool.model.BDdescription;
21  import oracle.sdoapi.OraSpatialManager;
22  import oracle.sdoapi.adapter.GeometryAdapter;
23  import oracle.sdoapi.adapter.GeometryInputTypeNotSupportedException;
24  import oracle.sdoapi.geom.InvalidGeometryException;
25  import oracle.sql.STRUCT;
26  
27  import org.apache.log4j.Logger;
28  
29  /**
30   * 
31   * MIGRATOOL Program to migrate spatial databsets.
32   * Copyright (C) 2007 Fábio Luiz Leite Júnior
33   * Universidade Federal de Campina Grande
34   * contact: fabioleite@gmail.com
35   *
36   * This program is free software; you can redistribute it and/or
37   * modify it under the terms of the GNU General Public License
38   * as published by the Free Software Foundation; either version 2
39   * of the License, or (at your option) any later version.
40   *
41   * This program is distributed in the hope that it will be useful,
42   * but WITHOUT ANY WARRANTY; without even the implied warranty of
43   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44   * GNU General Public License for more details.
45   *
46   * You should have received a copy of the GNU General Public License
47   * along with this program; if not, write to the Free Software
48   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
49   *
50   * @author fabio
51   *
52   */
53  
54  public class GeometricOriginDataSource extends AbstractOriginDataSource {
55  
56  	static Logger logger = Logger.getLogger(GeometricOriginDataSource.class);
57  	
58  	public GeometricOriginDataSource(BDdescription fromDescrition) {
59  		super(fromDescrition);
60  	}
61  
62  	public Collection getDataTable() throws DataSourceException, igis.datasources.DataSourceException{
63  		ResultSet rs = null;
64  		String cons = "select * from " + originDescription.getTableName();
65  		try {
66  			rs = dbConnection.createStatement().executeQuery(cons);
67  		} catch (SQLException e) {
68  			throw new DataSourceException("Problemas na consulta: \n" + cons,e);
69  		}
70  
71  		ResultSetMetaData rsmd = null;
72  		try {
73  			rsmd = rs.getMetaData();
74  		} catch (SQLException e) {
75  			throw new DataSourceException(
76  				"Problemas para obter os metadados da "
77  					+ " consulta: \n"
78  					+ cons,e);
79  		}
80  
81  		Iterator fieldsIt = null;
82  		Collection fields = null;
83  
84  		try {
85  
86  			fields = loadFields(rsmd);
87  			fieldsIt = fields.iterator();
88  		} catch (SQLException e) {
89  			throw new DataSourceException(
90  				"Problemas para preencher os campos da "
91  					+ " consulta: \n"
92  					+ cons,
93  				e);
94  		}
95  
96  		Unit result = new Unit();
97  
98  		result.setName(cons);
99  
100 		while (fieldsIt.hasNext()) {
101 			Field field = (Field) fieldsIt.next();
102 			result.defineField(field);
103 		}
104 
105 		Collection regs = createCollection();
106 
107 		try {
108 			while (rs.next()) {
109 				Registry reg = new Registry(result);
110 
111 				fieldsIt = fields.iterator();
112 
113 				boolean ok = true;
114 
115 				while (fieldsIt.hasNext()) {
116 					Field field = (Field) fieldsIt.next();
117 					int type = field.getType();
118 
119 					switch (type) {
120 
121 						case igis.datasources.Types.SPATIAL :
122 
123 							oracle.sdoapi.geom.Geometry geom = null;
124 							Object geomObj = null;
125 							try {
126 
127 								geomObj = rs.getObject(field.getName());
128 								if (geomObj == null)
129 									break;
130 								geom = getGeometryAdapter().importGeometry(geomObj);
131 							} catch (InvalidGeometryException e) {
132 								logger.fatal(
133 									"Geometria Inv�lida: "
134 										+ e.getMessage()
135 										+ "\nNo registro: "
136 										+ reg.toString());
137 								ok = false;
138 							} catch (GeometryInputTypeNotSupportedException e) {
139 								logger.fatal(
140 									"Geometria N�o suportada:"
141 										+ e.getMessage());
142 								ok = false;
143 							}
144 
145 							reg.setObject(field.getName(), geom);
146 							break;
147 
148 						case igis.datasources.Types.INTEGER :
149 							reg.setInt(
150 								field.getName(),
151 								rs.getInt(field.getName()));
152 							break;
153 						case igis.datasources.Types.REAL :
154 							double value = rs.getDouble(field.getName());
155 							int valueInt = (int) value;
156 							if (value - valueInt == 0)
157 								reg.setInteger(field.getName(), valueInt);
158 							else
159 								reg.setDouble(field.getName(), value);
160 							break;
161 						case igis.datasources.Types.CHARACTER :
162 							reg.setString(
163 								field.getName(),
164 								rs.getString(field.getName()));
165 							break;
166 						case igis.datasources.Types.DATE :
167 							SimpleDateFormat sdf =
168 								new SimpleDateFormat("dd/MM/yyyy");
169 							reg.setString(
170 								field.getName(),
171 								sdf.format(rs.getDate(field.getName())));
172 							break;
173 
174 					}
175 				}
176 
177 				if (ok) //TODO Refactoring
178 					regs.add(reg);
179 			}
180 		} catch (SQLException e) {
181 			throw new DataSourceException(
182 				"Problemas para preencher os dados da "
183 					+ " consulta: \n"
184 					+ cons,e);
185 		}
186 
187 		return regs;
188 	}
189 	
190 	/**
191 	 * M�todo que usa um adaptador de geometrias para o Oracle
192 	 * @param dataSource A fonte de dados
193 	 * @param con A conex�o
194 	 * @return O adaptador
195 	 */
196 	public GeometryAdapter getGeometryAdapter() {
197 		return OraSpatialManager.getGeometryAdapter("SDO", "8.1.6", STRUCT.class, STRUCT.class, null, dbConnection);
198 	}
199 
200 	private Collection createCollection() {
201 		return new Vector();
202 	}
203 
204 	/**
205 	 * Retorna o conjunto de fields de um ResultSetMetaData
206 	 * @param rsmd Os metadados de uma consulta
207 	 * @return Iterador com objetos do tipo field
208 	 * @throws SQLException
209 	 */
210 	public Collection loadFields(ResultSetMetaData rsmd) throws SQLException {
211 		Collection fields = new Vector();
212 		for (int i = 1; i <= rsmd.getColumnCount(); i++) {
213 			Field field = new Field();
214 			field.setName(rsmd.getColumnName(i));
215 			try {
216 				field.setNumber(i);
217 			} catch (InvalidFieldNumberException e) {
218 				e.printStackTrace();
219 			}
220 
221 			int fieldType = -1;
222 			int colType = rsmd.getColumnType(i);
223 
224 			switch (colType) {
225 
226 				case java.sql.Types.DOUBLE :
227 				case java.sql.Types.FLOAT :
228 				case java.sql.Types.DECIMAL :
229 				case java.sql.Types.NUMERIC :
230 					fieldType = igis.datasources.Types.REAL;
231 					break;
232 				case java.sql.Types.INTEGER :
233 				case java.sql.Types.SMALLINT :
234 					fieldType = igis.datasources.Types.INTEGER;
235 					break;
236 				case java.sql.Types.VARCHAR :
237 				case java.sql.Types.CHAR :
238 				case java.sql.Types.LONGVARCHAR :
239 					fieldType = igis.datasources.Types.CHARACTER;
240 					break;
241 				case java.sql.Types.DATE :
242 				case java.sql.Types.TIME :
243 				case java.sql.Types.TIMESTAMP :
244 					fieldType = igis.datasources.Types.DATE;
245 					break;
246 				case java.sql.Types.BINARY :
247 				case java.sql.Types.BLOB :
248 				case java.sql.Types.LONGVARBINARY :
249 				case java.sql.Types.VARBINARY :
250 					fieldType = igis.datasources.Types.BYTE;
251 					break;
252 				default :
253 					fieldType = igis.datasources.Types.SPATIAL;
254 					break;
255 			}
256 
257 			field.setType(fieldType);
258 
259 			fields.add(field);
260 		}
261 
262 		return fields;
263 	}
264 
265 
266 	public String toString() {
267 		return "GeometricOriginDataSource";
268 	}
269 
270 	public LogicTable getData() {
271 		return null;
272 	}
273 
274 }