001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/ResultBase.java#13 $
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) 2001-2002 Kana Software, Inc.
007    // Copyright (C) 2001-2008 Julian Hyde and others
008    // All Rights Reserved.
009    // You must accept the terms of that agreement to use this software.
010    //
011    // jhyde, 10 August, 2001
012    */
013    
014    package mondrian.olap;
015    
016    import java.util.Iterator;
017    import java.util.List;
018    import org.apache.log4j.Logger;
019    import java.io.PrintWriter;
020    
021    /**
022     * Skeleton implementation of {@link Result}.
023     *
024     * @author jhyde
025     * @since 10 August, 2001
026     * @version $Id: //open/mondrian/src/main/mondrian/olap/ResultBase.java#13 $
027     */
028    public abstract class ResultBase implements Result {
029        protected final Query query;
030        protected final Axis[] axes;
031        protected Axis slicerAxis;
032    
033        protected ResultBase(Query query, Axis[] axes) {
034            this.query = query;
035            this.axes = axes;
036        }
037    
038        protected abstract Logger getLogger();
039    
040        public Query getQuery() {
041            return query;
042        }
043    
044        // implement Result
045        public Axis[] getAxes() {
046            return axes;
047        }
048        // implement Result
049        public Axis getSlicerAxis() {
050            return slicerAxis;
051        }
052        // implement Result
053        public void print(PrintWriter pw) {
054            for (int i = -1; i < axes.length; i++) {
055                pw.println("Axis #" + (i + 1) + ":");
056                printAxis(pw, i < 0 ? slicerAxis : axes[i]);
057            }
058            // Usually there are 3 axes: {slicer, columns, rows}. Position is a
059            // {column, row} pair. We call printRows with axis=2. When it recurses
060            // to axis=-1, it prints.
061            int[] pos = new int[axes.length];
062            printRows(pw, axes.length - 1, pos);
063        }
064        private void printRows(PrintWriter pw, int axis, int[] pos) {
065            Axis _axis = axis < 0 ? slicerAxis : axes[axis];
066            List<Position> positions = _axis.getPositions();
067            int i = 0;
068            for (Position position : positions) {
069                if (axis < 0) {
070                    if (i > 0) {
071                        pw.print(", ");
072                    }
073                    printCell(pw, pos);
074                } else {
075                    pos[axis] = i;
076                    if (axis == 0) {
077                        int row = axis + 1 < pos.length ? pos[axis + 1] : 0;
078                        pw.print("Row #" + row + ": ");
079                    }
080                    printRows(pw, axis - 1, pos);
081                    if (axis == 0) {
082                        pw.println();
083                    }
084                }
085                i++;
086            }
087    /*
088            for (int i = 0, count = positions.size(); i < count; i++) {
089                if (axis < 0) {
090                    if (i > 0) {
091                        pw.print(", ");
092                    }
093                    printCell(pw, pos);
094                } else {
095                    pos[axis] = i;
096                    if (axis == 0) {
097                        int row = axis + 1 < pos.length ? pos[axis + 1] : 0;
098                        pw.print("Row #" + row + ": ");
099                    }
100                    printRows(pw, axis - 1, pos);
101                    if (axis == 0) {
102                        pw.println();
103                    }
104                }
105            }
106    */
107        }
108        private void printAxis(PrintWriter pw, Axis axis) {
109            List<Position> positions = axis.getPositions();
110            for (Position position : positions) {
111                boolean firstTime = true;
112                pw.print("{");
113                for (Member member : position) {
114                    if (member.getDimension().isHighCardinality()) {
115                        pw.println(" -- High cardinality dimension --}");
116                        return;
117                    }
118                    if (! firstTime) {
119                        pw.print(", ");
120                    }
121                    pw.print(member.getUniqueName());
122                    firstTime = false;
123                }
124                pw.println("}");
125            }
126        }
127        private void printCell(PrintWriter pw, int[] pos) {
128            Cell cell = getCell(pos);
129            pw.print(cell.getFormattedValue());
130        }
131    
132        /**
133         * Returns the current member of a given dimension at a given location.
134         */
135        public Member getMember(int[] pos, Dimension dimension) {
136            for (int i = -1; i < axes.length; i++) {
137                Axis axis = slicerAxis;
138                int index = 0;
139                if (i >= 0) {
140                    axis = axes[i];
141                    index = pos[i];
142                }
143                List<Position> positions = axis.getPositions();
144                Position position = positions.get(index);
145                for (Member member : position) {
146                    if (member.getDimension() == dimension) {
147                        return member;
148                    }
149                }
150            }
151            return dimension.getHierarchy().getDefaultMember();
152        }
153    
154        public void close() {
155        }
156    }
157    
158    
159    // End ResultBase.java