KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import mondrian.olap.AxisOrdinal;
20 import mondrian.olap.Exp;
21 import mondrian.olap.QueryAxis;
22 import mondrian.olap.Syntax;
23 import mondrian.mdx.UnresolvedFunCall;
24 import mondrian.mdx.MemberExpr;
25
26 import org.apache.log4j.Logger;
27
28 import com.tonbeller.jpivot.core.ExtensionSupport;
29 import com.tonbeller.jpivot.olap.model.Axis;
30 import com.tonbeller.jpivot.olap.model.Member;
31 import com.tonbeller.jpivot.olap.model.OlapException;
32 import com.tonbeller.jpivot.olap.model.Position;
33 import com.tonbeller.jpivot.olap.model.Result;
34 import com.tonbeller.jpivot.olap.navi.ChangeSlicer;
35
36 /**
37  * @author hh
38  */

39 public class MondrianChangeSlicer extends ExtensionSupport implements ChangeSlicer {
40
41   static Logger logger = Logger.getLogger(MondrianChangeSlicer.class);
42
43   /**
44    * Constructor sets ID
45    */

46   public MondrianChangeSlicer() {
47     super.setId(ChangeSlicer.ID);
48   }
49
50   /**
51    * @see com.tonbeller.jpivot.olap.navi.ChangeSlicer#getSlicer()
52    */

53   public Member[] getSlicer() {
54
55     MondrianModel model = (MondrianModel) getModel();
56     // use result rather than query
57
Result res = null;
58     try {
59       res = model.getResult();
60     } catch (OlapException ex) {
61       // do not handle
62
return new Member[0];
63     }
64
65     Axis slicer = res.getSlicer();
66     List JavaDoc positions = slicer.getPositions();
67     List JavaDoc members = new ArrayList JavaDoc();
68     for (Iterator JavaDoc iter = positions.iterator(); iter.hasNext();) {
69       Position pos = (Position) iter.next();
70       Member[] posMembers = pos.getMembers();
71       for (int i = 0; i < posMembers.length; i++) {
72         if (!members.contains(posMembers[i]))
73           members.add(posMembers[i]);
74       }
75     }
76
77     return (Member[]) members.toArray(new Member[0]);
78   }
79
80   /**
81    * @see com.tonbeller.jpivot.olap.navi.ChangeSlicer#setSlicer(Member[])
82    */

83   public void setSlicer(Member[] members) {
84     MondrianModel model = (MondrianModel) getModel();
85     MondrianQueryAdapter adapter = (MondrianQueryAdapter) model.getQueryAdapter();
86     mondrian.olap.Query monQuery = adapter.getMonQuery();
87
88     boolean logInfo = logger.isInfoEnabled();
89
90     if (members.length == 0) {
91       // empty slicer
92
monQuery.setSlicerAxis(null);
93       if (logInfo)
94         logger.info("slicer set to null");
95     } else {
96       Exp[] monExpr = new Exp[members.length];
97       for (int i = 0; i < monExpr.length; i++) {
98         monExpr[i] = createExpressionFor(monQuery, (MondrianMember) members[i]);
99       }
100
101       UnresolvedFunCall f = new UnresolvedFunCall("()", Syntax.Parentheses, monExpr);
102       monQuery.setSlicerAxis(new QueryAxis(false, f, AxisOrdinal.SLICER, QueryAxis.SubtotalVisibility.Undefined));
103       if (logInfo) {
104         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("slicer=(");
105         for (int i = 0; i < monExpr.length; i++) {
106           if (i > 0)
107             sb.append(",");
108           sb.append(monExpr[i].toString());
109         }
110         sb.append(")");
111         logger.info(sb.toString());
112       }
113     }
114     model.fireModelChanged();
115   }
116
117   protected Exp createExpressionFor(mondrian.olap.Query monQuery, MondrianMember member) {
118     return new MemberExpr(member.getMonMember());
119   }
120
121 } // End MondrianChangeSlicer
122
Popular Tags