View Javadoc

1   package migratool.datasources;
2   
3   import java.sql.PreparedStatement;
4   import java.sql.SQLException;
5   import java.util.ArrayList;
6   import java.util.Iterator;
7   import java.util.Vector;
8   
9   import migratool.access.Connector;
10  import migratool.model.BDdescription;
11  
12  import org.apache.log4j.Logger;
13  
14  /**
15   * 
16   * MIGRATOOL Program to migrate spatial databsets.
17   * Copyright (C) 2007 Fábio Luiz Leite Júnior
18   * Universidade Federal de Campina Grande
19   * contact: fabioleite@gmail.com
20   *
21   * This program is free software; you can redistribute it and/or
22   * modify it under the terms of the GNU General Public License
23   * as published by the Free Software Foundation; either version 2
24   * of the License, or (at your option) any later version.
25   *
26   * This program is distributed in the hope that it will be useful,
27   * but WITHOUT ANY WARRANTY; without even the implied warranty of
28   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29   * GNU General Public License for more details.
30   *
31   * You should have received a copy of the GNU General Public License
32   * along with this program; if not, write to the Free Software
33   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
34   *
35   * @author fabio
36   *
37   */
38  
39  public class DestinationDataSource extends AbstractDestinationDataSource {
40  
41  	static Logger logger = Logger.getLogger(DestinationDataSource.class);
42  
43  	/**
44  	 * @param destinationDescrition
45  	 */
46  	public DestinationDataSource(BDdescription destinationDescrition) {
47  		this.destinationDescrition = destinationDescrition;
48  		dbAccess = Connector.getInstance();
49  		dbConnection = dbAccess.getConnection(destinationDescrition);
50  	}
51  
52  	
53  	public void addData(ArrayList data) throws MigDestinationDataSourceException {
54  		
55  		try {
56  		
57  			int quantFields = ((Vector)data.get(0)).size();
58  			String variables = generateVariables(quantFields);
59  			PreparedStatement pst = null;
60  			String consulta = "INSERT INTO " + destinationDescrition.getTableName() + " VALUES " + variables;
61  			dbConnection.setAutoCommit(false);
62  			pst = dbConnection.prepareStatement(consulta);
63  			logger.debug("DestinationDataSource.addData() - consulta: " + consulta);
64  			Iterator itLines = data.iterator();
65  			int line = 1;
66  			Object insertValue = null;
67  			while(itLines.hasNext()){
68  				Vector currentLine = (Vector) itLines.next();
69  				for(int parameter = 1; parameter <= currentLine.size(); parameter++){
70  					insertValue = currentLine.elementAt(parameter-1);
71  	
72  //					if(insertValue == null){
73  //						insertValue = "null";
74  //						logger.debug("DestinationDataSource.addData() - valor insertValue eh null");
75  //					}
76  					pst.setObject(parameter,insertValue);
77  					logger.debug("DestinationDataSource.addData() - parameter: " + parameter +
78  													" valor: " + (insertValue));
79  				}
80  	
81  				pst.addBatch();
82  				logger.info("DestinationDataSource.addData() - adicionado linha " + line);
83  				line++;
84  			}
85  			logger.info("DestinationDataSource.addData() - consulta: " + consulta);
86  			pst.executeBatch();
87  			dbConnection.commit();
88  		} catch(SQLException sqlExcep) {
89  			throw new MigDestinationDataSourceException(sqlExcep);			
90  		}
91  		
92  	}
93  	public String toString(){
94  		return "DestinationDataSource";
95  	}
96  	
97  }