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 |
package org.gvsig.export.impl.service; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Iterator; |
5 |
import java.util.List; |
6 |
import java.util.function.Function; |
7 |
import org.apache.commons.collections.CollectionUtils; |
8 |
import org.apache.commons.lang3.StringUtils; |
9 |
import org.gvsig.export.ExportAttributes; |
10 |
import org.gvsig.fmap.dal.DALLocator; |
11 |
import org.gvsig.fmap.dal.DataTypes; |
12 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
13 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
14 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
15 |
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
16 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
17 |
import org.gvsig.fmap.dal.feature.FeatureType; |
18 |
import org.gvsig.tools.ToolsLocator; |
19 |
import org.gvsig.tools.dataTypes.DataType; |
20 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
21 |
import org.gvsig.tools.dynobject.DynStruct; |
22 |
import org.gvsig.tools.dynobject.Tags; |
23 |
import org.gvsig.tools.namestranslator.NamesTranslator; |
24 |
import org.gvsig.tools.persistence.PersistenceManager; |
25 |
import org.gvsig.tools.persistence.PersistentState; |
26 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
27 |
import org.slf4j.Logger; |
28 |
import org.slf4j.LoggerFactory; |
29 |
|
30 |
public final class DefaultExportAttributes implements ExportAttributes { |
31 |
|
32 |
private List<ExportAttribute> exportAttributes; |
33 |
private NamesTranslator namesTranslator = null; |
34 |
private FeatureType sourceFeatureType;
|
35 |
static final Logger LOGGER = LoggerFactory.getLogger(DefaultExportAttributes.class); |
36 |
private boolean active; |
37 |
private FeatureQuery query = null; |
38 |
|
39 |
public DefaultExportAttributes() {
|
40 |
this.namesTranslator = NamesTranslator.createBaseTranslator();
|
41 |
this.active = false; |
42 |
} |
43 |
|
44 |
public void fillExportAttributes(FeatureType ftype, FeatureQuery query) { |
45 |
|
46 |
ArrayList attrs = new ArrayList(); |
47 |
if (ftype != null) { |
48 |
for (FeatureAttributeDescriptor fad : ftype) {
|
49 |
DefaultExportAttribute exportAttribute = new DefaultExportAttribute(fad.getCopy(), this.getNamesTranslator()); |
50 |
exportAttribute.setNewType(fad.getDataType().getType()); |
51 |
exportAttribute.setSize(fad.getSize()); |
52 |
if (query == null || (query != null && query.getGroupByColumns().isEmpty())) { |
53 |
exportAttribute.setExported(isShowableDataType(fad.getDataType())); |
54 |
} else {
|
55 |
if (query.getAggregate(fad.getName()) != null || query.getGroupByColumns().contains(fad.getName())) { |
56 |
exportAttribute.setExported(isShowableDataType(fad.getDataType())); |
57 |
} else {
|
58 |
exportAttribute.setExported(false);
|
59 |
} |
60 |
} |
61 |
attrs.add(exportAttribute); |
62 |
} |
63 |
} |
64 |
|
65 |
if (query != null && query.getExtraColumn().getColumns() != null && !query.getExtraColumn().getColumns().isEmpty()) { |
66 |
for (FeatureAttributeDescriptor fad : query.getExtraColumn().getColumns()) {
|
67 |
DefaultExportAttribute exportAttribute = new DefaultExportAttribute(fad.getCopy(), this.getNamesTranslator()); |
68 |
this.getNamesTranslator().addSource(fad.getName());
|
69 |
exportAttribute.setNewType(fad.getDataType().getType()); |
70 |
exportAttribute.setSize(fad.getSize()); |
71 |
if (attributeAsComputedAndGroupBy(query, fad.getName())) {
|
72 |
exportAttribute.setComputed(false);
|
73 |
} |
74 |
exportAttribute.setExported(isShowableDataType(fad.getDataType())); |
75 |
attrs.add(exportAttribute); |
76 |
} |
77 |
} |
78 |
this.setExportAttributes(attrs);
|
79 |
|
80 |
} |
81 |
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 |
protected boolean isShowableDataType(DataType dataType) { |
93 |
if ( dataType.getType() != DataTypes.GEOMETRY && (
|
94 |
dataType.isContainer() |
95 |
|| dataType.isObject() |
96 |
|| dataType.getType() == DataTypes.ARRAY)) { |
97 |
return false; |
98 |
|
99 |
} else {
|
100 |
return true; |
101 |
} |
102 |
} |
103 |
|
104 |
|
105 |
@Override
|
106 |
public void setNamesTranslator(NamesTranslator namesTranslator) { |
107 |
if (this.namesTranslator != namesTranslator) { |
108 |
this.namesTranslator = namesTranslator;
|
109 |
if (this.sourceFeatureType != null) { |
110 |
this.namesTranslator.rebuild();
|
111 |
} |
112 |
} |
113 |
} |
114 |
|
115 |
@Override
|
116 |
public NamesTranslator getNamesTranslator() {
|
117 |
return this.namesTranslator; |
118 |
} |
119 |
|
120 |
@Override
|
121 |
public List<ExportAttribute> toList() { |
122 |
return this.exportAttributes; |
123 |
} |
124 |
|
125 |
@Override
|
126 |
public boolean isEmpty() { |
127 |
return this.exportAttributes.isEmpty(); |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public int size() { |
132 |
return this.exportAttributes.size(); |
133 |
} |
134 |
|
135 |
@Override
|
136 |
public Iterator<ExportAttribute> iterator() { |
137 |
return this.exportAttributes.iterator(); |
138 |
} |
139 |
|
140 |
@Override
|
141 |
public ExportAttribute get(int position) { |
142 |
return this.exportAttributes.get(position); |
143 |
} |
144 |
|
145 |
@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 |
|
155 |
@Override
|
156 |
public FeatureType getTargetFeatureType() {
|
157 |
EditableFeatureType targetFeatureType = DALLocator.getDataManager().createFeatureType(); |
158 |
for (ExportAttribute exportAttribute : this.exportAttributes) { |
159 |
if (!exportAttribute.isExported()) {
|
160 |
continue;
|
161 |
} |
162 |
FeatureAttributeDescriptor descriptor = exportAttribute.getDescriptor();//.getCopy();
|
163 |
EditableFeatureAttributeDescriptor targetDescriptor = DALLocator.getDataManager().createFeatureAttributeDescriptor( |
164 |
descriptor.getName(), |
165 |
descriptor.getType() |
166 |
); |
167 |
targetDescriptor.copyFrom(descriptor); |
168 |
|
169 |
targetDescriptor.setName(exportAttribute.getNewName()); |
170 |
if (targetDescriptor.isComputed() && !exportAttribute.isComputed()) {
|
171 |
targetDescriptor.setFeatureAttributeEmulator((FeatureAttributeEmulator) null);
|
172 |
} |
173 |
if (targetDescriptor.getType() != exportAttribute.getNewDataType()) {
|
174 |
targetDescriptor.setDefaultValue(null);
|
175 |
} |
176 |
targetDescriptor.setDataType(exportAttribute.getNewDataType()); |
177 |
|
178 |
targetDescriptor.setSize(exportAttribute.getSize()); |
179 |
|
180 |
targetFeatureType.addLike(targetDescriptor); |
181 |
} |
182 |
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 |
return targetFeatureType.getNotEditableCopy();
|
190 |
} |
191 |
|
192 |
@Override
|
193 |
public void setSourceFeatureType(FeatureType sourceFeatureType, FeatureQuery query) { |
194 |
this.query = query;
|
195 |
if (!sourceFeatureType.equals(this.sourceFeatureType)) { |
196 |
this.sourceFeatureType = sourceFeatureType;
|
197 |
|
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 |
this.fillExportAttributes(this.sourceFeatureType, query); |
206 |
} |
207 |
} |
208 |
|
209 |
@Override
|
210 |
public FeatureType getSourceFeatureType() {
|
211 |
return this.sourceFeatureType; |
212 |
} |
213 |
|
214 |
@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 |
|
226 |
@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 |
|
236 |
@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 |
|
246 |
@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 |
|
256 |
@Override
|
257 |
public ExportAttributes clone() throws CloneNotSupportedException { |
258 |
DefaultExportAttributes clone = (DefaultExportAttributes) super.clone();
|
259 |
clone.namesTranslator = this.namesTranslator.clone();
|
260 |
List cloneListAttribute = new ArrayList(); |
261 |
for (ExportAttribute exportAttribute : exportAttributes) {
|
262 |
cloneListAttribute.add(exportAttribute.clone()); |
263 |
} |
264 |
clone.setExportAttributes(cloneListAttribute); |
265 |
|
266 |
return clone;
|
267 |
} |
268 |
|
269 |
@Override
|
270 |
public void setExportAttributes(List<ExportAttribute> exportAttributes) { |
271 |
this.exportAttributes = exportAttributes;
|
272 |
} |
273 |
|
274 |
@Override
|
275 |
public void setActive(boolean active) { |
276 |
this.active = active;
|
277 |
} |
278 |
|
279 |
@Override
|
280 |
public boolean isActive() { |
281 |
return this.active; |
282 |
} |
283 |
|
284 |
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 |
definition.addDynFieldObject("namesTranslator").setClassOfValue(NamesTranslator.class);
|
291 |
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 |
List<ExportAttribute> data = new ArrayList<>(); |
308 |
while (it.hasNext()) {
|
309 |
ExportAttribute ref = (ExportAttribute) it.next(); |
310 |
data.add(ref); |
311 |
} |
312 |
this.exportAttributes = data;
|
313 |
|
314 |
NamesTranslator nameTranslatorState = (NamesTranslator) state.get("namesTranslator");
|
315 |
if (nameTranslatorState != null) { |
316 |
this.namesTranslator = nameTranslatorState;
|
317 |
} |
318 |
this.sourceFeatureType = (FeatureType) state.get("sourceFeatureType"); |
319 |
this.active = state.getBoolean("active"); |
320 |
} |
321 |
|
322 |
} |