KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > mondrian > MondrianPlaceHierarchies


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.mondrian;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 import mondrian.olap.Exp;
19 import mondrian.olap.Member;
20 import mondrian.olap.SchemaReader;
21 import mondrian.olap.Syntax;
22 import mondrian.mdx.UnresolvedFunCall;
23 import mondrian.mdx.MemberExpr;
24
25 import org.apache.log4j.Logger;
26
27 import com.tonbeller.jpivot.core.ExtensionSupport;
28 import com.tonbeller.jpivot.olap.model.Axis;
29 import com.tonbeller.jpivot.olap.model.Hierarchy;
30 import com.tonbeller.jpivot.olap.navi.PlaceHierarchiesOnAxes;
31 import com.tonbeller.jpivot.olap.query.Quax;
32
33 /**
34  * generate Mondrian axis according to navigator
35  */

36 public class MondrianPlaceHierarchies extends ExtensionSupport implements PlaceHierarchiesOnAxes {
37
38   private boolean expandAllMember = false;
39   ArrayList JavaDoc aMemberSet = null;
40   static Logger logger = Logger.getLogger(MondrianPlaceHierarchies.class);
41
42   /**
43    * Constructor sets ID
44    */

45   public MondrianPlaceHierarchies() {
46     super.setId(PlaceHierarchiesOnAxes.ID);
47   }
48
49   /**
50    * @see com.tonbeller.jpivot.olap.navi.PlaceHierarchiesOnAxes#createMemberExpression(Hierarchy)
51    */

52   public Object JavaDoc createMemberExpression(Hierarchy hier) {
53
54     mondrian.olap.Hierarchy monHier = ((MondrianHierarchy) hier).getMonHierarchy();
55
56     // if the query does not contain the hier,
57
// just return the highest level
58
MondrianModel model = (MondrianModel) getModel();
59     MondrianQueryAdapter adapter = (MondrianQueryAdapter) model.getQueryAdapter();
60
61     // find the Quax for this hier
62
Quax quax = adapter.findQuax(hier.getDimension());
63     if (quax == null) {
64       // the hierarchy was not found on any axis
65
SchemaReader scr = ((MondrianModel) getModel()).getMonConnection().getSchemaReader();
66       return MondrianUtil.topLevelMembers(monHier, expandAllMember, scr);
67       // return top level members of the hierarchy
68
}
69
70     // the member expression is the list of members plus the list of FunCalls
71
// for this dimension
72
int iDimension = quax.dimIdx(hier.getDimension());
73     return quax.genExpForDim(iDimension);
74   }
75
76   /**
77    * @see com.tonbeller.jpivot.olap.navi.PlaceHierarchiesOnAxes#setQueryAxis(Axis, Object[])
78    */

79   public void setQueryAxis(Axis target, Object JavaDoc[] memberExpressions) {
80
81     MondrianModel model = (MondrianModel) getModel();
82     MondrianQueryAdapter adapter = (MondrianQueryAdapter) model.getQueryAdapter();
83
84     // locate the appropriate query axis
85
int iQuax = ((MondrianAxis) target).getOrdinal();
86     if (adapter.isSwapAxes())
87       iQuax = (iQuax + 1) % 2;
88     Quax quax = adapter.getQuaxes()[iQuax];
89
90     int nDimension = 0;
91     for (int i = 0; i < memberExpressions.length; i++) {
92       if (memberExpressions[i] != null)
93         ++nDimension;
94     }
95
96     // if any of the member expressions is a memberlist from PlaceMembers
97
// we will have to reset sorting
98
Object JavaDoc[] sets = new Object JavaDoc[nDimension];
99     boolean changedMemberSet = false;
100     int j = 0;
101     for (int i = 0; i < memberExpressions.length; i++) {
102       // null possible due to access control
103
if (memberExpressions[i] instanceof List JavaDoc) {
104         List JavaDoc memberList = (List JavaDoc) memberExpressions[i];
105         Exp[] members = new Exp[memberList.size()];
106         for (int k = 0; k < members.length; k++) {
107             members[k] = new MemberExpr((Member) memberList.get(k));
108         }
109         if (members.length == 1)
110           sets[j++] = members[0];
111         else
112           sets[j++] = new UnresolvedFunCall("{}", Syntax.Braces, members);
113         changedMemberSet = true;
114       } else if (memberExpressions[i] != null) {
115         // object generated by createMemberExpression or CalcSet.createAxisExpression
116
sets[j++] = memberExpressions[i];
117       }
118     }
119
120     // generate the crossjoins
121
quax.regeneratePosTree(sets, true);
122
123     if (logger.isInfoEnabled()) {
124       String JavaDoc changed = "";
125       if (changedMemberSet)
126         changed = " changed by navi";
127       logger.info("setQueryAxis axis=" + quax.getOrdinal() + " nDimension=" + nDimension + changed);
128       logger.info("Expression for Axis=" + quax.toString());
129     }
130
131     // tell listeners, that the axis was changed.
132
quax.changed(this, changedMemberSet);
133
134     model.fireModelChanged();
135   }
136
137   /**
138    * @see PlaceHierarchiesOnAxes#setExpandAllMember
139    */

140   public void setExpandAllMember(boolean expandAllMember) {
141     this.expandAllMember = expandAllMember;
142   }
143
144   /**
145    * @see PlaceHierarchiesOnAxes#getExpandAllMember
146    */

147   public boolean getExpandAllMember() {
148     return expandAllMember;
149   }
150
151 } // End MondrianPlaceHierarchies
152
Popular Tags