29 |
******************************************************************************/ |
******************************************************************************/ |
30 |
package skrueger; |
package skrueger; |
31 |
|
|
32 |
|
import java.util.HashSet; |
33 |
|
import java.util.List; |
34 |
|
import java.util.Set; |
35 |
|
|
36 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
37 |
import org.geotools.feature.NameImpl; |
import org.geotools.feature.NameImpl; |
38 |
import org.opengis.feature.type.AttributeDescriptor; |
import org.opengis.feature.type.AttributeDescriptor; |
40 |
|
|
41 |
import skrueger.geotools.Copyable; |
import skrueger.geotools.Copyable; |
42 |
import skrueger.geotools.StyledLayerInterface; |
import skrueger.geotools.StyledLayerInterface; |
43 |
|
import skrueger.i8n.I8NUtil; |
44 |
import skrueger.i8n.Translation; |
import skrueger.i8n.Translation; |
45 |
|
|
46 |
/** |
/** |
47 |
* This class holds meta information about an attribute/column. This information |
* This class holds meta information about an attribute/column. This information |
48 |
* is used by {@link StyledLayerInterface}. |
* is used by {@link StyledLayerInterface} and many others.<br/> |
49 |
* |
* |
50 |
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
51 |
*/ |
*/ |
52 |
public class AttributeMetadata implements Copyable<AttributeMetadata> { |
public class AttributeMetadata implements Copyable<AttributeMetadata>, |
53 |
|
Comparable<AttributeMetadata> { |
54 |
|
|
55 |
static private final Logger LOGGER = Logger |
static private final Logger LOGGER = Logger |
56 |
.getLogger(AttributeMetadata.class); |
.getLogger(AttributeMetadata.class); |
57 |
|
|
58 |
protected Translation title = new Translation(); |
/** Translation of the attribute's description **/ |
59 |
protected Translation desc = new Translation(); |
protected Translation desc = new Translation(); |
60 |
protected boolean visible = true; |
|
61 |
protected String unit = ""; |
/** |
62 |
protected int colIdx; |
* For numerical attributes the value can be transformed by VALUE*X+A when |
63 |
|
* presented on screen. TODO not implemented yet |
64 |
|
**/ |
65 |
|
protected Double functionA = 0.; |
66 |
|
|
67 |
|
/** |
68 |
|
* For numerical attributes the value can be transformed by VALUE*X+A when |
69 |
|
* presented on screen. TODO not implemented yet |
70 |
|
**/ |
71 |
|
protected Double functionX = 1.; |
72 |
|
|
73 |
|
/** The Name of the attribute **/ |
74 |
private Name name; |
private Name name; |
75 |
|
|
76 |
/** |
/** |
77 |
|
* Allows to define general NODATA values for an attribute. e.g. -9999 can |
78 |
|
* be set and will always be interpreted as NULL internally and will usually |
79 |
|
* be ignored. This overcomes the problem, that |
80 |
|
**/ |
81 |
|
protected HashSet<Object> nodataValues = new HashSet<Object>(); |
82 |
|
|
83 |
|
/** Translation of the attribute's title **/ |
84 |
|
protected Translation title = new Translation(); |
85 |
|
|
86 |
|
/** |
87 |
|
* The unit append to all visualizations of values of this attribute (is not |
88 |
|
* null) |
89 |
|
**/ |
90 |
|
protected String unit = ""; |
91 |
|
|
92 |
|
/** Is the attribute visible to the user or ignored where possible **/ |
93 |
|
protected boolean visible = true; |
94 |
|
|
95 |
|
/** |
96 |
|
* When listed, the attributes are listed according to their {@link #weight} |
97 |
|
* (heavier => further down) |
98 |
|
* |
99 |
|
* @see #compareTo(AttributeMetadata) |
100 |
|
**/ |
101 |
|
protected int weight = 0; |
102 |
|
|
103 |
|
/** Only used for {@link Copyable<AttributeMetaData>#copy()} **/ |
104 |
|
private AttributeMetadata() { |
105 |
|
} |
106 |
|
|
107 |
|
public AttributeMetadata(final AttributeDescriptor attDesc, |
108 |
|
final int weight, final List<String> langs) { |
109 |
|
this(attDesc.getName(), langs); |
110 |
|
setWeight(weight); |
111 |
|
} |
112 |
|
|
113 |
|
public AttributeMetadata(final AttributeDescriptor attDesc, |
114 |
|
final List<String> langs) { |
115 |
|
this(attDesc.getName(), langs); |
116 |
|
} |
117 |
|
|
118 |
|
/** |
119 |
* Creates an {@link AttributeMetadata} object with the following |
* Creates an {@link AttributeMetadata} object with the following |
120 |
* information |
* information |
121 |
* |
* |
137 |
this.setName(name); |
this.setName(name); |
138 |
this.title = title; |
this.title = title; |
139 |
this.desc = desc; |
this.desc = desc; |
140 |
|
this.visible = visible; |
141 |
// The THE_GEOM and shall never be visible! |
this.unit = unit; |
142 |
if (name.getLocalPart().equalsIgnoreCase("the_geom")) this.visible = false; |
} |
143 |
|
|
144 |
|
/** |
145 |
|
* Creates an {@link AttributeMetadata} object with the following |
146 |
|
* information |
147 |
|
* |
148 |
|
* @param colIdx |
149 |
|
* The column index of this attribute in the underlying |
150 |
|
* table/dbf/etc... |
151 |
|
* @param visible |
152 |
|
* Shall this attribute be displayed or hidden from the user? |
153 |
|
* @param unit |
154 |
|
* {@link String} of the unit that the information is in |
155 |
|
*/ |
156 |
|
public AttributeMetadata(final Name name, final Boolean visible, |
157 |
|
final String unit) { |
158 |
|
this.setName(name); |
159 |
|
this.visible = visible; |
160 |
this.unit = unit; |
this.unit = unit; |
161 |
} |
} |
162 |
|
|
163 |
/** |
/** |
164 |
|
* Creates a new visible {@link AttributeMetadata} |
165 |
|
*/ |
166 |
|
public AttributeMetadata(final Name name, final List<String> langs) { |
167 |
|
this(name, true, new Translation(langs, name.getLocalPart()), |
168 |
|
new Translation(), ""); |
169 |
|
} |
170 |
|
|
171 |
|
/** |
172 |
|
* Creates a new visible {@link AttributeMetadata} |
173 |
|
*/ |
174 |
|
public AttributeMetadata(final Name name, final String defaultTitle, |
175 |
|
final List<String> langs) { |
176 |
|
this(name, true, new Translation(langs, defaultTitle), |
177 |
|
new Translation(), ""); |
178 |
|
} |
179 |
|
|
180 |
|
/** |
181 |
* Creates an {@link AttributeMetadata} object with the following |
* Creates an {@link AttributeMetadata} object with the following |
182 |
* information |
* information |
183 |
* |
* |
198 |
/** |
/** |
199 |
* Creates a new visible {@link AttributeMetadata} with default (no) values. |
* Creates a new visible {@link AttributeMetadata} with default (no) values. |
200 |
*/ |
*/ |
201 |
public AttributeMetadata(final String localName, final String defaultTitle) { |
public AttributeMetadata(final String localName, final List<String> langs) { |
202 |
this(localName, true, new Translation(defaultTitle), new Translation(), |
this(localName, true, new Translation(langs, localName), |
203 |
""); |
new Translation(), ""); |
204 |
} |
} |
205 |
|
|
206 |
/** |
/** |
207 |
* Creates a new visible {@link AttributeMetadata} with default (no) values. |
* Creates a new visible {@link AttributeMetadata} |
208 |
*/ |
*/ |
209 |
public AttributeMetadata(final Name name, final String defaultTitle) { |
public AttributeMetadata(final String localName, final String defaultTitle, |
210 |
this(name, true, new Translation(defaultTitle), new Translation(), ""); |
final List<String> langs) { |
211 |
|
this(localName, true, new Translation(langs, defaultTitle), |
212 |
|
new Translation(), ""); |
213 |
} |
} |
214 |
|
|
215 |
/** |
/** |
216 |
* Creates a new visible {@link AttributeMetadata} with default (no) values. |
* Orders the attributes according to their {@link #weight}. Heavier => |
217 |
|
* further down. |
218 |
*/ |
*/ |
219 |
public AttributeMetadata(final Name name) { |
@Override |
220 |
this(name, true, new Translation(name.getLocalPart()), |
public int compareTo(final AttributeMetadata atm2) { |
221 |
new Translation(), ""); |
return new Integer(weight).compareTo(atm2.getWeight()); |
222 |
} |
} |
223 |
|
|
224 |
/** |
/** |
225 |
* Creates a new visible {@link AttributeMetadata} with default (no) values. |
* @see Copyable inferface |
226 |
*/ |
*/ |
227 |
public AttributeMetadata(final String localName) { |
@Override |
228 |
this(localName, true, new Translation(localName), new Translation(), ""); |
public AttributeMetadata copy() { |
229 |
|
return copyTo(new AttributeMetadata()); |
230 |
} |
} |
231 |
|
|
232 |
/** Only used for {@link Copyable<AttributeMetaData>#copy()} **/ |
/** |
233 |
private AttributeMetadata() { |
* @see Copyable inferface |
234 |
|
*/ |
235 |
|
@Override |
236 |
|
public AttributeMetadata copyTo(final AttributeMetadata amd) { |
237 |
|
getTitle().copyTo(amd.getTitle()); |
238 |
|
getDesc().copyTo(amd.getDesc()); |
239 |
|
amd.setUnit(getUnit()); |
240 |
|
amd.setVisible(isVisible()); |
241 |
|
amd.setName(new NameImpl(getName().getNamespaceURI(), getName() |
242 |
|
.getLocalPart())); |
243 |
|
|
244 |
|
amd.setWeight(getWeight()); |
245 |
|
amd.setFunctionX(getFunctionX()); |
246 |
|
amd.setFunctionA(getFunctionA()); |
247 |
|
|
248 |
|
amd.setNodataValues(getNodataValues()); |
249 |
|
|
250 |
|
return amd; |
251 |
} |
} |
252 |
|
|
253 |
public AttributeMetadata(AttributeDescriptor attDesc) { |
// only to be used by copyTo() |
254 |
this(attDesc.getName()); |
private void setNodataValues(HashSet<Object> nodataValues2) { |
255 |
|
nodataValues = nodataValues2; |
256 |
} |
} |
257 |
|
|
258 |
public boolean isVisible() { |
public Translation getDesc() { |
259 |
return visible; |
return desc; |
260 |
} |
} |
261 |
|
|
262 |
public void setVisible(final Boolean visible) { |
public Double getFunctionA() { |
263 |
this.visible = visible; |
return functionA; |
264 |
|
} |
265 |
|
|
266 |
|
public Double getFunctionX() { |
267 |
|
return functionX; |
268 |
|
} |
269 |
|
|
270 |
|
/** |
271 |
|
* The local name. E.g. the name of the DBF column as a {@link String} |
272 |
|
*/ |
273 |
|
public String getLocalName() { |
274 |
|
return getName().getLocalPart(); |
275 |
|
} |
276 |
|
|
277 |
|
/** |
278 |
|
* The fully qualified {@link Name} of the attribute, e.g. |
279 |
|
* <code>org.bla.plo:blub</code> |
280 |
|
*/ |
281 |
|
public Name getName() { |
282 |
|
return name; |
283 |
|
} |
284 |
|
|
285 |
|
public HashSet<Object> getNodataValues() { |
286 |
|
return nodataValues; |
287 |
|
} |
288 |
|
|
289 |
|
/** |
290 |
|
* @return a number between 0 (bad) and 1 (good) that is calculated from the |
291 |
|
* amount of translation available. If this attribute is not |
292 |
|
* {@link #visible}, it will return 1. |
293 |
|
*/ |
294 |
|
public double getQuality(final List<String> languages) { |
295 |
|
|
296 |
|
if (!isVisible()) |
297 |
|
return 1.; |
298 |
|
|
299 |
|
return (I8NUtil.qmTranslation(languages, getTitle()) * 2. + I8NUtil |
300 |
|
.qmTranslation(languages, getDesc()) * 1.) / 3.; |
301 |
} |
} |
|
// |
|
|
// /** @return the index of this attribute in the underlying table/dbf **/ |
|
|
// public int getColIdx() { |
|
|
// return colIdx; |
|
|
// } |
|
302 |
|
|
303 |
public Translation getTitle() { |
public Translation getTitle() { |
304 |
return title; |
return title; |
305 |
} |
} |
306 |
|
|
307 |
public void setTitle(final Translation title) { |
public String getUnit() { |
308 |
this.title = title; |
return unit; |
309 |
} |
} |
310 |
|
|
311 |
public Translation getDesc() { |
public int getWeight() { |
312 |
return desc; |
return weight; |
313 |
|
} |
314 |
|
|
315 |
|
/** |
316 |
|
* Will the end-user see this attribute? |
317 |
|
*/ |
318 |
|
public boolean isVisible() { |
319 |
|
return visible; |
320 |
} |
} |
321 |
|
|
322 |
public void setDesc(final Translation desc) { |
public void setDesc(final Translation desc) { |
323 |
this.desc = desc; |
this.desc = desc; |
324 |
} |
} |
325 |
|
|
326 |
public String getUnit() { |
public void setFunctionA(final Double functionA) { |
327 |
return unit; |
this.functionA = functionA; |
328 |
|
} |
329 |
|
|
330 |
|
public void setFunctionX(final Double functionX) { |
331 |
|
this.functionX = functionX; |
332 |
|
} |
333 |
|
|
334 |
|
/** |
335 |
|
* The fully qualified Name of the attribute, e.g. org.bla.plo:blub |
336 |
|
*/ |
337 |
|
public void setLocalName(final String localName) { |
338 |
|
this.name = new NameImpl(localName); |
339 |
|
} |
340 |
|
|
341 |
|
/** |
342 |
|
* The fully qualified {@link Name} of the attribute, e.g. |
343 |
|
* <code>org.bla.plo:blub</code> |
344 |
|
*/ |
345 |
|
public void setName(final Name name) { |
346 |
|
this.name = name; |
347 |
|
} |
348 |
|
|
349 |
|
public void addNodataValue(Object nodataValue) { |
350 |
|
this.nodataValues.add(nodataValue); |
351 |
|
} |
352 |
|
|
353 |
|
public void removeNodataValue(Object nodataValue) { |
354 |
|
this.nodataValues.remove(nodataValue); |
355 |
|
} |
356 |
|
|
357 |
|
public void setTitle(final Translation title) { |
358 |
|
this.title = title; |
359 |
} |
} |
360 |
|
|
361 |
public void setUnit(final String unit) { |
public void setUnit(final String unit) { |
362 |
this.unit = unit; |
this.unit = unit; |
363 |
} |
} |
364 |
|
|
365 |
@Override |
public void setVisible(final boolean visible) { |
366 |
public AttributeMetadata copyTo(AttributeMetadata amd) { |
this.visible = visible; |
367 |
getTitle().copyTo(amd.getTitle()); |
} |
|
getDesc().copyTo(amd.getDesc()); |
|
|
amd.setUnit(getUnit()); |
|
|
amd.setVisible(isVisible()); |
|
|
amd.setName(new NameImpl(getName().getNamespaceURI(), getName().getLocalPart())); |
|
368 |
|
|
369 |
return amd; |
/** |
370 |
|
* Shall the end-user see this attribute? |
371 |
|
* |
372 |
|
* @param visible |
373 |
|
*/ |
374 |
|
public void setVisible(final Boolean visible) { |
375 |
|
// // The THE_GEOM and shall never be visible! |
376 |
|
// if (name.getLocalPart().equalsIgnoreCase("the_geom")) |
377 |
|
// this.visible = false; |
378 |
|
// else |
379 |
|
// this.visible = visible; |
380 |
|
|
381 |
|
this.visible = visible; |
382 |
} |
} |
383 |
|
|
384 |
@Override |
public void setWeight(final int weight) { |
385 |
public AttributeMetadata copy() { |
this.weight = weight; |
|
return copyTo( new AttributeMetadata()); |
|
386 |
} |
} |
387 |
|
|
388 |
/** |
/** |
389 |
* The local Name. E.g. the name of the DBF column as a String |
* For nicer debugging |
390 |
*/ |
*/ |
391 |
public String getLocalName() { |
@Override |
392 |
return getName().getLocalPart(); |
public String toString() { |
393 |
|
StringBuffer sb = new StringBuffer(); |
394 |
|
if (name != null) |
395 |
|
sb.append(name.toString() + " "); |
396 |
|
sb.append("weight=" + weight + " "); |
397 |
|
sb.append("title=" + getTitle().toString()); |
398 |
|
return sb.toString(); |
399 |
} |
} |
400 |
|
|
401 |
/** |
/** |
402 |
* The fully qualified Name of the attribute, e.g. org.bla.plo:blub |
* Takes any value object and checks it against the NODATA values. If the |
403 |
|
* value equals a NODATA value, <code>null</code> is returned. Otherwise the |
404 |
|
* same object is returned. |
405 |
|
* |
406 |
|
* Note: This method is called often. |
407 |
*/ |
*/ |
408 |
public Name getName() { |
public Object fiterNodata(final Object value) { |
409 |
return name; |
if (nodataValues.contains(value)) |
410 |
|
return null; |
411 |
|
return value; |
412 |
} |
} |
413 |
|
|
414 |
/** |
/** |
415 |
* The fully qualified Name of the attribute, e.g. org.bla.plo:blub |
* @return a nicely formatted String containing all NODATA values. Strings |
416 |
|
* are quoted fo that the empty String can be seen. |
417 |
*/ |
*/ |
418 |
public void setName(org.opengis.feature.type.Name name) { |
public String getNoDataValuesFormatted() { |
419 |
this.name = name; |
return formatNoDataValues(getNodataValues()); |
420 |
} |
} |
421 |
|
|
422 |
/** |
/** |
423 |
* The fully qualified Name of the attribute, e.g. org.bla.plo:blub |
* @return a nicely formatted String containing all NODATA values. Strings |
424 |
|
* are quoted fo that the empty String can be seen. |
425 |
*/ |
*/ |
426 |
public void setLocalName(String localName) { |
public static String formatNoDataValues(Set<Object> list) { |
427 |
this.name = new NameImpl(localName); |
String nicelyFormatted = ""; |
428 |
|
if (list != null) { |
429 |
|
if (list.size() == 0) |
430 |
|
nicelyFormatted = ""; |
431 |
|
else { |
432 |
|
for (Object ndo : list) { |
433 |
|
if (ndo instanceof String) |
434 |
|
nicelyFormatted += "\"" + ndo + "\""; |
435 |
|
else |
436 |
|
nicelyFormatted += ndo.toString(); |
437 |
|
|
438 |
|
nicelyFormatted += ","; |
439 |
|
} |
440 |
|
// Remove the extra comma |
441 |
|
nicelyFormatted = nicelyFormatted.substring(0, nicelyFormatted |
442 |
|
.length() - 1); |
443 |
|
} |
444 |
|
} |
445 |
|
return nicelyFormatted; |
446 |
} |
} |
447 |
} |
} |