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 @ 44395

History | View | Annotate | Download (9.12 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.feature.EditableFeatureAttributeDescriptor;
16
import org.gvsig.fmap.dal.feature.EditableFeatureType;
17
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
18
import org.gvsig.fmap.dal.feature.FeatureType;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21

    
22
public final class DefaultExportAttributes implements ExportAttributes {
23

    
24
    private List<ExportAttribute> exportAttributes;
25
    private AttributeNamesTranslator namesTranslator = null;
26
    private FeatureType sourceFeatureType;
27
    static final Logger LOGGER = LoggerFactory.getLogger(DefaultExportAttributes.class);
28
    private boolean active;
29

    
30
    public DefaultExportAttributes() { //FeatureType ftype) {
31
//        this.setSourceFeatureType(ftype);
32
        this.namesTranslator = new DummyAttributeNamesTranslator();
33
        this.active = false;
34
    }
35

    
36
    public void fillExportAttributes(FeatureType ftype) {
37
        ArrayList attrs = new ArrayList();
38
        if (ftype != null) {
39
            for (FeatureAttributeDescriptor fad : ftype) {
40
                DefaultExportAttribute exportAttribute = new DefaultExportAttribute(fad);
41
                String newName = this.getNamesTranslator().getNameSuggestion(fad.getName());
42
                exportAttribute.setNewName(newName);
43
                exportAttribute.setNewType(fad.getDataType().getType());
44
                exportAttribute.setSize(fad.getSize());
45
                exportAttribute.setExported(true);
46
                attrs.add(exportAttribute);
47
            }
48
        }
49
        this.setExportAttributes(attrs);
50
        this.fixAttributeNames();
51

    
52
    }
53

    
54
    @Override
55
    public void setNamesTranslator(AttributeNamesTranslator namesTranslator) {
56
        this.namesTranslator = namesTranslator;
57
        if (this.sourceFeatureType != null) {
58
            this.fillExportAttributes(sourceFeatureType);
59
        }
60
    }
61

    
62
    @Override
63
    public AttributeNamesTranslator getNamesTranslator() {
64
        return this.namesTranslator;
65
    }
66

    
67
    @Override
68
    public List<ExportAttribute> toList() {
69
        return this.exportAttributes;
70
    }
71

    
72
    @Override
73
    public boolean isEmpty() {
74
        return this.exportAttributes.isEmpty();
75
    }
76

    
77
    @Override
78
    public int size() {
79
        return this.exportAttributes.size();
80
    }
81

    
82
    @Override
83
    public Iterator<ExportAttribute> iterator() {
84
        return this.exportAttributes.iterator();
85
    }
86

    
87
    @Override
88
    public ExportAttribute get(int position) {
89
        return this.exportAttributes.get(position);
90
    }
91

    
92
    @Override
93
    public ExportAttribute getExportAttribute(String name) {
94
        for (ExportAttribute exportAttribute : this.exportAttributes) {
95
            if (StringUtils.equals(exportAttribute.getName(), name)) {
96
                return exportAttribute;
97
            }
98
        }
99
        return null;
100
    }
101

    
102
    @Override
103
    public FeatureType getTargetFeatureType() {
104
        if (!this.isAttributeNamesValid()) {
105
            this.fixAttributeNames();
106
            LOGGER.warn("An extra fix attributes names in the feature type has been made");
107
        }
108
        EditableFeatureType targetFeatureType = this.getSourceFeatureType().getCopy().getEditable();
109
        for (FeatureAttributeDescriptor attrSource : this.getSourceFeatureType()) {
110
            FeatureAttributeDescriptor attr = targetFeatureType.getAttributeDescriptor(attrSource.getName());
111
            String name = attr.getName();
112
            ExportAttribute exportAttr = this.getExportAttribute(name);
113
            if (!exportAttr.isExported()) {
114
                targetFeatureType.remove(name);
115
                continue;
116
            }
117

    
118
            EditableFeatureAttributeDescriptor eAttr = targetFeatureType.getEditableAttributeDescriptor(name);
119
            if (this.getTargetName(name) == null ? name != null : !this.getTargetName(name).equals(name)) {
120
                eAttr.setName(this.getTargetName(name));
121
            }
122
            int type = attr.getDataType().getType();
123
            if (type != this.getTargetType(name)) {
124
                eAttr.setDataType(this.getTargetType(name));
125
                eAttr.setDefaultValue(null); // TODO: delete default value
126
            }
127
            int size = exportAttr.getSize();
128
            eAttr.setSize(size);
129
        }
130
        return targetFeatureType.getNotEditableCopy();
131
    }
132

    
133
    @Override
134
    public void setSourceFeatureType(FeatureType sourceFeatureType) {
135
        if (sourceFeatureType != this.sourceFeatureType) {
136
            this.sourceFeatureType = sourceFeatureType;
137
            this.fillExportAttributes(this.sourceFeatureType);
138
        }
139
    }
140

    
141
    @Override
142
    public FeatureType getSourceFeatureType() {
143
        return this.sourceFeatureType;
144
    }
145

    
146
    @Override
147
    public String getTargetName(String name) {
148
        for (int i = 0; i < exportAttributes.size(); i++) {
149
            ExportAttribute exportAttribute = exportAttributes.get(i);
150
            if (StringUtils.equals(exportAttribute.getName(), name)) {
151
                String newName = exportAttribute.getNewName();
152
//TODO return value or fixed value?
153
//                if (!this.namesTranslator.isValidName(this, i, newName)) {
154
//                    newName = this.namesTranslator.getNameSuggestion(this, i, newName);
155
//                    //exportAttribute.setNewName(newName); //Change the name in the exportAttribute after chacking
156
//                }
157
                return newName;
158
            }
159
        }
160
        return null;
161
    }
162

    
163
    @Override
164
    public String getSourceName(String name) {
165
        for (ExportAttribute exportAttribute : this.exportAttributes) {
166
            if (StringUtils.equalsIgnoreCase(exportAttribute.getNewName(), name)) {
167
                return exportAttribute.getName();
168
            }
169
        }
170
        return null;
171
    }
172

    
173
    @Override
174
    public int getTargetType(String name) {
175
        for (ExportAttribute exportAttribute : this.exportAttributes) {
176
            if (StringUtils.equals(exportAttribute.getName(), name)) {
177
                return exportAttribute.getNewDataType();
178
            }
179
        }
180
        return 0;
181
    }
182

    
183
    @Override
184
    public int getSourceType(String name) {
185
        for (ExportAttribute exportAttribute : this.exportAttributes) {
186
            if (StringUtils.equals(exportAttribute.getNewName(), name)) {
187
                return exportAttribute.getDataType();
188
            }
189
        }
190
        return 0;
191
    }
192

    
193
    @Override
194
    public boolean isAttributeNamesValid() {
195
        if (this.namesTranslator == null) {
196
            return true;
197
        }
198
        for (int i = 0; i < exportAttributes.size(); i++) {
199
            ExportAttribute attr = exportAttributes.get(i);
200
            if (!this.namesTranslator.isValidName(this, i, attr.getNewName())) {
201
                return false;
202
            }
203
        }
204
        return true;
205
    }
206

    
207
    @Override
208
    public void fixAttributeNames() {
209
        if (this.isAttributeNamesValid() == true) {
210
            return;
211
        }
212
        if (this.namesTranslator == null) {
213
            return;
214
        }
215
        int n = 0;
216
        while (!this.isAttributeNamesValid()) {
217
            for (int i = 0; i < exportAttributes.size(); i++) {
218
                ExportAttribute attr = exportAttributes.get(i);
219
                String newName = attr.getNewName();
220
                if (!this.namesTranslator.isValidName(this, i, newName)) {
221
                    String sug = this.namesTranslator.getNameSuggestion(this, i, newName);
222
                    attr.setNewName(sug);
223
                }
224
            }
225
            if (n > 5000) {
226
                LOGGER.warn("Not been able to fix attribute field names, it will require a manual operation");
227
                break;
228
            }
229
            n += 1;
230
        }
231

    
232
    }
233

    
234
    @Override
235
    public ExportAttributes clone() throws CloneNotSupportedException {
236
        DefaultExportAttributes clone = (DefaultExportAttributes) super.clone();
237
        //clone.setSourceFeatureType(this.sourceFeatureType.getCopy());
238
        this.namesTranslator.clone();
239
        clone.setNamesTranslator(this.namesTranslator.clone());
240
        
241
        List cloneListAttribute = new ArrayList();
242
        for (ExportAttribute exportAttribute : exportAttributes) {
243
            cloneListAttribute.add(exportAttribute.clone());
244
        }
245
        clone.setExportAttributes(cloneListAttribute);
246

    
247
        return clone;
248
    }
249
//    private List<ExportAttribute> exportAttributes;
250
//    private AttributeNamesTranslator namesTranslator = null;
251
//    private FeatureType sourceFeatureType;
252

    
253
    @Override
254
    public void setExportAttributes(List<ExportAttribute> exportAttributes) {
255
        this.exportAttributes = exportAttributes;
256
    }
257

    
258
    @Override
259
    public void setActive(boolean active) {
260
        this.active = active;
261
    }
262

    
263
    @Override
264
    public boolean isActive() {
265
        return this.active;
266
    }
267
    
268
    
269
}