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

History | View | Annotate | Download (11.8 KB)

1 44270 omartinez
package org.gvsig.export.impl.service;
2
3
import java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.List;
6 44860 omartinez
import java.util.function.Function;
7 45162 omartinez
import org.apache.commons.collections.CollectionUtils;
8 44270 omartinez
import org.apache.commons.lang3.StringUtils;
9
import org.gvsig.export.ExportAttributes;
10 44801 omartinez
import org.gvsig.fmap.dal.DALLocator;
11 45008 omartinez
import org.gvsig.fmap.dal.DataTypes;
12 44270 omartinez
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
13
import org.gvsig.fmap.dal.feature.EditableFeatureType;
14
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
15 44572 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
16 44753 omartinez
import org.gvsig.fmap.dal.feature.FeatureQuery;
17 44270 omartinez
import org.gvsig.fmap.dal.feature.FeatureType;
18 44411 omartinez
import org.gvsig.tools.ToolsLocator;
19 45008 omartinez
import org.gvsig.tools.dataTypes.DataType;
20 44753 omartinez
import org.gvsig.tools.dataTypes.DataTypesManager;
21 44411 omartinez
import org.gvsig.tools.dynobject.DynStruct;
22 46070 jjdelcerro
import org.gvsig.tools.dynobject.Tags;
23 44860 omartinez
import org.gvsig.tools.namestranslator.NamesTranslator;
24 44411 omartinez
import org.gvsig.tools.persistence.PersistenceManager;
25
import org.gvsig.tools.persistence.PersistentState;
26
import org.gvsig.tools.persistence.exception.PersistenceException;
27 44300 omartinez
import org.slf4j.Logger;
28
import org.slf4j.LoggerFactory;
29 44270 omartinez
30 44300 omartinez
public final class DefaultExportAttributes implements ExportAttributes {
31 44270 omartinez
32 44411 omartinez
    private List<ExportAttribute> exportAttributes;
33 44860 omartinez
    private NamesTranslator namesTranslator = null;
34 44411 omartinez
    private FeatureType sourceFeatureType;
35
    static final Logger LOGGER = LoggerFactory.getLogger(DefaultExportAttributes.class);
36
    private boolean active;
37 44753 omartinez
    private FeatureQuery query = null;
38 44270 omartinez
39 44411 omartinez
    public DefaultExportAttributes() {
40 44860 omartinez
        this.namesTranslator = NamesTranslator.createBaseTranslator();
41 44411 omartinez
        this.active = false;
42
    }
43 44270 omartinez
44 44753 omartinez
    public void fillExportAttributes(FeatureType ftype, FeatureQuery query) {
45 44860 omartinez
46 44411 omartinez
        ArrayList attrs = new ArrayList();
47
        if (ftype != null) {
48
            for (FeatureAttributeDescriptor fad : ftype) {
49 44860 omartinez
                DefaultExportAttribute exportAttribute = new DefaultExportAttribute(fad.getCopy(), this.getNamesTranslator());
50 44411 omartinez
                exportAttribute.setNewType(fad.getDataType().getType());
51
                exportAttribute.setSize(fad.getSize());
52 44753 omartinez
                if (query == null || (query != null && query.getGroupByColumns().isEmpty())) {
53 45008 omartinez
                    exportAttribute.setExported(isShowableDataType(fad.getDataType()));
54 44753 omartinez
                } else {
55
                    if (query.getAggregate(fad.getName()) != null || query.getGroupByColumns().contains(fad.getName())) {
56 45008 omartinez
                        exportAttribute.setExported(isShowableDataType(fad.getDataType()));
57 44753 omartinez
                    } else {
58
                        exportAttribute.setExported(false);
59
                    }
60
                }
61 44411 omartinez
                attrs.add(exportAttribute);
62
            }
63
        }
64 44860 omartinez
65
        if (query != null && query.getExtraColumn().getColumns() != null && !query.getExtraColumn().getColumns().isEmpty()) {
66 44753 omartinez
            for (FeatureAttributeDescriptor fad : query.getExtraColumn().getColumns()) {
67 44860 omartinez
                DefaultExportAttribute exportAttribute = new DefaultExportAttribute(fad.getCopy(), this.getNamesTranslator());
68
                this.getNamesTranslator().addSource(fad.getName());
69 44753 omartinez
                exportAttribute.setNewType(fad.getDataType().getType());
70
                exportAttribute.setSize(fad.getSize());
71 45162 omartinez
                if (attributeAsComputedAndGroupBy(query, fad.getName())) {
72
                    exportAttribute.setComputed(false);
73
                }
74 45008 omartinez
                exportAttribute.setExported(isShowableDataType(fad.getDataType()));
75 44753 omartinez
                attrs.add(exportAttribute);
76
            }
77
        }
78 44411 omartinez
        this.setExportAttributes(attrs);
79 44270 omartinez
80 44411 omartinez
    }
81 45162 omartinez
    protected boolean attributeAsComputedAndGroupBy(FeatureQuery query, String attr) {
82
        if (!query.hasGroupByColumns()) {
83
            return false;
84
        }
85
        List<String> groupByCols = query.getGroupByColumns();
86
        if (groupByCols.isEmpty()) {
87
            return false;
88
        }
89
        return groupByCols.contains(attr);
90
    }
91
92 45008 omartinez
    protected boolean isShowableDataType(DataType dataType) {
93
        if ( dataType.getType() != DataTypes.GEOMETRY && (
94
                 dataType.isContainer()
95
                || dataType.isObject()
96
                || dataType.getType() == DataTypes.ARRAY)) {
97 44753 omartinez
            return false;
98 44270 omartinez
99 44753 omartinez
        } else {
100
            return true;
101
        }
102
    }
103 45008 omartinez
104 44753 omartinez
105 44411 omartinez
    @Override
106 44860 omartinez
    public void setNamesTranslator(NamesTranslator namesTranslator) {
107 44411 omartinez
        if (this.namesTranslator != namesTranslator) {
108
            this.namesTranslator = namesTranslator;
109
            if (this.sourceFeatureType != null) {
110 44860 omartinez
                this.namesTranslator.rebuild();
111 44411 omartinez
            }
112
        }
113
    }
114 44270 omartinez
115 44411 omartinez
    @Override
116 44860 omartinez
    public NamesTranslator getNamesTranslator() {
117 44411 omartinez
        return this.namesTranslator;
118
    }
119 44270 omartinez
120 44411 omartinez
    @Override
121
    public List<ExportAttribute> toList() {
122
        return this.exportAttributes;
123
    }
124 44270 omartinez
125 44411 omartinez
    @Override
126
    public boolean isEmpty() {
127
        return this.exportAttributes.isEmpty();
128
    }
129 44270 omartinez
130 44411 omartinez
    @Override
131
    public int size() {
132
        return this.exportAttributes.size();
133
    }
134 44270 omartinez
135 44411 omartinez
    @Override
136
    public Iterator<ExportAttribute> iterator() {
137
        return this.exportAttributes.iterator();
138
    }
139 44270 omartinez
140 44411 omartinez
    @Override
141
    public ExportAttribute get(int position) {
142
        return this.exportAttributes.get(position);
143
    }
144 44270 omartinez
145 44411 omartinez
    @Override
146
    public ExportAttribute getExportAttribute(String name) {
147
        for (ExportAttribute exportAttribute : this.exportAttributes) {
148
            if (StringUtils.equals(exportAttribute.getName(), name)) {
149
                return exportAttribute;
150
            }
151
        }
152
        return null;
153
    }
154 44270 omartinez
155 44411 omartinez
    @Override
156
    public FeatureType getTargetFeatureType() {
157 44810 omartinez
        EditableFeatureType targetFeatureType = DALLocator.getDataManager().createFeatureType();
158
        for (ExportAttribute exportAttribute : this.exportAttributes) {
159
            if (!exportAttribute.isExported()) {
160 44411 omartinez
                continue;
161
            }
162 46013 jjdelcerro
            FeatureAttributeDescriptor descriptor = exportAttribute.getDescriptor();//.getCopy();
163
            EditableFeatureAttributeDescriptor targetDescriptor = DALLocator.getDataManager().createFeatureAttributeDescriptor(
164
                    descriptor.getName(),
165
                    descriptor.getType()
166
            );
167 44810 omartinez
            targetDescriptor.copyFrom(descriptor);
168 44860 omartinez
169 44810 omartinez
            targetDescriptor.setName(exportAttribute.getNewName());
170 44860 omartinez
            if (targetDescriptor.isComputed() && !exportAttribute.isComputed()) {
171
                targetDescriptor.setFeatureAttributeEmulator((FeatureAttributeEmulator) null);
172 44572 jjdelcerro
            }
173 44860 omartinez
            if (targetDescriptor.getType() != exportAttribute.getNewDataType()) {
174 44810 omartinez
                targetDescriptor.setDefaultValue(null);
175 44411 omartinez
            }
176 44810 omartinez
            targetDescriptor.setDataType(exportAttribute.getNewDataType());
177 44860 omartinez
178 44810 omartinez
            targetDescriptor.setSize(exportAttribute.getSize());
179 44860 omartinez
180 44810 omartinez
            targetFeatureType.addLike(targetDescriptor);
181 44411 omartinez
        }
182 46070 jjdelcerro
        Tags srcTags = sourceFeatureType.getTags();
183
        if( srcTags!=null && !srcTags.isEmpty() ) {
184
            Tags targetTags = targetFeatureType.getTags();
185
            for (String tagName : srcTags) {
186
                targetTags.set(tagName, srcTags.get(tagName));
187
            }
188
        }
189 44411 omartinez
        return targetFeatureType.getNotEditableCopy();
190
    }
191 44270 omartinez
192 44411 omartinez
    @Override
193 44753 omartinez
    public void setSourceFeatureType(FeatureType sourceFeatureType, FeatureQuery query) {
194
        this.query = query;
195 44411 omartinez
        if (!sourceFeatureType.equals(this.sourceFeatureType)) {
196
            this.sourceFeatureType = sourceFeatureType;
197 44860 omartinez
198
            this.getNamesTranslator().updateSourceNames(sourceFeatureType, new Function() {
199
                @Override
200
                public Object apply(Object t) {
201
                    FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) t;
202
                    return attr.getName();
203
                }
204
            });
205 44753 omartinez
            this.fillExportAttributes(this.sourceFeatureType, query);
206 44411 omartinez
        }
207
    }
208 44270 omartinez
209 44411 omartinez
    @Override
210
    public FeatureType getSourceFeatureType() {
211
        return this.sourceFeatureType;
212
    }
213 44270 omartinez
214 44411 omartinez
    @Override
215
    public String getTargetName(String name) {
216
        for (int i = 0; i < exportAttributes.size(); i++) {
217
            ExportAttribute exportAttribute = exportAttributes.get(i);
218
            if (StringUtils.equals(exportAttribute.getName(), name)) {
219
                String newName = exportAttribute.getNewName();
220
                return newName;
221
            }
222
        }
223
        return null;
224
    }
225 44270 omartinez
226 44411 omartinez
    @Override
227
    public String getSourceName(String name) {
228
        for (ExportAttribute exportAttribute : this.exportAttributes) {
229
            if (StringUtils.equalsIgnoreCase(exportAttribute.getNewName(), name)) {
230
                return exportAttribute.getName();
231
            }
232
        }
233
        return null;
234
    }
235 44270 omartinez
236 44411 omartinez
    @Override
237
    public int getTargetType(String name) {
238
        for (ExportAttribute exportAttribute : this.exportAttributes) {
239
            if (StringUtils.equals(exportAttribute.getName(), name)) {
240
                return exportAttribute.getNewDataType();
241
            }
242
        }
243
        return 0;
244
    }
245 44270 omartinez
246 44411 omartinez
    @Override
247
    public int getSourceType(String name) {
248
        for (ExportAttribute exportAttribute : this.exportAttributes) {
249
            if (StringUtils.equals(exportAttribute.getNewName(), name)) {
250
                return exportAttribute.getDataType();
251
            }
252
        }
253
        return 0;
254
    }
255 44270 omartinez
256 44411 omartinez
    @Override
257
    public ExportAttributes clone() throws CloneNotSupportedException {
258
        DefaultExportAttributes clone = (DefaultExportAttributes) super.clone();
259 44860 omartinez
        clone.namesTranslator = this.namesTranslator.clone();
260 44411 omartinez
        List cloneListAttribute = new ArrayList();
261
        for (ExportAttribute exportAttribute : exportAttributes) {
262
            cloneListAttribute.add(exportAttribute.clone());
263
        }
264
        clone.setExportAttributes(cloneListAttribute);
265 44386 omartinez
266 44411 omartinez
        return clone;
267
    }
268 44395 omartinez
269 44411 omartinez
    @Override
270
    public void setExportAttributes(List<ExportAttribute> exportAttributes) {
271
        this.exportAttributes = exportAttributes;
272
    }
273 44395 omartinez
274 44411 omartinez
    @Override
275
    public void setActive(boolean active) {
276
        this.active = active;
277
    }
278 44396 omartinez
279 44411 omartinez
    @Override
280
    public boolean isActive() {
281
        return this.active;
282
    }
283 44396 omartinez
284 44411 omartinez
    public static void registerPersistence() {
285
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
286
        if (manager.getDefinition("ExportAttributes") == null) {
287
            DynStruct definition = manager.addDefinition(DefaultExportAttributes.class,
288
                    "ExportAttributes", "ExportAttributes persistence definition", null, null);
289
            definition.addDynFieldList("exportAttributes").setClassOfItems(ExportAttribute.class);
290 44860 omartinez
            definition.addDynFieldObject("namesTranslator").setClassOfValue(NamesTranslator.class);
291 44411 omartinez
            definition.addDynFieldObject("sourceFeatureType").setClassOfValue(FeatureType.class);
292
            definition.addDynFieldBoolean("active");
293
        }
294
    }
295
296
    @Override
297
    public void saveToState(PersistentState state) throws PersistenceException {
298
        state.set("exportAttributes", this.exportAttributes.iterator());
299
        state.set("namesTranslator", this.namesTranslator);
300
        state.set("sourceFeatureType", this.sourceFeatureType);
301
        state.set("active", this.active);
302
    }
303
304
    @Override
305
    public void loadFromState(PersistentState state) throws PersistenceException {
306
        Iterator it = state.getIterator("exportAttributes");
307 44860 omartinez
        List<ExportAttribute> data = new ArrayList<>();
308 44411 omartinez
        while (it.hasNext()) {
309
            ExportAttribute ref = (ExportAttribute) it.next();
310
            data.add(ref);
311
        }
312
        this.exportAttributes = data;
313
314 44860 omartinez
        NamesTranslator nameTranslatorState = (NamesTranslator) state.get("namesTranslator");
315 44411 omartinez
        if (nameTranslatorState != null) {
316
            this.namesTranslator = nameTranslatorState;
317
        }
318
        this.sourceFeatureType = (FeatureType) state.get("sourceFeatureType");
319
        this.active = state.getBoolean("active");
320
    }
321
322 44270 omartinez
}