Revision 44860 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

View differences:

DefaultExportAttributes.java
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 1
package org.gvsig.export.impl.service;
7 2

  
8 3
import java.util.ArrayList;
9 4
import java.util.Iterator;
10 5
import java.util.List;
6
import java.util.function.Function;
11 7
import org.apache.commons.lang3.StringUtils;
12 8
import org.gvsig.export.ExportAttributes;
13
import org.gvsig.export.spi.AttributeNamesTranslator;
14
import org.gvsig.export.spi.DummyAttributeNamesTranslator;
15 9
import org.gvsig.fmap.dal.DALLocator;
16 10
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
17 11
import org.gvsig.fmap.dal.feature.EditableFeatureType;
......
23 17
import org.gvsig.tools.dataTypes.DataTypes;
24 18
import org.gvsig.tools.dataTypes.DataTypesManager;
25 19
import org.gvsig.tools.dynobject.DynStruct;
20
import org.gvsig.tools.namestranslator.NamesTranslator;
26 21
import org.gvsig.tools.persistence.PersistenceManager;
27 22
import org.gvsig.tools.persistence.PersistentState;
28 23
import org.gvsig.tools.persistence.exception.PersistenceException;
......
32 27
public final class DefaultExportAttributes implements ExportAttributes {
33 28

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

  
41 36
    public DefaultExportAttributes() {
42
        this.namesTranslator = new DummyAttributeNamesTranslator();
37
        this.namesTranslator = NamesTranslator.createBaseTranslator();
43 38
        this.active = false;
44 39
    }
45 40

  
46 41
    public void fillExportAttributes(FeatureType ftype, FeatureQuery query) {
42

  
47 43
        ArrayList attrs = new ArrayList();
48 44
        if (ftype != null) {
49 45
            for (FeatureAttributeDescriptor fad : ftype) {
50
                DefaultExportAttribute exportAttribute = new DefaultExportAttribute(fad.getCopy());
51
                String newName = this.getNamesTranslator().getNameSuggestion(fad.getName());
52
                exportAttribute.setNewName(newName);
46
                DefaultExportAttribute exportAttribute = new DefaultExportAttribute(fad.getCopy(), this.getNamesTranslator());
53 47
                exportAttribute.setNewType(fad.getDataType().getType());
54 48
                exportAttribute.setSize(fad.getSize());
55 49
                if (query == null || (query != null && query.getGroupByColumns().isEmpty())) {
......
64 58
                attrs.add(exportAttribute);
65 59
            }
66 60
        }
67
        
68
        if (query!=null && query.getExtraColumn().getColumns()!=null && !query.getExtraColumn().getColumns().isEmpty() ) {
61

  
62
        if (query != null && query.getExtraColumn().getColumns() != null && !query.getExtraColumn().getColumns().isEmpty()) {
69 63
            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);
64
                DefaultExportAttribute exportAttribute = new DefaultExportAttribute(fad.getCopy(), this.getNamesTranslator());
65
                this.getNamesTranslator().addSource(fad.getName());
73 66
                exportAttribute.setNewType(fad.getDataType().getType());
74 67
                exportAttribute.setSize(fad.getSize());
75 68
                exportAttribute.setExported(isShowableDataType(fad.getDataType().getType()));
......
77 70
            }
78 71
        }
79 72
        this.setExportAttributes(attrs);
80
        this.fixAttributeNames();
81 73

  
82 74
    }
83
    
75

  
84 76
    protected boolean isShowableDataType(int dataType) {
85 77
        DataTypesManager dataTypemanager = ToolsLocator.getDataTypesManager();
86 78
        if (dataTypemanager.isContainer(dataType)
......
94 86
    }
95 87

  
96 88
    @Override
97
    public void setNamesTranslator(AttributeNamesTranslator namesTranslator) {
89
    public void setNamesTranslator(NamesTranslator namesTranslator) {
98 90
        if (this.namesTranslator != namesTranslator) {
99 91
            this.namesTranslator = namesTranslator;
100 92
            if (this.sourceFeatureType != null) {
101
                this.fillExportAttributes(sourceFeatureType, this.query);
93
                this.namesTranslator.rebuild();
102 94
            }
103 95
        }
104 96
    }
105 97

  
106 98
    @Override
107
    public AttributeNamesTranslator getNamesTranslator() {
99
    public NamesTranslator getNamesTranslator() {
108 100
        return this.namesTranslator;
109 101
    }
110 102

  
......
145 137

  
146 138
    @Override
147 139
    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 140
        EditableFeatureType targetFeatureType = DALLocator.getDataManager().createFeatureType();
153 141
        for (ExportAttribute exportAttribute : this.exportAttributes) {
154 142
            if (!exportAttribute.isExported()) {
......
157 145
            FeatureAttributeDescriptor descriptor = exportAttribute.getDescriptor().getCopy();
158 146
            EditableFeatureAttributeDescriptor targetDescriptor = DALLocator.getDataManager().createFeatureAttributeDescriptor();
159 147
            targetDescriptor.copyFrom(descriptor);
160
            
148

  
161 149
            targetDescriptor.setName(exportAttribute.getNewName());
162
            if( targetDescriptor.isComputed() && !exportAttribute.isComputed() ) {
163
                targetDescriptor.setFeatureAttributeEmulator((FeatureAttributeEmulator)null);
150
            if (targetDescriptor.isComputed() && !exportAttribute.isComputed()) {
151
                targetDescriptor.setFeatureAttributeEmulator((FeatureAttributeEmulator) null);
164 152
            }
165
            if (targetDescriptor.getType()!=exportAttribute.getNewDataType()) {
153
            if (targetDescriptor.getType() != exportAttribute.getNewDataType()) {
166 154
                targetDescriptor.setDefaultValue(null);
167 155
            }
168 156
            targetDescriptor.setDataType(exportAttribute.getNewDataType());
169
            
157

  
170 158
            targetDescriptor.setSize(exportAttribute.getSize());
171
            
159

  
172 160
            targetFeatureType.addLike(targetDescriptor);
173 161
        }
174
        
162

  
175 163
        return targetFeatureType.getNotEditableCopy();
176 164
    }
177 165

  
......
180 168
        this.query = query;
181 169
        if (!sourceFeatureType.equals(this.sourceFeatureType)) {
182 170
            this.sourceFeatureType = sourceFeatureType;
171

  
172
            this.getNamesTranslator().updateSourceNames(sourceFeatureType, new Function() {
173
                @Override
174
                public Object apply(Object t) {
175
                    FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) t;
176
                    return attr.getName();
177
                }
178
            });
183 179
            this.fillExportAttributes(this.sourceFeatureType, query);
184 180
        }
185 181
    }
......
195 191
            ExportAttribute exportAttribute = exportAttributes.get(i);
196 192
            if (StringUtils.equals(exportAttribute.getName(), name)) {
197 193
                String newName = exportAttribute.getNewName();
198
//TODO return value or fixed value?
199
//                if (!this.namesTranslator.isValidName(this, i, newName)) {
200
//                    newName = this.namesTranslator.getNameSuggestion(this, i, newName);
201
//                    //exportAttribute.setNewName(newName); //Change the name in the exportAttribute after chacking
202
//                }
203 194
                return newName;
204 195
            }
205 196
        }
......
237 228
    }
238 229

  
239 230
    @Override
240
    public boolean isAttributeNamesValid() {
241
        if (this.namesTranslator == null) {
242
            return true;
243
        }
244
        for (int i = 0; i < exportAttributes.size(); i++) {
245
            ExportAttribute attr = exportAttributes.get(i);
246
            if (!this.namesTranslator.isValidName(this, i, attr.getNewName())) {
247
                return false;
248
            }
249
        }
250
        return true;
251
    }
252

  
253
    @Override
254
    public void fixAttributeNames() {
255
        if (this.isAttributeNamesValid() == true) {
256
            return;
257
        }
258
        if (this.namesTranslator == null) {
259
            return;
260
        }
261
        int n = 0;
262
        while (!this.isAttributeNamesValid()) {
263
            for (int i = 0; i < exportAttributes.size(); i++) {
264
                ExportAttribute attr = exportAttributes.get(i);
265
                String newName = attr.getNewName();
266
                if (!this.namesTranslator.isValidName(this, i, newName)) {
267
                    String sug = this.namesTranslator.getNameSuggestion(this, i, newName);
268
                    attr.setNewName(sug);
269
                }
270
            }
271
            if (n > 5000) {
272
                LOGGER.warn("Not been able to fix attribute field names, it will require a manual operation");
273
                break;
274
            }
275
            n += 1;
276
        }
277

  
278
    }
279

  
280
    @Override
281 231
    public ExportAttributes clone() throws CloneNotSupportedException {
282 232
        DefaultExportAttributes clone = (DefaultExportAttributes) super.clone();
283
        this.namesTranslator.clone();
284
        clone.setNamesTranslator(this.namesTranslator.clone());
285

  
233
        clone.namesTranslator = this.namesTranslator.clone();
286 234
        List cloneListAttribute = new ArrayList();
287 235
        for (ExportAttribute exportAttribute : exportAttributes) {
288 236
            cloneListAttribute.add(exportAttribute.clone());
......
313 261
            DynStruct definition = manager.addDefinition(DefaultExportAttributes.class,
314 262
                    "ExportAttributes", "ExportAttributes persistence definition", null, null);
315 263
            definition.addDynFieldList("exportAttributes").setClassOfItems(ExportAttribute.class);
316
            definition.addDynFieldObject("namesTranslator").setClassOfValue(AttributeNamesTranslator.class);
264
            definition.addDynFieldObject("namesTranslator").setClassOfValue(NamesTranslator.class);
317 265
            definition.addDynFieldObject("sourceFeatureType").setClassOfValue(FeatureType.class);
318 266
            definition.addDynFieldBoolean("active");
319 267
        }
......
330 278
    @Override
331 279
    public void loadFromState(PersistentState state) throws PersistenceException {
332 280
        Iterator it = state.getIterator("exportAttributes");
333
        List<ExportAttribute> data = new ArrayList<ExportAttribute>();
281
        List<ExportAttribute> data = new ArrayList<>();
334 282
        while (it.hasNext()) {
335 283
            ExportAttribute ref = (ExportAttribute) it.next();
336 284
            data.add(ref);
337 285
        }
338 286
        this.exportAttributes = data;
339 287

  
340
        AttributeNamesTranslator nameTranslatorState = (AttributeNamesTranslator) state.get("namesTranslator");
288
        NamesTranslator nameTranslatorState = (NamesTranslator) state.get("namesTranslator");
341 289
        if (nameTranslatorState != null) {
342 290
            this.namesTranslator = nameTranslatorState;
343 291
        }

Also available in: Unified diff