Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.lib / org.gvsig.exportto.lib.impl / src / main / java / org / gvsig / export / impl / service / DefaultExportAttributes.java @ 44801

History | View | Annotate | Download (14.9 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.export.impl.service;
7

    
8
import java.util.ArrayList;
9
import java.util.Iterator;
10
import java.util.List;
11
import org.apache.commons.lang3.StringUtils;
12
import org.gvsig.export.ExportAttributes;
13
import org.gvsig.export.spi.AttributeNamesTranslator;
14
import org.gvsig.export.spi.DummyAttributeNamesTranslator;
15
import org.gvsig.fmap.dal.DALLocator;
16
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
17
import org.gvsig.fmap.dal.feature.EditableFeatureType;
18
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
19
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
20
import org.gvsig.fmap.dal.feature.FeatureQuery;
21
import org.gvsig.fmap.dal.feature.FeatureType;
22
import org.gvsig.tools.ToolsLocator;
23
import org.gvsig.tools.dataTypes.DataTypes;
24
import org.gvsig.tools.dataTypes.DataTypesManager;
25
import org.gvsig.tools.dynobject.DynStruct;
26
import org.gvsig.tools.persistence.PersistenceManager;
27
import org.gvsig.tools.persistence.PersistentState;
28
import org.gvsig.tools.persistence.exception.PersistenceException;
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31

    
32
public final class DefaultExportAttributes implements ExportAttributes {
33

    
34
    private List<ExportAttribute> exportAttributes;
35
    private AttributeNamesTranslator namesTranslator = null;
36
    private FeatureType sourceFeatureType;
37
    static final Logger LOGGER = LoggerFactory.getLogger(DefaultExportAttributes.class);
38
    private boolean active;
39
    private FeatureQuery query = null;
40

    
41
    public DefaultExportAttributes() {
42
        this.namesTranslator = new DummyAttributeNamesTranslator();
43
        this.active = false;
44
    }
45

    
46
    public void fillExportAttributes(FeatureType ftype, FeatureQuery query) {
47
        ArrayList attrs = new ArrayList();
48
        if (ftype != null) {
49
            for (FeatureAttributeDescriptor fad : ftype) {
50
                DefaultExportAttribute exportAttribute = new DefaultExportAttribute(fad.getCopy());
51
                String newName = this.getNamesTranslator().getNameSuggestion(fad.getName());
52
                exportAttribute.setNewName(newName);
53
                exportAttribute.setNewType(fad.getDataType().getType());
54
                exportAttribute.setSize(fad.getSize());
55
                if (query == null || (query != null && query.getGroupByColumns().isEmpty())) {
56
                    exportAttribute.setExported(isShowableDataType(fad.getDataType().getType()));
57
                } else {
58
                    if (query.getAggregate(fad.getName()) != null || query.getGroupByColumns().contains(fad.getName())) {
59
                        exportAttribute.setExported(isShowableDataType(fad.getDataType().getType()));
60
                    } else {
61
                        exportAttribute.setExported(false);
62
                    }
63
                }
64
                attrs.add(exportAttribute);
65
            }
66
        }
67
        
68
        if (query!=null && query.getExtraColumn().getColumns()!=null && !query.getExtraColumn().getColumns().isEmpty() ) {
69
            for (FeatureAttributeDescriptor fad : query.getExtraColumn().getColumns()) {
70
                DefaultExportAttribute exportAttribute = new DefaultExportAttribute(fad.getCopy());
71
                String newName = this.getNamesTranslator().getNameSuggestion(fad.getName());
72
                exportAttribute.setNewName(newName);
73
                exportAttribute.setNewType(fad.getDataType().getType());
74
                exportAttribute.setSize(fad.getSize());
75
                exportAttribute.setExported(isShowableDataType(fad.getDataType().getType()));
76
                attrs.add(exportAttribute);
77
            }
78
        }
79
        this.setExportAttributes(attrs);
80
        this.fixAttributeNames();
81

    
82
    }
83
    
84
    protected boolean isShowableDataType(int dataType) {
85
        DataTypesManager dataTypemanager = ToolsLocator.getDataTypesManager();
86
        if (dataTypemanager.isContainer(dataType)
87
                || dataTypemanager.isObject(dataType)
88
                || dataType == DataTypes.ARRAY) {
89
            return false;
90

    
91
        } else {
92
            return true;
93
        }
94
    }
95

    
96
    @Override
97
    public void setNamesTranslator(AttributeNamesTranslator namesTranslator) {
98
        if (this.namesTranslator != namesTranslator) {
99
            this.namesTranslator = namesTranslator;
100
            if (this.sourceFeatureType != null) {
101
                this.fillExportAttributes(sourceFeatureType, this.query);
102
            }
103
        }
104
    }
105

    
106
    @Override
107
    public AttributeNamesTranslator getNamesTranslator() {
108
        return this.namesTranslator;
109
    }
110

    
111
    @Override
112
    public List<ExportAttribute> toList() {
113
        return this.exportAttributes;
114
    }
115

    
116
    @Override
117
    public boolean isEmpty() {
118
        return this.exportAttributes.isEmpty();
119
    }
120

    
121
    @Override
122
    public int size() {
123
        return this.exportAttributes.size();
124
    }
125

    
126
    @Override
127
    public Iterator<ExportAttribute> iterator() {
128
        return this.exportAttributes.iterator();
129
    }
130

    
131
    @Override
132
    public ExportAttribute get(int position) {
133
        return this.exportAttributes.get(position);
134
    }
135

    
136
    @Override
137
    public ExportAttribute getExportAttribute(String name) {
138
        for (ExportAttribute exportAttribute : this.exportAttributes) {
139
            if (StringUtils.equals(exportAttribute.getName(), name)) {
140
                return exportAttribute;
141
            }
142
        }
143
        return null;
144
    }
145

    
146
    @Override
147
    public FeatureType getTargetFeatureType() {
148
        if (!this.isAttributeNamesValid()) {
149
            this.fixAttributeNames();
150
            LOGGER.warn("An extra fix attributes names in the feature type has been made");
151
        }
152
        EditableFeatureType targetFeatureType = this.getSourceFeatureType().getCopy().getEditable();
153
        for (FeatureAttributeDescriptor attrSource : this.getSourceFeatureType()) {
154
            FeatureAttributeDescriptor attr = targetFeatureType.getAttributeDescriptor(attrSource.getName());
155
            String name = attr.getName();
156
            ExportAttribute exportAttr = this.getExportAttribute(name);
157
            if (!exportAttr.isExported()) {
158
                targetFeatureType.remove(name);
159
                continue;
160
            }
161
            EditableFeatureAttributeDescriptor eAttr = targetFeatureType.getEditableAttributeDescriptor(name);
162
            if( eAttr.isComputed() && !exportAttr.isComputed() ) {
163
                eAttr.setFeatureAttributeEmulator((FeatureAttributeEmulator)null);
164
            }
165
            if (this.getTargetName(name) == null && name != null && !this.getTargetName(name).equals(name)) {
166
                eAttr.setName(this.getTargetName(name));
167
            }
168
            int type = attr.getDataType().getType();
169
            if (type != this.getTargetType(name)) {
170
                eAttr.setDataType(this.getTargetType(name));
171
                eAttr.setDefaultValue(null); // TODO: delete default value
172
            }
173
            int size = exportAttr.getSize();
174
            eAttr.setSize(size);
175
        }
176
        if (this.query!=null && this.query.getExtraColumn().getColumns()!=null && !this.query.getExtraColumn().getColumns().isEmpty()) {
177
            for (EditableFeatureAttributeDescriptor attrExtraOriginal : this.query.getExtraColumn().getColumns()) {
178
                EditableFeatureAttributeDescriptor attrExtra = DALLocator.getDataManager().createFeatureAttributeDescriptor();
179
                attrExtra.copyFrom(attrExtraOriginal);
180
                String name = attrExtra.getName();
181
                ExportAttribute exportAttr = this.getExportAttribute(name);
182
                if (!exportAttr.isExported()) {
183
                    //targetFeatureType.remove(name);
184
                    continue;
185
                }                
186
                if (this.getTargetName(name) == null && name != null && !this.getTargetName(name).equals(name)) {
187
                    attrExtra.setName(this.getTargetName(name));
188
                }
189
                int type = attrExtra.getDataType().getType();
190
                if (type != this.getTargetType(name)) {
191
                    attrExtra.setDataType(this.getTargetType(name));
192
                    attrExtra.setDefaultValue(null); // TODO: delete default value
193
                }
194
                int size = exportAttr.getSize();
195
                attrExtra.setSize(size);
196
                if (!exportAttr.isComputed()) {
197
                    attrExtra.setFeatureAttributeEmulator((FeatureAttributeEmulator)null);
198
                    if (size==0) {
199
                        attrExtra.setSize(50);//TODO default value para valor 0 de size
200
                    }
201
                }
202
                EditableFeatureAttributeDescriptor addedAttributeDescriptor = 
203
                        targetFeatureType.add(attrExtra.getName(), attrExtra.getType());
204
                addedAttributeDescriptor.copyFrom(attrExtra);
205
            }        
206
        }
207
        
208
        return targetFeatureType.getNotEditableCopy();
209
    }
210

    
211
    @Override
212
    public void setSourceFeatureType(FeatureType sourceFeatureType, FeatureQuery query) {
213
        this.query = query;
214
        if (!sourceFeatureType.equals(this.sourceFeatureType)) {
215
            this.sourceFeatureType = sourceFeatureType;
216
            this.fillExportAttributes(this.sourceFeatureType, query);
217
        }
218
    }
219

    
220
    @Override
221
    public FeatureType getSourceFeatureType() {
222
        return this.sourceFeatureType;
223
    }
224

    
225
    @Override
226
    public String getTargetName(String name) {
227
        for (int i = 0; i < exportAttributes.size(); i++) {
228
            ExportAttribute exportAttribute = exportAttributes.get(i);
229
            if (StringUtils.equals(exportAttribute.getName(), name)) {
230
                String newName = exportAttribute.getNewName();
231
//TODO return value or fixed value?
232
//                if (!this.namesTranslator.isValidName(this, i, newName)) {
233
//                    newName = this.namesTranslator.getNameSuggestion(this, i, newName);
234
//                    //exportAttribute.setNewName(newName); //Change the name in the exportAttribute after chacking
235
//                }
236
                return newName;
237
            }
238
        }
239
        return null;
240
    }
241

    
242
    @Override
243
    public String getSourceName(String name) {
244
        for (ExportAttribute exportAttribute : this.exportAttributes) {
245
            if (StringUtils.equalsIgnoreCase(exportAttribute.getNewName(), name)) {
246
                return exportAttribute.getName();
247
            }
248
        }
249
        return null;
250
    }
251

    
252
    @Override
253
    public int getTargetType(String name) {
254
        for (ExportAttribute exportAttribute : this.exportAttributes) {
255
            if (StringUtils.equals(exportAttribute.getName(), name)) {
256
                return exportAttribute.getNewDataType();
257
            }
258
        }
259
        return 0;
260
    }
261

    
262
    @Override
263
    public int getSourceType(String name) {
264
        for (ExportAttribute exportAttribute : this.exportAttributes) {
265
            if (StringUtils.equals(exportAttribute.getNewName(), name)) {
266
                return exportAttribute.getDataType();
267
            }
268
        }
269
        return 0;
270
    }
271

    
272
    @Override
273
    public boolean isAttributeNamesValid() {
274
        if (this.namesTranslator == null) {
275
            return true;
276
        }
277
        for (int i = 0; i < exportAttributes.size(); i++) {
278
            ExportAttribute attr = exportAttributes.get(i);
279
            if (!this.namesTranslator.isValidName(this, i, attr.getNewName())) {
280
                return false;
281
            }
282
        }
283
        return true;
284
    }
285

    
286
    @Override
287
    public void fixAttributeNames() {
288
        if (this.isAttributeNamesValid() == true) {
289
            return;
290
        }
291
        if (this.namesTranslator == null) {
292
            return;
293
        }
294
        int n = 0;
295
        while (!this.isAttributeNamesValid()) {
296
            for (int i = 0; i < exportAttributes.size(); i++) {
297
                ExportAttribute attr = exportAttributes.get(i);
298
                String newName = attr.getNewName();
299
                if (!this.namesTranslator.isValidName(this, i, newName)) {
300
                    String sug = this.namesTranslator.getNameSuggestion(this, i, newName);
301
                    attr.setNewName(sug);
302
                }
303
            }
304
            if (n > 5000) {
305
                LOGGER.warn("Not been able to fix attribute field names, it will require a manual operation");
306
                break;
307
            }
308
            n += 1;
309
        }
310

    
311
    }
312

    
313
    @Override
314
    public ExportAttributes clone() throws CloneNotSupportedException {
315
        DefaultExportAttributes clone = (DefaultExportAttributes) super.clone();
316
        this.namesTranslator.clone();
317
        clone.setNamesTranslator(this.namesTranslator.clone());
318

    
319
        List cloneListAttribute = new ArrayList();
320
        for (ExportAttribute exportAttribute : exportAttributes) {
321
            cloneListAttribute.add(exportAttribute.clone());
322
        }
323
        clone.setExportAttributes(cloneListAttribute);
324

    
325
        return clone;
326
    }
327

    
328
    @Override
329
    public void setExportAttributes(List<ExportAttribute> exportAttributes) {
330
        this.exportAttributes = exportAttributes;
331
    }
332

    
333
    @Override
334
    public void setActive(boolean active) {
335
        this.active = active;
336
    }
337

    
338
    @Override
339
    public boolean isActive() {
340
        return this.active;
341
    }
342

    
343
    public static void registerPersistence() {
344
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
345
        if (manager.getDefinition("ExportAttributes") == null) {
346
            DynStruct definition = manager.addDefinition(DefaultExportAttributes.class,
347
                    "ExportAttributes", "ExportAttributes persistence definition", null, null);
348
            definition.addDynFieldList("exportAttributes").setClassOfItems(ExportAttribute.class);
349
            definition.addDynFieldObject("namesTranslator").setClassOfValue(AttributeNamesTranslator.class);
350
            definition.addDynFieldObject("sourceFeatureType").setClassOfValue(FeatureType.class);
351
            definition.addDynFieldBoolean("active");
352
        }
353
    }
354

    
355
    @Override
356
    public void saveToState(PersistentState state) throws PersistenceException {
357
        state.set("exportAttributes", this.exportAttributes.iterator());
358
        state.set("namesTranslator", this.namesTranslator);
359
        state.set("sourceFeatureType", this.sourceFeatureType);
360
        state.set("active", this.active);
361
    }
362

    
363
    @Override
364
    public void loadFromState(PersistentState state) throws PersistenceException {
365
        Iterator it = state.getIterator("exportAttributes");
366
        List<ExportAttribute> data = new ArrayList<ExportAttribute>();
367
        while (it.hasNext()) {
368
            ExportAttribute ref = (ExportAttribute) it.next();
369
            data.add(ref);
370
        }
371
        this.exportAttributes = data;
372

    
373
        AttributeNamesTranslator nameTranslatorState = (AttributeNamesTranslator) state.get("namesTranslator");
374
        if (nameTranslatorState != null) {
375
            this.namesTranslator = nameTranslatorState;
376
        }
377
        this.sourceFeatureType = (FeatureType) state.get("sourceFeatureType");
378
        this.active = state.getBoolean("active");
379
    }
380

    
381
}