svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libGeocoding / src / org / gvsig / normalization / operations / AbstractNormalization.java @ 29125
History | View | Annotate | Download (9.31 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2008 Prodevelop S.L. main development
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.normalization.operations; |
29 |
|
30 |
import java.sql.Types; |
31 |
import java.text.ParseException; |
32 |
import java.text.SimpleDateFormat; |
33 |
import java.util.ArrayList; |
34 |
import java.util.Date; |
35 |
import java.util.List; |
36 |
|
37 |
import javax.swing.event.ChangeEvent; |
38 |
import javax.swing.event.ChangeListener; |
39 |
|
40 |
import org.gvsig.normalization.pattern.Datevalue; |
41 |
import org.gvsig.normalization.pattern.Decimalvalue; |
42 |
import org.gvsig.normalization.pattern.Element; |
43 |
import org.gvsig.normalization.pattern.Fieldtype; |
44 |
import org.gvsig.normalization.pattern.Integervalue; |
45 |
import org.gvsig.normalization.pattern.Patternnormalization; |
46 |
import org.gvsig.normalization.pattern.Stringvalue; |
47 |
|
48 |
import com.hardcode.gdbms.engine.values.Value; |
49 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
50 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
51 |
|
52 |
/**
|
53 |
* Abstract Normalization Class
|
54 |
*
|
55 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
56 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
|
57 |
*/
|
58 |
|
59 |
public abstract class AbstractNormalization implements Normalization, |
60 |
ChangeListener {
|
61 |
|
62 |
private List listeners = new ArrayList(); |
63 |
protected NormAlgorithm nAlgorithm = null; |
64 |
protected Patternnormalization pattern = null; |
65 |
protected String[] nameNewFields = null; |
66 |
protected int[] posNameNewFields = null; |
67 |
|
68 |
/**
|
69 |
* Constructor
|
70 |
*
|
71 |
* @param pat
|
72 |
* pattern
|
73 |
*/
|
74 |
public AbstractNormalization(Patternnormalization pat) {
|
75 |
this.pattern = pat;
|
76 |
this.nAlgorithm = new NormAlgorithm(pat); |
77 |
this.nAlgorithm.registerListener(this); |
78 |
loadNamesNewFields(); |
79 |
} |
80 |
|
81 |
/**
|
82 |
* Get the pattern
|
83 |
*
|
84 |
* @return pattern
|
85 |
*/
|
86 |
public Patternnormalization getPattern() {
|
87 |
return pattern;
|
88 |
} |
89 |
|
90 |
/**
|
91 |
* Get the number of rows
|
92 |
*
|
93 |
* @return number of rows
|
94 |
*/
|
95 |
public abstract long getRowCount(); |
96 |
|
97 |
/**
|
98 |
* This method formats the date from a date pattern
|
99 |
*
|
100 |
* @see http
|
101 |
* ://java.sun.com/j2se/1.5.0/docs/api/java/text/SimpleDateFormat.html
|
102 |
*
|
103 |
* @param date
|
104 |
* @param pattern
|
105 |
* @return Value
|
106 |
* @throws ParseException
|
107 |
*/
|
108 |
public Value formatDates(String date, String pattern) throws ParseException { |
109 |
|
110 |
Value result = ValueFactory.createNullValue(); |
111 |
SimpleDateFormat sdf = new SimpleDateFormat(pattern); |
112 |
Date fecha = sdf.parse(date);
|
113 |
result = ValueFactory.createValue(fecha); |
114 |
return result;
|
115 |
} |
116 |
|
117 |
/**
|
118 |
* This method in its base implementations returns the same value as the
|
119 |
* rowCount method
|
120 |
*
|
121 |
* @return number of rows estimated
|
122 |
*/
|
123 |
public long getEstimatedRowCount() { |
124 |
return getRowCount();
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* Tasks necessary before normalize
|
129 |
*
|
130 |
* @return process ok
|
131 |
*/
|
132 |
public boolean preProcess() { |
133 |
return false; |
134 |
} |
135 |
|
136 |
/**
|
137 |
* Tasks necessary after normalize
|
138 |
*
|
139 |
* @return process ok
|
140 |
*/
|
141 |
public boolean postProcess() { |
142 |
return false; |
143 |
} |
144 |
|
145 |
/**
|
146 |
* Get the number of the new imported fields
|
147 |
*
|
148 |
* @return number of imported fields
|
149 |
*/
|
150 |
public int numberNewImportedFields() { |
151 |
|
152 |
int contImported = 0; |
153 |
int contTotal = this.pattern.getElements().size(); |
154 |
|
155 |
for (int i = 0; i < contTotal; i++) { |
156 |
if (((Element) this.pattern.getElements().get(i)).getImportfield()) { |
157 |
contImported++; |
158 |
} |
159 |
} |
160 |
return contImported;
|
161 |
} |
162 |
|
163 |
/**
|
164 |
* Register a listeners
|
165 |
*
|
166 |
* @param l
|
167 |
* listener
|
168 |
*/
|
169 |
public void registerListener(ChangeListener l) { |
170 |
this.listeners.add(l);
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* Remove a listener
|
175 |
*
|
176 |
* @param l
|
177 |
* listener
|
178 |
*/
|
179 |
public void removeListener(ChangeListener l) { |
180 |
this.listeners.remove(l);
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* Remove all listeners
|
185 |
*/
|
186 |
public void removeAllListeners() { |
187 |
this.nAlgorithm.removeAllListeners();
|
188 |
this.listeners.clear();
|
189 |
} |
190 |
|
191 |
/**
|
192 |
* Update the list listeners
|
193 |
*
|
194 |
* @param evt
|
195 |
*/
|
196 |
public void update(ChangeEvent evt) { |
197 |
|
198 |
for (int i = 0; i < listeners.size(); i++) { |
199 |
((ChangeListener) listeners.get(i)).stateChanged(evt);
|
200 |
} |
201 |
} |
202 |
|
203 |
/**
|
204 |
* Add a new message
|
205 |
*
|
206 |
* @param message
|
207 |
*/
|
208 |
public void update(String message) { |
209 |
ChangeEvent evt = new ChangeEvent(message); |
210 |
update(evt); |
211 |
} |
212 |
|
213 |
/**
|
214 |
* Add new event
|
215 |
*
|
216 |
* @param e
|
217 |
*/
|
218 |
public void stateChanged(ChangeEvent e) { |
219 |
update(e); |
220 |
} |
221 |
|
222 |
/**
|
223 |
* Delete all listeners
|
224 |
*/
|
225 |
public void clearConsoleInfo() { |
226 |
listeners.clear(); |
227 |
} |
228 |
|
229 |
/**
|
230 |
* This method loads the names of the new fields and his position
|
231 |
*/
|
232 |
protected void loadNamesNewFields() { |
233 |
|
234 |
int co = pattern.getElements().size();
|
235 |
List elems = new ArrayList(); |
236 |
List pos = new ArrayList(); |
237 |
for (int i = 0; i < co; i++) { |
238 |
Element elem = (Element) pattern.getElements().get(i); |
239 |
if (elem.getImportfield()) {
|
240 |
elems.add(elem.getFieldname()); |
241 |
pos.add(new Integer(i)); |
242 |
} |
243 |
} |
244 |
int cou = elems.size();
|
245 |
|
246 |
this.nameNewFields = new String[cou]; |
247 |
this.posNameNewFields = new int[cou]; |
248 |
String aux;
|
249 |
int auxint;
|
250 |
for (int i = 0; i < cou; i++) { |
251 |
aux = (String) elems.get(i);
|
252 |
auxint = ((Integer) pos.get(i)).intValue();
|
253 |
this.nameNewFields[i] = aux;
|
254 |
this.posNameNewFields[i] = auxint;
|
255 |
} |
256 |
} |
257 |
|
258 |
/**
|
259 |
* This method gets the Date format of a field from pattern
|
260 |
*
|
261 |
* @param posi
|
262 |
* @return date formatted
|
263 |
*/
|
264 |
protected String getDateFormat(int posi) { |
265 |
String format = ""; |
266 |
Element adres = (Element) pattern.getElements().get(posi); |
267 |
format = adres.getFieldtype().getDatevalue().getDatevalueformat(); |
268 |
return format;
|
269 |
} |
270 |
|
271 |
/**
|
272 |
* This method gets the field type from the pattern
|
273 |
*
|
274 |
* @param adr
|
275 |
* @return field description
|
276 |
*/
|
277 |
protected FieldDescription createFieldDescFromPattern(Element adr) { |
278 |
|
279 |
FieldDescription fd = new FieldDescription();
|
280 |
// Field Name and Field Alias
|
281 |
fd.setFieldName(adr.getFieldname()); |
282 |
fd.setFieldAlias(adr.getFieldname()); |
283 |
|
284 |
// Field Type and particular width, precision
|
285 |
Fieldtype nft = adr.getFieldtype(); |
286 |
// Field type (STRING)
|
287 |
if (((Stringvalue) nft.getStringvalue()) != null) { |
288 |
fd.setFieldType(Types.VARCHAR);
|
289 |
Stringvalue strVal = ((Stringvalue) nft.getStringvalue()); |
290 |
fd.setFieldLength(strVal.getStringvaluewidth()); |
291 |
} |
292 |
// Field type (DATE)
|
293 |
if (((Datevalue) nft.getDatevalue()) != null) { |
294 |
fd.setFieldType(Types.DATE);
|
295 |
Datevalue dateVal = ((Datevalue) nft.getDatevalue()); |
296 |
dateVal.getDatevalueformat().trim().length(); |
297 |
fd.setFieldLength(dateVal.getDatevalueformat().trim().length()); |
298 |
} |
299 |
// Field type (INTEGER)
|
300 |
if (((Integervalue) nft.getIntegervalue()) != null) { |
301 |
fd.setFieldType(Types.INTEGER);
|
302 |
Integervalue intVal = ((Integervalue) nft.getIntegervalue()); |
303 |
fd.setFieldLength(intVal.getIntegervaluewidth()); |
304 |
} |
305 |
// Field type (DOUBLE)
|
306 |
if (((Decimalvalue) nft.getDecimalvalue()) != null) { |
307 |
fd.setFieldType(Types.DOUBLE);
|
308 |
Decimalvalue decVal = ((Decimalvalue) nft.getDecimalvalue()); |
309 |
fd.setFieldLength(decVal.getDecimalvalueint()); |
310 |
fd.setFieldDecimalCount(decVal.getDecimalvaluedec()); |
311 |
} |
312 |
return fd;
|
313 |
} |
314 |
|
315 |
/**
|
316 |
* Create a new value
|
317 |
*
|
318 |
* @param row
|
319 |
* @param tipoCampo
|
320 |
* @param posi
|
321 |
* @param cadena
|
322 |
* @return
|
323 |
*/
|
324 |
protected Value createValue(int row, int tipoCampo, int posi, String cadena) { |
325 |
Value val = ValueFactory.createNullValue(); |
326 |
// DATE
|
327 |
if (tipoCampo == Types.DATE) { |
328 |
try {
|
329 |
String format = getDateFormat(posi);
|
330 |
val = formatDates(cadena, format); |
331 |
return val;
|
332 |
} catch (Exception e) { |
333 |
val = ValueFactory.createNullValue(); |
334 |
update("ERROR.errorformattingdaterow." + (row + 1)); |
335 |
} |
336 |
} |
337 |
// Integer
|
338 |
if (tipoCampo == Types.INTEGER) { |
339 |
try {
|
340 |
val = ValueFactory.createValueByType(cadena, tipoCampo); |
341 |
return val;
|
342 |
} catch (Exception e) { |
343 |
val = ValueFactory.createNullValue(); |
344 |
update("ERROR.errorformattingintegerrow." + (row + 1)); |
345 |
} |
346 |
} |
347 |
// Decimal
|
348 |
if (tipoCampo == Types.DOUBLE) { |
349 |
try {
|
350 |
val = ValueFactory.createValueByType(cadena, tipoCampo); |
351 |
return val;
|
352 |
} catch (Exception e) { |
353 |
val = ValueFactory.createNullValue(); |
354 |
update("ERROR.errorformattingdecimalrow." + (row + 1)); |
355 |
} |
356 |
} |
357 |
// String
|
358 |
if (tipoCampo == Types.VARCHAR) { |
359 |
try {
|
360 |
val = ValueFactory.createValueByType(cadena, tipoCampo); |
361 |
return val;
|
362 |
} catch (Exception e) { |
363 |
val = ValueFactory.createNullValue(); |
364 |
update("ERROR.errorformattingstringrow." + (row + 1)); |
365 |
} |
366 |
} |
367 |
return val;
|
368 |
} |
369 |
|
370 |
} |