View Javadoc

1   package migratool;
2   
3   import igis.datasources.DataSourceException;
4   
5   import java.sql.SQLException;
6   import java.util.ArrayList;
7   import java.util.Collection;
8   
9   import migratool.datasources.AbstractDestinationDataSource;
10  import migratool.datasources.AbstractOriginDataSource;
11  import migratool.datasources.LogicTable;
12  import migratool.datasources.MigDestinationDataSourceException;
13  import migratool.datasources.superDataSourceFactory.DataSourceFactoryIF;
14  import migratool.datasources.superDataSourceFactory.SuperDataSourceFactory;
15  import migratool.definition.parser.MigBean;
16  
17  import org.apache.log4j.Logger;
18  
19  /**
20   * 
21   * MIGRATOOL Program to migrate spatial databsets.
22   * Copyright (C) 2007 Fábio Luiz Leite Júnior
23   * Universidade Federal de Campina Grande
24   * contact: fabioleite@gmail.com
25   *
26   * This program is free software; you can redistribute it and/or
27   * modify it under the terms of the GNU General Public License
28   * as published by the Free Software Foundation; either version 2
29   * of the License, or (at your option) any later version.
30   *
31   * This program is distributed in the hope that it will be useful,
32   * but WITHOUT ANY WARRANTY; without even the implied warranty of
33   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34   * GNU General Public License for more details.
35   *
36   * You should have received a copy of the GNU General Public License
37   * along with this program; if not, write to the Free Software
38   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
39   *
40   * @author fabio
41   * 
42   * Classe responsavel por gerenciar a criaçâo dos beans de descriçâo
43   * dos bancos para a lógica do sistema, terá duas opções xml via métodos
44   * diretos. Caso seja xml deve-se passar o caminho do arquivo, se for do
45   * modo direto deve-se passar as informações para que se crie as descrições.
46   *
47   */
48  
49  public class Migratool {
50  	
51  	private AbstractOriginDataSource originDataSource;
52  	private AbstractDestinationDataSource destinationDataSource;
53  	private DataSourceFactoryIF dataSourceFactory;
54  	private MigBean migDescription;
55  	private BDbeansGenerator generator;
56  	static Logger logger = Logger.getLogger(Migratool.class);
57  	
58  	public Migratool(){
59  		generator = new BDbeansGenerator();
60  	}
61  	
62  	public void init(String path) throws MigratoolException{
63  		migDescription = generator.getDefinitions(path);
64  		
65  		if(migDescription == null)
66  			throw new MigratoolException();
67  			
68  		dataSourceFactory = SuperDataSourceFactory.getInstance(migDescription);
69  		originDataSource = dataSourceFactory.getOriginDataSource();
70  		try {
71  			destinationDataSource = dataSourceFactory.getDestinationDataSource();
72  		} catch (migratool.datasources.DataSourceException e) {
73  			 throw new MigratoolException(e);
74  		}
75  	}
76  	
77  	public boolean testOriginDataSource(){
78  		return originDataSource.testDataBase();
79  	}
80  	
81  	public boolean testDestinationDataSource(){
82  		return destinationDataSource.testDataBase();
83  	}
84  	
85  //	public void createDestinationSchema() throws SQLException{
86  //		ResultSetMetaData rsmd = originDataSource.getTableMetaData();
87  //		destinationDataSource.createSchema(rsmd);
88  //		
89  //	}
90  	
91  	public void migrate() throws DataSourceException, migratool.datasources.DataSourceException{
92  		Collection lTemp2 = null;
93  		LogicTable lTemp = null;
94  		ArrayList lines = null;
95  		if(migDescription.isGeometric()){
96  			logger.debug("Migrando fontes geometricas");
97  			lTemp2 = originDataSource.getDataTable();
98  			lines = new ArrayList(lTemp2);
99  		}else{
100 			lTemp = originDataSource.getData();
101 			logger.info("Migratool.migrate() - Numero de linhas na origem: " + lTemp.getSize());
102 			lines = new ArrayList(lTemp.getLinhas());
103 		}
104 		logger.info("Migratool.migrate() - Numero de linhas em lines: " + lines.size());
105 		destinationDataSource.addData(lines);
106 	}
107 	
108 	public void clearConnections(){
109 		try {
110 			originDataSource.closeConnection();
111 			destinationDataSource.closeConnetion();
112 		} catch (MigDestinationDataSourceException e) {
113 			logger.error(e);
114 		} catch (SQLException sqlex) {
115 			logger.error(sqlex);
116 		}
117 		
118 	}
119 	
120 
121 }