KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > xmla > XMLA_Result


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

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 /**
28  * Query Result XMLA
29  */

30 public class XMLA_Result extends ResultBase implements QueryResultHandler {
31  
32   static final String JavaDoc XSI_URI = "http://www.w3.org/2001/XMLSchema-instance";
33
34   static Logger logger = Logger.getLogger(XMLA_Result.class);
35
36   XMLA_Axis axis; // axis beeing currently processed
37

38   private XMLA_Member[] tuple; // current tuple
39
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   /**
48    * Constructor
49    * @param model the associated MondrianModel
50    */

51   public XMLA_Result(XMLA_Model model, XMLA_SOAP soap, String JavaDoc catalog, String JavaDoc 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 JavaDoc catalog, String JavaDoc 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   /**
69    * handle AxisInfo tag
70    * @see com.tonbeller.bii.olap.model.QueryResultHandler#handleAxisInfo
71    */

72   public void handleAxisInfo(String JavaDoc axisName, int axisOrdinal) {
73     axis = new XMLA_Axis(axisOrdinal, axisName);
74     if (axisOrdinal == -1)
75       slicer = axis; // -1 = slicer
76
else
77       axesList.add(axis);
78   }
79
80   /**
81    * handle HierarchyInfo tag
82    * add hierarchy to Axis beeing currently processed
83    * @see com.tonbeller.bii.olap.model.QueryResultHandler#handleHierInfo
84    */

85   public void handleHierInfo(String JavaDoc 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   /**
97    * handle axis
98    * @see com.tonbeller.bii.olap.model.QueryResultHandler#handleAxis(java.lang.String, int)
99    */

100   public void handleAxis(String JavaDoc axisName, int axisOrdinal) {
101     axis = lookupAxisByName(axisName);
102   }
103
104   /**
105    * handle Tuple
106    * @see com.tonbeller.bii.olap.model.QueryResultHandler#handleTuple
107    */

108   public void handleTuple(int axisOrdinal, int positionOrdinal) {
109     int n = axis.getHierarchies().length;
110     tuple = new XMLA_Member[n];
111   }
112
113   /**
114    * handle member
115    * @see com.tonbeller.bii.olap.model.QueryResultHandler#handleMember
116    */

117   public void handleMember(
118     String JavaDoc uniqueName,
119     String JavaDoc caption,
120     String JavaDoc levUName,
121     String JavaDoc 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       // not there yet, create +add it
131
// the result does not contain all member properties,
132
// but it should be sufficient.
133

134       //long levelNumber = -1; //Microsoft
135
// attribute Hierarchy is not reliable (SAP)
136

137       boolean isCalc = ((XMLA_Model) model).isMemberInFormulas(uniqueName);
138       member = new XMLA_Member(((XMLA_Model) model), uniqueName, caption, lev, isCalc);
139     } else {
140       // for a calculated member, the level might be wrong (guessed)
141
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     // desired member properties here
149
// if (model.isSAP() &&
150
if (!member.isCalculated()) {
151       member.clearProps();
152       Map props;
153       XMLA_Model xmod = (XMLA_Model) model;
154       // HHTASK - performance better use map with *tag* as key
155
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 JavaDoc tag = (String JavaDoc) itOtherProps.next();
162         if (xmod.isSAP() || xmod.isMondrian()) {
163           if (!tag.startsWith("_"))
164             continue; // SAP Property tags always(?) start with "_"
165
}
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 JavaDoc nam = prop.getName();
171             String JavaDoc val = (String JavaDoc) otherProps.get(tag);
172             if (val != null && val.length() > 0) {
173               // TODO set alignment of property - see Property.setAlignmet()
174
member.addProp(new PropertyImpl(nam, val));
175             }
176           }
177         }
178       }
179       //XMLA_MemberProp
180
} else {
181       // calculated
182
if (member.getLevel().getHierarchy().getDimension().isMeasure()) {
183         // calculated measure
184
axisOrdinalMeasures = axisOrdinal;
185         calcMeasurePos.put(new Integer JavaDoc(positionOrdinal), member);
186       }
187     }
188
189     tuple[memberOrdinal] = member;
190
191     if (memberOrdinal == tuple.length - 1) {
192       // this is the last member in the tuple
193
// add the position to the axis
194

195       // for a slicerAxis, we do not want the "All" members
196
if (axisOrdinal == -1) {
197         // slicer
198
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) { //MDLEVEL_TYPE_ALL, see oledb.h
204
// this is an All Member on the slicer axis, do not add it
205
tuple[i] = null;
206           } else {
207             ++n;
208           }
209         } // for
210
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   /**
228    * handle Celldata , start of cell loop
229    * @see com.tonbeller.bii.olap.model.QueryResultHandler#handleCellData()
230    */

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; // # positions on axis 0
238
nCells = nCells * nPositions;
239     }
240     // create all cells, as the XMLA result contains only nonempty cells
241
for (int i = 0; i < nCells; i++) {
242 // dsf pass the model to the cell so that drillthrough knows about the model
243
aCells.add(new XMLA_Cell(i,((XMLA_Model) model)));
244     }
245   }
246
247   /**
248    * handle Cell
249    * @see com.tonbeller.bii.olap.model.QueryResultHandler#handleCell
250    */

251   public void handleCell(int iOrdinal, Object JavaDoc value, String JavaDoc fmtValue, String JavaDoc 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       // is this cell for a calculated measure ?
260
// cell ordinal = posY*(AxisXSize-1) + posX
261
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 JavaDoc(posX));
266       else if (axisOrdinalMeasures == 1)
267         m = (XMLA_Member) calcMeasurePos.get(new Integer JavaDoc(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 JavaDoc prop = (String JavaDoc) 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           } //PropLoop
286
}
287       }
288     }
289   }
290
291   private XMLA_Axis lookupAxisByName(String JavaDoc 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   /**
305    * @see com.tonbeller.jpivot.olap.model.Result#getAxes()
306    */

307   public Axis[] getAxes() {
308     return (XMLA_Axis[]) axesList.toArray(new XMLA_Axis[0]);
309   }
310
311 // dsf add getters and setters for drillheader and drillrows
312

313 /**
314  * @return Returns the drillHeaders.
315  */

316 public Map getDrillHeader() {
317     return drillHeader;
318 }
319 /**
320  * @param drillHeaders The drillHeaders to set.
321  */

322 public void setDrillHeader(Map drillHeader) {
323     this.drillHeader = drillHeader;
324 }
325 /**
326  * @return Returns the drillRows.
327  */

328 public List getDrillRows() {
329     return drillRows;
330 }
331 /**
332  * @param drillRows The drillRows to set.
333  */

334 public void setDrillRows(List drillRows) {
335     this.drillRows = drillRows;
336 }
337 } // End XMLA_Result
338
Popular Tags