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