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-2012 Pentaho
008// All Rights Reserved.
009*/
010package mondrian.olap4j;
011
012import mondrian.mdx.LevelExpr;
013import mondrian.mdx.UnresolvedFunCall;
014import mondrian.olap.*;
015import mondrian.olap.type.TypeUtil;
016
017import org.olap4j.Axis;
018import org.olap4j.CellSetAxisMetaData;
019import org.olap4j.metadata.Hierarchy;
020import org.olap4j.metadata.Property;
021
022import java.util.*;
023
024/**
025 * Implementation of {@link org.olap4j.CellSetMetaData}
026 * for the Mondrian OLAP engine.
027 *
028 * @author jhyde
029  @since Nov 17, 2007
030*/
031class MondrianOlap4jCellSetAxisMetaData implements CellSetAxisMetaData {
032    private final QueryAxis queryAxis;
033    private final MondrianOlap4jCellSetMetaData cellSetMetaData;
034    private final List<Property> propertyList = new ArrayList<Property>();
035
036    /**
037     * Creates a MondrianOlap4jCellSetAxisMetaData.
038     *
039     * @param cellSetMetaData Cell set axis metadata
040     * @param queryAxis Query axis
041     */
042    MondrianOlap4jCellSetAxisMetaData(
043        MondrianOlap4jCellSetMetaData cellSetMetaData,
044        QueryAxis queryAxis)
045    {
046        if (queryAxis == null) {
047            queryAxis = new QueryAxis(
048                false, null, AxisOrdinal.StandardAxisOrdinal.SLICER,
049                QueryAxis.SubtotalVisibility.Undefined);
050        }
051        this.queryAxis = queryAxis;
052        this.cellSetMetaData = cellSetMetaData;
053
054        // populate property list
055        for (Id id : queryAxis.getDimensionProperties()) {
056            final String[] names = id.toStringArray();
057            Property olap4jProperty = null;
058            if (names.length == 1) {
059                olap4jProperty =
060                    Util.lookup(
061                        Property.StandardMemberProperty.class, names[0]);
062                if (olap4jProperty == null) {
063                    olap4jProperty =
064                        MondrianOlap4jProperty.MEMBER_EXTENSIONS.get(names[0]);
065                }
066            }
067            if (olap4jProperty == null) {
068                final UnresolvedFunCall call =
069                    (UnresolvedFunCall)
070                        Util.lookup(
071                            cellSetMetaData.query, id.getSegments(), true);
072                olap4jProperty =
073                    new MondrianOlap4jProperty(
074                        Util.lookupProperty(
075                            ((LevelExpr) call.getArg(0)).getLevel(),
076                            call.getFunName()));
077            }
078            propertyList.add(olap4jProperty);
079        }
080    }
081
082    public Axis getAxisOrdinal() {
083        return Axis.Factory.forOrdinal(
084            queryAxis.getAxisOrdinal().logicalOrdinal());
085    }
086
087    public List<Hierarchy> getHierarchies() {
088        return getHierarchiesNonFilter();
089    }
090
091    /**
092     * Returns the hierarchies on a non-filter axis.
093     *
094     * @return List of hierarchies, never null
095     */
096    private List<Hierarchy> getHierarchiesNonFilter() {
097        final Exp exp = queryAxis.getSet();
098        if (exp == null) {
099            return Collections.emptyList();
100        }
101        List<Hierarchy> hierarchyList = new ArrayList<Hierarchy>();
102        for (mondrian.olap.Hierarchy hierarchy
103            : TypeUtil.getHierarchies(exp.getType()))
104        {
105            hierarchyList.add(
106                cellSetMetaData.olap4jStatement.olap4jConnection.toOlap4j(
107                    hierarchy));
108        }
109        return hierarchyList;
110    }
111
112    public List<Property> getProperties() {
113        return propertyList;
114    }
115}
116
117// End MondrianOlap4jCellSetAxisMetaData.java