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;
014
015import java.util.List;
016
017/**
018 * Abstract implementation for a {@link Dimension}.
019 *
020 * @author jhyde
021 * @since 6 August, 2001
022 */
023public abstract class DimensionBase
024    extends OlapElementBase
025    implements Dimension
026{
027    protected final String name;
028    protected final String uniqueName;
029    protected final String description;
030    protected final boolean highCardinality;
031    protected Hierarchy[] hierarchies;
032    protected DimensionType dimensionType;
033
034    /**
035     * Creates a DimensionBase.
036     *
037     * @param name Name
038     * @param dimensionType Type
039     * @param highCardinality Whether high-cardinality
040     */
041    protected DimensionBase(
042        String name,
043        String caption,
044        boolean visible,
045        String description,
046        DimensionType dimensionType,
047        boolean highCardinality)
048    {
049        this.name = name;
050        this.caption = caption;
051        this.visible = visible;
052        this.uniqueName = Util.makeFqName(name);
053        this.description = description;
054        this.dimensionType = dimensionType;
055        this.highCardinality = highCardinality;
056    }
057
058    public String getUniqueName() {
059        return uniqueName;
060    }
061
062    public String getName() {
063        return name;
064    }
065
066    public String getDescription() {
067        return description;
068    }
069
070    public Hierarchy[] getHierarchies() {
071        return hierarchies;
072    }
073
074    public Hierarchy getHierarchy() {
075        return hierarchies[0];
076    }
077
078    public Dimension getDimension() {
079        return this;
080    }
081
082    public DimensionType getDimensionType() {
083        return dimensionType;
084    }
085
086    public String getQualifiedName() {
087        return MondrianResource.instance().MdxDimensionName.str(
088            getUniqueName());
089    }
090
091    public boolean isMeasures() {
092        return getUniqueName().equals(MEASURES_UNIQUE_NAME);
093    }
094
095    public OlapElement lookupChild(
096        SchemaReader schemaReader, Id.Segment s, MatchType matchType)
097    {
098        OlapElement oe = null;
099        if (s instanceof Id.NameSegment) {
100            oe = lookupHierarchy((Id.NameSegment) s);
101        }
102
103        // Original mondrian behavior:
104        // If the user is looking for [Marital Status].[Marital Status] we
105        // should not return oe "Marital Status", because he is
106        // looking for level - we can check that by checking of hierarchy and
107        // dimension name is the same.
108        //
109        if (!MondrianProperties.instance().SsasCompatibleNaming.get()) {
110            if (oe == null || oe.getName().equalsIgnoreCase(getName())) {
111                OlapElement oeLevel =
112                    getHierarchy().lookupChild(schemaReader, s, matchType);
113                if (oeLevel != null) {
114                    return oeLevel; // level match overrides hierarchy match
115                }
116            }
117            return oe;
118        } else {
119            // New (SSAS-compatible) behavior. If there is no matching
120            // hierarchy, find the first level with the given name.
121            if (oe != null) {
122                return oe;
123            }
124            final List<Hierarchy> hierarchyList =
125                schemaReader.getDimensionHierarchies(this);
126            for (Hierarchy hierarchy : hierarchyList) {
127                oe = hierarchy.lookupChild(schemaReader, s, matchType);
128                if (oe != null) {
129                    return oe;
130                }
131            }
132            return null;
133        }
134    }
135
136    public boolean isHighCardinality() {
137        return this.highCardinality;
138    }
139
140    private Hierarchy lookupHierarchy(Id.NameSegment s) {
141        for (Hierarchy hierarchy : hierarchies) {
142            if (Util.equalName(hierarchy.getName(), s.getName())) {
143                return hierarchy;
144            }
145        }
146        return null;
147    }
148}
149
150// End DimensionBase.java