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

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

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

branches/1.0-gt2-2.6/src/skrueger/AttributeMetadata.java revision 518 by alfonx, Fri Nov 13 18:16:38 2009 UTC branches/2.0-RC2/src/skrueger/AttributeMetadata.java revision 666 by alfonx, Wed Feb 3 18:05:50 2010 UTC
# Line 30  Line 30 
30  package skrueger;  package skrueger;
31    
32  import java.util.ArrayList;  import java.util.ArrayList;
33    import java.util.HashSet;
34  import java.util.List;  import java.util.List;
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.geotools.feature.Feature.NULL;
39  import org.opengis.feature.type.AttributeDescriptor;  import org.opengis.feature.type.AttributeDescriptor;
40  import org.opengis.feature.type.Name;  import org.opengis.feature.type.Name;
41    
# Line 50  import skrueger.i8n.Translation; Line 52  import skrueger.i8n.Translation;
52   */   */
53  public class AttributeMetadata implements Copyable<AttributeMetadata>,  public class AttributeMetadata implements Copyable<AttributeMetadata>,
54                  Comparable<AttributeMetadata> {                  Comparable<AttributeMetadata> {
55            
56          static private final Logger LOGGER = Logger          static private final Logger LOGGER = Logger
57                          .getLogger(AttributeMetadata.class);                          .getLogger(AttributeMetadata.class);
58    
         // 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();  
   
59          /** Translation of the attribute's description **/          /** Translation of the attribute's description **/
60          protected Translation desc = new Translation();          protected Translation desc = new Translation();
61    
62          /** Is the attribute visible to the user or ignored where possible **/          /**
63          protected boolean visible = true;           * For numerical attributes the value can be transformed by VALUE*X+A when
64             * presented on screen. TODO not implemented yet
65             **/
66            protected Double functionA = 0.;
67    
68          /**          /**
69           * 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
70           * null)           * presented on screen. TODO not implemented yet
71           **/           **/
72          protected String unit = "";          protected Double functionX = 1.;
73    
74          /** The Name of the attribute **/          /** The Name of the attribute **/
75          private Name name;          private Name name;
76    
77          public int getWeight() {          /**
78                  return weight;           * Allows to define general NODATA values for an attribute. e.g. -9999 can
79          }           * be set and will always be interpreted as NULL internally and will usually
80             * be ignored. This overcomes the problem, that
81          public void setWeight(int weight) {           **/
82                  this.weight = weight;          protected final HashSet<Object> nodataValues = new HashSet<Object>();
         }  
   
         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;  
         }  
83    
84          public Double getFunctionA() {          /** Translation of the attribute's title **/
85                  return functionA;          protected Translation title = new Translation();
         }  
86    
87          public void setFunctionA(Double functionA) {          /**
88                  this.functionA = functionA;           * The unit append to all visualizations of values of this attribute (is not
89          }           * null)
90             **/
91            protected String unit = "";
92    
93          public void setVisible(boolean visible) {          /** Is the attribute visible to the user or ignored where possible **/
94                  this.visible = visible;          protected boolean visible = true;
         }  
95    
96          /**          /**
97           * When listed, the attributes are listed according to their weight (heavier           * When listed, the attributes are listed according to their {@link #weight} (heavier
98           * => further down)           * => further down)
99             * @see #compareTo(AttributeMetadata)
100           **/           **/
101          protected int weight = 0;          protected int weight = 0;
102    
103          /**          /** Only used for {@link Copyable<AttributeMetaData>#copy()} **/
104           * Allows to define general NODATA values for an attribute. e.g. -9999 can          private AttributeMetadata() {
105           * be set and will always be interpreted as NULL internally and will usually          }
          * be ignored.  
          **/  
         protected List<Object> nodataValues = new ArrayList<Object>();  
106    
107          /**          public AttributeMetadata(final AttributeDescriptor attDesc, final int weight,
108           * For numerical attributes the value can be transformed by VALUE*X+A when                          final List<String> langs) {
109           * presented on screen                  this(attDesc.getName(), langs);
110           **/                  setWeight(weight);
111          protected Double functionX = 1.;          }
112    
113          /**          public AttributeMetadata(final AttributeDescriptor attDesc, final List<String> langs) {
114           * For numerical attributes the value can be transformed by VALUE*X+A when                  this(attDesc.getName(), langs);
115           * presented on screen          }
          **/  
         protected Double functionA = 0.;  
116    
117          /**          /**
118           * Creates an {@link AttributeMetadata} object with the following           * Creates an {@link AttributeMetadata} object with the following
# Line 165  public class AttributeMetadata implement Line 136  public class AttributeMetadata implement
136                  this.setName(name);                  this.setName(name);
137                  this.title = title;                  this.title = title;
138                  this.desc = desc;                  this.desc = desc;
139                    this.visible = visible;
140                    this.unit = unit;
141            }
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, final String unit) {
157                    this.setName(name);
158                    this.visible = visible;
159                  this.unit = unit;                  this.unit = unit;
160          }          }
161    
162          /**          /**
163             * Creates a new visible {@link AttributeMetadata}
164             */
165            public AttributeMetadata(final Name name, final List<String> langs) {
166                    this(name, true, new Translation(langs, name.getLocalPart()),
167                                    new Translation(), "");
168            }
169    
170            /**
171             * Creates a new visible {@link AttributeMetadata}
172             */
173            public AttributeMetadata(final Name name, final String defaultTitle,
174                            final List<String> langs) {
175                    this(name, true, new Translation(langs, defaultTitle),
176                                    new Translation(), "");
177            }
178    
179            /**
180           * Creates an {@link AttributeMetadata} object with the following           * Creates an {@link AttributeMetadata} object with the following
181           * information           * information
182           *           *
# Line 196  public class AttributeMetadata implement Line 197  public class AttributeMetadata implement
197          /**          /**
198           * Creates a new visible {@link AttributeMetadata} with default (no) values.           * Creates a new visible {@link AttributeMetadata} with default (no) values.
199           */           */
200          public AttributeMetadata(final String localName, final String defaultTitle) {          public AttributeMetadata(final String localName, final List<String> langs) {
201                  this(localName, true, new Translation(defaultTitle), new Translation(),                  this(localName, true, new Translation(langs, localName),
202                                  "");                                  new Translation(), "");
203          }          }
204    
205          /**          /**
206           * Creates a new visible {@link AttributeMetadata} with default (no) values.           * Creates a new visible {@link AttributeMetadata}
207           */           */
208          public AttributeMetadata(final Name name, final String defaultTitle) {          public AttributeMetadata(final String localName, final String defaultTitle,
209                  this(name, true, new Translation(defaultTitle), new Translation(), "");                          final List<String> langs) {
210                    this(localName, true, new Translation(langs, defaultTitle),
211                                    new Translation(), "");
212          }          }
213    
214          /**          /**
215           * Creates a new visible {@link AttributeMetadata} with default (no) values.           * Orders the attributes according to their {@link #weight}. Heavier =>
216             * further down.
217           */           */
218          public AttributeMetadata(final Name name) {          @Override
219                  this(name, true, new Translation(name.getLocalPart()),          public int compareTo(final AttributeMetadata atm2) {
220                                  new Translation(), "");                  return new Integer(weight).compareTo(atm2.getWeight());
221          }          }
222    
223          /**          /**
224           * Creates a new visible {@link AttributeMetadata} with default (no) values.           * @see Copyable inferface
225           */           */
226          public AttributeMetadata(final String localName) {          @Override
227                  this(localName, true, new Translation(localName), new Translation(), "");          public AttributeMetadata copy() {
228                    return copyTo(new AttributeMetadata());
229          }          }
230    
231          /** Only used for {@link Copyable<AttributeMetaData>#copy()} **/          /**
232          private AttributeMetadata() {           * @see Copyable inferface
233             */
234            @Override
235            public AttributeMetadata copyTo(final AttributeMetadata amd) {
236                    getTitle().copyTo(amd.getTitle());
237                    getDesc().copyTo(amd.getDesc());
238                    amd.setUnit(getUnit());
239                    amd.setVisible(isVisible());
240                    amd.setName(new NameImpl(getName().getNamespaceURI(), getName()
241                                    .getLocalPart()));
242    
243                    amd.setWeight(getWeight());
244                    amd.setFunctionX(getFunctionX());
245                    amd.setFunctionA(getFunctionA());
246    
247                    for (final Object nodataValue : getNodataValues()) {
248                            amd.getNodataValues().add(nodataValue);
249                    }
250    
251                    return amd;
252          }          }
253    
254          public AttributeMetadata(AttributeDescriptor attDesc) {          public Translation getDesc() {
255                  this(attDesc.getName());                  return desc;
256          }          }
257    
258          public boolean isVisible() {          public Double getFunctionA() {
259                  return visible;                  return functionA;
260          }          }
261    
262          public void setVisible(final Boolean visible) {          public Double getFunctionX() {
263                    return functionX;
264            }
265    
266                  // The THE_GEOM and shall never be visible! // TODO MAKE BETTER          /**
267                  if (name.getLocalPart().equalsIgnoreCase("the_geom"))           * The local name. E.g. the name of the DBF column as a {@link String}
268                          this.visible = false;           */
269                  else          public String getLocalName() {
270                          this.visible = visible;                  return getName().getLocalPart();
                   
                 this.visible = visible;  
271          }          }
272    
273          public Translation getTitle() {          /**
274                  return title;           * The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code>
275             */
276            public Name getName() {
277                    return name;
278          }          }
279    
280          public void setTitle(final Translation title) {          public HashSet<Object> getNodataValues() {
281                  this.title = title;                  return nodataValues;
282          }          }
283    
284          public Translation getDesc() {          /**
285                  return desc;           * @return a number between 0 (bad) and 1 (good) that is calculated from the
286             *         amount of translation available. If this attribute is not
287             *         {@link #visible}, it will return 1.
288             */
289            public double getQuality(final List<String> languages) {
290    
291                    if (!isVisible())
292                            return 1.;
293    
294                    return (I8NUtil.qmTranslation(languages, getTitle()) * 2. + I8NUtil
295                                    .qmTranslation(languages, getDesc()) * 1.) / 3.;
296          }          }
297    
298          public void setDesc(final Translation desc) {          public Translation getTitle() {
299                  this.desc = desc;                  return title;
300          }          }
301    
302          public String getUnit() {          public String getUnit() {
303                  return unit;                  return unit;
304          }          }
305    
306          public void setUnit(final String unit) {          public int getWeight() {
307                  this.unit = unit;                  return weight;
308          }          }
309    
310          @Override          /**
311          public AttributeMetadata copyTo(AttributeMetadata amd) {           * Will the end-user see this attribute?
312                  getTitle().copyTo(amd.getTitle());           */
313                  getDesc().copyTo(amd.getDesc());          public boolean isVisible() {
314                  amd.setUnit(getUnit());                  return visible;
315                  amd.setVisible(isVisible());          }
                 amd.setName(new NameImpl(getName().getNamespaceURI(), getName()  
                                 .getLocalPart()));  
   
                 amd.setWeight(getWeight());  
                 amd.setFunctionX(getFunctionX());  
                 amd.setFunctionA(getFunctionA());  
   
                 for (Object nodataValue : getNodataValues()) {  
                         amd.getNodataValues().add(nodataValue);  
                 }  
316    
317                  return amd;          public void setDesc(final Translation desc) {
318                    this.desc = desc;
319          }          }
320    
321          @Override          public void setFunctionA(final Double functionA) {
322          public AttributeMetadata copy() {                  this.functionA = functionA;
                 return copyTo(new AttributeMetadata());  
323          }          }
324    
325          /**          public void setFunctionX(final Double functionX) {
326           * The local Name. E.g. the name of the DBF column as a String                  this.functionX = functionX;
          */  
         public String getLocalName() {  
                 return getName().getLocalPart();  
327          }          }
328    
329          /**          /**
330           * The fully qualified Name of the attribute, e.g. org.bla.plo:blub           * The fully qualified Name of the attribute, e.g. org.bla.plo:blub
331           */           */
332          public Name getName() {          public void setLocalName(final String localName) {
333                  return name;                  this.name = new NameImpl(localName);
334          }          }
335    
336          /**          /**
337           * The fully qualified Name of the attribute, e.g. org.bla.plo:blub           * The fully qualified {@link Name} of the attribute, e.g. <code>org.bla.plo:blub</code>
338           */           */
339          public void setName(org.opengis.feature.type.Name name) {          public void setName(final Name name) {
340                  this.name = name;                  this.name = name;
341          }          }
342    
343            public void addNodataValue(Object nodataValue) {
344                    this.nodataValues.add(nodataValue);
345            }
346            
347            public void removeNodataValue(Object nodataValue) {
348                    this.nodataValues.remove(nodataValue);
349            }
350    
351            public void setTitle(final Translation title) {
352                    this.title = title;
353            }
354    
355            public void setUnit(final String unit) {
356                    this.unit = unit;
357            }
358    
359            public void setVisible(final boolean visible) {
360                    this.visible = visible;
361            }
362    
363          /**          /**
364           * The fully qualified Name of the attribute, e.g. org.bla.plo:blub           * Shall the end-user see this attribute?
365             * @param visible
366           */           */
367          public void setLocalName(String localName) {          public void setVisible(final Boolean visible) {
368                  this.name = new NameImpl(localName);  //              // The THE_GEOM and shall never be visible!
369    //              if (name.getLocalPart().equalsIgnoreCase("the_geom"))
370    //                      this.visible = false;
371    //              else
372    //                      this.visible = visible;
373    
374                    this.visible = visible;
375          }          }
376    
377            public void setWeight(final int weight) {
378                    this.weight = weight;
379            }
380            
381          /**          /**
382           * Orders the Attributes according to their weight. Heavier => further down.           * For nicer debugging
383           */           */
384          @Override          @Override
385          public int compareTo(AttributeMetadata atm2) {          public String toString() {
386                  // Double double1 = new Double(1./weight);                  StringBuffer sb = new StringBuffer();
387                  // double double2 = 1./atm2.getWeight();                  if (name != null) sb.append(name.toString()+" ");
388                  return new Integer(weight).compareTo(atm2.getWeight());                  sb.append("weight="+weight+" ");
389                    sb.append("title="+getTitle().toString());
390                    return sb.toString();
391          }          }
392    
393          /**          /**
394           * @return a number between 0 (bad) and 1 (good) that is calculated from the amount of translation available in the visible attributes           * Takes any value object and checks it against the NODATA values. If the
395             * value equals a NODATA value, <code>null</code> is returned. Otherwise the
396             * same object is returned.
397             *
398             * Note: This method is called often.
399           */           */
400          public double getQuality(List<String> languages) {          public Object fiterNodata(final Object value) {
401                  return (I8NUtil.qmTranslation(languages, getTitle()) * 2. + I8NUtil                  if (nodataValues.contains(value)) return null;
402                                  .qmTranslation(languages, getDesc()) * 1.) / 3.;                  return value;
403          }          }
404  }  }

Legend:
Removed from v.518  
changed lines
  Added in v.666

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26