1 13 package com.tonbeller.jpivot.mondrian; 14 15 import mondrian.olap.Exp; 16 import mondrian.olap.Literal; 17 import mondrian.olap.Syntax; 18 import mondrian.mdx.MemberExpr; 19 import mondrian.mdx.UnresolvedFunCall; 20 21 import org.apache.log4j.Logger; 22 23 import com.tonbeller.jpivot.olap.navi.SortRank; 24 import com.tonbeller.jpivot.olap.query.QuaxChangeListener; 25 import com.tonbeller.jpivot.olap.query.SortRankBase; 26 27 32 public class MondrianSortRank extends SortRankBase implements SortRank, QuaxChangeListener { 33 34 static Logger logger = Logger.getLogger(MondrianSortRank.class); 35 36 public MondrianSortRank() { 37 super.setId(SortRank.ID); 38 } 39 40 43 public void addSortToQuery() { 44 if (sorting && sortPosMembers != null) { 45 MondrianModel model = (MondrianModel) getModel(); 46 mondrian.olap.Query monQuery = ((MondrianQueryAdapter)model.getQueryAdapter()).getMonQuery(); 47 48 switch (sortMode) { 49 case com.tonbeller.jpivot.olap.navi.SortRank.ASC : 50 case com.tonbeller.jpivot.olap.navi.SortRank.DESC : 51 case com.tonbeller.jpivot.olap.navi.SortRank.BASC : 52 case com.tonbeller.jpivot.olap.navi.SortRank.BDESC : 53 orderAxis(monQuery, sortMode); 55 break; 56 case com.tonbeller.jpivot.olap.navi.SortRank.TOPCOUNT : 57 topBottomAxis(monQuery, "TopCount"); 58 break; 59 case com.tonbeller.jpivot.olap.navi.SortRank.BOTTOMCOUNT : 60 topBottomAxis(monQuery, "BottomCount"); 61 break; 62 default : 63 return; } 65 } 66 } 67 68 73 static private String sortModeName(int sortMode) { 74 switch (sortMode) { 75 case com.tonbeller.jpivot.olap.navi.SortRank.ASC : 76 return "ASC"; 77 case com.tonbeller.jpivot.olap.navi.SortRank.DESC : 78 return "DESC"; 79 case com.tonbeller.jpivot.olap.navi.SortRank.BASC : 80 return "BASC"; 81 case com.tonbeller.jpivot.olap.navi.SortRank.BDESC : 82 return "BDESC"; 83 default : 84 return null; 85 } 86 } 87 88 93 private void orderAxis(mondrian.olap.Query monQuery, int sortMode) { 94 mondrian.olap.QueryAxis monAx = monQuery.getAxes()[quaxToSort.getOrdinal()]; 97 Exp setForAx = monAx.getSet(); 98 Exp memToSort; 99 if (sortPosMembers.length == 1) { 100 memToSort = new MemberExpr(((MondrianMember) sortPosMembers[0]).getMonMember()); 101 } else { 102 MemberExpr[] memberExprs = new MemberExpr[sortPosMembers.length]; 103 for (int i = 0; i < memberExprs.length; i++) { 104 memberExprs[i] = new MemberExpr(((MondrianMember) sortPosMembers[i]).getMonMember()); 105 } 106 memToSort = new UnresolvedFunCall("()", Syntax.Parentheses, memberExprs); 107 } 108 String sDirection = sortModeName(sortMode); 109 UnresolvedFunCall funOrder = 110 new UnresolvedFunCall("Order", new Exp[] { setForAx, memToSort, Literal.createSymbol(sDirection)}); 111 monAx.setSet(funOrder); 112 } 113 114 117 private void topBottomAxis(mondrian.olap.Query monQuery, String function) { 118 mondrian.olap.QueryAxis monAx = monQuery.getAxes()[quaxToSort.getOrdinal()]; 121 Exp setForAx = monAx.getSet(); 122 Exp memToSort; 123 if (sortPosMembers.length > 1) { 124 MemberExpr[] memberExprs = new MemberExpr[sortPosMembers.length]; 125 for (int i = 0; i < memberExprs.length; i++) { 126 memberExprs[i] = new MemberExpr(((MondrianMember) sortPosMembers[i]).getMonMember()); 127 } 128 memToSort = new UnresolvedFunCall("()", Syntax.Parentheses, memberExprs); 129 } else { 130 memToSort = new MemberExpr(((MondrianMember) sortPosMembers[0]).getMonMember()); 131 } 132 UnresolvedFunCall funOrder = 133 new UnresolvedFunCall( 134 function, 135 new Exp[] { setForAx, Literal.create(new Integer (topBottomCount)), memToSort }); 136 monAx.setSet(funOrder); 137 } 138 139 144 187 188 } | Popular Tags |