View Javadoc

1   package migratool.datasources.geometric.textFile;
2   
3   import igis.datasources.Registry;
4   
5   import java.io.BufferedWriter;
6   import java.io.FileWriter;
7   import java.io.IOException;
8   import java.util.ArrayList;
9   import java.util.Iterator;
10  
11  import migratool.datasources.AbstractDestinationDataSource;
12  import migratool.datasources.DataSourceException;
13  import migratool.datasources.MigDestinationDataSourceException;
14  import migratool.model.BDdescription;
15  
16  import org.apache.log4j.Logger;
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 class GeomTextFileDestinationDataSource extends
44  		AbstractDestinationDataSource {
45  
46  	static Logger logger = 
47  		Logger.getLogger(GeomTextFileDestinationDataSource.class.getName());
48  	
49  	private BufferedWriter outFile = null;
50  
51  	/**
52  	 * @param destinationDescrition
53  	 * @throws DataSourceException
54  	 */
55  	public GeomTextFileDestinationDataSource(BDdescription destinationDescrition) throws DataSourceException {
56  		this.destinationDescrition = destinationDescrition;
57  		try {
58  			// abrir o arquivo
59  			outFile = 
60  				new BufferedWriter(
61  						new FileWriter(destinationDescrition.getTableName()+".sql"));
62  		} catch (IOException e) {
63  			throw new MigDestinationDataSourceException(e);
64  		}
65  		
66  		logger.debug("abriu o arquivo");
67  	}
68  
69  	public void addData(ArrayList data) throws MigDestinationDataSourceException {
70  		
71  		try {
72  
73  			int quantFields = ((Registry) data.get(0)).getFieldsNames().size();
74  			logger.debug("quantFields - " + quantFields);
75  			
76  			Iterator itLines = data.iterator();
77  			int line = 1;
78  			Object insertValue = null;
79  			while (itLines.hasNext()) {
80  				String consulta =
81  					"INSERT INTO "
82  						+ destinationDescrition.getTableName()
83  						+ " VALUES ( ";
84  				int srid = -1;
85  				Registry currentLine = (Registry) itLines.next();				
86  				for (int parameter = 0;	parameter < currentLine.getFieldsNames().size(); parameter++) {
87  					insertValue = currentLine.getValue(parameter+1);
88  					
89  					if (insertValue == null) {
90  						insertValue = "null";
91  						logger.debug("addData() - valor insertValue eh null");
92  					}
93  					
94  					logger.debug("addData() - parameter: " + parameter + " valor: " + (insertValue));
95  					
96  					if (destinationDescrition.getPositionGeometryField() == (parameter+1)){
97  						
98  						logger.info("addData() - o q vai ser inserido: " + insertValue.toString());
99  						//Obtendo o SRID oracle
100 						consulta +=	insertValue.toString() + ", ";				
101 						logger.info("SRID da geometria " + srid);
102 						logger.debug("Parametro  " + parameter);
103 						
104 					} else {
105 						
106 						logger.debug("ParametroNG  " + parameter);
107 						
108 						if(!insertValue.equals("null")) {
109 						
110 							try {
111 								Double.parseDouble(insertValue.toString());
112 							} catch (NumberFormatException e) {
113 								insertValue = "'" + insertValue + "'";
114 							}
115 						}
116 						
117 						consulta += insertValue + ", ";
118 					}
119 				}
120 				
121 				/*
122 				 * Retirando a �ltima v�rgula e acrescentando ); no final da
123 				 * consulta
124 				 */
125 				consulta = consulta.substring(0,consulta.length() - 2);
126 				consulta += ");";
127 				logger.debug("addData() - consulta: " + consulta);
128 				
129 				// escrever no arquivo
130 				outFile.write(consulta);
131 				outFile.newLine();
132 	
133 				logger.info("addData() - adicionado linha " + line);
134 	
135 				line++;
136 			}
137 			
138 		} catch(IOException ioExcep) {
139 			throw new MigDestinationDataSourceException(ioExcep);
140 		} 
141 		
142 	}
143 	
144 	/**
145 	 * 
146 	 */
147 	public void closeConnetion() throws MigDestinationDataSourceException {
148 		try {
149 			outFile.close();
150 		} catch (IOException ioExcep) {
151 			throw new MigDestinationDataSourceException(ioExcep);
152 		}
153 	}
154 
155 	public String toString(){
156 		return "GeomTextFileDestinationDataSource";
157 	}
158 }