001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap4j/MondrianOlap4jCellSetAxis.java#4 $
003    // This software is subject to the terms of the Common Public License
004    // Agreement, available at the following URL:
005    // http://www.opensource.org/licenses/cpl.html.
006    // Copyright (C) 2007-2008 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009    */
010    package mondrian.olap4j;
011    
012    import mondrian.olap.AxisOrdinal;
013    import org.olap4j.*;
014    import org.olap4j.metadata.*;
015    
016    import java.util.*;
017    
018    /**
019     * Implementation of {@link org.olap4j.CellSetAxis}
020     * for the Mondrian OLAP engine.
021     *
022     * @author jhyde
023     * @version $Id: //open/mondrian/src/main/mondrian/olap4j/MondrianOlap4jCellSetAxis.java#4 $
024     * @since May 24, 2007
025     */
026    class MondrianOlap4jCellSetAxis implements CellSetAxis {
027        private final MondrianOlap4jCellSet olap4jCellSet;
028        private final mondrian.olap.QueryAxis queryAxis;
029        private final mondrian.olap.Axis axis;
030    
031        MondrianOlap4jCellSetAxis(
032            MondrianOlap4jCellSet olap4jCellSet,
033            mondrian.olap.QueryAxis queryAxis,
034            mondrian.olap.Axis axis)
035        {
036            assert olap4jCellSet != null;
037            assert queryAxis != null;
038            assert axis != null;
039            this.olap4jCellSet = olap4jCellSet;
040            this.queryAxis = queryAxis;
041            this.axis = axis;
042        }
043    
044        public Axis getAxisOrdinal() {
045            switch (queryAxis.getAxisOrdinal()) {
046            case SLICER:
047                return Axis.FILTER;
048            default:
049                return Axis.valueOf(queryAxis.getAxisOrdinal().name());
050            }
051        }
052    
053        public CellSet getCellSet() {
054            return olap4jCellSet;
055        }
056    
057        public CellSetAxisMetaData getAxisMetaData() {
058            final AxisOrdinal axisOrdinal = queryAxis.getAxisOrdinal();
059            switch (axisOrdinal) {
060            case SLICER:
061                return olap4jCellSet.getMetaData().getFilterAxisMetaData();
062            default:
063                return olap4jCellSet.getMetaData().getAxesMetaData().get(
064                    axisOrdinal.logicalOrdinal());
065            }
066        }
067    
068        public List<Position> getPositions() {
069            switch (getAxisOrdinal()) {
070            case FILTER:
071                final List<Hierarchy> hierarchyList =
072                    getAxisMetaData().getHierarchies();
073                final Member[] members = new Member[hierarchyList.size()];
074                final MondrianOlap4jConnection olap4jConnection =
075                    olap4jCellSet.olap4jStatement.olap4jConnection;
076                for (mondrian.olap.Member member : axis.getPositions().get(0)) {
077                    final MondrianOlap4jHierarchy hierarchy =
078                        olap4jConnection.toOlap4j(
079                            member.getHierarchy());
080                    members[hierarchyList.indexOf(hierarchy)] =
081                        olap4jConnection.toOlap4j(
082                            member);
083                }
084                int k = -1;
085                for (Hierarchy hierarchy : hierarchyList) {
086                    ++k;
087                    if (members[k] == null) {
088                        members[k] =
089                            ((MondrianOlap4jHierarchy) hierarchy).getDefaultMember();
090                    }
091                }
092                final Position position = new Position() {
093                    public List<Member> getMembers() {
094                        return Arrays.asList(members);
095                    }
096    
097                    public int getOrdinal() {
098                        return 0;
099                    }
100                };
101                return Collections.singletonList(position);
102            default:
103                return new AbstractList<Position>() {
104                    public Position get(final int index) {
105                        final mondrian.olap.Position mondrianPosition =
106                            axis.getPositions().get(index);
107                        return new MondrianOlap4jPosition(mondrianPosition, index);
108                    }
109    
110                    public int size() {
111                        return axis.getPositions().size();
112                    }
113                };
114            }
115        }
116    
117        public int getPositionCount() {
118            return getPositions().size();
119        }
120    
121        public ListIterator<Position> iterator() {
122            return getPositions().listIterator();
123        }
124    
125        private class MondrianOlap4jPosition implements Position {
126            private final mondrian.olap.Position mondrianPosition;
127            private final int index;
128    
129            public MondrianOlap4jPosition(
130                mondrian.olap.Position mondrianPosition, int index) {
131                this.mondrianPosition = mondrianPosition;
132                this.index = index;
133            }
134    
135            public List<Member> getMembers() {
136                return new AbstractList<Member>() {
137                    public Member get(int index) {
138                        final mondrian.olap.Member mondrianMember =
139                            mondrianPosition.get(index);
140                        return olap4jCellSet.olap4jStatement.olap4jConnection
141                            .toOlap4j(mondrianMember);
142                    }
143    
144                    public int size() {
145                        return mondrianPosition.size();
146                    }
147                };
148            }
149    
150            public int getOrdinal() {
151                return index;
152            }
153        }
154    }
155    
156    // End MondrianOlap4jCellSetAxis.java