KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > olap > query > MdxOlapModel


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.olap.query;
14
15 import java.util.List JavaDoc;
16
17 import org.apache.log4j.Logger;
18
19 import com.tonbeller.jpivot.core.ModelSupport;
20 import com.tonbeller.jpivot.olap.model.Member;
21 import com.tonbeller.jpivot.olap.model.OlapException;
22 import com.tonbeller.jpivot.util.TreeNode;
23
24 /**
25  * Olap Model based on MDX
26  */

27 public abstract class MdxOlapModel extends ModelSupport {
28
29   static Logger logger = Logger.getLogger(MdxOlapModel.class);
30
31   /**
32    * @return the current MDX statement
33    */

34   public abstract String JavaDoc getCurrentMdx();
35
36   /**
37    * restore quaxes from QuaxBeans
38    * @param quaxes
39    * @param quaxBeans
40    * @throws OlapException
41    */

42   protected void quaxesFromBeans(Quax[] quaxes, QuaxBean[] quaxBeans) throws OlapException {
43     for (int i = 0; i < quaxes.length; i++) {
44       boolean qubonMode = quaxBeans[i].isQubonMode();
45
46       quaxes[i].setQubonMode(qubonMode);
47
48       quaxes[i].setGenerateIndex(quaxBeans[i].getGenerateIndex());
49       quaxes[i].setGenerateMode(quaxBeans[i].getGenerateMode());
50
51       quaxes[i].setNHierExclude(quaxBeans[i].getNHierExclude());
52
53       // handle Position Tree
54
PositionNodeBean rootBean = quaxBeans[i].getPosTreeRoot();
55
56       TreeNode rootNode = createPosTreeFromBean(rootBean);
57       quaxes[i].setPosTreeRoot(rootNode, true);
58       quaxes[i].changed(this, false);
59
60       quaxes[i].setHierarchizeNeeded(quaxBeans[i].isHierarchizeNeeded());
61
62     } // for i (quaxes)
63
}
64
65   /**
66    * restore sort settings from memento
67    * @param sortExt
68    * @param memento
69    */

70   protected void restoreSort(SortRankBase sortExt, Memento memento) {
71     HandleSort: if (sortExt != null) {
72       String JavaDoc[] sortPosUniqueNames = memento.getSortPosMembers();
73       if (sortPosUniqueNames != null) {
74         Member[] members = new Member[sortPosUniqueNames.length];
75         for (int i = 0; i < members.length; i++) {
76           Member m = lookupMemberByUName(sortPosUniqueNames[i]);
77           if (m == null) {
78             logger.warn("sort position member not found " + sortPosUniqueNames[i]);
79             break HandleSort;
80           }
81           members[i] = m;
82         }
83         sortExt.setSortPosMembers(members);
84         sortExt.setQuaxToSort(memento.getQuaxToSort());
85         sortExt.setSortMode(memento.getSortMode());
86         sortExt.setSorting(memento.isSorting());
87         sortExt.setTopBottomCount(memento.getTopBottomCount());
88       }
89     }
90   }
91
92   /**
93    *
94    * @param rootBean
95    * @return
96    */

97   private TreeNode createPosTreeFromBean(PositionNodeBean rootBean) throws OlapException {
98     ExpBean expBean = rootBean.getReference(); // null for root
99
Object JavaDoc exp;
100     if (expBean == null)
101       exp = null;
102     else
103       exp = createExpFromBean(expBean);
104     TreeNode node = new TreeNode(exp);
105     PositionNodeBean[] beanChildren = rootBean.getChildren();
106     if(beanChildren == null)
107       return node; // empty result
108
for (int i = 0; i < beanChildren.length; i++) {
109       TreeNode childNode = createPosTreeFromBean(beanChildren[i]);
110       node.addChildNode(childNode);
111     }
112     return node;
113   }
114
115   /**
116    * populate quax bean from quax
117    * @param quaxBean
118    * @param quax
119    * @param createExpBean - static method to be called
120    * @throws OlapException
121    */

122   protected void beanFromQuax(QuaxBean quaxBean, Quax quax) throws OlapException {
123
124     quaxBean.setOrdinal(quax.getOrdinal());
125     quaxBean.setNDimension(quax.getNDimension());
126     boolean qubonMode = quax.isQubonMode();
127     quaxBean.setQubonMode(qubonMode);
128     PositionNodeBean posTreeRoot = createPosTreeBean(quax.getPosTreeRoot(), null);
129     quaxBean.setPosTreeRoot(posTreeRoot);
130
131     quaxBean.setHierarchizeNeeded(quax.isHierarchizeNeeded());
132     quaxBean.setGenerateIndex(quax.getGenerateIndex());
133     quaxBean.setGenerateMode(quax.getGenerateMode());
134     quaxBean.setNHierExclude(quax.getNHierExclude());
135   }
136
137   /**
138    *
139    * @param node
140    * @param createExpBean - static method to be called
141    * @return
142    * @throws OlapException
143    */

144   PositionNodeBean createPosTreeBean(TreeNode node, ExpBean reference) throws OlapException {
145     PositionNodeBean bean = new PositionNodeBean();
146     bean.setReference(reference);
147     if (node == null) {
148       // possible, PosTreeRoot = null for empty axis
149
bean.setChildren( new PositionNodeBean[0]);
150       return bean;
151     }
152     List JavaDoc childrenList = node.getChildren();
153     PositionNodeBean[] children = new PositionNodeBean[childrenList.size()];
154     for (int i = 0; i < children.length; i++) {
155       TreeNode childNode = (TreeNode) childrenList.get(i);
156       ExpBean refBean = this.createBeanFromExp(childNode.getReference());
157       children[i] = createPosTreeBean(childNode, refBean);
158     }
159     bean.setChildren(children);
160     return bean;
161   }
162
163   /**
164    * store sort parameters to memento
165    * @param sortExt
166    * @param memento
167    */

168   protected void storeSort(SortRankBase sortExt, Memento memento) {
169     Member[] sortPosMembers = sortExt.getSortPosMembers();
170     if (sortPosMembers != null) {
171       String JavaDoc[] uniqueNames = new String JavaDoc[sortPosMembers.length];
172       for (int i = 0; i < uniqueNames.length; i++) {
173         uniqueNames[i] = ((MDXMember) sortPosMembers[i]).getUniqueName();
174       }
175       memento.setSortPosMembers(uniqueNames);
176       memento.setQuaxToSort(sortExt.getQuaxToSort());
177       memento.setTopBottomCount(sortExt.getTopBottomCount());
178       memento.setSortMode(sortExt.getSortMode());
179       memento.setSorting(sortExt.isSorting());
180     }
181   }
182
183   protected abstract Object JavaDoc createExpFromBean(ExpBean expBean) throws OlapException;
184   protected abstract ExpBean createBeanFromExp(Object JavaDoc exp) throws OlapException;
185
186   public abstract Member lookupMemberByUName(String JavaDoc uniqueName);
187
188 } // MdxOlapModel
189
Popular Tags