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 @ 44753
History | View | Annotate | Download (14.6 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.FeatureAttributeEmulator; |
19 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
20 |
import org.gvsig.fmap.dal.feature.FeatureType; |
21 |
import org.gvsig.tools.ToolsLocator; |
22 |
import org.gvsig.tools.dataTypes.DataTypes; |
23 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
24 |
import org.gvsig.tools.dynobject.DynStruct; |
25 |
import org.gvsig.tools.persistence.PersistenceManager; |
26 |
import org.gvsig.tools.persistence.PersistentState; |
27 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
28 |
import org.slf4j.Logger; |
29 |
import org.slf4j.LoggerFactory; |
30 |
|
31 |
public final class DefaultExportAttributes implements ExportAttributes { |
32 |
|
33 |
private List<ExportAttribute> exportAttributes; |
34 |
private AttributeNamesTranslator namesTranslator = null; |
35 |
private FeatureType sourceFeatureType;
|
36 |
static final Logger LOGGER = LoggerFactory.getLogger(DefaultExportAttributes.class); |
37 |
private boolean active; |
38 |
private FeatureQuery query = null; |
39 |
|
40 |
public DefaultExportAttributes() {
|
41 |
this.namesTranslator = new DummyAttributeNamesTranslator(); |
42 |
this.active = false; |
43 |
} |
44 |
|
45 |
public void fillExportAttributes(FeatureType ftype, FeatureQuery query) { |
46 |
ArrayList attrs = new ArrayList(); |
47 |
if (ftype != null) { |
48 |
for (FeatureAttributeDescriptor fad : ftype) {
|
49 |
DefaultExportAttribute exportAttribute = new DefaultExportAttribute(fad);
|
50 |
String newName = this.getNamesTranslator().getNameSuggestion(fad.getName()); |
51 |
exportAttribute.setNewName(newName); |
52 |
exportAttribute.setNewType(fad.getDataType().getType()); |
53 |
exportAttribute.setSize(fad.getSize()); |
54 |
if (query == null || (query != null && query.getGroupByColumns().isEmpty())) { |
55 |
exportAttribute.setExported(isShowableDataType(fad.getDataType().getType())); |
56 |
} else {
|
57 |
if (query.getAggregate(fad.getName()) != null || query.getGroupByColumns().contains(fad.getName())) { |
58 |
exportAttribute.setExported(isShowableDataType(fad.getDataType().getType())); |
59 |
} else {
|
60 |
exportAttribute.setExported(false);
|
61 |
} |
62 |
} |
63 |
attrs.add(exportAttribute); |
64 |
} |
65 |
} |
66 |
|
67 |
if (query!=null && query.getExtraColumn().getColumns()!=null && !query.getExtraColumn().getColumns().isEmpty() ) { |
68 |
for (FeatureAttributeDescriptor fad : query.getExtraColumn().getColumns()) {
|
69 |
DefaultExportAttribute exportAttribute = new DefaultExportAttribute(fad);
|
70 |
String newName = this.getNamesTranslator().getNameSuggestion(fad.getName()); |
71 |
exportAttribute.setNewName(newName); |
72 |
exportAttribute.setNewType(fad.getDataType().getType()); |
73 |
exportAttribute.setSize(fad.getSize()); |
74 |
exportAttribute.setExported(isShowableDataType(fad.getDataType().getType())); |
75 |
attrs.add(exportAttribute); |
76 |
} |
77 |
} |
78 |
this.setExportAttributes(attrs);
|
79 |
this.fixAttributeNames();
|
80 |
|
81 |
} |
82 |
|
83 |
protected boolean isShowableDataType(int dataType) { |
84 |
DataTypesManager dataTypemanager = ToolsLocator.getDataTypesManager(); |
85 |
if (dataTypemanager.isContainer(dataType)
|
86 |
|| dataTypemanager.isObject(dataType) |
87 |
|| dataType == DataTypes.ARRAY) { |
88 |
return false; |
89 |
|
90 |
} else {
|
91 |
return true; |
92 |
} |
93 |
} |
94 |
|
95 |
@Override
|
96 |
public void setNamesTranslator(AttributeNamesTranslator namesTranslator) { |
97 |
if (this.namesTranslator != namesTranslator) { |
98 |
this.namesTranslator = namesTranslator;
|
99 |
if (this.sourceFeatureType != null) { |
100 |
this.fillExportAttributes(sourceFeatureType, this.query); |
101 |
} |
102 |
} |
103 |
} |
104 |
|
105 |
@Override
|
106 |
public AttributeNamesTranslator getNamesTranslator() {
|
107 |
return this.namesTranslator; |
108 |
} |
109 |
|
110 |
@Override
|
111 |
public List<ExportAttribute> toList() { |
112 |
return this.exportAttributes; |
113 |
} |
114 |
|
115 |
@Override
|
116 |
public boolean isEmpty() { |
117 |
return this.exportAttributes.isEmpty(); |
118 |
} |
119 |
|
120 |
@Override
|
121 |
public int size() { |
122 |
return this.exportAttributes.size(); |
123 |
} |
124 |
|
125 |
@Override
|
126 |
public Iterator<ExportAttribute> iterator() { |
127 |
return this.exportAttributes.iterator(); |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public ExportAttribute get(int position) { |
132 |
return this.exportAttributes.get(position); |
133 |
} |
134 |
|
135 |
@Override
|
136 |
public ExportAttribute getExportAttribute(String name) { |
137 |
for (ExportAttribute exportAttribute : this.exportAttributes) { |
138 |
if (StringUtils.equals(exportAttribute.getName(), name)) {
|
139 |
return exportAttribute;
|
140 |
} |
141 |
} |
142 |
return null; |
143 |
} |
144 |
|
145 |
@Override
|
146 |
public FeatureType getTargetFeatureType() {
|
147 |
if (!this.isAttributeNamesValid()) { |
148 |
this.fixAttributeNames();
|
149 |
LOGGER.warn("An extra fix attributes names in the feature type has been made");
|
150 |
} |
151 |
EditableFeatureType targetFeatureType = this.getSourceFeatureType().getCopy().getEditable();
|
152 |
for (FeatureAttributeDescriptor attrSource : this.getSourceFeatureType()) { |
153 |
FeatureAttributeDescriptor attr = targetFeatureType.getAttributeDescriptor(attrSource.getName()); |
154 |
String name = attr.getName();
|
155 |
ExportAttribute exportAttr = this.getExportAttribute(name);
|
156 |
if (!exportAttr.isExported()) {
|
157 |
targetFeatureType.remove(name); |
158 |
continue;
|
159 |
} |
160 |
EditableFeatureAttributeDescriptor eAttr = targetFeatureType.getEditableAttributeDescriptor(name); |
161 |
if( eAttr.isComputed() && !exportAttr.isComputed() ) {
|
162 |
eAttr.setFeatureAttributeEmulator((FeatureAttributeEmulator)null);
|
163 |
} |
164 |
if (this.getTargetName(name) == null && name != null && !this.getTargetName(name).equals(name)) { |
165 |
eAttr.setName(this.getTargetName(name));
|
166 |
} |
167 |
int type = attr.getDataType().getType();
|
168 |
if (type != this.getTargetType(name)) { |
169 |
eAttr.setDataType(this.getTargetType(name));
|
170 |
eAttr.setDefaultValue(null); // TODO: delete default value |
171 |
} |
172 |
int size = exportAttr.getSize();
|
173 |
eAttr.setSize(size); |
174 |
} |
175 |
if (this.query!=null && this.query.getExtraColumn().getColumns()!=null && !this.query.getExtraColumn().getColumns().isEmpty()) { |
176 |
for (EditableFeatureAttributeDescriptor attrExtra : this.query.getExtraColumn().getColumns()) { |
177 |
//FeatureAttributeDescriptor attr = targetFeatureType.getAttributeDescriptor(attrSource.getName());
|
178 |
String name = attrExtra.getName();
|
179 |
ExportAttribute exportAttr = this.getExportAttribute(name);
|
180 |
if (!exportAttr.isExported()) {
|
181 |
//targetFeatureType.remove(name);
|
182 |
continue;
|
183 |
} |
184 |
//EditableFeatureAttributeDescriptor eAttr = targetFeatureType.getEditableAttributeDescriptor(name);
|
185 |
// if( eAttr.isComputed() && !exportAttr.isComputed() ) {
|
186 |
// eAttr.setFeatureAttributeEmulator((FeatureAttributeEmulator)null);
|
187 |
// }
|
188 |
if (this.getTargetName(name) == null && name != null && !this.getTargetName(name).equals(name)) { |
189 |
attrExtra.setName(this.getTargetName(name));
|
190 |
} |
191 |
int type = attrExtra.getDataType().getType();
|
192 |
if (type != this.getTargetType(name)) { |
193 |
attrExtra.setDataType(this.getTargetType(name));
|
194 |
attrExtra.setDefaultValue(null); // TODO: delete default value |
195 |
} |
196 |
int size = exportAttr.getSize();
|
197 |
attrExtra.setSize(size); |
198 |
} |
199 |
} |
200 |
|
201 |
return targetFeatureType.getNotEditableCopy();
|
202 |
} |
203 |
|
204 |
@Override
|
205 |
public void setSourceFeatureType(FeatureType sourceFeatureType, FeatureQuery query) { |
206 |
this.query = query;
|
207 |
if (!sourceFeatureType.equals(this.sourceFeatureType)) { |
208 |
this.sourceFeatureType = sourceFeatureType;
|
209 |
this.fillExportAttributes(this.sourceFeatureType, query); |
210 |
} |
211 |
} |
212 |
|
213 |
@Override
|
214 |
public FeatureType getSourceFeatureType() {
|
215 |
return this.sourceFeatureType; |
216 |
} |
217 |
|
218 |
@Override
|
219 |
public String getTargetName(String name) { |
220 |
for (int i = 0; i < exportAttributes.size(); i++) { |
221 |
ExportAttribute exportAttribute = exportAttributes.get(i); |
222 |
if (StringUtils.equals(exportAttribute.getName(), name)) {
|
223 |
String newName = exportAttribute.getNewName();
|
224 |
//TODO return value or fixed value?
|
225 |
// if (!this.namesTranslator.isValidName(this, i, newName)) {
|
226 |
// newName = this.namesTranslator.getNameSuggestion(this, i, newName);
|
227 |
// //exportAttribute.setNewName(newName); //Change the name in the exportAttribute after chacking
|
228 |
// }
|
229 |
return newName;
|
230 |
} |
231 |
} |
232 |
return null; |
233 |
} |
234 |
|
235 |
@Override
|
236 |
public String getSourceName(String name) { |
237 |
for (ExportAttribute exportAttribute : this.exportAttributes) { |
238 |
if (StringUtils.equalsIgnoreCase(exportAttribute.getNewName(), name)) {
|
239 |
return exportAttribute.getName();
|
240 |
} |
241 |
} |
242 |
return null; |
243 |
} |
244 |
|
245 |
@Override
|
246 |
public int getTargetType(String name) { |
247 |
for (ExportAttribute exportAttribute : this.exportAttributes) { |
248 |
if (StringUtils.equals(exportAttribute.getName(), name)) {
|
249 |
return exportAttribute.getNewDataType();
|
250 |
} |
251 |
} |
252 |
return 0; |
253 |
} |
254 |
|
255 |
@Override
|
256 |
public int getSourceType(String name) { |
257 |
for (ExportAttribute exportAttribute : this.exportAttributes) { |
258 |
if (StringUtils.equals(exportAttribute.getNewName(), name)) {
|
259 |
return exportAttribute.getDataType();
|
260 |
} |
261 |
} |
262 |
return 0; |
263 |
} |
264 |
|
265 |
@Override
|
266 |
public boolean isAttributeNamesValid() { |
267 |
if (this.namesTranslator == null) { |
268 |
return true; |
269 |
} |
270 |
for (int i = 0; i < exportAttributes.size(); i++) { |
271 |
ExportAttribute attr = exportAttributes.get(i); |
272 |
if (!this.namesTranslator.isValidName(this, i, attr.getNewName())) { |
273 |
return false; |
274 |
} |
275 |
} |
276 |
return true; |
277 |
} |
278 |
|
279 |
@Override
|
280 |
public void fixAttributeNames() { |
281 |
if (this.isAttributeNamesValid() == true) { |
282 |
return;
|
283 |
} |
284 |
if (this.namesTranslator == null) { |
285 |
return;
|
286 |
} |
287 |
int n = 0; |
288 |
while (!this.isAttributeNamesValid()) { |
289 |
for (int i = 0; i < exportAttributes.size(); i++) { |
290 |
ExportAttribute attr = exportAttributes.get(i); |
291 |
String newName = attr.getNewName();
|
292 |
if (!this.namesTranslator.isValidName(this, i, newName)) { |
293 |
String sug = this.namesTranslator.getNameSuggestion(this, i, newName); |
294 |
attr.setNewName(sug); |
295 |
} |
296 |
} |
297 |
if (n > 5000) { |
298 |
LOGGER.warn("Not been able to fix attribute field names, it will require a manual operation");
|
299 |
break;
|
300 |
} |
301 |
n += 1;
|
302 |
} |
303 |
|
304 |
} |
305 |
|
306 |
@Override
|
307 |
public ExportAttributes clone() throws CloneNotSupportedException { |
308 |
DefaultExportAttributes clone = (DefaultExportAttributes) super.clone();
|
309 |
this.namesTranslator.clone();
|
310 |
clone.setNamesTranslator(this.namesTranslator.clone());
|
311 |
|
312 |
List cloneListAttribute = new ArrayList(); |
313 |
for (ExportAttribute exportAttribute : exportAttributes) {
|
314 |
cloneListAttribute.add(exportAttribute.clone()); |
315 |
} |
316 |
clone.setExportAttributes(cloneListAttribute); |
317 |
|
318 |
return clone;
|
319 |
} |
320 |
|
321 |
@Override
|
322 |
public void setExportAttributes(List<ExportAttribute> exportAttributes) { |
323 |
this.exportAttributes = exportAttributes;
|
324 |
} |
325 |
|
326 |
@Override
|
327 |
public void setActive(boolean active) { |
328 |
this.active = active;
|
329 |
} |
330 |
|
331 |
@Override
|
332 |
public boolean isActive() { |
333 |
return this.active; |
334 |
} |
335 |
|
336 |
public static void registerPersistence() { |
337 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
338 |
if (manager.getDefinition("ExportAttributes") == null) { |
339 |
DynStruct definition = manager.addDefinition(DefaultExportAttributes.class, |
340 |
"ExportAttributes", "ExportAttributes persistence definition", null, null); |
341 |
definition.addDynFieldList("exportAttributes").setClassOfItems(ExportAttribute.class);
|
342 |
definition.addDynFieldObject("namesTranslator").setClassOfValue(AttributeNamesTranslator.class);
|
343 |
definition.addDynFieldObject("sourceFeatureType").setClassOfValue(FeatureType.class);
|
344 |
definition.addDynFieldBoolean("active");
|
345 |
} |
346 |
} |
347 |
|
348 |
@Override
|
349 |
public void saveToState(PersistentState state) throws PersistenceException { |
350 |
state.set("exportAttributes", this.exportAttributes.iterator()); |
351 |
state.set("namesTranslator", this.namesTranslator); |
352 |
state.set("sourceFeatureType", this.sourceFeatureType); |
353 |
state.set("active", this.active); |
354 |
} |
355 |
|
356 |
@Override
|
357 |
public void loadFromState(PersistentState state) throws PersistenceException { |
358 |
Iterator it = state.getIterator("exportAttributes"); |
359 |
List<ExportAttribute> data = new ArrayList<ExportAttribute>(); |
360 |
while (it.hasNext()) {
|
361 |
ExportAttribute ref = (ExportAttribute) it.next(); |
362 |
data.add(ref); |
363 |
} |
364 |
this.exportAttributes = data;
|
365 |
|
366 |
AttributeNamesTranslator nameTranslatorState = (AttributeNamesTranslator) state.get("namesTranslator");
|
367 |
if (nameTranslatorState != null) { |
368 |
this.namesTranslator = nameTranslatorState;
|
369 |
} |
370 |
this.sourceFeatureType = (FeatureType) state.get("sourceFeatureType"); |
371 |
this.active = state.getBoolean("active"); |
372 |
} |
373 |
|
374 |
} |