KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > xmla > XMLA_SortRank


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.xmla;
14
15 import com.tonbeller.jpivot.olap.mdxparse.Exp;
16 import com.tonbeller.jpivot.olap.mdxparse.FunCall;
17 import com.tonbeller.jpivot.olap.mdxparse.Literal;
18 import com.tonbeller.jpivot.olap.mdxparse.ParsedQuery;
19 import com.tonbeller.jpivot.olap.mdxparse.QueryAxis;
20 import com.tonbeller.jpivot.olap.model.Member;
21 import com.tonbeller.jpivot.olap.model.Position;
22 import com.tonbeller.jpivot.olap.navi.SortRank;
23 import com.tonbeller.jpivot.olap.query.SortRankBase;
24
25 /**
26  * SortRank Implementation XMLA
27  */

28 public class XMLA_SortRank extends SortRankBase implements SortRank {
29
30   /**
31   * returns true, if one of the members is a measure
32   * @param position the position to check for sortability
33   * @return true, if the position is sortable
34   * @see com.tonbeller.jpivot.olap.navi.SortRank#isSortable(Position)
35   */

36   public boolean isSortable(Position position) {
37     Member[] members = position.getMembers();
38     for (int i = 0; i < members.length; i++)
39       if (members[i].getLevel().getHierarchy().getDimension().isMeasure())
40         return true;
41     return false;
42   }
43
44   /**
45    * apply sort to query
46    */

47   public void addSortToQuery() {
48     if (sorting && sortPosMembers != null) {
49       XMLA_Model model = (XMLA_Model) getModel();
50       ParsedQuery pq = ((XMLA_QueryAdapter)model.getQueryAdapter()).getParsedQuery();
51
52       switch (sortMode) {
53         case com.tonbeller.jpivot.olap.navi.SortRank.ASC :
54         case com.tonbeller.jpivot.olap.navi.SortRank.DESC :
55         case com.tonbeller.jpivot.olap.navi.SortRank.BASC :
56         case com.tonbeller.jpivot.olap.navi.SortRank.BDESC :
57           // call sort
58
orderAxis(pq);
59           break;
60         case com.tonbeller.jpivot.olap.navi.SortRank.TOPCOUNT :
61           topBottomAxis(pq, "TopCount");
62           break;
63         case com.tonbeller.jpivot.olap.navi.SortRank.BOTTOMCOUNT :
64           topBottomAxis(pq, "BottomCount");
65           break;
66         default :
67           return; // do nothing
68
}
69     }
70   }
71
72   /**
73    * add Order Funcall to QueryAxis
74    * @param monAx
75    * @param monSortMode
76    */

77   private void orderAxis(ParsedQuery pq) {
78     // Order(TopCount) is allowed, Order(Order) is not permitted
79
QueryAxis[] queryAxes = pq.getAxes();
80     QueryAxis qa = queryAxes[quaxToSort.getOrdinal()];
81     Exp setForAx = qa.getExp();
82
83     // setForAx is the top level Exp of the axis
84
// put an Order FunCall around
85
Exp[] args = new Exp[3];
86     args[0] = setForAx; // the set to be sorted is the set representing the query axis
87
// if we got more than 1 position member, generate a tuple for the 2.arg
88
Exp sortExp;
89     if (sortPosMembers.length > 1) {
90       sortExp = new FunCall("()", (XMLA_Member[]) sortPosMembers, FunCall.TypeParentheses);
91     } else {
92       sortExp = (XMLA_Member) sortPosMembers[0];
93     }
94     args[1] = sortExp;
95     args[2] = Literal.createString(sortMode2String(sortMode));
96     FunCall order = new FunCall("Order", args, FunCall.TypeFunction);
97     qa.setExp(order);
98   }
99
100   /**
101    * add Top/BottomCount Funcall to QueryAxis
102    * @param monAx
103    * @param nShow
104    */

105   private void topBottomAxis(ParsedQuery pq, String JavaDoc function) {
106     // TopCount(TopCount) and TopCount(Order) is not permitted
107

108     QueryAxis[] queryAxes = pq.getAxes();
109     QueryAxis qa = queryAxes[quaxToSort.getOrdinal()];
110     Exp setForAx = qa.getExp();
111     Exp sortExp;
112     // if we got more than 1 position member, generate a tuple
113
if (sortPosMembers.length > 1) {
114        sortExp = new FunCall("()", (XMLA_Member[]) sortPosMembers, FunCall.TypeParentheses);
115      } else {
116        sortExp = (XMLA_Member) sortPosMembers[0];
117      }
118      
119     Exp[] args = new Exp[3];
120     args[0] = setForAx; // the set representing the query axis
121
args[1] = Literal.create(new Integer JavaDoc(topBottomCount));
122     args[2] = sortExp;
123     FunCall topbottom = new FunCall(function, args, FunCall.TypeFunction);
124     qa.setExp(topbottom);
125   }
126
127
128   /**
129   * @param sort mode according to JPivot
130   * @return sort mode String according to MDX
131   */

132   static private String JavaDoc sortMode2String(int sortMode) {
133     switch (sortMode) {
134       case com.tonbeller.jpivot.olap.navi.SortRank.ASC :
135         return "ASC";
136       case com.tonbeller.jpivot.olap.navi.SortRank.DESC :
137         return "DESC";
138       case com.tonbeller.jpivot.olap.navi.SortRank.BASC :
139         return "BASC";
140       case com.tonbeller.jpivot.olap.navi.SortRank.BDESC :
141         return "BDESC";
142       default :
143         return ""; // should not happen
144
}
145   }
146
147 } // End XMLA_SortRank
148
Popular Tags