001    /*
002    // $Id: AxisTransform.java 482 2012-01-05 23:27:27Z jhyde $
003    //
004    // Licensed to Julian Hyde under one or more contributor license
005    // agreements. See the NOTICE file distributed with this work for
006    // additional information regarding copyright ownership.
007    //
008    // Julian Hyde licenses this file to you under the Apache License,
009    // Version 2.0 (the "License"); you may not use this file except in
010    // compliance with the License. You may obtain a copy of the License at:
011    //
012    // http://www.apache.org/licenses/LICENSE-2.0
013    //
014    // Unless required by applicable law or agreed to in writing, software
015    // distributed under the License is distributed on an "AS IS" BASIS,
016    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017    // See the License for the specific language governing permissions and
018    // limitations under the License.
019    */
020    package org.olap4j.transform;
021    
022    import org.olap4j.Axis;
023    import org.olap4j.mdx.*;
024    
025    /**
026     * Abstract representation of an MDX query transform acting on
027     * a single query axis (e.g. drill-down on member, roll-up, ...)
028     *
029     * @author etdub
030     * @version $Id: AxisTransform.java 482 2012-01-05 23:27:27Z jhyde $
031     * @since Aug 7, 2008
032     *
033     */
034    public abstract class AxisTransform implements MdxQueryTransform {
035    
036        protected final Axis axis;
037    
038        protected AxisTransform(Axis axis) {
039            this.axis = axis;
040        }
041    
042        public SelectNode apply(SelectNode sn) {
043            // do a deep copy of the existing query SelectNode before
044            // modifying it:
045            SelectNode newSelectNode = sn.deepCopy();
046    
047            for (AxisNode an : newSelectNode.getAxisList()) {
048                if (an.getAxis() == axis) {
049                    // this is the axis we're drilling
050    
051                    ParseTreeNode initialAxisExp = an.getExpression();
052    
053                    // apply the drill operation
054                    ParseTreeNode newAxisExp =
055                        processAxisExp(initialAxisExp);
056    
057                    // replace the expression in the axis by the new generated one
058                    an.setExpression(newAxisExp);
059                }
060            }
061            return newSelectNode;
062        }
063    
064        protected abstract ParseTreeNode processAxisExp(ParseTreeNode axisExp);
065    
066    }
067    
068    // End AxisTransform.java