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

History | View | Annotate | Download (9.1 KB)

1 44270 omartinez
/*
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 44386 omartinez
import java.util.logging.Level;
12 44270 omartinez
import org.apache.commons.collections.ListUtils;
13
import org.apache.commons.lang3.StringUtils;
14
import org.gvsig.export.ExportAttributes;
15
import org.gvsig.export.ExportLocator;
16
import org.gvsig.export.spi.AttributeNamesTranslator;
17 44386 omartinez
import org.gvsig.export.spi.DummyAttributeNamesTranslator;
18 44270 omartinez
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
19
import org.gvsig.fmap.dal.feature.EditableFeatureType;
20
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
21
import org.gvsig.fmap.dal.feature.FeatureType;
22
import org.gvsig.tools.dataTypes.DataType;
23
import org.gvsig.tools.util.UnmodifiableBasicListAdapter;
24 44300 omartinez
import org.slf4j.Logger;
25
import org.slf4j.LoggerFactory;
26 44270 omartinez
27 44300 omartinez
public final class DefaultExportAttributes implements ExportAttributes {
28 44270 omartinez
29
    private List<ExportAttribute> exportAttributes;
30 44300 omartinez
    private AttributeNamesTranslator namesTranslator = null;
31 44270 omartinez
    private FeatureType sourceFeatureType;
32 44300 omartinez
    static final Logger LOGGER = LoggerFactory.getLogger(DefaultExportAttributes.class);
33 44270 omartinez
34 44386 omartinez
    public DefaultExportAttributes() { //FeatureType ftype) {
35
//        this.setSourceFeatureType(ftype);
36
        this.namesTranslator = new DummyAttributeNamesTranslator();
37 44270 omartinez
    }
38
39
    public void fillExportAttributes(FeatureType ftype) {
40
        ArrayList attrs = new ArrayList();
41 44300 omartinez
        if (ftype != null) {
42
            for (FeatureAttributeDescriptor fad : ftype) {
43
                DefaultExportAttribute exportAttribute = new DefaultExportAttribute(fad);
44 44386 omartinez
                String newName = this.getNamesTranslator().getNameSuggestion(fad.getName());
45
                exportAttribute.setNewName(newName);
46 44300 omartinez
                exportAttribute.setNewType(fad.getDataType().getType());
47
                exportAttribute.setSize(fad.getSize());
48
                exportAttribute.setExported(true);
49
                attrs.add(exportAttribute);
50
            }
51 44270 omartinez
        }
52 44386 omartinez
        this.setExportAttributes(attrs);
53
        this.fixAttributeNames();
54 44270 omartinez
55
    }
56
57
    @Override
58
    public void setNamesTranslator(AttributeNamesTranslator namesTranslator) {
59
        this.namesTranslator = namesTranslator;
60 44386 omartinez
        if (this.sourceFeatureType != null) {
61
            this.fillExportAttributes(sourceFeatureType);
62
        }
63 44270 omartinez
    }
64
65
    @Override
66
    public AttributeNamesTranslator getNamesTranslator() {
67
        return this.namesTranslator;
68
    }
69
70
    @Override
71
    public List<ExportAttribute> toList() {
72
        return this.exportAttributes;
73
    }
74
75
    @Override
76
    public boolean isEmpty() {
77
        return this.exportAttributes.isEmpty();
78
    }
79
80
    @Override
81
    public int size() {
82
        return this.exportAttributes.size();
83
    }
84
85
    @Override
86
    public Iterator<ExportAttribute> iterator() {
87
        return this.exportAttributes.iterator();
88
    }
89
90
    @Override
91
    public ExportAttribute get(int position) {
92
        return this.exportAttributes.get(position);
93
    }
94
95
    @Override
96
    public ExportAttribute getExportAttribute(String name) {
97
        for (ExportAttribute exportAttribute : this.exportAttributes) {
98
            if (StringUtils.equals(exportAttribute.getName(), name)) {
99
                return exportAttribute;
100
            }
101
        }
102
        return null;
103
    }
104
105
    @Override
106
    public FeatureType getTargetFeatureType() {
107 44300 omartinez
        if (!this.isAttributeNamesValid()) {
108
            this.fixAttributeNames();
109 44386 omartinez
            LOGGER.warn("An extra fix attributes names in the feature type has been made");
110 44300 omartinez
        }
111 44270 omartinez
        EditableFeatureType targetFeatureType = this.getSourceFeatureType().getCopy().getEditable();
112
        for (FeatureAttributeDescriptor attrSource : this.getSourceFeatureType()) {
113
            FeatureAttributeDescriptor attr = targetFeatureType.getAttributeDescriptor(attrSource.getName());
114
            String name = attr.getName();
115
            ExportAttribute exportAttr = this.getExportAttribute(name);
116
            if (!exportAttr.isExported()) {
117
                targetFeatureType.remove(name);
118
                continue;
119
            }
120 44300 omartinez
121 44270 omartinez
            EditableFeatureAttributeDescriptor eAttr = targetFeatureType.getEditableAttributeDescriptor(name);
122
            if (this.getTargetName(name) == null ? name != null : !this.getTargetName(name).equals(name)) {
123
                eAttr.setName(this.getTargetName(name));
124
            }
125
            int type = attr.getDataType().getType();
126
            if (type != this.getTargetType(name)) {
127
                eAttr.setDataType(this.getTargetType(name));
128
                eAttr.setDefaultValue(null); // TODO: delete default value
129
            }
130
            int size = exportAttr.getSize();
131
            eAttr.setSize(size);
132
        }
133
        return targetFeatureType.getNotEditableCopy();
134
    }
135
136
    @Override
137
    public void setSourceFeatureType(FeatureType sourceFeatureType) {
138 44386 omartinez
        if (sourceFeatureType != this.sourceFeatureType) {
139
            this.sourceFeatureType = sourceFeatureType;
140
            this.fillExportAttributes(this.sourceFeatureType);
141
        }
142 44270 omartinez
    }
143
144
    @Override
145
    public FeatureType getSourceFeatureType() {
146
        return this.sourceFeatureType;
147
    }
148
149
    @Override
150
    public String getTargetName(String name) {
151 44300 omartinez
        for (int i = 0; i < exportAttributes.size(); i++) {
152
            ExportAttribute exportAttribute = exportAttributes.get(i);
153 44270 omartinez
            if (StringUtils.equals(exportAttribute.getName(), name)) {
154 44300 omartinez
                String newName = exportAttribute.getNewName();
155
//TODO return value or fixed value?
156
//                if (!this.namesTranslator.isValidName(this, i, newName)) {
157
//                    newName = this.namesTranslator.getNameSuggestion(this, i, newName);
158
//                    //exportAttribute.setNewName(newName); //Change the name in the exportAttribute after chacking
159
//                }
160
                return newName;
161 44270 omartinez
            }
162
        }
163
        return null;
164
    }
165
166
    @Override
167
    public String getSourceName(String name) {
168
        for (ExportAttribute exportAttribute : this.exportAttributes) {
169 44297 jjdelcerro
            if (StringUtils.equalsIgnoreCase(exportAttribute.getNewName(), name)) {
170 44270 omartinez
                return exportAttribute.getName();
171
            }
172
        }
173
        return null;
174
    }
175
176
    @Override
177
    public int getTargetType(String name) {
178
        for (ExportAttribute exportAttribute : this.exportAttributes) {
179
            if (StringUtils.equals(exportAttribute.getName(), name)) {
180
                return exportAttribute.getNewDataType();
181
            }
182
        }
183
        return 0;
184
    }
185
186
    @Override
187
    public int getSourceType(String name) {
188
        for (ExportAttribute exportAttribute : this.exportAttributes) {
189
            if (StringUtils.equals(exportAttribute.getNewName(), name)) {
190
                return exportAttribute.getDataType();
191
            }
192
        }
193
        return 0;
194
    }
195
196 44300 omartinez
    @Override
197
    public boolean isAttributeNamesValid() {
198 44386 omartinez
        if (this.namesTranslator == null) {
199 44300 omartinez
            return true;
200
        }
201
        for (int i = 0; i < exportAttributes.size(); i++) {
202
            ExportAttribute attr = exportAttributes.get(i);
203
            if (!this.namesTranslator.isValidName(this, i, attr.getNewName())) {
204
                return false;
205
            }
206
        }
207
        return true;
208
    }
209
210
    @Override
211
    public void fixAttributeNames() {
212
        if (this.isAttributeNamesValid() == true) {
213
            return;
214
        }
215 44386 omartinez
        if (this.namesTranslator == null) {
216 44300 omartinez
            return;
217
        }
218
        int n = 0;
219
        while (!this.isAttributeNamesValid()) {
220
            for (int i = 0; i < exportAttributes.size(); i++) {
221
                ExportAttribute attr = exportAttributes.get(i);
222
                String newName = attr.getNewName();
223
                if (!this.namesTranslator.isValidName(this, i, newName)) {
224
                    String sug = this.namesTranslator.getNameSuggestion(this, i, newName);
225
                    attr.setNewName(sug);
226
                }
227
            }
228 44386 omartinez
            if (n > 5000) {
229 44300 omartinez
                LOGGER.warn("Not been able to fix attribute field names, it will require a manual operation");
230
                break;
231
            }
232
            n += 1;
233
        }
234
235
    }
236
237 44386 omartinez
    @Override
238
    public ExportAttributes clone() throws CloneNotSupportedException {
239
        DefaultExportAttributes clone = (DefaultExportAttributes) super.clone();
240
        //clone.setSourceFeatureType(this.sourceFeatureType.getCopy());
241
        this.namesTranslator.clone();
242
        clone.setNamesTranslator(this.namesTranslator.clone());
243
244
        List cloneListAttribute = new ArrayList();
245
        for (ExportAttribute exportAttribute : exportAttributes) {
246
            cloneListAttribute.add(exportAttribute.clone());
247
        }
248
        clone.setExportAttributes(cloneListAttribute);
249
250
        return clone;
251
    }
252
//    private List<ExportAttribute> exportAttributes;
253
//    private AttributeNamesTranslator namesTranslator = null;
254
//    private FeatureType sourceFeatureType;
255
256
    @Override
257
    public void setExportAttributes(List<ExportAttribute> exportAttributes) {
258
        this.exportAttributes = exportAttributes;
259
    }
260 44270 omartinez
}