29 |
******************************************************************************/ |
******************************************************************************/ |
30 |
package skrueger; |
package skrueger; |
31 |
|
|
32 |
import java.util.ArrayList; |
import java.util.HashSet; |
33 |
import java.util.List; |
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; |
51 |
*/ |
*/ |
52 |
public class AttributeMetadata implements Copyable<AttributeMetadata>, |
public class AttributeMetadata implements Copyable<AttributeMetadata>, |
53 |
Comparable<AttributeMetadata> { |
Comparable<AttributeMetadata> { |
54 |
|
|
55 |
static private final Logger LOGGER = Logger |
static private final Logger LOGGER = Logger |
56 |
.getLogger(AttributeMetadata.class); |
.getLogger(AttributeMetadata.class); |
57 |
|
|
76 |
/** |
/** |
77 |
* Allows to define general NODATA values for an attribute. e.g. -9999 can |
* 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 |
* be set and will always be interpreted as NULL internally and will usually |
79 |
* be ignored. TODO not implemented yet |
* be ignored. This overcomes the problem, that |
80 |
**/ |
**/ |
81 |
protected List<Object> nodataValues = new ArrayList<Object>(); |
protected HashSet<Object> nodataValues = new HashSet<Object>(); |
82 |
|
|
83 |
/** Translation of the attribute's title **/ |
/** Translation of the attribute's title **/ |
84 |
protected Translation title = new Translation(); |
protected Translation title = new Translation(); |
93 |
protected boolean visible = true; |
protected boolean visible = true; |
94 |
|
|
95 |
/** |
/** |
96 |
* When listed, the attributes are listed according to their {@link #weight} (heavier |
* When listed, the attributes are listed according to their {@link #weight} |
97 |
* => further down) |
* (heavier => further down) |
98 |
|
* |
99 |
* @see #compareTo(AttributeMetadata) |
* @see #compareTo(AttributeMetadata) |
100 |
**/ |
**/ |
101 |
protected int weight = 0; |
protected int weight = 0; |
104 |
private AttributeMetadata() { |
private AttributeMetadata() { |
105 |
} |
} |
106 |
|
|
107 |
public AttributeMetadata(final AttributeDescriptor attDesc, final int weight, |
public AttributeMetadata(final AttributeDescriptor attDesc, |
108 |
final List<String> langs) { |
final int weight, final List<String> langs) { |
109 |
this(attDesc.getName(), langs); |
this(attDesc.getName(), langs); |
110 |
setWeight(weight); |
setWeight(weight); |
111 |
} |
} |
112 |
|
|
113 |
public AttributeMetadata(final AttributeDescriptor attDesc, final List<String> langs) { |
public AttributeMetadata(final AttributeDescriptor attDesc, |
114 |
|
final List<String> langs) { |
115 |
this(attDesc.getName(), langs); |
this(attDesc.getName(), langs); |
116 |
} |
} |
117 |
|
|
142 |
} |
} |
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; |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
* Creates a new visible {@link AttributeMetadata} |
* Creates a new visible {@link AttributeMetadata} |
165 |
*/ |
*/ |
166 |
public AttributeMetadata(final Name name, final List<String> langs) { |
public AttributeMetadata(final Name name, final List<String> langs) { |
169 |
} |
} |
170 |
|
|
171 |
/** |
/** |
172 |
* Creates a new visible {@link AttributeMetadata} |
* Creates a new visible {@link AttributeMetadata} |
173 |
*/ |
*/ |
174 |
public AttributeMetadata(final Name name, final String defaultTitle, |
public AttributeMetadata(final Name name, final String defaultTitle, |
175 |
final List<String> langs) { |
final List<String> langs) { |
204 |
} |
} |
205 |
|
|
206 |
/** |
/** |
207 |
* Creates a new visible {@link AttributeMetadata} |
* Creates a new visible {@link AttributeMetadata} |
208 |
*/ |
*/ |
209 |
public AttributeMetadata(final String localName, final String defaultTitle, |
public AttributeMetadata(final String localName, final String defaultTitle, |
210 |
final List<String> langs) { |
final List<String> langs) { |
245 |
amd.setFunctionX(getFunctionX()); |
amd.setFunctionX(getFunctionX()); |
246 |
amd.setFunctionA(getFunctionA()); |
amd.setFunctionA(getFunctionA()); |
247 |
|
|
248 |
for (final Object nodataValue : getNodataValues()) { |
amd.setNodataValues(getNodataValues()); |
|
amd.getNodataValues().add(nodataValue); |
|
|
} |
|
249 |
|
|
250 |
return amd; |
return amd; |
251 |
} |
} |
252 |
|
|
253 |
|
// only to be used by copyTo() |
254 |
|
private void setNodataValues(HashSet<Object> nodataValues2) { |
255 |
|
nodataValues = nodataValues2; |
256 |
|
} |
257 |
|
|
258 |
public Translation getDesc() { |
public Translation getDesc() { |
259 |
return desc; |
return desc; |
260 |
} |
} |
275 |
} |
} |
276 |
|
|
277 |
/** |
/** |
278 |
* The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code> |
* The fully qualified {@link Name} of the attribute, e.g. |
279 |
|
* <code>org.bla.plo:blub</code> |
280 |
*/ |
*/ |
281 |
public Name getName() { |
public Name getName() { |
282 |
return name; |
return name; |
283 |
} |
} |
284 |
|
|
285 |
public List<Object> getNodataValues() { |
public HashSet<Object> getNodataValues() { |
286 |
return nodataValues; |
return nodataValues; |
287 |
} |
} |
288 |
|
|
339 |
} |
} |
340 |
|
|
341 |
/** |
/** |
342 |
* The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code> |
* 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) { |
public void setName(final Name name) { |
346 |
this.name = name; |
this.name = name; |
347 |
} |
} |
348 |
|
|
349 |
public void setNodataValues(final List<Object> nodataValues) { |
public void addNodataValue(Object nodataValue) { |
350 |
this.nodataValues = nodataValues; |
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) { |
public void setTitle(final Translation title) { |
368 |
|
|
369 |
/** |
/** |
370 |
* Shall the end-user see this attribute? |
* Shall the end-user see this attribute? |
371 |
|
* |
372 |
* @param visible |
* @param visible |
373 |
*/ |
*/ |
374 |
public void setVisible(final Boolean visible) { |
public void setVisible(final Boolean visible) { |
375 |
// // The THE_GEOM and shall never be visible! |
// // The THE_GEOM and shall never be visible! |
376 |
// if (name.getLocalPart().equalsIgnoreCase("the_geom")) |
// if (name.getLocalPart().equalsIgnoreCase("the_geom")) |
377 |
// this.visible = false; |
// this.visible = false; |
378 |
// else |
// else |
379 |
// this.visible = visible; |
// this.visible = visible; |
380 |
|
|
381 |
this.visible = visible; |
this.visible = visible; |
382 |
} |
} |
384 |
public void setWeight(final int weight) { |
public void setWeight(final int weight) { |
385 |
this.weight = weight; |
this.weight = weight; |
386 |
} |
} |
387 |
|
|
388 |
/** |
/** |
389 |
* For nicer debugging |
* For nicer debugging |
390 |
*/ |
*/ |
391 |
@Override |
@Override |
392 |
public String toString() { |
public String toString() { |
393 |
StringBuffer sb = new StringBuffer(); |
StringBuffer sb = new StringBuffer(); |
394 |
if (name != null) sb.append(name.toString()+" "); |
if (name != null) |
395 |
sb.append("weight="+weight+" "); |
sb.append(name.toString() + " "); |
396 |
sb.append("title="+getTitle().toString()); |
sb.append("weight=" + weight + " "); |
397 |
|
sb.append("title=" + getTitle().toString()); |
398 |
return sb.toString(); |
return sb.toString(); |
399 |
} |
} |
400 |
|
|
401 |
|
/** |
402 |
|
* 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 Object fiterNodata(final Object value) { |
409 |
|
if (nodataValues.contains(value)) |
410 |
|
return null; |
411 |
|
return value; |
412 |
|
} |
413 |
|
|
414 |
|
/** |
415 |
|
* @return a nicely formatted String containing all NODATA values. Strings |
416 |
|
* are quoted fo that the empty String can be seen. |
417 |
|
*/ |
418 |
|
public String getNoDataValuesFormatted() { |
419 |
|
return formatNoDataValues(getNodataValues()); |
420 |
|
} |
421 |
|
|
422 |
|
/** |
423 |
|
* @return a nicely formatted String containing all NODATA values. Strings |
424 |
|
* are quoted fo that the empty String can be seen. |
425 |
|
*/ |
426 |
|
public static String formatNoDataValues(Set<Object> list) { |
427 |
|
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 |
} |
} |