1 13 package com.tonbeller.jpivot.mondrian; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 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 39 public class MondrianChangeSlicer extends ExtensionSupport implements ChangeSlicer { 40 41 static Logger logger = Logger.getLogger(MondrianChangeSlicer.class); 42 43 46 public MondrianChangeSlicer() { 47 super.setId(ChangeSlicer.ID); 48 } 49 50 53 public Member[] getSlicer() { 54 55 MondrianModel model = (MondrianModel) getModel(); 56 Result res = null; 58 try { 59 res = model.getResult(); 60 } catch (OlapException ex) { 61 return new Member[0]; 63 } 64 65 Axis slicer = res.getSlicer(); 66 List positions = slicer.getPositions(); 67 List members = new ArrayList (); 68 for (Iterator 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 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 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 sb = new StringBuffer ("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 } | Popular Tags |