1 13 14 package com.tonbeller.jpivot.olap.query; 15 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 import org.apache.log4j.Logger; 21 22 import com.tonbeller.jpivot.core.Extension; 23 import com.tonbeller.jpivot.core.ModelSupport; 24 import com.tonbeller.jpivot.olap.model.Axis; 25 import com.tonbeller.jpivot.olap.model.Dimension; 26 import com.tonbeller.jpivot.olap.model.Hierarchy; 27 import com.tonbeller.jpivot.olap.model.Member; 28 import com.tonbeller.jpivot.olap.model.Position; 29 import com.tonbeller.jpivot.olap.model.Result; 30 import com.tonbeller.jpivot.olap.navi.CalcSet; 31 import com.tonbeller.jpivot.olap.navi.SortRank; 32 33 36 public abstract class QueryAdapter { 37 38 static Logger logger = Logger.getLogger(QueryAdapter.class); 39 40 protected ModelSupport model; 41 42 protected Quax[] quaxes; protected boolean useQuax = false; 44 protected boolean axesSwapped = false; 45 protected boolean genMDXHierarchize = false; 46 protected SortRankBase sortMan = null; 47 48 52 protected QueryAdapter(ModelSupport model) { 53 this.model = model; 54 Extension sortExt = model.getExtension(SortRank.ID); 55 if (sortExt != null) { 56 sortMan = (SortRankBase) sortExt; 57 } 58 axesSwapped = false; 59 } 60 61 64 public Quax[] getQuaxes() { 65 return quaxes; 66 } 67 68 71 public void setQuaxes(Quax[] quaxes) { 72 this.quaxes = quaxes; 73 } 74 75 80 public Quax findQuax(Dimension dim) { 81 for (int i = 0; i < quaxes.length; i++) { 82 if (quaxes[i].dimIdx(dim) >= 0) 83 return quaxes[i]; 84 } 85 return null; 86 } 87 88 94 public void afterExecute(Result result) { 95 96 Axis[] axes = result.getAxes(); 97 98 if (!useQuax) { 101 AxisLoop : for (int i = 0; i < axes.length; i++) { 102 List positions = axes[i].getPositions(); 103 quaxes[iASwap(i)].init(positions); 104 } } else { 106 int iQuaxToSort = -1; 108 if (sortMan != null) 109 iQuaxToSort = sortMan.activeQuaxToSort(); 110 if (!genMDXHierarchize) { 111 Hierarchize : for (int i = 0; i < quaxes.length; i++) { 113 if (quaxes[i].isHierarchizeNeeded() && i != iQuaxToSort) 114 ((ResultBase) result).hierarchize(iASwap(i)); 115 } 116 } 117 118 QuaxLoop : for (int i = 0; i < quaxes.length; i++) { 119 120 List positions = axes[iASwap(i)].getPositions(); 121 122 if (quaxes[i].getGenerateMode() == CalcSet.GENERATE) { 126 quaxes[i].resetGenerate(); 127 quaxes[i].init(positions); 128 continue QuaxLoop; 129 } 130 131 int nDimension = quaxes[i].getNDimension(); 135 for (int j = 0; j < nDimension; j++) { 136 if (quaxes[i].isUnknownFunction(j)) { 138 List memList = memListForHier(j, positions); 139 quaxes[i].setHierMemberList(j, memList); 140 } 141 } } } 144 145 if (logger.isDebugEnabled()) { 146 AxisLoop : for (int i = 0; i < axes.length; i++) { 148 List positions = axes[i].getPositions(); 149 logger.debug("Positions of axis " + i); 150 151 if (positions.size() == 0) { 152 logger.debug("0 positions"); 154 } else { 155 int nDimension = ((Position) positions.get(0)).getMembers().length; 156 PositionLoop : for (Iterator iter = positions.iterator(); iter.hasNext();) { 157 Position pos = (Position) iter.next(); 158 Member[] mems = pos.getMembers(); 159 StringBuffer sb = new StringBuffer (); 160 for (int j = 0; j < nDimension; j++) { 161 if (j > 0) 162 sb.append(" * "); 163 Member[] memsj = new Member[j + 1]; 164 for (int k = 0; k <= j; k++) 165 memsj[k] = mems[k]; 166 if (this.canExpand(memsj)) 167 sb.append("(+)"); 168 else if (this.canCollapse(memsj)) 169 sb.append("(-)"); 170 else 171 sb.append(" "); 172 sb.append(((MDXElement) mems[j]).getUniqueName()); 173 } 174 logger.debug(sb.toString()); 175 } } 177 178 } } 180 } 181 182 187 private List memListForHier(int hierIndex, List positions) { 188 List memList = new ArrayList (); 189 PositionLoop : for (Iterator iter = positions.iterator(); iter.hasNext();) { 190 Position pos = (Position) iter.next(); 191 Member m = pos.getMembers()[hierIndex]; 192 if (!memList.contains(m)) 193 memList.add(m); 194 } 195 return memList; 196 } 197 198 203 protected abstract Object createMemberSet(List memList); 204 205 209 219 public abstract boolean canExpand(Member member); 220 221 227 public abstract boolean canExpand(Member[] pathMembers); 228 229 234 public abstract boolean canCollapse(Member member); 235 236 241 public abstract boolean canCollapse(Member[] pathMembers); 242 243 250 public void expand(Member member) { 251 Dimension dim = member.getLevel().getHierarchy().getDimension(); 252 Quax quax = findQuax(dim); 253 if (logger.isInfoEnabled()) 254 logger.info("expand Member" + poString(null, member)); 255 if ((quax == null) || !quax.canExpand(member)) { 256 logger.fatal("Expand Member failed for" + ((MDXElement) member).getUniqueName()); 257 return; 259 } 260 quax.expand(member); 261 model.fireModelChanged(); 262 } 263 264 271 public void expand(Member[] pathMembers) { 272 Member member = pathMembers[pathMembers.length - 1]; 273 Dimension dim = member.getLevel().getHierarchy().getDimension(); 274 Quax quax = findQuax(dim); 275 276 if (logger.isDebugEnabled()) 277 logger.info("expand Path" + poString(pathMembers, null)); 278 if ((quax == null) || !quax.canExpand(pathMembers)) { 279 logger.fatal("Expand failed for" + poString(pathMembers, null)); 280 throw new java.lang.IllegalArgumentException ("cannot expand"); 281 } 282 283 quax.expand(pathMembers); 284 model.fireModelChanged(); 285 } 286 287 293 public void collapse(Member member) { 294 Dimension dim = member.getLevel().getHierarchy().getDimension(); 295 296 if (logger.isInfoEnabled()) { 297 logger.info("collapse " + ((MDXElement) member).getUniqueName()); 298 } 299 Quax quax = findQuax(dim); 300 if (quax == null) { 301 logger.info("collapse Quax was null " + ((MDXElement) member).getUniqueName()); 302 return; 303 } 304 quax.collapse(member); 305 306 model.fireModelChanged(); 307 } 308 309 315 public void collapse(Member[] pathMembers) { 316 317 if (logger.isDebugEnabled()) { 318 logger.debug("collapse" + poString(pathMembers, null)); 319 } 320 321 Member member = pathMembers[pathMembers.length - 1]; 322 Dimension dim = member.getLevel().getHierarchy().getDimension(); 323 Quax quax = findQuax(dim); 324 if (quax == null) { 325 logger.debug("collapse Quax was null" + poString(pathMembers, null)); 326 return; 327 } 328 329 quax.collapse(pathMembers); 330 model.fireModelChanged(); 331 } 332 333 337 340 public abstract boolean canDrillDown(Member member); 341 342 346 public boolean canDrillUp(Hierarchy hier) { 347 348 Quax quax = findQuax(hier.getDimension()); 349 return (quax == null) ? false : quax.canDrillUp(hier); 350 } 351 352 358 public void drillDown(Member member) { 359 360 Quax quax = findQuax(member.getLevel().getHierarchy().getDimension()); 362 363 if (quax == null) { 364 logger.info("drillDown Quax was null" + poString(null, member)); 365 return; 366 } 367 368 quax.drillDown(member); 370 371 model.fireModelChanged(); 372 373 if (logger.isInfoEnabled()) { 374 logger.info("drillDown " + poString(null, member)); 375 } 376 } 377 378 383 public void drillUp(Hierarchy hier) { 384 385 Quax quax = findQuax(hier.getDimension()); 387 if (quax == null) { 388 logger.info("drillUp Hierarchy Quax was null" + hier.getLabel()); 389 return; 390 } 391 quax.drillUp(hier); 392 393 model.fireModelChanged(); 394 395 if (logger.isInfoEnabled()) 396 logger.info("drillUp Hierarchy " + hier.getLabel()); 397 } 398 399 403 406 public boolean isSwapAxes() { 407 return axesSwapped; 408 } 409 410 415 public int iASwap(int i) { 416 if (axesSwapped) 417 return (i + 1) % 2; 418 else 419 return i; 420 } 421 422 425 431 432 436 442 protected String poString(Member[] posMembers, Member member) { 443 StringBuffer sb = new StringBuffer (); 444 if (posMembers != null) { 445 sb.append(" Position="); 446 for (int i = 0; i < posMembers.length; i++) { 447 if (i > 0) 448 sb.append(" "); 449 sb.append(((MDXElement) posMembers[i]).getUniqueName()); 450 } 451 } 452 if (member != null) { 453 sb.append(" Member="); 454 sb.append(((MDXElement) member).getUniqueName()); 455 } 456 return sb.toString(); 457 } 458 459 460 463 public boolean isUseQuax() { 464 return useQuax; 465 } 466 467 470 public void setUseQuax(boolean b) { 471 useQuax = b; 472 } 473 474 478 481 public interface QueryAdapterHolder { 482 QueryAdapter getQueryAdapter(); 483 } 484 485 } | Popular Tags |