KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > mondrian > MondrianMember


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.mondrian;
14
15 import mondrian.olap.SchemaReader;
16
17 import com.tonbeller.jpivot.olap.model.Alignable;
18 import com.tonbeller.jpivot.olap.model.Level;
19 import com.tonbeller.jpivot.olap.model.Member;
20 import com.tonbeller.jpivot.olap.model.Property;
21 import com.tonbeller.jpivot.olap.model.Visitor;
22 import com.tonbeller.jpivot.olap.model.impl.PropertyImpl;
23 import com.tonbeller.jpivot.olap.query.MDXMember;
24
25 /**
26  * MondrianMember is an adapter class for the Mondrian Member.
27  */

28 public class MondrianMember implements Member, MDXMember {
29
30   private mondrian.olap.Member monMember;
31   private MondrianLevel level;
32   private MondrianModel model;
33   private Property[] properties = null;
34
35   /**
36    * Constructor
37    * @param monMember corresponding Mondrian Member
38    * @param level Olap hierarchy parent object
39    */

40   protected MondrianMember(
41     mondrian.olap.Member monMember,
42     MondrianLevel level,
43     MondrianModel model) {
44     this.monMember = monMember;
45     this.level = level;
46     this.model = model;
47     level.addMember(this);
48
49     mondrian.olap.Property[] props = monMember.getLevel().getProperties();
50     if (props != null) {
51       properties = new Property[props.length];
52       for (int i = 0; i < props.length; i++) {
53         MondrianProp prop = new MondrianProp();
54         if (props[i].getType() == mondrian.olap.Property.Datatype.TYPE_NUMERIC)
55           prop.setAlignment(Alignable.Alignment.RIGHT);
56         String JavaDoc propName = props[i].getName();
57         prop.setName(propName);
58         String JavaDoc caption = props[i].getCaption();
59         if (caption != null && !caption.equals(propName)){
60           // name and caption are different
61
// we want to show caption instead of name
62
prop.setLabel(caption);
63           prop.setMondrianName(propName);
64           // if the property has a separate Label, then it does not require normalization
65
// since it is to be displayed as-is
66
prop.setNormalizable(false);
67         } else {
68           prop.setLabel(propName);
69         }
70         String JavaDoc propValue = monMember.getPropertyFormattedValue(propName);
71         prop.setValue(propValue);
72         properties[i] = prop;
73       }
74     }
75
76   }
77
78   public String JavaDoc getLabel() {
79     return monMember.getCaption();
80   }
81
82   /**
83    * @see com.tonbeller.jpivot.olap.model.Member#getRootDistance()
84    */

85   public int getRootDistance() {
86       SchemaReader scr = model.getMonConnection().getSchemaReader();
87       mondrian.olap.Member m = monMember;
88       int rootDistance = 0;
89       while (true) {
90           m = scr.getMemberParent(m);
91           if (m == null)
92               return rootDistance;
93           rootDistance += 1;
94       }
95   }
96
97   /**
98    * @see com.tonbeller.jpivot.olap.model.Member#getLevel()
99    */

100   public Level getLevel() {
101     return level;
102   }
103
104   /**
105    * @see com.tonbeller.jpivot.olap.model.PropertyHolder#getProperties()
106    */

107   public Property[] getProperties() {
108
109     if (properties == null || properties.length == 0)
110       return new Property[0]; // or null ???
111

112     return properties;
113   }
114
115   /**
116    * @return parent
117    * @see com.tonbeller.jpivot.olap.query.MDXMember#getParent()
118    */

119   public Member getParent() {
120     mondrian.olap.Member monParent = monMember.getParentMember();
121     MondrianMember parent = model.addMember(monParent);
122     return parent;
123   }
124
125   /**
126    * @return parent unique name
127    * @see com.tonbeller.jpivot.olap.model.MDXMember#getParentUniqueName()
128    */

129   public String JavaDoc getParentUniqueName() {
130     return monMember.getParentUniqueName();
131   }
132
133   /**
134    * @return true, if it is an "All" member
135    */

136   public boolean isAll() {
137     return monMember.isAll();
138   }
139
140   
141   /**
142    * @see com.tonbeller.jpivot.olap.model.PropertyHolder#getProperty(String)
143    */

144   public Property getProperty(String JavaDoc name) {
145
146     if (properties == null || properties.length == 0)
147       return null;
148
149     for (int i = 0; i < properties.length; i++) {
150       if (name.equals(properties[i].getName()))
151         return properties[i];
152     }
153
154     return null; // not found
155
}
156
157   /**
158    * @see com.tonbeller.jpivot.olap.model.Visitable#accept(Visitor)
159    */

160   public void accept(Visitor visitor) {
161     visitor.visitMember(this);
162   }
163
164   /**
165    * Returns the corresponding Mondrian Member.
166    * @return mondrian.olap.Member
167    */

168   public mondrian.olap.Member getMonMember() {
169     return monMember;
170   }
171
172   /**
173    * @return the unique name
174    */

175   public String JavaDoc getUniqueName() {
176     return monMember.getUniqueName();
177   }
178   /**
179   * @return true,if the member is calculated
180   */

181   public boolean isCalculated() {
182     return monMember.isCalculated();
183   }
184   
185   public Object JavaDoc getRootDecoree() {
186     return this;
187   }
188
189   /**
190    * @see java.lang.Object#equals(java.lang.Object)
191    */

192   public boolean equals(Object JavaDoc obj) {
193     if (!(obj instanceof MondrianMember))
194       return false;
195     mondrian.olap.Member mm = ((MondrianMember) obj).getMonMember();
196     return monMember.equals(mm);
197   }
198
199   /**
200    * @see java.lang.Object#hashCode()
201    */

202   public int hashCode() {
203     return monMember.hashCode();
204   }
205   
206   /**
207    * a mondrian property can have a caption different from name
208    * we only show the caption
209    */

210   public static class MondrianProp extends PropertyImpl {
211     String JavaDoc mondrianName = null; // only set if different from name
212

213     /**
214      * @return Returns the mondrianName.
215      */

216     public String JavaDoc getMondrianName() {
217       return mondrianName;
218     }
219
220     /**
221      * @param mondrianName The mondrianName to set.
222      */

223     public void setMondrianName(String JavaDoc mondrianName) {
224       this.mondrianName = mondrianName;
225     }
226   } // MondrianProp
227

228
229 } // MondrianMember
230
Popular Tags