KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.log4j.Logger;
16
17 import com.tonbeller.jpivot.core.ExtensionSupport;
18 import com.tonbeller.jpivot.olap.navi.NonEmpty;
19
20 /**
21  * @author hh
22  *
23  * Implementation of the NonEmpty Extension for Mondrian Data Source.
24 */

25 public class MondrianNonEmpty extends ExtensionSupport implements NonEmpty {
26
27   static Logger logger = Logger.getLogger(MondrianNonEmpty.class);
28
29   /**
30    * Constructor sets ID
31    */

32   public MondrianNonEmpty() {
33     super.setId(NonEmpty.ID);
34   }
35
36   /**
37    * @see com.tonbeller.jpivot.olap.navi.NonEmpty#isNonEmpty()
38    */

39   public boolean isNonEmpty() {
40
41     MondrianModel m = (MondrianModel)getModel();
42     MondrianQueryAdapter adapter = (MondrianQueryAdapter) m.getQueryAdapter();
43     if (adapter == null)
44       return false; // not initialized
45

46     mondrian.olap.Query monQuery = adapter.getMonQuery();
47
48     // loop over query axes
49
// say yes if any axis has the nonEmpty flag
50
// say no. if none of the axes have the NON EMPTY
51

52     for (int i = 0; i < monQuery.getAxes().length; i++) {
53       mondrian.olap.QueryAxis qAxis = monQuery.getAxes()[i];
54       /*
55             if ( containsOnlyMeasures(qAxis.set) )
56               continue;
57       */

58       if (qAxis.isNonEmpty())
59         return true;
60     }
61     return false; // none of the axes NON EMPTY so far
62

63   }
64
65   /**
66    * @see com.tonbeller.jpivot.olap.navi.NonEmpty#setNonEmpty(boolean)
67    */

68   public void setNonEmpty(boolean nonEmpty) {
69
70     MondrianModel m = (MondrianModel)getModel();
71     MondrianQueryAdapter adapter = (MondrianQueryAdapter) m.getQueryAdapter();
72     if (adapter == null) {
73       logger.error("setNonEmpty query adapter=null");
74       return; // not initialized
75
}
76
77     mondrian.olap.Query monQuery = adapter.getMonQuery();
78
79     // loop over query axes
80
// set the nonEmpty flag, for all axes,
81

82     boolean bChange = false;
83     for (int i = 0; i < monQuery.getAxes().length; i++) {
84       mondrian.olap.QueryAxis qAxis = monQuery.getAxes()[i];
85       /*
86             if ( containsOnlyMeasures(qAxis.set) )
87               continue;
88       */

89       if (qAxis.isNonEmpty() != nonEmpty) {
90         qAxis.setNonEmpty(nonEmpty);
91         bChange = true;
92       }
93     }
94
95     if (bChange && logger.isInfoEnabled())
96         logger.info("Non Empty =" + nonEmpty);
97  
98      if (bChange)
99        ((MondrianModel)getModel()).fireModelChanged();
100
101   }
102
103   /**
104    * check, whether set contains members, which are not measures
105    */

106   /*
107     private boolean containsOnlyMeasures(Exp exp) {
108       if ( exp instanceof FunCall ) {
109         FunCall f = (FunCall)exp;
110         if ( f.isSet() // set, examine every single arg
111           || f.isCallToTuple() // check the members in the tuple
112           || f.isCallToCrossJoin() ) { // analyze every set of the crossjoin
113           for ( int i = 0; i < f.args.length; i ++ ) {
114             if ( !containsOnlyMeasures(f.args[i]) )
115               return false;
116           }
117         } else {
118           // other function, examine first argument
119           if ( !containsOnlyMeasures(f.args[0]) )
120             return false;
121         }
122       } else {
123         // supposed to be a member
124         if ( exp instanceof mondrian.olap.Member &&
125              !((mondrian.olap.Member)exp).isMeasure() )
126           return false;
127       }
128       return true;
129     }
130   */

131
132 } // End MondrianNonEmpty
133
Popular Tags