1 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 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; 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 56 public void quaxChanged(Quax quax, Object source, boolean changedByNavi) { 57 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 80 public int getTopBottomCount() { 81 return topBottomCount; 82 } 83 84 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 109 public int getSortMode() { 110 return sortMode; 111 } 112 113 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 135 public boolean isCurrentSorting(Position position) { 136 if (!sorting || sortPosMembers == null) return false; 138 else 139 return OlapUtils.compareMembers(sortPosMembers, position.getMembers()); 140 } 141 142 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 162 public void sort(Axis membersToSort, Position position) { 163 164 boolean logInfo = logger.isInfoEnabled(); 165 166 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 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 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 202 public boolean isSorting() { 203 return sorting; 204 } 205 206 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 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 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 246 public Member[] getSortPosMembers() { 247 return sortPosMembers; 248 } 249 250 253 public void setSortPosMembers(Member[] sortPosMembers) { 254 this.sortPosMembers = sortPosMembers; 255 } 256 257 260 public abstract void addSortToQuery(); 261 262 265 public int getQuaxToSort() { 266 return quaxToSort.getOrdinal(); 267 } 268 269 272 public int activeQuaxToSort() { 273 if (sorting && sortPosMembers != null) 274 return quaxToSort.getOrdinal(); 275 else 276 return -1; 277 } 278 279 283 public void setQuaxToSort(int ordinal) { 284 QueryAdapter.QueryAdapterHolder model = (QueryAdapterHolder) getModel(); 285 quaxToSort = model.getQueryAdapter().getQuaxes()[ordinal]; 286 } 287 288 291 public boolean isSortOnQuery() { 292 return sorting && sortPosMembers != null; 293 } 294 295 } | Popular Tags |