View Javadoc

1   /*
2    * Created on 31/05/2004
3    */
4   package migratool.datasources;
5   
6   import java.sql.Connection;
7   import java.sql.ResultSet;
8   import java.sql.ResultSetMetaData;
9   import java.sql.SQLException;
10  import java.sql.Statement;
11  import java.util.Collection;
12  import java.util.Iterator;
13  import java.util.Vector;
14  
15  import migratool.access.Connector;
16  import migratool.model.BDdescription;
17  
18  /**
19   * 
20   * MIGRATOOL Program to migrate spatial databsets.
21   * Copyright (C) 2007 Fábio Luiz Leite Júnior
22   * Universidade Federal de Campina Grande
23   * contact: fabioleite@gmail.com
24   *
25   * This program is free software; you can redistribute it and/or
26   * modify it under the terms of the GNU General Public License
27   * as published by the Free Software Foundation; either version 2
28   * of the License, or (at your option) any later version.
29   *
30   * This program is distributed in the hope that it will be useful,
31   * but WITHOUT ANY WARRANTY; without even the implied warranty of
32   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33   * GNU General Public License for more details.
34   *
35   * You should have received a copy of the GNU General Public License
36   * along with this program; if not, write to the Free Software
37   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
38   *
39   * @author fabio
40   *
41   */
42  
43  public abstract class AbstractOriginDataSource {
44  
45  	protected Connector dbAccess;
46  	protected Connection dbConnection;
47  	protected BDdescription originDescription;
48  	
49  	/**
50  	* @param fromDescrition
51  	*/
52  	public AbstractOriginDataSource(BDdescription fromDescrition) {
53  		this.originDescription = fromDescrition;
54  		dbAccess = Connector.getInstance();
55  		dbConnection = dbAccess.getConnection(originDescription);
56  	}
57  
58  	public boolean testDataBase() {
59  		boolean success = true;
60  		ResultSet rs = null;
61  		Statement stmt = null;
62  		if (dbConnection == null)
63  			success = false;
64  
65  		try {
66  			stmt = dbConnection.createStatement();
67  			rs =
68  				stmt.executeQuery(
69  					"select * from " + originDescription.getTableName());
70  		} catch (SQLException e) {
71  			e.printStackTrace();
72  			success = false;
73  		}
74  		if (rs == null)
75  			success = false;
76  
77  		try {
78  			rs.close();
79  			stmt.close();
80  		} catch (SQLException e1) {
81  			e1.printStackTrace();
82  			success = false;
83  		}
84  
85  		return success;
86  	}
87  	/**
88  	 * Retorna um Tabela l�gica contendo na os metadados e os dados
89  	 * @return logicTable contendo os resultados como atributos.
90  	 */
91  	public abstract LogicTable getData();
92  	
93  	/**
94  	 * Retorna uma cole��o de registros semelhante ao iGIS
95  	 * @return Collection contendo os resultados como atributos.
96  	 */
97  	public abstract Collection getDataTable() throws DataSourceException, igis.datasources.DataSourceException;
98  
99  	protected static Vector getProximaLinha(ResultSet rs, Vector rsmd)
100 		throws SQLException {
101 		Vector linhaCorrente = new Vector();
102 		//System.out.println(rsmd.size());
103 		Iterator itRsmd = rsmd.iterator();
104 		while (itRsmd.hasNext()) {
105 			linhaCorrente.addElement(rs.getObject((String) itRsmd.next()));
106 		}
107 		return linhaCorrente;
108 	}
109 
110 	/**
111 	 * Fecha a conec��o com o banco de dados.
112 	 */
113 	public void closeConnection() throws SQLException {
114 		dbConnection.close();
115 	}
116 
117 	public ResultSetMetaData getTableMetaData() {
118 		ResultSet rs = null;
119 		Statement stmt = null;
120 		ResultSetMetaData rsmd = null;
121 
122 		try {
123 			stmt = dbConnection.createStatement();
124 			rs =
125 				stmt.executeQuery(
126 					"select * from " + originDescription.getTableName());
127 			rsmd = rs.getMetaData();
128 		} catch (SQLException e) {
129 			e.printStackTrace();
130 		}
131 		return rsmd;
132 	}
133 
134 }