Statistics
| Revision:

root / trunk / libraries / libGeocoding / src / org / gvsig / normalization / operations / AbstractNormalization.java @ 23019

History | View | Annotate | Download (9.88 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.BitSet;
35
import java.util.Date;
36
import java.util.List;
37

    
38
import javax.swing.event.ChangeEvent;
39
import javax.swing.event.ChangeListener;
40

    
41
import org.gvsig.normalization.patterns.Datevalue;
42
import org.gvsig.normalization.patterns.Decimalvalue;
43
import org.gvsig.normalization.patterns.Element;
44
import org.gvsig.normalization.patterns.Fieldtype;
45
import org.gvsig.normalization.patterns.Integervalue;
46
import org.gvsig.normalization.patterns.Patternnormalization;
47
import org.gvsig.normalization.patterns.Stringvalue;
48

    
49
import com.hardcode.gdbms.engine.values.Vabvlue;
50
import com.hardcode.gdbms.engine.values.ValueFactory;
51
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
52
import com.iver.cit.gvsig.fmap.edition.IEditableSource;
53
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
54
import com.iver.cit.gvsig.project.documents.table.gui.Table;
55

    
56
/**
57
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
58
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
59
 */
60

    
61
public abstract class AbstractNormalization implements Normalization,
62
                ChangeListener {
63

    
64
        @SuppressWarnings("unchecked")
65
        private List listeners = new ArrayList();
66

    
67
        // ATTRIBUTES
68
        protected NormAlgorithm nAlgorithm;
69

    
70
        // Pattern
71
        protected Patternnormalization pattern;
72

    
73
        protected String[] nameNewFields;
74
        protected int[] posNameNewFields;
75

    
76
        /**
77
         * builder
78
         * 
79
         * @param pat
80
         *            pattern
81
         */
82
        public AbstractNormalization(Patternnormalization pat) {
83
                this.pattern = pat;
84
                nAlgorithm = new NormAlgorithm(pat);
85
                nAlgorithm.registerListener(this);
86
                loadNamesNewFields();
87
        }
88

    
89
        /**
90
         * This method gets the name of the main field
91
         * 
92
         * @param _tab
93
         * @return name of the selected field
94
         */
95
        public static String getNameMainField(Table _tab) {
96

    
97
                BitSet _columnSelected = _tab.getSelectedFieldIndices();
98
                int po = _columnSelected.nextSetBit(0);
99
                ProjectTable oriPT = _tab.getModel();
100
                IEditableSource ies = oriPT.getModelo();
101
                FieldDescription[] fd = ies.getFieldsDescription();
102
                String name = fd[po].getFieldName();
103
                return name.toUpperCase();
104
        }
105

    
106
        /**
107
         * Get the pattern
108
         * 
109
         * @return pattern
110
         */
111
        public Patternnormalization getPattern() {
112
                return pattern;
113
        }
114

    
115
        /**
116
         * Get the number of rows
117
         * 
118
         * @return number of rows
119
         */
120
        public abstract long getRowCount();
121

    
122
        /**
123
         * This method formats the date from a date pattern
124
         * 
125
         * @see http://java.sun.com/j2se/1.5.0/docs/api/java/text/SimpleDateFormat.html
126
         * 
127
         * @param date
128
         * @param pattern
129
         * @return Value
130
         * @throws ParseException
131
         */
132
        public Value formatDates(String date, String pattern) throws ParseException {
133
                Value result = ValueFactory.createNullValue();
134

    
135
                Date fecha = null;
136

    
137
                SimpleDateFormat sdf = new SimpleDateFormat(pattern);
138
                fecha = sdf.parse(date);
139
                result = ValueFactory.createValue(fecha);
140

    
141
                return result;
142
        }
143

    
144
        /**
145
         * This method in its base implementations returns the same value as the
146
         * rowCount method
147
         * 
148
         * @return number of rows estimated
149
         */
150
        public long getEstimatedRowCount() {
151
                return getRowCount();
152
        }
153

    
154
        /**
155
         * Tasks necessary before normalize
156
         * 
157
         * @return process ok
158
         */
159
        public boolean preProcess() {
160
                return false;
161
        }
162

    
163
        /**
164
         * Tasks necessary after normalize
165
         * 
166
         * @return process ok
167
         */
168
        public boolean postProcess() {
169
                return false;
170
        }
171

    
172
        /**
173
         * Get the number of the new imported fields
174
         * 
175
         * @return number of imported fields
176
         */
177
        public int numNewImportedFields() {
178
                int contImported = 0;
179
                int contTotal = this.pattern.getElements().size();
180

    
181
                for (int i = 0; i < contTotal; i++) {
182
                        if (((Element) this.pattern.getElements().get(i)).getImportfield()) {
183
                                contImported++;
184
                        }
185
                }
186
                return contImported;
187
        }
188

    
189
        /**
190
         * Register a listeners
191
         * 
192
         * @param l
193
         *            listener
194
         */
195
        @SuppressWarnings("unchecked")
196
        public void registerListener(ChangeListener l) {
197
                this.listeners.add(l);
198
        }
199

    
200
        /**
201
         * Remove a listener
202
         * 
203
         * @param l
204
         *            listener
205
         */
206
        public void removeListener(ChangeListener l) {
207
                this.listeners.remove(l);
208
        }
209

    
210
        /**
211
         * Renove all listeners
212
         */
213
        public void removeAllListeners() {
214
                this.nAlgorithm.removeAllListeners();
215
                this.listeners.clear();
216
        }
217

    
218
        /**
219
         * 
220
         * @param evt
221
         */
222
        public void update(ChangeEvent evt) {
223

    
224
                for (int i = 0; i < listeners.size(); i++) {
225
                        ((ChangeListener) listeners.get(i)).stateChanged(evt);
226
                }
227
        }
228

    
229
        /**
230
         * Add a new message
231
         * 
232
         * @param message
233
         */
234
        public void update(String message) {
235
                ChangeEvent evt = new ChangeEvent(message);
236
                update(evt);
237
        }
238

    
239
        /**
240
         * Add new event
241
         * 
242
         * @param e
243
         */
244
        public void stateChanged(ChangeEvent e) {
245
                update(e);
246
        }
247

    
248
        /**
249
         * Delete all listeners
250
         */
251
        public void clearConsoleInfo() {
252
                listeners.clear();
253
        }
254

    
255
        /**
256
         * This method loads the names of the new fields and his position
257
         */
258
        @SuppressWarnings("unchecked")
259
        protected void loadNamesNewFields() {
260
                int co = pattern.getElements().size();
261
                ArrayList eles = new ArrayList();
262
                ArrayList pos = new ArrayList();
263
                for (int i = 0; i < co; i++) {
264
                        Element ade = (Element) pattern.getElements().get(i);
265
                        if (ade.getImportfield()) {
266
                                eles.add(ade.getFieldname());
267
                                pos.add(new Integer(i));
268
                        }
269
                }
270
                int cou = eles.size();
271

    
272
                nameNewFields = new String[cou];
273
                posNameNewFields = new int[cou];
274
                String aux;
275
                int auxint;
276
                for (int i = 0; i < cou; i++) {
277
                        aux = (String) eles.get(i);
278
                        auxint = ((Integer) pos.get(i)).intValue();
279
                        nameNewFields[i] = aux;
280
                        posNameNewFields[i] = auxint;
281
                }
282
        }
283

    
284
        /**
285
         * This method gets the Date format of a field from pattern
286
         * 
287
         * @param posi
288
         * @return date formatted
289
         */
290
        protected String getDateFormat(int posi) {
291
                String format = "";
292
                Element adres = (Element) pattern.getElements().get(posi);
293
                format = adres.getFieldtype().getDatevalue().getDatevalueformat();
294
                return format;
295
        }
296

    
297
        /**
298
         * This method gets the field type from the pattern
299
         * 
300
         * @param adr
301
         * @return field description
302
         */
303
        protected FieldDescription readAddressNormPattern(Element adr) {
304

    
305
                FieldDescription fd = new FieldDescription();
306
                // Field Name and Field Alias
307
                fd.setFieldName(adr.getFieldname());
308
                fd.setFieldAlias(adr.getFieldname());
309

    
310
                // Field Type and particular width, precision
311
                Fieldtype nft = adr.getFieldtype();
312
                // Field type (STRING)
313
                if (((Stringvalue) nft.getStringvalue()) != null) {
314
                        fd.setFieldType(Types.VARCHAR);
315
                        Stringvalue strVal = ((Stringvalue) nft.getStringvalue());
316
                        fd.setFieldLength(strVal.getStringvaluewidth());
317
                }
318
                // Field type (DATE)
319
                if (((Datevalue) nft.getDatevalue()) != null) {
320
                        fd.setFieldType(Types.DATE);
321
                        Datevalue dateVal = ((Datevalue) nft.getDatevalue());
322
                        dateVal.getDatevalueformat().trim().length();
323
                        fd.setFieldLength(dateVal.getDatevalueformat().trim().length());
324
                }
325
                // Field type (INTEGER)
326
                if (((Integervalue) nft.getIntegervalue()) != null) {
327
                        fd.setFieldType(Types.INTEGER);
328
                        Integervalue intVal = ((Integervalue) nft.getIntegervalue());
329
                        fd.setFieldLength(intVal.getIntegervaluewidth());
330
                }
331
                // Field type (DOUBLE)
332
                if (((Decimalvalue) nft.getDecimalvalue()) != null) {
333
                        fd.setFieldType(Types.DOUBLE);
334
                        Decimalvalue decVal = ((Decimalvalue) nft.getDecimalvalue());
335
                        fd.setFieldLength(decVal.getDecimalvalueint());
336
                        fd.setFieldDecimalCount(decVal.getDecimalvaluedec());
337
                }
338
                return fd;
339
        }
340

    
341
        protected Value createValue(int row, int tipoCampo, int posi, String cadena) {
342
                Value val = ValueFactory.createNullValue();
343
                // DATE
344
                if (tipoCampo == Types.DATE) {
345
                        try {
346
                                String format = getDateFormat(posi);
347
                                val = formatDates(cadena, format);
348
                                return val;
349
                        } catch (Exception e) {
350
                                val = ValueFactory.createNullValue();
351
                                update("ERROR.errorformattingdaterow." + (row + 1));
352
                        }
353
                }
354
                // Integer
355
                if (tipoCampo == Types.INTEGER) {
356
                        try {
357
                                val = ValueFactory.createValueByType(cadena, tipoCampo);
358
                                return val;
359
                        } catch (Exception e) {
360
                                val = ValueFactory.createNullValue();
361
                                update("ERROR.errorformattingintegerrow." + (row + 1));
362
                        }
363
                }
364
                // Decimal
365
                if (tipoCampo == Types.DOUBLE) {
366
                        try {
367
                                val = ValueFactory.createValueByType(cadena, tipoCampo);
368
                                return val;
369
                        } catch (Exception e) {
370
                                val = ValueFactory.createNullValue();
371
                                update("ERROR.errorformattingdecimalrow." + (row + 1));
372
                        }
373
                }
374
                // String
375
                if (tipoCampo == Types.VARCHAR) {
376
                        try {
377
                                val = ValueFactory.createValueByType(cadena, tipoCampo);
378
                                return val;
379
                        } catch (Exception e) {
380
                                val = ValueFactory.createNullValue();
381
                                update("ERROR.errorformattingstringrow." + (row + 1));
382
                        }
383
                }
384
                return val;
385
        }
386

    
387
}