1 13 package com.tonbeller.jpivot.xmla; 14 15 import java.util.*; 16 17 import org.apache.log4j.Logger; 18 19 import com.tonbeller.jpivot.olap.model.Axis; 20 import com.tonbeller.jpivot.olap.model.OlapException; 21 import com.tonbeller.jpivot.olap.model.QueryResultHandler; 22 import com.tonbeller.jpivot.olap.model.impl.FormatStringParser; 23 import com.tonbeller.jpivot.olap.model.impl.PropertyImpl; 24 import com.tonbeller.jpivot.olap.query.ResultBase; 25 import com.tonbeller.jpivot.xmla.XMLA_PropValAssign.ValAssign; 26 27 30 public class XMLA_Result extends ResultBase implements QueryResultHandler { 31 32 static final String XSI_URI = "http://www.w3.org/2001/XMLSchema-instance"; 33 34 static Logger logger = Logger.getLogger(XMLA_Result.class); 35 36 XMLA_Axis axis; 38 private XMLA_Member[] tuple; private FormatStringParser formatStringParser = new FormatStringParser(); 40 private int axisOrdinalMeasures = -1; 41 private int nXPositions = 0; 42 private Map calcMeasurePos = new HashMap(); 43 private Map drillHeader; 44 private List drillRows; 45 46 47 51 public XMLA_Result(XMLA_Model model, XMLA_SOAP soap, String catalog, String mdx) 52 throws OlapException { 53 super(model); 54 soap.executeQuery(mdx, catalog, this); 55 } 56 57 public XMLA_Result(XMLA_Model model, XMLA_SOAP soap, String catalog, String mdx, boolean drillthrough) 58 throws OlapException { 59 super(model); 60 if (drillthrough) { 61 soap.executeDrillQuery(mdx, catalog, this); 62 } else { 63 soap.executeQuery(mdx, catalog, this); 64 65 } 66 } 67 68 72 public void handleAxisInfo(String axisName, int axisOrdinal) { 73 axis = new XMLA_Axis(axisOrdinal, axisName); 74 if (axisOrdinal == -1) 75 slicer = axis; else 77 axesList.add(axis); 78 } 79 80 85 public void handleHierInfo(String hierName, int axisOrdinal, int number) { 86 XMLA_Hierarchy hier; 87 if(hierName.indexOf("[") > -1 && hierName.indexOf("]") > -1){ 88 hier = ((XMLA_Model) model).lookupHierByUName( hierName ); 89 }else{ 90 hier = ((XMLA_Model) model).lookupHierByUName("[" + hierName + "]" ); 91 } 92 93 axis.addHier(hier); 94 } 95 96 100 public void handleAxis(String axisName, int axisOrdinal) { 101 axis = lookupAxisByName(axisName); 102 } 103 104 108 public void handleTuple(int axisOrdinal, int positionOrdinal) { 109 int n = axis.getHierarchies().length; 110 tuple = new XMLA_Member[n]; 111 } 112 113 117 public void handleMember( 118 String uniqueName, 119 String caption, 120 String levUName, 121 String displayInfo, 122 Map otherProps, 123 int axisOrdinal, 124 int positionOrdinal, 125 int memberOrdinal) { 126 XMLA_Member member = (XMLA_Member) ((XMLA_Model) model).lookupMemberByUName(uniqueName); 127 XMLA_Level lev = ((XMLA_Model) model).lookupLevelByUName(levUName); 128 logger.debug("handleMember: uniqueName - " + uniqueName + ", level - " + levUName); 129 if (member == null) { 130 134 137 boolean isCalc = ((XMLA_Model) model).isMemberInFormulas(uniqueName); 138 member = new XMLA_Member(((XMLA_Model) model), uniqueName, caption, lev, isCalc); 139 } else { 140 XMLA_Level mLev = (XMLA_Level) member.getLevel(); 142 if (!mLev.equals(lev)) { 143 lev = mLev; 144 member.setLevel(mLev); 145 } 146 } 147 member.setDisplayInfo(displayInfo); 148 if (!member.isCalculated()) { 151 member.clearProps(); 152 Map props; 153 XMLA_Model xmod = (XMLA_Model) model; 154 if (xmod.isSAP() || xmod.isMondrian()) 156 props = ((XMLA_Dimension) member.getDimension()).getProps(); 157 else 158 props = ((XMLA_Level) member.getLevel()).getProps(); 159 Iterator itOtherProps = otherProps.keySet().iterator(); 160 while (itOtherProps.hasNext()) { 161 String tag = (String ) itOtherProps.next(); 162 if (xmod.isSAP() || xmod.isMondrian()) { 163 if (!tag.startsWith("_")) 164 continue; } 166 Iterator itProps = props.values().iterator(); 167 while (itProps.hasNext()) { 168 XMLA_MemberProp prop = (XMLA_MemberProp) itProps.next(); 169 if (prop.getXmlTag().equals(tag)) { 170 String nam = prop.getName(); 171 String val = (String ) otherProps.get(tag); 172 if (val != null && val.length() > 0) { 173 member.addProp(new PropertyImpl(nam, val)); 175 } 176 } 177 } 178 } 179 } else { 181 if (member.getLevel().getHierarchy().getDimension().isMeasure()) { 183 axisOrdinalMeasures = axisOrdinal; 185 calcMeasurePos.put(new Integer (positionOrdinal), member); 186 } 187 } 188 189 tuple[memberOrdinal] = member; 190 191 if (memberOrdinal == tuple.length - 1) { 192 195 if (axisOrdinal == -1) { 197 int n = 0; 199 for (int i = 0; i < tuple.length; i++) { 200 XMLA_Member m = tuple[i]; 201 lev = (XMLA_Level) m.getLevel(); 202 203 if (lev.getType() == 1) { tuple[i] = null; 206 } else { 207 ++n; 208 } 209 } if (n < tuple.length) { 211 XMLA_Member[] newTuple = new XMLA_Member[n]; 212 int j = 0; 213 for (int i = 0; i < tuple.length; i++) { 214 if (tuple[i] != null) 215 newTuple[j++] = tuple[i]; 216 } 217 tuple = newTuple; 218 } 219 } 220 221 XMLA_Position pos = new XMLA_Position(axisOrdinal); 222 pos.setMembers(tuple); 223 axis.addPosition(pos); 224 } 225 } 226 227 231 public void handleCellData() { 232 int nCells = 1; 233 for (Iterator iter = axesList.iterator(); iter.hasNext();) { 234 XMLA_Axis ax = (XMLA_Axis) iter.next(); 235 int nPositions = ax.getPositions().size(); 236 if (nXPositions == 0) 237 nXPositions = nPositions; nCells = nCells * nPositions; 239 } 240 for (int i = 0; i < nCells; i++) { 242 aCells.add(new XMLA_Cell(i,((XMLA_Model) model))); 244 } 245 } 246 247 251 public void handleCell(int iOrdinal, Object value, String fmtValue, String fontSize) { 252 253 XMLA_Cell cell = (XMLA_Cell) aCells.get(iOrdinal); 254 cell.setValue(value); 255 cell.setFormattedValue(fmtValue, formatStringParser); 256 if (fontSize != null) { 257 int iFontSize = Integer.parseInt(fontSize); 258 259 int posY = iOrdinal / nXPositions; 262 int posX = iOrdinal - posY * nXPositions; 263 XMLA_Member m = null; 264 if (axisOrdinalMeasures == 0) 265 m = (XMLA_Member) calcMeasurePos.get(new Integer (posX)); 266 else if (axisOrdinalMeasures == 1) 267 m = (XMLA_Member) calcMeasurePos.get(new Integer (posY)); 268 if (m != null) { 269 Map calcMeasurePropMap = ((XMLA_Model) model).getCalcMeasurePropMap(); 270 XMLA_PropValAssign cmprops = (XMLA_PropValAssign) calcMeasurePropMap.get(m.getUniqueName()); 271 if (cmprops != null) { 272 Set propSet = cmprops.getPropMap().keySet(); 273 PropLoop : for (Iterator iter = propSet.iterator(); iter.hasNext();) { 274 String prop = (String ) iter.next(); 275 List valAssignList = cmprops.getValAssignList(prop); 276 for (Iterator iterator = valAssignList.iterator(); iterator.hasNext();) { 277 XMLA_PropValAssign.ValAssign vAssign = (ValAssign) iterator.next(); 278 int mask = vAssign.getBitMask(); 279 int mVal = mask & iFontSize; 280 if (mVal == vAssign.getBitVal()) { 281 cell.addProperty(prop, vAssign.getVal()); 282 continue PropLoop; 283 } 284 } 285 } } 287 } 288 } 289 } 290 291 private XMLA_Axis lookupAxisByName(String name) { 292 293 if (name.equals("SlicerAxis")) 294 return (XMLA_Axis) slicer; 295 296 for (Iterator iter = axesList.iterator(); iter.hasNext();) { 297 XMLA_Axis ax = (XMLA_Axis) iter.next(); 298 if (ax.getName().equals(name)) 299 return ax; 300 } 301 return null; 302 } 303 304 307 public Axis[] getAxes() { 308 return (XMLA_Axis[]) axesList.toArray(new XMLA_Axis[0]); 309 } 310 311 313 316 public Map getDrillHeader() { 317 return drillHeader; 318 } 319 322 public void setDrillHeader(Map drillHeader) { 323 this.drillHeader = drillHeader; 324 } 325 328 public List getDrillRows() { 329 return drillRows; 330 } 331 334 public void setDrillRows(List drillRows) { 335 this.drillRows = drillRows; 336 } 337 } | Popular Tags |