Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libGeocoding / src / org / gvsig / normalization / operations / JoinedTableNormalization.java @ 29125

History | View | Annotate | Download (7.34 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.io.File;
31
import java.util.List;
32

    
33
import org.apache.log4j.Logger;
34
import org.gvsig.normalization.pattern.Element;
35
import org.gvsig.normalization.pattern.Patternnormalization;
36

    
37
import com.hardcode.gdbms.engine.data.driver.DriverException;
38
import com.hardcode.gdbms.engine.values.Value;
39
import com.iver.cit.gvsig.fmap.core.DefaultRow;
40
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
41
import com.iver.cit.gvsig.fmap.drivers.TableDefinition;
42
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
43
import com.iver.cit.gvsig.fmap.edition.EditionException;
44
import com.iver.cit.gvsig.fmap.edition.IEditableSource;
45
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
46
import com.iver.cit.gvsig.fmap.edition.writers.dbf.DbfWriter;
47
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
48

    
49
/**
50
 * Normalize registries of the field of table in a new Table with relate fields
51
 * 
52
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
53
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
54
 * 
55
 */
56
public class JoinedTableNormalization extends TableNormalization implements
57
                NormalizationNewTable {
58

    
59
        private static final Logger log = Logger
60
                        .getLogger(JoinedTableNormalization.class);
61

    
62
        protected String[] nameRelateFields;
63
        private NormalizationTableDriver driver;
64
        private FieldDescription[] fieldDescNewTable;
65
        private File file;
66

    
67
        /**
68
         * Constructor for a join normalization type
69
         * 
70
         * @param source
71
         * @param index
72
         * @param pattern2
73
         * @param relateNames
74
         */
75
        public JoinedTableNormalization(IEditableSource source, int index,
76
                        Patternnormalization pattern2, String[] relateNames, File afile) {
77
                super(source, index, pattern2);
78

    
79
                this.file = afile;
80
                this.nameRelateFields = relateNames;
81
                this.fieldDescNewTable = createFieldDescNewTable();
82
                this.driver = new NormalizationTableDriver(this.fieldDescNewTable);
83
        }
84

    
85
        /**
86
         * This method answers if this process generates a new Table
87
         * 
88
         * @return true
89
         */
90
        public boolean createNewTable() {
91
                return true;
92
        }
93

    
94
        /**
95
         * Tasks necessary before normalize
96
         * 
97
         * @return process ok
98
         */
99

    
100
        public boolean preProcess() {
101
                update("INFO.normalizing");
102
                return true;
103
        }
104

    
105
        /**
106
         * Tasks necessary after normalize
107
         * 
108
         * @return process ok
109
         */
110

    
111
        public boolean postProcess() {
112
                DbfWriter dbf = new DbfWriter();
113
                dbf.setFile(this.file);
114
                TableDefinition td = new TableDefinition();
115
                td.setName("secondTable");
116
                td.setFieldsDesc(driver.getFieldDescriptions());
117
                try {
118
                        dbf.initialize(td);
119
                } catch (EditionException e1) {
120
                        log.error("ERROR initializing the dbf file", e1);
121
                        update("ERROR.errorwrittingdbf");
122
                        return false;
123
                }
124
                try {
125
                        dbf.preProcess();
126
                } catch (EditionException e1) {
127
                        log.error("ERROR in the preprocess", e1);
128
                        update("ERROR.errorwrittingdbf");
129
                        return false;
130
                }
131

    
132
                DefaultRowEdited dre;
133
                for (int i = 0; i < getRowCount(); i++) {
134
                        try {
135
                                Value[] vals = driver.getValues(i);
136
                                DefaultRow dr = new DefaultRow(vals);
137
                                dre = new DefaultRowEdited(dr, IRowEdited.STATUS_ADDED, i);
138
                                dbf.process(dre);
139
                        } catch (Exception ioe) {
140
                                log.warn("ERROR traversing driver", ioe);
141
                        }
142
                }
143
                try {
144
                        dbf.postProcess();
145
                } catch (EditionException e) {
146
                        log.error("ERROR in the postprocess", e);
147
                        update("ERROR.errorwrittingdbf");
148
                        return false;
149
                }
150

    
151
                update("INFO.endnormalizing");
152
                return true;
153
        }
154

    
155
        /**
156
         * Get the new table driver
157
         * 
158
         * @return driver
159
         */
160
        public NormalizationTableDriver getDriver() {
161
                return this.driver;
162
        }
163

    
164
        /**
165
         * Get DBF file of the new table
166
         * 
167
         * @return file
168
         */
169
        public File getOuputFile() {
170
                return this.file;
171
        }
172

    
173
        /**
174
         * This method add the selected row new Values
175
         * 
176
         * @param values
177
         * @param row
178
         */
179
        protected void editRow(Value[] vals, int row) {
180
                driver.addRow(vals);
181
        }
182

    
183
        /**
184
         * 
185
         */
186
        protected void fillValues(Value[] vals, List splitString, int row)
187
                        throws DriverException {
188

    
189
                int posi = -1;
190
                /* COPY THE MAIN TABLE JOIN ATTRIBUTES */
191
                if (nameRelateFields != null) {
192
                        int positionInColumnMainTable;
193
                        SelectableDataSource sds = editableSourceTable.getRecordset();
194
                        // COPY MAIN ELEMENTS
195
                        for (int k = 0; k < nameRelateFields.length; k++) {
196
                                posi++;
197
                                positionInColumnMainTable = sds
198
                                                .getFieldIndexByName((String) nameRelateFields[k]);
199
                                vals[posi] = sds.getFieldValue(row, positionInColumnMainTable);
200
                        }
201
                }
202
                /* COPY NEW VALUES */
203
                String cadena;
204
                int tipoCampo;
205
                for (int j = 0; j < nameNewFields.length; j++) {
206
                        posi++;
207
                        try {
208
                                cadena = (String) splitString.get(posNameNewFields[j]);
209
                        } catch (Exception e) {
210
                                log.warn("Null chain replaced for white");
211
                                update("ERROR.errornullsubstring." + (row + 1));
212
                                cadena = "";
213
                        }
214
                        cadena = cadena.trim();
215
                        tipoCampo = driver.getFieldType(posi);
216
                        // create values
217
                        vals[posi] = createValue(row, tipoCampo, posi, cadena);
218
                }
219
        }
220

    
221
        /**
222
         * Create the array of values with the necessary size
223
         * 
224
         * @return
225
         */
226
        protected Value[] createValueArray() {
227
                int nnv = nameNewFields.length;
228
                int nvals = nameRelateFields == null ? nnv : nnv
229
                                + nameRelateFields.length;
230
                Value[] vals = new Value[nvals];
231
                return vals;
232
        }
233

    
234
        /**
235
         * Create a Filed description new table
236
         * 
237
         * @return
238
         */
239
        private FieldDescription[] createFieldDescNewTable() {
240

    
241
                // number of fields
242
                int num = nameRelateFields != null ? nameRelateFields.length
243
                                + nameNewFields.length : nameNewFields.length;
244
                FieldDescription[] res = new FieldDescription[num];
245
                // Relates fields
246
                int posi = -1;
247
                int ind1;
248
                FieldDescription auxfd = null;
249
                if (nameRelateFields != null) {
250
                        for (int i = 0; i < nameRelateFields.length; i++) {
251
                                posi++;
252
                                try {
253
                                        ind1 = editableSourceTable.getRecordset()
254
                                                        .getFieldIndexByName(nameRelateFields[i]);
255
                                        auxfd = fieldDescTable[ind1];
256
                                        res[posi] = auxfd;
257
                                } catch (Exception e) {
258
                                        e.printStackTrace();
259
                                }
260
                        }
261
                }
262
                // Pattern fields
263
                for (int j = 0; j < pattern.getElements().size(); j++) {
264
                        if (((Element) pattern.getElements().get(j)).getImportfield()) {
265
                                posi++;
266
                                auxfd = createFieldDescFromPattern((Element) pattern
267
                                                .getElements().get(j));
268
                                res[posi] = auxfd;
269
                        }
270
                }
271
                return res;
272
        }
273

    
274
}