001    /*
002    // $Id: WithMemberNode.java 349 2010-08-12 08:49:13Z jhyde $
003    // This software is subject to the terms of the Eclipse Public License v1.0
004    // Agreement, available at the following URL:
005    // http://www.eclipse.org/legal/epl-v10.html.
006    // Copyright (C) 2007-2010 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009     */
010    package org.olap4j.mdx;
011    
012    import org.olap4j.type.Type;
013    
014    import java.util.List;
015    import java.io.PrintWriter;
016    
017    /**
018     * Parse tree node which declares a calculated member. Represented as the
019     * <code>WITH MEMBER</code> clause of an MDX <code>SELECT</code> statement.
020     *
021     * @version $Id: WithMemberNode.java 349 2010-08-12 08:49:13Z jhyde $
022     * @author jhyde
023     */
024    public class WithMemberNode implements ParseTreeNode {
025    
026        private final ParseRegion region;
027    
028        /** name of set or member */
029        private final IdentifierNode name;
030    
031        /** defining expression */
032        private ParseTreeNode expression;
033    
034        // properties of member, such as SOLVE_ORDER
035        private final List<PropertyValueNode> memberPropertyList;
036    
037        /**
038         * Constructs a formula specifying a member.
039         *
040         * @param region Source code region
041         * @param name   Name of member being declared
042         * @param exp    Expression for value of member
043         * @param memberPropertyList Collection of properties of member
044         */
045        public WithMemberNode(
046            ParseRegion region,
047            IdentifierNode name,
048            ParseTreeNode exp,
049            List<PropertyValueNode> memberPropertyList)
050        {
051            this.region = region;
052            this.name = name;
053            this.expression = exp;
054            this.memberPropertyList = memberPropertyList;
055        }
056    
057        public ParseRegion getRegion() {
058            return region;
059        }
060    
061        public void unparse(ParseTreeWriter writer) {
062            PrintWriter pw = writer.getPrintWriter();
063            pw.print("MEMBER ");
064            name.unparse(writer);
065            writer.indent();
066            pw.println(" AS");
067            // The MDX language, and olap4j's parser, allows formulas in calculated
068            // members and sets to be specified with and without single quotes.
069            expression.unparse(writer);
070            if (memberPropertyList != null) {
071                for (PropertyValueNode memberProperty : memberPropertyList) {
072                    pw.print(", ");
073                    memberProperty.unparse(writer);
074                }
075            }
076            writer.outdent();
077        }
078    
079        /**
080         * Returns the name of the member declared.
081         *
082         * <p>The name is as specified in the parse tree; it may not be identical
083         * to the unique name of the member.
084         *
085         * @return Name of member
086         */
087        public IdentifierNode getIdentifier() {
088            return name;
089        }
090    
091        /**
092         * Returns the expression to evaluate to calculate the member.
093         *
094         * @return expression
095         */
096        public ParseTreeNode getExpression() {
097            return expression;
098        }
099    
100        /**
101         * Sets the expression to evaluate to calculate the member.
102         *
103         * @param expression Expression
104         */
105        public void setExpression(ParseTreeNode expression) {
106            this.expression = expression;
107        }
108    
109    
110        public <T> T accept(ParseTreeVisitor<T> visitor) {
111            T t = visitor.visit(this);
112            name.accept(visitor);
113            expression.accept(visitor);
114            return t;
115        }
116    
117        public Type getType() {
118            // not an expression
119            throw new UnsupportedOperationException();
120        }
121    
122        /**
123         * Returns the list of properties of this member.
124         *
125         * <p>The list may be empty, but is never null.
126         * Each entry is a (name, expression) pair.
127         *
128         * @return list of properties
129         */
130        public List<PropertyValueNode> getMemberPropertyList() {
131            return memberPropertyList;
132        }
133    
134        public WithMemberNode deepCopy() {
135            return new WithMemberNode(
136                this.region, // immutable
137                this.name.deepCopy(),
138                this.expression.deepCopy(),
139                MdxUtil.deepCopyList(memberPropertyList));
140        }
141    }
142    
143    // End WithMemberNode.java