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) 2001-2005 Julian Hyde
008// Copyright (C) 2005-2012 Pentaho and others
009// All Rights Reserved.
010*/
011package mondrian.olap;
012
013import mondrian.resource.MondrianResource;
014import mondrian.spi.MemberFormatter;
015
016/**
017 * Skeleton implementation of {@link Level}.
018 *
019 * @author jhyde
020 * @since 6 August, 2001
021 */
022public abstract class LevelBase
023    extends OlapElementBase
024    implements Level
025{
026    protected final Hierarchy hierarchy;
027    protected final String name;
028    protected final String uniqueName;
029    protected final String description;
030    protected final int depth;
031    protected final LevelType levelType;
032    protected MemberFormatter memberFormatter;
033    protected int  approxRowCount;
034
035    protected LevelBase(
036        Hierarchy hierarchy,
037        String name,
038        String caption,
039        boolean visible,
040        String description,
041        int depth,
042        LevelType levelType)
043    {
044        this.hierarchy = hierarchy;
045        this.name = name;
046        this.caption = caption;
047        this.visible = visible;
048        this.description = description;
049        this.uniqueName = Util.makeFqName(hierarchy, name);
050        this.depth = depth;
051        this.levelType = levelType;
052    }
053
054    /**
055     * Sets the approximate number of members in this Level.
056     * @see #getApproxRowCount()
057     */
058    public void setApproxRowCount(int approxRowCount) {
059        this.approxRowCount = approxRowCount;
060    }
061
062    // from Element
063    public String getQualifiedName() {
064        return MondrianResource.instance().MdxLevelName.str(getUniqueName());
065    }
066
067    public LevelType getLevelType() {
068        return levelType;
069    }
070
071    public String getUniqueName() {
072        return uniqueName;
073    }
074
075    public String getName() {
076        return name;
077    }
078
079    public String getDescription() {
080        return description;
081    }
082
083    public Hierarchy getHierarchy() {
084        return hierarchy;
085    }
086
087    public Dimension getDimension() {
088        return hierarchy.getDimension();
089    }
090
091    public int getDepth() {
092        return depth;
093    }
094
095    public Level getChildLevel() {
096        int childDepth = depth + 1;
097        Level[] levels = hierarchy.getLevels();
098        return (childDepth < levels.length)
099            ? levels[childDepth]
100            : null;
101    }
102
103    public Level getParentLevel() {
104        int parentDepth = depth - 1;
105        Level[] levels = hierarchy.getLevels();
106        return (parentDepth >= 0)
107            ? levels[parentDepth]
108            : null;
109    }
110
111    public abstract boolean isAll();
112
113    public boolean isMeasure() {
114        return hierarchy.getName().equals("Measures");
115    }
116
117    public OlapElement lookupChild(
118        SchemaReader schemaReader, Id.Segment s, MatchType matchType)
119    {
120        if (areMembersUnique()
121            && s instanceof Id.NameSegment)
122        {
123            return Util.lookupHierarchyRootMember(
124                schemaReader, hierarchy, ((Id.NameSegment) s), matchType);
125        } else {
126            return null;
127        }
128    }
129
130    public MemberFormatter getMemberFormatter() {
131        return memberFormatter;
132    }
133}
134
135
136// End LevelBase.java