KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > xmla > navigator > hierarchy > HierarchyItem


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 org.openi.xmla.navigator.hierarchy;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.apache.log4j.Logger;
21
22 import com.tonbeller.jpivot.olap.model.Dimension;
23 import com.tonbeller.jpivot.olap.model.Hierarchy;
24 import com.tonbeller.jpivot.olap.model.Member;
25 import com.tonbeller.jpivot.olap.model.OlapUtils;
26 import com.tonbeller.jpivot.olap.navi.ChangeSlicer;
27 import com.tonbeller.jpivot.olap.navi.PlaceMembersOnAxes;
28 import com.tonbeller.wcf.catedit.Item;
29 import com.tonbeller.wcf.controller.RequestContext;
30 import com.tonbeller.wcf.controller.RequestListener;
31 import com.tonbeller.wcf.utils.DomUtils;
32
33 /**
34  * Wraps a hierarchy for HierarchyNavigator.
35  * Contains two selections, one multiple selection for axis view and
36  * another one, single selection for slicer view. If the item is moved
37  * around between axis and slicer category it does not forget its
38  * selection.
39  *
40  * @author av
41  */

42 public class HierarchyItem implements Item, RequestListener, Comparable JavaDoc {
43
44   private static final Logger logger = Logger.getLogger(HierarchyItem.class);
45
46   // the GUI component
47
private HierarchyNavigator navigator;
48
49   // one of rows, columns or slicer
50
private AbstractCategory category;
51
52   // the hierarchy this item is representing
53
private Hierarchy hierarchy;
54
55   // the selection in case this is contained in axis category
56
private List JavaDoc axisSelection;
57   private boolean axisSelectionDirty;
58
59   // the selection in case this is contained in slicer category
60
private List JavaDoc slicerSelection;
61   private boolean slicerSelectionDirty;
62
63   // list of (calculated) members that are about to be deleted in this hierarchy
64
private List JavaDoc deleted = new ArrayList JavaDoc();
65
66   // some expression to be placed on the axis instead of a hierarchy or selection
67
private Object JavaDoc expression;
68
69   private String JavaDoc id = DomUtils.randomId();
70
71   private Dimension dimension;
72
73   public String JavaDoc getId() {
74     return id;
75   }
76
77   public HierarchyItem(AbstractCategory category, Hierarchy hierarchy) {
78     this.category = category;
79     this.hierarchy = hierarchy;
80     this.dimension = hierarchy.getDimension();
81     this.navigator = category.getNavigator();
82     navigator.getTempDispatcher().addRequestListener(id, null, this);
83   }
84
85   void initializeSlicerSelection() {
86     ChangeSlicer slicerExtension = navigator.getSlicerExtension();
87     slicerSelection = new ArrayList JavaDoc();
88     if (slicerExtension != null) {
89       Member[] members = slicerExtension.getSlicer();
90       loop : for (int i = 0; i < members.length; i++) {
91         if (members[i].getLevel().getHierarchy().equals(hierarchy)) {
92           slicerSelection.add(members[i]);
93           break loop;
94         }
95       }
96     }
97   }
98
99   void initializeAxisSelection() {
100     PlaceMembersOnAxes memberExtension = navigator.getMemberExtension();
101     axisSelection = new ArrayList JavaDoc();
102     if (memberExtension != null) {
103       List JavaDoc members = memberExtension.findVisibleMembers(hierarchy);
104       axisSelection.addAll(members);
105     }
106   }
107
108   private void clear() {
109     axisSelection = null;
110     slicerSelection = null;
111     deleted.clear();
112     expression = null;
113   }
114
115   /**
116    * this item has been moved from one category to another. e.g., this hierarchy has been moved
117    * from rows to filters.
118    */

119   public void setCategory(AbstractCategory category) {
120     this.category = category;
121   }
122
123   public AbstractCategory getCategory() {
124     return category;
125   }
126
127   /**
128    * Returns the current hierarchy.
129    * @return Hierarchy
130    */

131   public Hierarchy getHierarchy() {
132     return hierarchy;
133   }
134
135   /**
136    * returns the Dimension of this HierarchyItem
137    */

138   public Dimension getDimension() {
139     return dimension;
140   }
141
142   public String JavaDoc getLabel() {
143     return hierarchy.getLabel();
144   }
145
146   /**
147    * called when the user clicks on this item.
148    */

149   public void request(RequestContext context) throws Exception JavaDoc {
150     category.itemClicked(context, this);
151   }
152
153   /**
154    * Returns the axisSelection.
155    * @return Set
156    */

157   public List JavaDoc getAxisSelection() {
158     if (axisSelection == null)
159       initializeAxisSelection();
160     return axisSelection;
161   }
162
163   /**
164    * Returns the slicerSelection.
165    * @return Set
166    */

167   public List JavaDoc getSlicerSelection() {
168     if (slicerSelection == null)
169       initializeSlicerSelection();
170     return slicerSelection;
171   }
172
173   /**
174    * Sets the axisSelection.
175    * @param axisSelection The axisSelection to set
176    */

177   public void setAxisSelection(Collection JavaDoc selection) {
178     clear();
179     updateHierarchy(selection);
180     if (axisSelection == null)
181       axisSelection = new ArrayList JavaDoc();
182     else
183       axisSelection.clear();
184     axisSelection.addAll(selection);
185     axisSelectionDirty = true;
186     category.setDirty(true);
187     expression = null;
188   }
189
190   /**
191    * Sets the slicerSelection.
192    * @param slicerSelection The slicerSelection to set
193    */

194   public void setSlicerSelection(Collection JavaDoc selection) {
195     clear();
196     updateHierarchy(selection);
197     if (slicerSelection == null)
198       slicerSelection = new ArrayList JavaDoc();
199     else
200       slicerSelection.clear();
201     slicerSelection.addAll(selection);
202     slicerSelectionDirty = true;
203     category.setDirty(true);
204     expression = null;
205   }
206
207   private void updateHierarchy(Collection JavaDoc selection) {
208     if (selection == null || selection.isEmpty())
209       hierarchy = dimension.getHierarchies()[0];
210     else {
211       Member m = (Member) selection.iterator().next();
212       hierarchy = m.getLevel().getHierarchy();
213       if (!hierarchy.getDimension().equals(dimension))
214         logger.error("invalid dimension in " + hierarchy.getLabel());
215     }
216
217   }
218
219   /**
220    * Returns the axisSelectionDirty.
221    * @return boolean
222    */

223   public boolean isAxisSelectionDirty() {
224     return axisSelectionDirty;
225   }
226
227   /**
228    * Returns the slicerSelectionDirty.
229    * @return boolean
230    */

231   public boolean isSlicerSelectionDirty() {
232     return slicerSelectionDirty;
233   }
234
235   /**
236    * Sets the axisSelectionDirty.
237    * @param axisSelectionDirty The axisSelectionDirty to set
238    */

239   public void setAxisSelectionDirty(boolean axisSelectionDirty) {
240     this.axisSelectionDirty = axisSelectionDirty;
241   }
242
243   /**
244    * Sets the slicerSelectionDirty.
245    * @param slicerSelectionDirty The slicerSelectionDirty to set
246    */

247   public void setSlicerSelectionDirty(boolean slicerSelectionDirty) {
248     this.slicerSelectionDirty = slicerSelectionDirty;
249   }
250
251   public void setSelection(Collection JavaDoc selection) {
252     category.setSelection(this, selection);
253   }
254   /**
255    * validates the selection.
256    * @param selection
257    * @return null on success, error message on error
258    */

259   public String JavaDoc validateSelection(Collection JavaDoc selection) {
260     return category.validateSelection(this, selection);
261   }
262
263   /**
264    * lexical compare for GUI lists
265    */

266   public int compareTo(Object JavaDoc arg) {
267     HierarchyItem that = (HierarchyItem) arg;
268     return this.hierarchy.getLabel().compareTo(that.getHierarchy().getLabel());
269   }
270
271   /**
272    * removes deleted members from the selections.
273    */

274   public void removeFromSelection(Set JavaDoc deleted) {
275     if (axisSelection != null)
276       axisSelection.removeAll(deleted);
277     if (slicerSelection != null)
278       slicerSelection.removeAll(deleted);
279   }
280
281   /**
282    * the collection of members that shall be deleted from the query.
283    * (i.e. remove calculated members)
284    */

285   public Collection JavaDoc getDeleted() {
286     return deleted;
287   }
288
289   /**
290    * the collection of members that shall be deleted from the query.
291    * (i.e. remove calculated members)
292    */

293   public void setDeleted(Collection JavaDoc c) {
294     deleted.clear();
295     deleted.addAll(c);
296   }
297
298   public Object JavaDoc getExpression() {
299     return expression;
300   }
301
302   public void setExpression(Object JavaDoc object) {
303     clear();
304     expression = object;
305     category.setDirty(true);
306   }
307
308   public boolean isMovable() {
309     return !OlapUtils.isSingleRecord(hierarchy);
310   }
311
312   public boolean isClickable() {
313     return navigator.getHierarchyItemClickHandler() != null;
314   }
315
316 }
317
Popular Tags