001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2007-2011 Pentaho
008// All Rights Reserved.
009*/
010package mondrian.olap4j;
011
012import mondrian.calc.TupleList;
013import mondrian.olap.AxisOrdinal;
014import mondrian.rolap.RolapAxis;
015
016import org.olap4j.*;
017import org.olap4j.metadata.Member;
018
019import java.util.*;
020
021/**
022 * Implementation of {@link org.olap4j.CellSetAxis}
023 * for the Mondrian OLAP engine.
024 *
025 * @author jhyde
026 * @since May 24, 2007
027 */
028class MondrianOlap4jCellSetAxis implements CellSetAxis {
029    private final MondrianOlap4jCellSet olap4jCellSet;
030    private final mondrian.olap.QueryAxis queryAxis;
031    private final RolapAxis axis;
032
033    /**
034     * Creates a MondrianOlap4jCellSetAxis.
035     *
036     * @param olap4jCellSet Cell set
037     * @param queryAxis Query axis
038     * @param axis Axis
039     */
040    MondrianOlap4jCellSetAxis(
041        MondrianOlap4jCellSet olap4jCellSet,
042        mondrian.olap.QueryAxis queryAxis,
043        RolapAxis axis)
044    {
045        assert olap4jCellSet != null;
046        assert queryAxis != null;
047        assert axis != null;
048        this.olap4jCellSet = olap4jCellSet;
049        this.queryAxis = queryAxis;
050        this.axis = axis;
051    }
052
053    public Axis getAxisOrdinal() {
054        return Axis.Factory.forOrdinal(
055            queryAxis.getAxisOrdinal().logicalOrdinal());
056    }
057
058    public CellSet getCellSet() {
059        return olap4jCellSet;
060    }
061
062    public CellSetAxisMetaData getAxisMetaData() {
063        final AxisOrdinal axisOrdinal = queryAxis.getAxisOrdinal();
064        if (axisOrdinal.isFilter()) {
065            return olap4jCellSet.getMetaData().getFilterAxisMetaData();
066        } else {
067            return olap4jCellSet.getMetaData().getAxesMetaData().get(
068                axisOrdinal.logicalOrdinal());
069        }
070    }
071
072    public List<Position> getPositions() {
073        return new AbstractList<Position>() {
074            public Position get(final int index) {
075                return new MondrianOlap4jPosition(axis.getTupleList(), index);
076            }
077
078            public int size() {
079                return axis.getTupleList().size();
080            }
081        };
082    }
083
084    public int getPositionCount() {
085        return getPositions().size();
086    }
087
088    public ListIterator<Position> iterator() {
089        return getPositions().listIterator();
090    }
091
092    private class MondrianOlap4jPosition implements Position {
093        private final TupleList tupleList;
094        private final int index;
095
096        /**
097         * Creates a MondrianOlap4jPosition.
098         *
099         * @param tupleList Tuple list
100         * @param index Index of tuple
101         */
102        public MondrianOlap4jPosition(
103            TupleList tupleList,
104            int index)
105        {
106            this.tupleList = tupleList;
107            this.index = index;
108        }
109
110        public List<Member> getMembers() {
111            return new AbstractList<Member>() {
112                public Member get(int slice) {
113                    final mondrian.olap.Member mondrianMember =
114                        tupleList.get(slice, index);
115                    return olap4jCellSet.olap4jStatement.olap4jConnection
116                        .toOlap4j(mondrianMember);
117                }
118
119                public int size() {
120                    return tupleList.getArity();
121                }
122            };
123        }
124
125        public int getOrdinal() {
126            return index;
127        }
128    }
129}
130
131// End MondrianOlap4jCellSetAxis.java