1 13 package com.tonbeller.jpivot.mondrian; 14 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import mondrian.olap.*; 19 import mondrian.mdx.MemberExpr; 20 import mondrian.mdx.UnresolvedFunCall; 21 22 import org.apache.log4j.Logger; 23 24 import com.tonbeller.jpivot.olap.model.Dimension; 25 import com.tonbeller.jpivot.olap.model.Hierarchy; 26 import com.tonbeller.jpivot.olap.model.Member; 27 import com.tonbeller.jpivot.olap.query.Quax; 28 import com.tonbeller.jpivot.olap.query.QuaxChangeListener; 29 import com.tonbeller.jpivot.olap.query.QueryAdapter; 30 31 34 public class MondrianQueryAdapter extends QueryAdapter implements QuaxChangeListener { 35 36 static Logger logger = Logger.getLogger(MondrianQueryAdapter.class); 37 38 private String originalMDX; 39 private Query monQuery = null; private Query cloneQuery = null; private int nAxes; 43 private final SchemaReader scr; 44 45 48 MondrianQueryAdapter(MondrianModel model, mondrian.olap.Query monQuery) { 49 super(model); 50 this.monQuery = monQuery; 51 scr = model.getMonConnection().getSchemaReader(); 52 53 genMDXHierarchize = true; 55 nAxes = monQuery.getAxes().length; 57 quaxes = new MondrianQuax[nAxes]; 58 for (int i = 0; i < monQuery.getAxes().length; i++) { 59 mondrian.olap.Hierarchy[] monHiers = monQuery.getMdxHierarchiesOnAxis( 60 AxisOrdinal.forLogicalOrdinal(i)); 61 quaxes[i] = new MondrianQuax(i, monQuery.getAxes()[i], model); 62 Hierarchy[] hiers = new Hierarchy[monHiers.length]; 63 for (int j = 0; j < hiers.length; j++) { 64 hiers[j] = model.lookupHierarchy(monHiers[j].getUniqueName()); 65 } 66 quaxes[i].setHiers(hiers); 67 quaxes[i].addChangeListener(this); 68 } 69 } 70 71 74 public void quaxChanged(Quax quax, Object source, boolean changedByNavi) { 75 useQuax = true; 76 } 77 78 82 public Query getMonQuery() { 83 84 if (monQuery != null) 85 return monQuery; 86 87 try { 88 logger.warn("NOT EXPECTED getMonQuery calling parseQuery"); 89 MondrianModel mmodel = (MondrianModel) model; 90 monQuery = mmodel.getConnection().parseQuery(mmodel.getMdxQuery()); 91 } catch (Exception ex) { 92 logger.fatal("getMonQuery parse error", ex); 94 } 95 return monQuery; 96 } 97 98 101 public void setMonQuery(Query q) { 102 this.monQuery = q; 103 } 104 105 115 protected void onExecute() { 116 117 if (useQuax) { 119 int iQuaxToSort = -1; 120 if (sortMan != null) 121 iQuaxToSort = sortMan.activeQuaxToSort(); 122 123 for (int i = 0; i < quaxes.length; i++) { 124 if (quaxes[i].getPosTreeRoot() == null) 125 continue; 126 boolean doHierarchize = false; 127 if (genMDXHierarchize && quaxes[i].isHierarchizeNeeded() && i != iQuaxToSort) { 128 doHierarchize = true; 129 if (logger.isDebugEnabled()) 130 logger.debug("MDX Generation added Hierarchize()"); 131 } 132 133 monQuery.getAxes()[iASwap(i)].setSet((Exp) quaxes[i].genExp(doHierarchize)); 134 } } 136 137 if (sortMan != null) { 139 if (!useQuax) { 140 if (cloneQuery == null) { 146 if (sortMan.isSortOnQuery()) 147 cloneQuery = monQuery.safeClone(); 148 } else { 149 if (sortMan.isSortOnQuery()) 151 monQuery = cloneQuery.safeClone(); 152 else 153 monQuery = cloneQuery; 154 } 155 } 156 sortMan.addSortToQuery(); 157 } 158 159 long t1 = System.currentTimeMillis(); 160 String mdx = monQuery.toString(); 161 long t2 = System.currentTimeMillis(); 162 logger.info("monQuery.toString took " + (t2 - t1) + " millisec"); 163 ((MondrianModel) model).setCurrentMdx(mdx); 164 165 if (logger.isDebugEnabled()) 166 logger.debug(mdx); 167 168 } 169 170 173 protected String getCurrentMdx() { 174 175 String mdx = monQuery.toString(); 176 return mdx; 177 } 178 179 184 protected Object createMemberSet(List memList) { 185 Exp[] exps = new Exp[memList.size()]; 186 int i = 0; 187 for (Iterator iter = memList.iterator(); iter.hasNext();) { 188 MondrianMember m = (MondrianMember) iter.next(); 189 exps[i++] = new MemberExpr(m.getMonMember()); 190 } 191 UnresolvedFunCall f = new UnresolvedFunCall("{}", Syntax.Braces, exps); 192 return f; 193 } 194 195 199 209 public boolean canExpand(Member member) { 210 mondrian.olap.Member monMember = ((MondrianMember) member).getMonMember(); 211 if (monMember.isCalculatedInQuery()) 213 return false; 214 215 if (!scr.isDrillable(monMember)) 216 return false; 217 218 Dimension dim = member.getLevel().getHierarchy().getDimension(); 219 Quax quax = findQuax(dim); 220 return (quax == null) ? false : quax.canExpand(member); 221 } 222 223 229 public boolean canExpand(Member[] pathMembers) { 230 231 MondrianMember m = (MondrianMember) pathMembers[pathMembers.length - 1]; 232 mondrian.olap.Member monMember = m.getMonMember(); 233 if (monMember.isCalculatedInQuery()) 235 return false; 236 237 if (!scr.isDrillable(monMember)) 238 return false; 239 240 Dimension dim = m.getLevel().getHierarchy().getDimension(); 241 Quax quax = findQuax(dim); 242 return (quax == null) ? false : quax.canExpand(pathMembers); 243 } 244 245 250 public boolean canCollapse(Member member) { 251 252 if (((MondrianMember) member).getMonMember().isCalculatedInQuery()) 254 return false; 255 Dimension dim = member.getLevel().getHierarchy().getDimension(); 256 Quax quax = findQuax(dim); 257 return (quax == null) ? false : quax.canCollapse(member); 258 } 259 260 265 public boolean canCollapse(Member[] pathMembers) { 266 267 Member member = pathMembers[pathMembers.length - 1]; 268 if (((MondrianMember) member).getMonMember().isCalculatedInQuery()) 270 return false; 271 Dimension dim = member.getLevel().getHierarchy().getDimension(); 272 Quax quax = findQuax(dim); 273 return (quax == null) ? false : quax.canCollapse(pathMembers); 274 } 275 276 283 public void expand(Member member) { 284 Dimension dim = member.getLevel().getHierarchy().getDimension(); 285 Quax quax = findQuax(dim); 286 287 if (logger.isInfoEnabled()) 288 logger.info("expand Member" + poString(null, member)); 289 if ((quax == null) || !quax.canExpand(member)) { 290 logger.fatal("Expand Member failed for " + ((MondrianMember) member).getUniqueName()); 291 return; 293 } 294 quax.expand(member); 295 model.fireModelChanged(); 296 } 297 298 305 public void expand(Member[] pathMembers) { 306 307 MondrianMember m = (MondrianMember) pathMembers[pathMembers.length - 1]; 308 Dimension dim = m.getLevel().getHierarchy().getDimension(); 309 Quax quax = findQuax(dim); 310 311 if (logger.isDebugEnabled()) 312 logger.info("expand Path" + poString(pathMembers, null)); 313 if ((quax == null) || !quax.canExpand(pathMembers)) { 314 logger.fatal("Expand failed for" + poString(pathMembers, null)); 315 throw new java.lang.IllegalArgumentException ("cannot expand"); 316 } 317 318 quax.expand(pathMembers); 319 model.fireModelChanged(); 320 } 321 322 326 329 public boolean canDrillDown(Member member) { 330 mondrian.olap.Member monMember = ((MondrianMember) member).getMonMember(); 331 if (!scr.isDrillable(monMember)) 332 return false; 333 Dimension dim = member.getLevel().getHierarchy().getDimension(); 334 Quax quax = findQuax(dim); 335 return (quax == null) ? false : quax.canDrillDown(member); 336 } 337 338 342 346 public void setSwapAxes(boolean swap) { 347 if (monQuery.getAxes().length != 2) 348 return; 349 if (swap != axesSwapped) { 351 monQuery.swapAxes(); 352 axesSwapped = swap; 353 if (logger.isInfoEnabled()) { 354 logger.info("swapAxes " + axesSwapped); 355 } 356 model.fireModelChanged(); 357 } 358 } 359 360 } | Popular Tags |