1 13 package com.tonbeller.jpivot.olap.mdxparse; 14 15 import com.tonbeller.jpivot.util.StringUtil; 16 17 22 public class Formula implements Exp { 23 24 boolean isMember; 25 String [] names; 26 Exp exp; 27 MemberProperty[] memberProperties; 28 29 30 Formula(String [] names, Exp exp) { 31 this(false, names, exp, new MemberProperty[0]); 32 } 33 34 35 Formula(String [] names, Exp exp, MemberProperty[] memberProperties) { 36 this(true, names, exp, memberProperties); 37 } 38 39 private Formula(boolean isMember, String [] names, Exp exp, MemberProperty[] memberProperties) { 40 this.isMember = isMember; 41 this.names = names; 42 this.exp = exp; 43 this.memberProperties = memberProperties; 44 } 45 46 50 public boolean isMember() { 51 return isMember; 52 } 53 54 58 public String getUniqeName() { 59 String 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 73 public String getFirstName() { 74 return names[0]; 75 } 76 77 81 public String getLastName() { 82 int n = names.length - 1; 83 return names[n]; 84 } 85 86 89 public String toMdx() { 90 StringBuffer sb = new StringBuffer (); 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 120 public Object 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 132 public void accept(ExpVisitor visitor) { 133 visitor.visitFormula(this); 134 } 135 136 139 public Exp getExp() { 140 return exp; 141 } 142 143 } | Popular Tags |