KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > olap > mdxparse > Formula


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.mdxparse;
14
15 import com.tonbeller.jpivot.util.StringUtil;
16
17 /**
18  * Formula representing a WITH MEMBER ...
19  * or a WITH SET ...
20  *
21  */

22 public class Formula implements Exp {
23
24   boolean isMember;
25   String JavaDoc[] names;
26   Exp exp;
27   MemberProperty[] memberProperties;
28
29   /** Construct formula specifying a set. */
30   Formula(String JavaDoc[] names, Exp exp) {
31     this(false, names, exp, new MemberProperty[0]);
32   }
33
34   /** Construct a formula specifying a member. */
35   Formula(String JavaDoc[] names, Exp exp, MemberProperty[] memberProperties) {
36     this(true, names, exp, memberProperties);
37   }
38
39   private Formula(boolean isMember, String JavaDoc[] names, Exp exp, MemberProperty[] memberProperties) {
40     this.isMember = isMember;
41     this.names = names;
42     this.exp = exp;
43     this.memberProperties = memberProperties;
44   }
45
46   /**
47     * Returns the isMember.
48     * @return boolean
49     */

50   public boolean isMember() {
51     return isMember;
52   }
53
54   /**
55    * return the unique name
56    * @return String
57    */

58   public String JavaDoc getUniqeName() {
59     String JavaDoc str = "";
60     for (int i = 0; i < names.length; i++) {
61       if (i > 0)
62         str += ".";
63       str += StringUtil.bracketsAround(names[i]);
64     }
65
66     return str;
67   }
68
69   /**
70    * return the first name (Dimension/Hierarchy)
71    * @return String
72    */

73   public String JavaDoc getFirstName() {
74     return names[0];
75   }
76
77   /**
78     * return the Last name (eg. member name)
79     * @return String
80     */

81   public String JavaDoc getLastName() {
82     int n = names.length - 1;
83     return names[n];
84   }
85
86   /**
87    * format to MDX
88    */

89   public String JavaDoc toMdx() {
90     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
91     if (isMember) {
92       sb.append("MEMBER ");
93     } else {
94       sb.append("SET ");
95
96     }
97
98     for (int i = 0; i < names.length; i++) {
99       if (i > 0)
100         sb.append('.');
101       sb.append(StringUtil.bracketsAround(names[i]));
102     }
103
104     sb.append(" AS '");
105     sb.append(exp.toMdx());
106     sb.append('\'');
107
108     for (int i = 0; i < memberProperties.length; i++) {
109       sb.append(',');
110       sb.append(memberProperties[i].toMdx());
111     }
112
113     return sb.toString();
114   }
115
116   /**
117    *
118    * @see java.lang.Object#clone()
119    */

120   public Object JavaDoc clone() {
121     MemberProperty[] cloneMemberProperties = new MemberProperty[memberProperties.length];
122     for (int i = 0; i < cloneMemberProperties.length; i++) {
123       cloneMemberProperties[i] = (MemberProperty) memberProperties[i].clone();
124     }
125     return new Formula(isMember, names, (Exp) exp.clone(), cloneMemberProperties);
126
127   }
128
129   /**
130    * @see com.tonbeller.jpivot.olap.mdxparse.Exp#accept
131    */

132   public void accept(ExpVisitor visitor) {
133     visitor.visitFormula(this);
134   }
135
136   /**
137    * @return Exp for formula
138    */

139   public Exp getExp() {
140     return exp;
141   }
142
143 } // End Formula
144
Popular Tags