/[schmitzm]/trunk/src/skrueger/AttributeMetadataInterface.java
ViewVC logotype

Diff of /trunk/src/skrueger/AttributeMetadataInterface.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

branches/1.0-gt2-2.6/src/skrueger/AttributeMetadata.java revision 533 by alfonx, Thu Nov 19 17:27:01 2009 UTC branches/2.0-RC2/src/skrueger/AttributeMetadata.java revision 681 by alfonx, Tue Feb 9 22:08:26 2010 UTC
# Line 29  Line 29 
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;
# Line 50  import skrueger.i8n.Translation; Line 51  import skrueger.i8n.Translation;
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    
         // public class WeightComparator implements Comparator<AttributeDescriptor>  
         // {  
         //  
         // @Override  
         // public int compare(AttributeDescriptor o1, AttributeDescriptor o2) {  
         // // TODO Auto-generated method stub  
         // return 0;  
         // }  
         //                
         // };  
   
         /** Translation of the attribute's title **/  
         protected Translation title = new Translation();  
   
58          /** Translation of the attribute's description **/          /** Translation of the attribute's description **/
59          protected Translation desc = new Translation();          protected Translation desc = new Translation();
60    
61          /** Is the attribute visible to the user or ignored where possible **/          /**
62          protected boolean visible = true;           * 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           * The unit append to all visualizations of values of this attribute (is not           * For numerical attributes the value can be transformed by VALUE*X+A when
69           * null)           * presented on screen. TODO not implemented yet
70           **/           **/
71          protected String unit = "";          protected Double functionX = 1.;
72    
73          /** The Name of the attribute **/          /** The Name of the attribute **/
74          private Name name;          private Name name;
75    
         public int getWeight() {  
                 return weight;  
         }  
   
         public void setWeight(int weight) {  
                 this.weight = weight;  
         }  
   
         public List<Object> getNodataValues() {  
                 return nodataValues;  
         }  
   
         public void setNodataValues(List<Object> nodataValues) {  
                 this.nodataValues = nodataValues;  
         }  
   
         public Double getFunctionX() {  
                 return functionX;  
         }  
   
         public void setFunctionX(Double functionX) {  
                 this.functionX = functionX;  
         }  
   
         public Double getFunctionA() {  
                 return functionA;  
         }  
   
         public void setFunctionA(Double functionA) {  
                 this.functionA = functionA;  
         }  
   
         public void setVisible(boolean visible) {  
                 this.visible = visible;  
         }  
   
         /**  
          * When listed, the attributes are listed according to their weight (heavier  
          * => further down)  
          **/  
         protected int weight = 0;  
   
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.           * 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 **/
84            protected Translation title = new Translation();
85    
86          /**          /**
87           * For numerical attributes the value can be transformed by VALUE*X+A when           * The unit append to all visualizations of values of this attribute (is not
88           * presented on screen           * null)
89           **/           **/
90          protected Double functionX = 1.;          protected String unit = "";
91    
92            /** Is the attribute visible to the user or ignored where possible **/
93            protected boolean visible = true;
94    
95          /**          /**
96           * For numerical attributes the value can be transformed by VALUE*X+A when           * When listed, the attributes are listed according to their {@link #weight}
97           * presented on screen           * (heavier => further down)
98             *
99             * @see #compareTo(AttributeMetadata)
100           **/           **/
101          protected Double functionA = 0.;          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
# Line 165  public class AttributeMetadata implement Line 137  public class AttributeMetadata implement
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                    this.unit = unit;
142            }
143    
144                  // The THE_GEOM and shall never be visible!          /**
145                  if (name.getLocalPart().equalsIgnoreCase("the_geom"))           * Creates an {@link AttributeMetadata} object with the following
146                          this.visible = false;           * information
147                  else           *
148                          this.visible = visible;           * @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           *           *
# Line 196  public class AttributeMetadata implement Line 198  public class AttributeMetadata implement
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, List<String> langs) {          public AttributeMetadata(final String localName, final List<String> langs) {
202                  this(localName, true, new Translation(langs, 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, List<String> langs) {          public AttributeMetadata(final String localName, final String defaultTitle,
210                  this(name, true, new Translation(langs, 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, List<String> langs) {          @Override
220                  this(name, true, new Translation(langs, 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, List<String> langs) {          @Override
228                  this(localName, true, new Translation(langs, 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, List<String> langs) {          // only to be used by copyTo()
254                  this(attDesc.getName(), langs);          private void setNodataValues(HashSet<Object> nodataValues2) {
255                    nodataValues = nodataValues2;
256          }          }
257    
258          public AttributeMetadata(AttributeDescriptor attDesc, int weight, List<String> langs) {          public Translation getDesc() {
259                  this(attDesc.getName(), langs);                  return desc;
                 setWeight(weight);  
260          }          }
261    
262          public boolean isVisible() {          public Double getFunctionA() {
263                  return visible;                  return functionA;
264          }          }
265    
266          public void setVisible(final Boolean visible) {          public Double getFunctionX() {
267                    return functionX;
268            }
269    
270                  // The THE_GEOM and shall never be visible! // TODO MAKE BETTER          /**
271                  if (name.getLocalPart().equalsIgnoreCase("the_geom"))           * The local name. E.g. the name of the DBF column as a {@link String}
272                          this.visible = false;           */
273                  else          public String getLocalName() {
274                          this.visible = visible;                  return getName().getLocalPart();
                   
                 this.visible = visible;  
275          }          }
276    
277          public Translation getTitle() {          /**
278                  return title;           * 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 void setTitle(final Translation title) {          public HashSet<Object> getNodataValues() {
286                  this.title = title;                  return nodataValues;
287          }          }
288    
289          public Translation getDesc() {          /**
290                  return desc;           * @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          }          }
302    
303          public void setDesc(final Translation desc) {          public Translation getTitle() {
304                  this.desc = desc;                  return title;
305          }          }
306    
307          public String getUnit() {          public String getUnit() {
308                  return unit;                  return unit;
309          }          }
310    
311          public void setUnit(final String unit) {          public int getWeight() {
312                  this.unit = unit;                  return weight;
313          }          }
314    
315          @Override          /**
316          public AttributeMetadata copyTo(AttributeMetadata amd) {           * Will the end-user see this attribute?
317                  getTitle().copyTo(amd.getTitle());           */
318                  getDesc().copyTo(amd.getDesc());          public boolean isVisible() {
319                  amd.setUnit(getUnit());                  return visible;
320                  amd.setVisible(isVisible());          }
                 amd.setName(new NameImpl(getName().getNamespaceURI(), getName()  
                                 .getLocalPart()));  
321    
322                  amd.setWeight(getWeight());          public void setDesc(final Translation desc) {
323                  amd.setFunctionX(getFunctionX());                  this.desc = desc;
324                  amd.setFunctionA(getFunctionA());          }
325    
326                  for (Object nodataValue : getNodataValues()) {          public void setFunctionA(final Double functionA) {
327                          amd.getNodataValues().add(nodataValue);                  this.functionA = functionA;
328                  }          }
329    
330                  return amd;          public void setFunctionX(final Double functionX) {
331                    this.functionX = functionX;
332          }          }
333    
334          @Override          /**
335          public AttributeMetadata copy() {           * The fully qualified Name of the attribute, e.g. org.bla.plo:blub
336                  return copyTo(new AttributeMetadata());           */
337            public void setLocalName(final String localName) {
338                    this.name = new NameImpl(localName);
339          }          }
340    
341          /**          /**
342           * The local Name. E.g. the name of the DBF column as a String           * The fully qualified {@link Name} of the attribute, e.g.
343             * <code>org.bla.plo:blub</code>
344           */           */
345          public String getLocalName() {          public void setName(final Name name) {
346                  return getName().getLocalPart();                  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) {
362                    this.unit = unit;
363            }
364    
365            public void setVisible(final boolean visible) {
366                    this.visible = visible;
367          }          }
368    
369          /**          /**
370           * The fully qualified Name of the attribute, e.g. org.bla.plo:blub           * Shall the end-user see this attribute?
371             *
372             * @param visible
373           */           */
374          public Name getName() {          public void setVisible(final Boolean visible) {
375                  return name;                  // // 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            public void setWeight(final int weight) {
385                    this.weight = weight;
386          }          }
387    
388          /**          /**
389           * The fully qualified Name of the attribute, e.g. org.bla.plo:blub           * For nicer debugging
390           */           */
391          public void setName(org.opengis.feature.type.Name name) {          @Override
392                  this.name = name;          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 void setLocalName(String localName) {          public Object fiterNodata(final Object value) {
409                  this.name = new NameImpl(localName);                  if (nodataValues.contains(value))
410                            return null;
411                    return value;
412          }          }
413    
414          /**          /**
415           * Orders the Attributes according to their weight. Heavier => further down.           * @return a nicely formatted String containing all NODATA values. Strings
416             *         are quoted fo that the empty String can be seen.
417           */           */
418          @Override          public String getNoDataValuesFormatted() {
419          public int compareTo(AttributeMetadata atm2) {                  return formatNoDataValues(getNodataValues());
                 // Double double1 = new Double(1./weight);  
                 // double double2 = 1./atm2.getWeight();  
                 return new Integer(weight).compareTo(atm2.getWeight());  
420          }          }
421    
422          /**          /**
423           * @return a number between 0 (bad) and 1 (good) that is calculated from the amount of translation available in the visible attributes           * @return a nicely formatted String containing all NODATA values. Strings
424             *         are quoted fo that the empty String can be seen.
425           */           */
426          public double getQuality(List<String> languages) {          public static String formatNoDataValues(Set<Object> list) {
427                  return (I8NUtil.qmTranslation(languages, getTitle()) * 2. + I8NUtil                  String nicelyFormatted = "";
428                                  .qmTranslation(languages, getDesc()) * 1.) / 3.;                  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  }  }

Legend:
Removed from v.533  
changed lines
  Added in v.681

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26