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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
53
54
55 public GeomTextFileDestinationDataSource(BDdescription destinationDescrition) throws DataSourceException {
56 this.destinationDescrition = destinationDescrition;
57 try {
58
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
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
123
124
125 consulta = consulta.substring(0,consulta.length() - 2);
126 consulta += ");";
127 logger.debug("addData() - consulta: " + consulta);
128
129
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 }