KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > olap > query > SortRankBase


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.olap.query;
14
15 import org.apache.log4j.Logger;
16
17 import com.tonbeller.jpivot.core.ExtensionSupport;
18 import com.tonbeller.jpivot.core.ModelSupport;
19 import com.tonbeller.jpivot.olap.model.Axis;
20 import com.tonbeller.jpivot.olap.model.Dimension;
21 import com.tonbeller.jpivot.olap.model.Hierarchy;
22 import com.tonbeller.jpivot.olap.model.Member;
23 import com.tonbeller.jpivot.olap.model.OlapUtils;
24 import com.tonbeller.jpivot.olap.model.Position;
25 import com.tonbeller.jpivot.olap.navi.SortRank;
26 import com.tonbeller.jpivot.olap.query.QueryAdapter.QueryAdapterHolder;
27
28 /**
29  * @author hh
30  *
31  * Implementation base of the Sort Extension
32  */

33 public abstract class SortRankBase
34   extends ExtensionSupport
35   implements SortRank, QuaxChangeListener {
36
37   protected int topBottomCount = 10;
38   protected int sortMode = com.tonbeller.jpivot.olap.navi.SortRank.ASC;
39   protected boolean sorting = false;
40   protected Member[] sortPosMembers = null;
41   protected Quax quaxToSort = null; // this is the Quax to be sorted
42

43   static final private int STATE_NONE = 0;
44   static final private int STATE_TOP = 1;
45   static final private int STATE_BOTTOM = 2;
46
47   static Logger logger = Logger.getLogger(SortRankBase.class);
48
49   public SortRankBase() {
50     super.setId(SortRank.ID);
51   }
52
53   /**
54    * implement QuaxChangeListener
55    */

56   public void quaxChanged(Quax quax, Object JavaDoc source, boolean changedByNavi) {
57     // if the axis to sort (normaly *not* the measures)
58
// was changed by the Navi GUI, we want to switch sorting off
59
if (quax != quaxToSort)
60       return;
61     if (!changedByNavi)
62       return;
63
64     if (!sorting)
65       return;
66
67     boolean logDebug = logger.isDebugEnabled();
68     if (logDebug)
69       logger.debug("Quax changed by navi - switch sorting off");
70
71     sorting = false;
72     ModelSupport model = (ModelSupport) this.getModel();
73     model.fireModelChanged();
74   }
75
76   /**
77    * @see com.tonbeller.jpivot.olap.navi.SortRank#getTopBottomCount()
78    * @return top/bottom count
79    */

80   public int getTopBottomCount() {
81     return topBottomCount;
82   }
83
84   /**
85    * @see com.tonbeller.jpivot.mondrian.olap.navi.SortRank#setTopBottomCount(int)
86    * @param top/bottom count
87    */

88   public void setTopBottomCount(int topBottomCount) {
89
90     boolean logInfo = logger.isInfoEnabled();
91
92     if (this.topBottomCount == topBottomCount)
93       return;
94     if (logInfo)
95       logger.info("change topBottomCount from " + this.topBottomCount + " to " + topBottomCount);
96     this.topBottomCount = topBottomCount;
97
98     if (sorting
99       && sortPosMembers != null
100       && (sortMode == SortRank.TOPCOUNT || sortMode == SortRank.BOTTOMCOUNT)) {
101       ((ModelSupport) getModel()).fireModelChanged();
102     }
103   }
104
105   /**
106    * @see com.tonbeller.jpivot.olap.navi.SortRank#getSortMode()
107    * @return sort mode (ASC,DESC,BASC,BDESC,TOPCOUNT,BOTTOMCOUNT)
108    */

109   public int getSortMode() {
110     return sortMode;
111   }
112
113   /**
114    * @see com.tonbeller.jpivot.olap.navi.SortRank#setSortMode(int)
115    * @param sort mode (ASC,DESC,BASC,BDESC)
116    */

117   public void setSortMode(int sortMode) {
118     if (this.sortMode == sortMode)
119       return;
120
121     boolean logInfo = logger.isInfoEnabled();
122     if (logInfo)
123       logger.info("change topBottomCount from " + this.sortMode + " to " + sortMode);
124     this.sortMode = sortMode;
125     if (sorting && sortPosMembers != null) {
126       ((ModelSupport) getModel()).fireModelChanged();
127     }
128   }
129
130   /**
131    * @see com.tonbeller.jpivot.olap.navi.SortRank#isCurrentSorting(Position)
132    * @param position to be checked
133    * @return true, if position is the current sorting position
134    */

135   public boolean isCurrentSorting(Position position) {
136     if (!sorting || sortPosMembers == null) // not sorting
137
return false;
138     else
139       return OlapUtils.compareMembers(sortPosMembers, position.getMembers());
140   }
141
142   /**
143    * returns true, if one of the members is a measure
144    * @param position the position to check for sortability
145    * @return true, if the position is sortable
146    * @see com.tonbeller.jpivot.olap.navi.SortRank#isSortable(Position)
147    */

148   public boolean isSortable(Position position) {
149     Member[] members = position.getMembers();
150     for (int i = 0; i < members.length; i++)
151       if (members[i].getLevel().getHierarchy().getDimension().isMeasure())
152         return true;
153     return false;
154   }
155
156   /**
157    * @see com.tonbeller.jpivot.olap.navi.SortRank#sort(Axis, Position)
158    * @param membersToSort Axis containing the members to be sorted
159    * @param position Position on "other axis" defining the members by which
160    * the membersToSort are sorted
161    */

162   public void sort(Axis membersToSort, Position position) {
163
164     boolean logInfo = logger.isInfoEnabled();
165
166     // if the axis to sort does not contain any positions - sorting is not posssible
167
if (membersToSort.getPositions().isEmpty()) {
168       logger.warn("reject sort, the axis to be sorted is empty");
169       sorting = false;
170       return;
171     }
172
173     QueryAdapter.QueryAdapterHolder model = (QueryAdapter.QueryAdapterHolder) getModel();
174     sortPosMembers = position.getMembers();
175
176     // find the axis to sort
177
Hierarchy[] hiers = membersToSort.getHierarchies();
178     Dimension dim = hiers[0].getDimension();
179     quaxToSort = model.getQueryAdapter().findQuax(dim);
180     if (quaxToSort == null) {
181       logger.warn("reject sort, the Quax is null");
182       sorting = false;
183       return;
184     }
185
186     if (logInfo) {
187       String JavaDoc poString = "";
188       Member[] members = position.getMembers();
189       for (int i = 0; i < members.length; i++) {
190         if (i > 0)
191           poString += " ";
192         poString += ((MDXElement) members[i]).getUniqueName();
193       }
194       logger.info("change Sort Position " + poString + " iAxisToSort=" + quaxToSort.getOrdinal());
195     }
196
197     ((ModelSupport) getModel()).fireModelChanged();
198   }
199
200   /**
201    */

202   public boolean isSorting() {
203     return sorting;
204   }
205
206   /**
207    * @task support for natural sorting
208    */

209   public void setSorting(boolean sorting) {
210     if (sorting == this.sorting)
211       return;
212
213     boolean logInfo = logger.isInfoEnabled();
214     if (logInfo)
215       logger.info("change sorting to " + sorting);
216
217     this.sorting = sorting;
218     ((ModelSupport) getModel()).fireModelChanged();
219   }
220
221   /**
222    * reset to initial state
223    */

224   public void reset() {
225
226     boolean logDebug = logger.isDebugEnabled();
227     if (logDebug)
228       logger.debug("SortRank set to initial state");
229
230     topBottomCount = 10;
231     sortMode = com.tonbeller.jpivot.olap.navi.SortRank.ASC;
232     sorting = false;
233     sortPosMembers = null;
234
235     // we want to know, whenever a quax is changed
236
Quax[] quaxes = ((QueryAdapter.QueryAdapterHolder) getModel()).getQueryAdapter().getQuaxes();
237     for (int i = 0; i < quaxes.length; i++) {
238       quaxes[i].addChangeListener(this);
239     }
240
241   }
242
243   /**
244    * @return
245    */

246   public Member[] getSortPosMembers() {
247     return sortPosMembers;
248   }
249
250   /**
251    * @param members
252    */

253   public void setSortPosMembers(Member[] sortPosMembers) {
254     this.sortPosMembers = sortPosMembers;
255   }
256
257   /**
258    * apply sort to query must be implemented for specific olap sources
259    */

260   public abstract void addSortToQuery();
261
262   /**
263    * @return ordinal of quax to sort
264    */

265   public int getQuaxToSort() {
266     return quaxToSort.getOrdinal();
267   }
268
269   /**
270    * @return ordinal of quax to sort, if sorting is active
271    */

272   public int activeQuaxToSort() {
273     if (sorting && sortPosMembers != null)
274       return quaxToSort.getOrdinal();
275     else
276       return -1;
277   }
278
279   /**
280    * set quax to sort
281    * @parameter ordinal of quax to sort
282    */

283   public void setQuaxToSort(int ordinal) {
284     QueryAdapter.QueryAdapterHolder model = (QueryAdapterHolder) getModel();
285     quaxToSort = model.getQueryAdapter().getQuaxes()[ordinal];
286   }
287
288   /**
289    * @return true, if there is a sort for the query
290    */

291   public boolean isSortOnQuery() {
292     return sorting && sortPosMembers != null;
293   }
294
295 } // End SortRankBase
296
Popular Tags