KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 package com.tonbeller.jpivot.xmla;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.apache.log4j.Logger;
21
22 import com.tonbeller.jpivot.core.ExtensionSupport;
23 import com.tonbeller.jpivot.olap.mdxparse.FunCall;
24 import com.tonbeller.jpivot.olap.mdxparse.ParsedQuery;
25 import com.tonbeller.jpivot.olap.model.Axis;
26 import com.tonbeller.jpivot.olap.model.Member;
27 import com.tonbeller.jpivot.olap.model.OlapException;
28 import com.tonbeller.jpivot.olap.model.Position;
29 import com.tonbeller.jpivot.olap.model.Result;
30 import com.tonbeller.jpivot.olap.navi.ChangeSlicer;
31 import com.tonbeller.jpivot.olap.query.MDXElement;
32 import com.tonbeller.jpivot.util.ArrayUtil;
33
34 /**
35  * change slicer extension
36  */

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

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

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

81   public void setSlicer(Member[] members) {
82     XMLA_Model model = (XMLA_Model) getModel();
83     XMLA_QueryAdapter adapter = (XMLA_QueryAdapter) model.getQueryAdapter();
84     ParsedQuery pq = adapter.getParsedQuery();
85
86     boolean logInfo = logger.isInfoEnabled();
87
88     if (members.length == 0) {
89       // empty slicer
90
pq.setSlicer(null); // ???
91
if (logInfo)
92         logger.info("slicer set to null");
93     } else {
94       FunCall f = new FunCall("()",(XMLA_Member[]) ArrayUtil.naturalCast(members), FunCall.TypeParentheses);
95       pq.setSlicer(f);
96       if (logInfo) {
97         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("slicer=(");
98         for (int i = 0; i < members.length; i++) {
99           if (i > 0)
100             sb.append(",");
101           sb.append(((MDXElement)members[i]).getUniqueName());
102         }
103         sb.append(")");
104         logger.info(sb.toString());
105       }
106     }
107
108     model.fireModelChanged();
109   }
110
111 } // End XMLA_ChangeSlicer
112
Popular Tags