001    /*
002    // $Id: //open/mondrian-release/3.2/src/main/mondrian/olap4j/MondrianOlap4jCell.java#2 $
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 mondrian.olap4j;
011    
012    import mondrian.rolap.RolapCell;
013    
014    import mondrian.rolap.SqlStatement;
015    import org.olap4j.*;
016    import org.olap4j.metadata.Property;
017    
018    import java.sql.ResultSet;
019    import java.util.*;
020    
021    /**
022     * Implementation of {@link Cell}
023     * for the Mondrian OLAP engine.
024     *
025     * @author jhyde
026     * @version $Id: //open/mondrian-release/3.2/src/main/mondrian/olap4j/MondrianOlap4jCell.java#2 $
027     * @since May 24, 2007
028     */
029    class MondrianOlap4jCell implements Cell {
030        private final int[] coordinates;
031        private final MondrianOlap4jCellSet olap4jCellSet;
032        private final mondrian.olap.Cell cell;
033    
034        /**
035         * Creates a MondrianOlap4jCell.
036         *
037         * @param coordinates Coordinates
038         * @param olap4jCellSet Cell set
039         * @param cell Cell in native Mondrian representation
040         */
041        MondrianOlap4jCell(
042            int[] coordinates,
043            MondrianOlap4jCellSet olap4jCellSet,
044            mondrian.olap.Cell cell)
045        {
046            assert coordinates != null;
047            assert olap4jCellSet != null;
048            assert cell != null;
049            this.coordinates = coordinates;
050            this.olap4jCellSet = olap4jCellSet;
051            this.cell = cell;
052        }
053    
054        public CellSet getCellSet() {
055            return olap4jCellSet;
056        }
057    
058        public int getOrdinal() {
059            return (Integer) cell.getPropertyValue(
060                mondrian.olap.Property.CELL_ORDINAL.name);
061        }
062    
063        public List<Integer> getCoordinateList() {
064            ArrayList<Integer> list = new ArrayList<Integer>(coordinates.length);
065            for (int coordinate : coordinates) {
066                list.add(coordinate);
067            }
068            return list;
069        }
070    
071        public Object getPropertyValue(Property property) {
072            // We assume that mondrian properties have the same name as olap4j
073            // properties.
074            return cell.getPropertyValue(property.getName());
075        }
076    
077        public boolean isEmpty() {
078            // FIXME
079            return cell.isNull();
080        }
081    
082        public boolean isError() {
083            return cell.isError();
084        }
085    
086        public boolean isNull() {
087            return cell.isNull();
088        }
089    
090        public double getDoubleValue() throws OlapException {
091            Object o = cell.getValue();
092            if (o instanceof Number) {
093                Number number = (Number) o;
094                return number.doubleValue();
095            }
096            throw olap4jCellSet.olap4jStatement.olap4jConnection.helper
097                .createException(this, "not a number");
098        }
099    
100        public String getErrorText() {
101            Object o = cell.getValue();
102            if (o instanceof Throwable) {
103                return ((Throwable) o).getMessage();
104            } else {
105                return null;
106            }
107        }
108    
109        public Object getValue() {
110            return cell.getValue();
111        }
112    
113        public String getFormattedValue() {
114            return cell.getFormattedValue();
115        }
116    
117        public ResultSet drillThrough() throws OlapException {
118            return drillThroughInternal(-1, -1);
119        }
120    
121        /**
122         * Executes drill-through on this cell.
123         *
124         * <p>Not a part of the public API. Package-protected because this method
125         * also implements the DRILLTHROUGH statement.
126         *
127         * @param maxRowCount Maximum number of rows to retrieve, <= 0 if unlimited
128         * @param firstRowOrdinal Ordinal of row to skip to (1-based), or 0 to
129         *   start from beginning
130         * @return Result set
131         * @throws OlapException on error
132         */
133        ResultSet drillThroughInternal(
134            int maxRowCount,
135            int firstRowOrdinal) throws OlapException
136        {
137            final SqlStatement sqlStmt =
138                ((RolapCell) cell).drillThroughInternal(
139                    maxRowCount, firstRowOrdinal, null, false, null);
140            return sqlStmt.getWrappedResultSet();
141        }
142    
143        public void setValue(
144            Object newValue,
145            AllocationPolicy allocationPolicy,
146            Object... allocationArgs)
147        {
148            Scenario scenario =
149                olap4jCellSet.olap4jStatement.olap4jConnection.getScenario();
150            cell.setValue(scenario, newValue, allocationPolicy, allocationArgs);
151        }
152    }
153    
154    // End MondrianOlap4jCell.java