Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / org.gvsig.geocoding / src / org / gvsig / normalization / impl / NormalizationTransform.java @ 32474

History | View | Annotate | Download (9.09 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
/**
29
 * 
30
 * 
31
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
32
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
33
 */
34

    
35
package org.gvsig.normalization.impl;
36

    
37
import java.text.ParseException;
38
import java.text.SimpleDateFormat;
39
import java.util.ArrayList;
40
import java.util.Date;
41
import java.util.Iterator;
42
import java.util.List;
43

    
44
import org.gvsig.fmap.dal.DataTypes;
45
import org.gvsig.fmap.dal.exception.DataException;
46
import org.gvsig.fmap.dal.feature.EditableFeature;
47
import org.gvsig.fmap.dal.feature.EditableFeatureType;
48
import org.gvsig.fmap.dal.feature.Feature;
49
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
50
import org.gvsig.fmap.dal.feature.FeatureStore;
51
import org.gvsig.fmap.dal.feature.FeatureType;
52
import org.gvsig.normalization.Normalizer;
53
import org.gvsig.normalization.algorithm.NormalizationAlgorithm;
54
import org.gvsig.normalization.pattern.Datevalue;
55
import org.gvsig.normalization.pattern.Decimalvalue;
56
import org.gvsig.normalization.pattern.Element;
57
import org.gvsig.normalization.pattern.Fieldtype;
58
import org.gvsig.normalization.pattern.Integervalue;
59
import org.gvsig.normalization.pattern.NormalizationPattern;
60
import org.gvsig.normalization.pattern.Stringvalue;
61
import org.gvsig.tools.persistence.PersistenceException;
62
import org.gvsig.tools.persistence.PersistentState;
63
import org.slf4j.Logger;
64
import org.slf4j.LoggerFactory;
65

    
66
public class NormalizationTransform extends Normalizer {
67

    
68
        private static final Logger log = LoggerFactory
69
                        .getLogger(NormalizationTransform.class);
70
        
71
        private int positionFieldSelected;
72
        private NormalizationAlgorithm algorithm = null;
73
        private List<int[]> newFields = new ArrayList<int[]>();
74
        private FeatureType originalFeatureType = null;
75

    
76
        /**
77
         * Constructor
78
         */
79
        public NormalizationTransform() {
80
                super();
81
        }
82

    
83
        /**
84
         * Initialize transform
85
         * 
86
         * @param store
87
         * @param pat
88
         * @param position
89
         * @param algo
90
         * @throws DataException
91
         */
92
        public void initialize(FeatureStore store, NormalizationPattern pat,
93
                        int position, NormalizationAlgorithm algo) throws DataException {
94
                setFeatureStore(store);
95
                this.setPattern(pat);
96
                this.setAlgorithm(algo);
97
                this.positionFieldSelected = position;
98
                this.newFields.clear();
99
                this.originalFeatureType = this.getFeatureStore()
100
                                .getDefaultFeatureType();
101
                // modify feature type
102
                this.transformFeatureType();
103
        }
104

    
105
        /**
106
         * Apply transform
107
         */
108
        public void applyTransform(Feature source, EditableFeature target)
109
                        throws DataException {
110

    
111
                // copy the data from store1 into the resulting feature
112
                this.copySourceToTarget(source, target);
113

    
114
                String chain = source.get(this.positionFieldSelected).toString();
115

    
116
                List<String> chains = this.splitChain(chain);
117

    
118
                for (int i = 0; i < newFields.size(); i++) {
119
                        int[] field = newFields.get(i);
120
                        int fieldPosition = field[0];
121
                        int fieldType = field[1];
122

    
123
                        if (fieldPosition != -1 && fieldType != -1) {
124
                                String value = chains.get(i);
125
                                switch (fieldType) {
126
                                case DataTypes.STRING:
127
                                        target.setString(fieldPosition, value);
128
                                        break;
129
                                case DataTypes.DATE:
130
                                        String datePat = getDateFormat(i);
131
                                        Date dateValue = null;
132
                                        try {
133
                                                dateValue = formatDates(value, datePat);
134
                                        } catch (ParseException e) {
135
                                                log.error("Error parsing the date", e);
136
                                        }
137
                                        target.setDate(fieldPosition, dateValue);
138
                                        break;
139
                                case DataTypes.INT:
140
                                        int intValue = new Integer(value);
141
                                        target.setInt(fieldPosition, intValue);
142
                                        break;
143
                                case DataTypes.DOUBLE:
144
                                        double douValue = new Double(value);
145
                                        target.setDouble(fieldPosition, douValue);
146
                                        break;
147
                                }
148
                        }
149
                }
150
        }
151

    
152
        /**
153
         * 
154
         */
155
        public FeatureType getSourceFeatureTypeFrom(FeatureType targetFeatureType) {
156
                return this.originalFeatureType;
157
        }
158

    
159
        /**
160
         * 
161
         */
162
        public boolean isTransformsOriginalValues() {
163
                return false;
164
        }
165

    
166
        /**
167
         * 
168
         */
169
        public void loadFromState(PersistentState state)
170
                        throws PersistenceException {
171
        }
172

    
173
        /**
174
         * 
175
         */
176
        public void saveToState(PersistentState state) throws PersistenceException {
177

    
178
        }
179

    
180
        /**
181
         * Transform Default Feature Type from pattern
182
         * 
183
         * @throws DataException
184
         */
185
        private void transformFeatureType() throws DataException {
186

    
187
                FeatureType type = this.getFeatureStore().getDefaultFeatureType();
188
                EditableFeatureType eType = type.getEditable();
189

    
190
                Iterator<Element> eles = getPattern().getElements().iterator();
191
                while (eles.hasNext()) {
192
                        Element ele = eles.next();
193
                        String fieldName = ele.getFieldname();
194
                        Fieldtype fieldType = ele.getFieldtype();
195
                        boolean impor = ele.getImportfield();
196
                        if (impor) {
197
                                // Field type (STRING)
198
                                if (((Stringvalue) fieldType.getStringvalue()) != null) {
199
                                        Stringvalue val = fieldType.getStringvalue();
200
                                        int siz = val.getStringvaluewidth();
201
                                        eType.add(fieldName, DataTypes.STRING, siz);
202
                                        int position = eType.size() - 1;
203
                                        int[] fi = { position, DataTypes.STRING };
204
                                        newFields.add(fi);
205
                                }
206
                                // Field type (DATE)
207
                                else if (((Datevalue) fieldType.getDatevalue()) != null) {
208
                                        eType.add(fieldName, DataTypes.DATE);
209
                                        int position = eType.size() - 1;
210
                                        int[] fi = { position, DataTypes.DATE };
211
                                        newFields.add(fi);
212
                                }
213
                                // Field type (INTEGER)
214
                                else if (((Integervalue) fieldType.getIntegervalue()) != null) {
215
                                        Integervalue val = fieldType.getIntegervalue();
216
                                        int siz = val.getIntegervaluewidth();
217
                                        eType.add(fieldName, DataTypes.INT, siz);
218
                                        int position = eType.size() - 1;
219
                                        int[] fi = { position, DataTypes.INT };
220
                                        newFields.add(fi);
221
                                }
222
                                // Field type (DOUBLE)
223
                                else if (((Decimalvalue) fieldType.getDecimalvalue()) != null) {
224
                                        Decimalvalue val = fieldType.getDecimalvalue();
225
                                        int sizeint = val.getDecimalvalueint();
226
                                        int sizedec = val.getDecimalvaluedec();
227
                                        eType.add(fieldName, DataTypes.DOUBLE, sizeint + 1
228
                                                        + sizedec);
229
                                        int position = eType.size() - 1;
230
                                        int[] fi = { position, DataTypes.DOUBLE };
231
                                        newFields.add(fi);
232
                                }
233
                        } else {
234
                                int[] fi = { -1, -1 };
235
                                newFields.add(fi);
236
                        }
237

    
238
                }
239
                FeatureType type2 = eType.getNotEditableCopy();
240
                List<FeatureType> allTypes = new ArrayList<FeatureType>();
241
                allTypes.add(type2);
242
                this.setFeatureTypes(allTypes, type2);
243
                System.out.println("");
244
        }
245

    
246
        /**
247
         * Split chain
248
         * 
249
         * @param chain
250
         * @return
251
         */
252
        private List<String> splitChain(String chain) {
253
                List<String> chains = this.algorithm.splitChain(chain);
254
                List<String> filteredchains = this.algorithm.filterSplitChains(chains);
255
                return filteredchains;
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
        private String getDateFormat(int posi) {
265
                String format = "";
266
                Element adres = (Element) getPattern().getElements().get(posi);
267
                format = adres.getFieldtype().getDatevalue().getDatevalueformat();
268
                return format;
269
        }
270

    
271
        /**
272
         * This method formats the date from a date pattern
273
         * 
274
         * @see http 
275
         *      ://java.sun.com/j2se/1.5.0/docs/api/java/text/SimpleDateFormat.html
276
         * 
277
         * @param date
278
         * @param pattern
279
         * @return Value
280
         * @throws ParseException
281
         */
282
        private Date formatDates(String date, String pattern) throws ParseException {
283
                SimpleDateFormat sdf = new SimpleDateFormat(pattern);
284
                Date fecha = sdf.parse(date);
285
                return fecha;
286
        }
287

    
288
        /**
289
         * Copy features
290
         * 
291
         * @param source
292
         * @param target
293
         */
294
        private void copySourceToTarget(Feature source, EditableFeature target) {
295
                FeatureAttributeDescriptor attr, attrTrg;
296
                FeatureType ftSrc = source.getType();
297
                FeatureType ftTrg = target.getType();
298

    
299
                for (int i = 0; i < source.getType().size(); i++) {
300
                        attr = ftSrc.getAttributeDescriptor(i);
301
                        attrTrg = ftTrg.getAttributeDescriptor(attr.getName());
302
                        if (attrTrg != null) {
303
                                try {
304
                                        target.set(attrTrg.getIndex(), source.get(i));
305
                                } catch (IllegalArgumentException e) {
306
                                        attrTrg = ftTrg.getAttributeDescriptor(attr.getName());
307
                                        target.set(attrTrg.getIndex(), attrTrg.getDefaultValue());
308
                                }
309

    
310
                        }
311
                }
312

    
313
        }
314

    
315
}