001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/DimensionBase.java#23 $
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, 6 August, 2001
012    */
013    
014    package mondrian.olap;
015    
016    import mondrian.resource.MondrianResource;
017    
018    /**
019     * Abstract implementation for a {@link Dimension}.
020     *
021     * @author jhyde
022     * @since 6 August, 2001
023     * @version $Id: //open/mondrian/src/main/mondrian/olap/DimensionBase.java#23 $
024     */
025    public abstract class DimensionBase
026        extends OlapElementBase
027        implements Dimension {
028    
029        protected final String name;
030        protected final String uniqueName;
031        protected final String description;
032        protected final boolean highCardinality;
033        protected Hierarchy[] hierarchies;
034        protected DimensionType dimensionType;
035    
036        protected DimensionBase(
037                String name,
038                DimensionType dimensionType,
039                final boolean highCardinality)
040        {
041            this.name = name;
042            this.uniqueName = Util.makeFqName(name);
043            this.description = null;
044            this.dimensionType = dimensionType;
045            this.highCardinality = highCardinality;
046        }
047    
048        public String getUniqueName() {
049            return uniqueName;
050        }
051    
052        public String getName() {
053            return name;
054        }
055    
056        public String getDescription() {
057            return description;
058        }
059    
060        public Hierarchy[] getHierarchies() {
061            return hierarchies;
062        }
063    
064        public Hierarchy getHierarchy() {
065            return hierarchies[0];
066        }
067    
068        public Dimension getDimension() {
069            return this;
070        }
071    
072        public DimensionType getDimensionType() {
073            return dimensionType;
074        }
075    
076        public String getQualifiedName() {
077            return MondrianResource.instance().MdxDimensionName.str(getUniqueName());
078        }
079    
080        public boolean isMeasures() {
081            return getUniqueName().equals(MEASURES_UNIQUE_NAME);
082        }
083    
084        public boolean usesDimension(Dimension dimension) {
085            return dimension == this;
086        }
087    
088        public OlapElement lookupChild(SchemaReader schemaReader, Id.Segment s)
089        {
090            return lookupChild(schemaReader, s, MatchType.EXACT);
091        }
092    
093        public OlapElement lookupChild(
094            SchemaReader schemaReader, Id.Segment s, MatchType matchType)
095        {
096            OlapElement oe = lookupHierarchy(s);
097            // If the user is looking for [Marital Status].[Marital Status] we
098            // should not return oe "Marital Status", because he is
099            // looking for level - we can check that by checking of hierarchy and
100            // dimension name is the same.
101            if ((oe == null) || oe.getName().equalsIgnoreCase(getName())) {
102                OlapElement oeLevel =
103                    getHierarchy().lookupChild(schemaReader, s, matchType);
104                if (oeLevel != null) {
105                    oe = oeLevel; // level match overrides hierarchy match
106                }
107            }
108    
109            if (getLogger().isDebugEnabled()) {
110                StringBuilder buf = new StringBuilder(64);
111                buf.append("DimensionBase.lookupChild: ");
112                buf.append("name=");
113                buf.append(getName());
114                buf.append(", childname=");
115                buf.append(s);
116                if (oe == null) {
117                    buf.append(" returning null");
118                } else {
119                    buf.append(" returning elementname=" + oe.getName());
120                }
121                getLogger().debug(buf.toString());
122            }
123    
124            return oe;
125        }
126    
127        public boolean isHighCardinality() {
128            return this.highCardinality;
129        }
130    
131        private Hierarchy lookupHierarchy(Id.Segment s) {
132            for (Hierarchy hierarchy : hierarchies) {
133                if (hierarchy.getName().equalsIgnoreCase(s.name)) {
134                    return hierarchy;
135                }
136            }
137            return null;
138        }
139    }
140    
141    
142    // End DimensionBase.java