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

History | View | Annotate | Download (10.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.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.gvsig.tools.ToolsLocator;
20
import org.gvsig.tools.dynobject.DynStruct;
21
import org.gvsig.tools.persistence.PersistenceManager;
22
import org.gvsig.tools.persistence.PersistentState;
23
import org.gvsig.tools.persistence.exception.PersistenceException;
24
import org.slf4j.Logger;
25
import org.slf4j.LoggerFactory;
26

    
27
public final class DefaultExportAttributes implements ExportAttributes {
28

    
29
    private List<ExportAttribute> exportAttributes;
30
    private AttributeNamesTranslator namesTranslator = null;
31
    private FeatureType sourceFeatureType;
32
    static final Logger LOGGER = LoggerFactory.getLogger(DefaultExportAttributes.class);
33
    private boolean active;
34

    
35
    public DefaultExportAttributes() {
36
        this.namesTranslator = new DummyAttributeNamesTranslator();
37
        this.active = false;
38
    }
39

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

    
56
    }
57

    
58
    @Override
59
    public void setNamesTranslator(AttributeNamesTranslator namesTranslator) {
60
        if (this.namesTranslator != namesTranslator) {
61
            this.namesTranslator = namesTranslator;
62
            if (this.sourceFeatureType != null) {
63
                this.fillExportAttributes(sourceFeatureType);
64
            }
65
        }
66
    }
67

    
68
    @Override
69
    public AttributeNamesTranslator getNamesTranslator() {
70
        return this.namesTranslator;
71
    }
72

    
73
    @Override
74
    public List<ExportAttribute> toList() {
75
        return this.exportAttributes;
76
    }
77

    
78
    @Override
79
    public boolean isEmpty() {
80
        return this.exportAttributes.isEmpty();
81
    }
82

    
83
    @Override
84
    public int size() {
85
        return this.exportAttributes.size();
86
    }
87

    
88
    @Override
89
    public Iterator<ExportAttribute> iterator() {
90
        return this.exportAttributes.iterator();
91
    }
92

    
93
    @Override
94
    public ExportAttribute get(int position) {
95
        return this.exportAttributes.get(position);
96
    }
97

    
98
    @Override
99
    public ExportAttribute getExportAttribute(String name) {
100
        for (ExportAttribute exportAttribute : this.exportAttributes) {
101
            if (StringUtils.equals(exportAttribute.getName(), name)) {
102
                return exportAttribute;
103
            }
104
        }
105
        return null;
106
    }
107

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

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

    
139
    @Override
140
    public void setSourceFeatureType(FeatureType sourceFeatureType) {
141
        if (!sourceFeatureType.equals(this.sourceFeatureType)) {
142
            this.sourceFeatureType = sourceFeatureType;
143
            this.fillExportAttributes(this.sourceFeatureType);
144
        }
145
    }
146

    
147
    @Override
148
    public FeatureType getSourceFeatureType() {
149
        return this.sourceFeatureType;
150
    }
151

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

    
169
    @Override
170
    public String getSourceName(String name) {
171
        for (ExportAttribute exportAttribute : this.exportAttributes) {
172
            if (StringUtils.equalsIgnoreCase(exportAttribute.getNewName(), name)) {
173
                return exportAttribute.getName();
174
            }
175
        }
176
        return null;
177
    }
178

    
179
    @Override
180
    public int getTargetType(String name) {
181
        for (ExportAttribute exportAttribute : this.exportAttributes) {
182
            if (StringUtils.equals(exportAttribute.getName(), name)) {
183
                return exportAttribute.getNewDataType();
184
            }
185
        }
186
        return 0;
187
    }
188

    
189
    @Override
190
    public int getSourceType(String name) {
191
        for (ExportAttribute exportAttribute : this.exportAttributes) {
192
            if (StringUtils.equals(exportAttribute.getNewName(), name)) {
193
                return exportAttribute.getDataType();
194
            }
195
        }
196
        return 0;
197
    }
198

    
199
    @Override
200
    public boolean isAttributeNamesValid() {
201
        if (this.namesTranslator == null) {
202
            return true;
203
        }
204
        for (int i = 0; i < exportAttributes.size(); i++) {
205
            ExportAttribute attr = exportAttributes.get(i);
206
            if (!this.namesTranslator.isValidName(this, i, attr.getNewName())) {
207
                return false;
208
            }
209
        }
210
        return true;
211
    }
212

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

    
238
    }
239

    
240
    @Override
241
    public ExportAttributes clone() throws CloneNotSupportedException {
242
        DefaultExportAttributes clone = (DefaultExportAttributes) super.clone();
243
        this.namesTranslator.clone();
244
        clone.setNamesTranslator(this.namesTranslator.clone());
245

    
246
        List cloneListAttribute = new ArrayList();
247
        for (ExportAttribute exportAttribute : exportAttributes) {
248
            cloneListAttribute.add(exportAttribute.clone());
249
        }
250
        clone.setExportAttributes(cloneListAttribute);
251

    
252
        return clone;
253
    }
254

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

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

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

    
270
    public static void registerPersistence() {
271
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
272
        if (manager.getDefinition("ExportAttributes") == null) {
273
            DynStruct definition = manager.addDefinition(DefaultExportAttributes.class,
274
                    "ExportAttributes", "ExportAttributes persistence definition", null, null);
275
            definition.addDynFieldList("exportAttributes").setClassOfItems(ExportAttribute.class);
276
            definition.addDynFieldObject("namesTranslator").setClassOfValue(AttributeNamesTranslator.class);
277
            definition.addDynFieldObject("sourceFeatureType").setClassOfValue(FeatureType.class);
278
            definition.addDynFieldBoolean("active");
279
        }
280
    }
281

    
282
    @Override
283
    public void saveToState(PersistentState state) throws PersistenceException {
284
        state.set("exportAttributes", this.exportAttributes.iterator());
285
        state.set("namesTranslator", this.namesTranslator);
286
        state.set("sourceFeatureType", this.sourceFeatureType);
287
        state.set("active", this.active);
288
    }
289

    
290
    @Override
291
    public void loadFromState(PersistentState state) throws PersistenceException {
292
        Iterator it = state.getIterator("exportAttributes");
293
        List<ExportAttribute> data = new ArrayList<ExportAttribute>();
294
        while (it.hasNext()) {
295
            ExportAttribute ref = (ExportAttribute) it.next();
296
            data.add(ref);
297
        }
298
        this.exportAttributes = data;
299

    
300
        AttributeNamesTranslator nameTranslatorState = (AttributeNamesTranslator) state.get("namesTranslator");
301
        if (nameTranslatorState != null) {
302
            this.namesTranslator = nameTranslatorState;
303
        }
304
        this.sourceFeatureType = (FeatureType) state.get("sourceFeatureType");
305
        this.active = state.getBoolean("active");
306
    }
307

    
308
}