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 @ 44396
History | View | Annotate | Download (7.59 KB)
1 | 44270 | omartinez | /*
|
---|---|---|---|
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 | 44386 | omartinez | import org.gvsig.export.spi.DummyAttributeNamesTranslator; |
15 | 44270 | omartinez | 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 | 44300 | omartinez | import org.slf4j.Logger; |
20 | import org.slf4j.LoggerFactory; |
||
21 | 44270 | omartinez | |
22 | 44300 | omartinez | public final class DefaultExportAttributes implements ExportAttributes { |
23 | 44270 | omartinez | |
24 | 44396 | omartinez | private List<ExportAttribute> exportAttributes; |
25 | private AttributeNamesTranslator namesTranslator = null; |
||
26 | private FeatureType sourceFeatureType;
|
||
27 | static final Logger LOGGER = LoggerFactory.getLogger(DefaultExportAttributes.class); |
||
28 | private boolean active; |
||
29 | 44270 | omartinez | |
30 | 44396 | omartinez | public DefaultExportAttributes() { //FeatureType ftype) { |
31 | 44386 | omartinez | // this.setSourceFeatureType(ftype);
|
32 | 44396 | omartinez | this.namesTranslator = new DummyAttributeNamesTranslator(); |
33 | this.active = false; |
||
34 | } |
||
35 | 44270 | omartinez | |
36 | 44396 | omartinez | public void fillExportAttributes(FeatureType ftype) { |
37 | ArrayList attrs = new ArrayList(); |
||
38 | if (ftype != null) { |
||
39 | for (FeatureAttributeDescriptor fad : ftype) {
|
||
40 | DefaultExportAttribute exportAttribute = new DefaultExportAttribute(fad);
|
||
41 | String newName = this.getNamesTranslator().getNameSuggestion(fad.getName()); |
||
42 | exportAttribute.setNewName(newName); |
||
43 | exportAttribute.setNewType(fad.getDataType().getType()); |
||
44 | exportAttribute.setSize(fad.getSize()); |
||
45 | exportAttribute.setExported(true);
|
||
46 | attrs.add(exportAttribute); |
||
47 | } |
||
48 | } |
||
49 | this.setExportAttributes(attrs);
|
||
50 | this.fixAttributeNames();
|
||
51 | 44270 | omartinez | |
52 | 44396 | omartinez | } |
53 | 44270 | omartinez | |
54 | 44396 | omartinez | @Override
|
55 | public void setNamesTranslator(AttributeNamesTranslator namesTranslator) { |
||
56 | this.namesTranslator = namesTranslator;
|
||
57 | if (this.sourceFeatureType != null) { |
||
58 | this.fillExportAttributes(sourceFeatureType);
|
||
59 | } |
||
60 | } |
||
61 | 44270 | omartinez | |
62 | 44396 | omartinez | @Override
|
63 | public AttributeNamesTranslator getNamesTranslator() {
|
||
64 | return this.namesTranslator; |
||
65 | } |
||
66 | 44270 | omartinez | |
67 | 44396 | omartinez | @Override
|
68 | public List<ExportAttribute> toList() { |
||
69 | return this.exportAttributes; |
||
70 | } |
||
71 | 44270 | omartinez | |
72 | 44396 | omartinez | @Override
|
73 | public boolean isEmpty() { |
||
74 | return this.exportAttributes.isEmpty(); |
||
75 | } |
||
76 | 44270 | omartinez | |
77 | 44396 | omartinez | @Override
|
78 | public int size() { |
||
79 | return this.exportAttributes.size(); |
||
80 | } |
||
81 | 44270 | omartinez | |
82 | 44396 | omartinez | @Override
|
83 | public Iterator<ExportAttribute> iterator() { |
||
84 | return this.exportAttributes.iterator(); |
||
85 | } |
||
86 | 44270 | omartinez | |
87 | 44396 | omartinez | @Override
|
88 | public ExportAttribute get(int position) { |
||
89 | return this.exportAttributes.get(position); |
||
90 | } |
||
91 | 44270 | omartinez | |
92 | 44396 | omartinez | @Override
|
93 | public ExportAttribute getExportAttribute(String name) { |
||
94 | for (ExportAttribute exportAttribute : this.exportAttributes) { |
||
95 | if (StringUtils.equals(exportAttribute.getName(), name)) {
|
||
96 | return exportAttribute;
|
||
97 | } |
||
98 | } |
||
99 | return null; |
||
100 | } |
||
101 | 44270 | omartinez | |
102 | 44396 | omartinez | @Override
|
103 | public FeatureType getTargetFeatureType() {
|
||
104 | if (!this.isAttributeNamesValid()) { |
||
105 | this.fixAttributeNames();
|
||
106 | LOGGER.warn("An extra fix attributes names in the feature type has been made");
|
||
107 | } |
||
108 | EditableFeatureType targetFeatureType = this.getSourceFeatureType().getCopy().getEditable();
|
||
109 | for (FeatureAttributeDescriptor attrSource : this.getSourceFeatureType()) { |
||
110 | FeatureAttributeDescriptor attr = targetFeatureType.getAttributeDescriptor(attrSource.getName()); |
||
111 | String name = attr.getName();
|
||
112 | ExportAttribute exportAttr = this.getExportAttribute(name);
|
||
113 | if (!exportAttr.isExported()) {
|
||
114 | targetFeatureType.remove(name); |
||
115 | continue;
|
||
116 | } |
||
117 | 44300 | omartinez | |
118 | 44396 | omartinez | EditableFeatureAttributeDescriptor eAttr = targetFeatureType.getEditableAttributeDescriptor(name); |
119 | if (this.getTargetName(name) == null ? name != null : !this.getTargetName(name).equals(name)) { |
||
120 | eAttr.setName(this.getTargetName(name));
|
||
121 | } |
||
122 | int type = attr.getDataType().getType();
|
||
123 | if (type != this.getTargetType(name)) { |
||
124 | eAttr.setDataType(this.getTargetType(name));
|
||
125 | eAttr.setDefaultValue(null); // TODO: delete default value |
||
126 | } |
||
127 | int size = exportAttr.getSize();
|
||
128 | eAttr.setSize(size); |
||
129 | } |
||
130 | return targetFeatureType.getNotEditableCopy();
|
||
131 | } |
||
132 | 44270 | omartinez | |
133 | 44396 | omartinez | @Override
|
134 | public void setSourceFeatureType(FeatureType sourceFeatureType) { |
||
135 | if (sourceFeatureType != this.sourceFeatureType) { |
||
136 | this.sourceFeatureType = sourceFeatureType;
|
||
137 | this.fillExportAttributes(this.sourceFeatureType); |
||
138 | } |
||
139 | } |
||
140 | 44270 | omartinez | |
141 | 44396 | omartinez | @Override
|
142 | public FeatureType getSourceFeatureType() {
|
||
143 | return this.sourceFeatureType; |
||
144 | } |
||
145 | 44270 | omartinez | |
146 | 44396 | omartinez | @Override
|
147 | public String getTargetName(String name) { |
||
148 | for (int i = 0; i < exportAttributes.size(); i++) { |
||
149 | ExportAttribute exportAttribute = exportAttributes.get(i); |
||
150 | if (StringUtils.equals(exportAttribute.getName(), name)) {
|
||
151 | String newName = exportAttribute.getNewName();
|
||
152 | 44300 | omartinez | //TODO return value or fixed value?
|
153 | // if (!this.namesTranslator.isValidName(this, i, newName)) {
|
||
154 | // newName = this.namesTranslator.getNameSuggestion(this, i, newName);
|
||
155 | // //exportAttribute.setNewName(newName); //Change the name in the exportAttribute after chacking
|
||
156 | // }
|
||
157 | 44396 | omartinez | return newName;
|
158 | } |
||
159 | } |
||
160 | return null; |
||
161 | } |
||
162 | 44270 | omartinez | |
163 | 44396 | omartinez | @Override
|
164 | public String getSourceName(String name) { |
||
165 | for (ExportAttribute exportAttribute : this.exportAttributes) { |
||
166 | if (StringUtils.equalsIgnoreCase(exportAttribute.getNewName(), name)) {
|
||
167 | return exportAttribute.getName();
|
||
168 | } |
||
169 | } |
||
170 | return null; |
||
171 | } |
||
172 | 44270 | omartinez | |
173 | 44396 | omartinez | @Override
|
174 | public int getTargetType(String name) { |
||
175 | for (ExportAttribute exportAttribute : this.exportAttributes) { |
||
176 | if (StringUtils.equals(exportAttribute.getName(), name)) {
|
||
177 | return exportAttribute.getNewDataType();
|
||
178 | } |
||
179 | } |
||
180 | return 0; |
||
181 | } |
||
182 | 44270 | omartinez | |
183 | 44396 | omartinez | @Override
|
184 | public int getSourceType(String name) { |
||
185 | for (ExportAttribute exportAttribute : this.exportAttributes) { |
||
186 | if (StringUtils.equals(exportAttribute.getNewName(), name)) {
|
||
187 | return exportAttribute.getDataType();
|
||
188 | } |
||
189 | } |
||
190 | return 0; |
||
191 | } |
||
192 | 44270 | omartinez | |
193 | 44396 | omartinez | @Override
|
194 | public boolean isAttributeNamesValid() { |
||
195 | if (this.namesTranslator == null) { |
||
196 | return true; |
||
197 | } |
||
198 | for (int i = 0; i < exportAttributes.size(); i++) { |
||
199 | ExportAttribute attr = exportAttributes.get(i); |
||
200 | if (!this.namesTranslator.isValidName(this, i, attr.getNewName())) { |
||
201 | return false; |
||
202 | } |
||
203 | } |
||
204 | return true; |
||
205 | } |
||
206 | 44300 | omartinez | |
207 | 44396 | omartinez | @Override
|
208 | public void fixAttributeNames() { |
||
209 | if (this.isAttributeNamesValid() == true) { |
||
210 | return;
|
||
211 | } |
||
212 | if (this.namesTranslator == null) { |
||
213 | return;
|
||
214 | } |
||
215 | int n = 0; |
||
216 | while (!this.isAttributeNamesValid()) { |
||
217 | for (int i = 0; i < exportAttributes.size(); i++) { |
||
218 | ExportAttribute attr = exportAttributes.get(i); |
||
219 | String newName = attr.getNewName();
|
||
220 | if (!this.namesTranslator.isValidName(this, i, newName)) { |
||
221 | String sug = this.namesTranslator.getNameSuggestion(this, i, newName); |
||
222 | attr.setNewName(sug); |
||
223 | } |
||
224 | } |
||
225 | if (n > 5000) { |
||
226 | LOGGER.warn("Not been able to fix attribute field names, it will require a manual operation");
|
||
227 | break;
|
||
228 | } |
||
229 | n += 1;
|
||
230 | } |
||
231 | 44300 | omartinez | |
232 | 44396 | omartinez | } |
233 | 44300 | omartinez | |
234 | 44396 | omartinez | @Override
|
235 | public ExportAttributes clone() throws CloneNotSupportedException { |
||
236 | DefaultExportAttributes clone = (DefaultExportAttributes) super.clone();
|
||
237 | this.namesTranslator.clone();
|
||
238 | clone.setNamesTranslator(this.namesTranslator.clone());
|
||
239 | 44386 | omartinez | |
240 | 44396 | omartinez | List cloneListAttribute = new ArrayList(); |
241 | for (ExportAttribute exportAttribute : exportAttributes) {
|
||
242 | cloneListAttribute.add(exportAttribute.clone()); |
||
243 | } |
||
244 | clone.setExportAttributes(cloneListAttribute); |
||
245 | 44386 | omartinez | |
246 | 44396 | omartinez | return clone;
|
247 | } |
||
248 | 44395 | omartinez | |
249 | 44396 | omartinez | @Override
|
250 | public void setExportAttributes(List<ExportAttribute> exportAttributes) { |
||
251 | this.exportAttributes = exportAttributes;
|
||
252 | } |
||
253 | 44395 | omartinez | |
254 | 44396 | omartinez | @Override
|
255 | public void setActive(boolean active) { |
||
256 | this.active = active;
|
||
257 | } |
||
258 | |||
259 | @Override
|
||
260 | public boolean isActive() { |
||
261 | return this.active; |
||
262 | } |
||
263 | |||
264 | 44270 | omartinez | } |