001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/HierarchyBase.java#27 $
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     * Skeleton implementation for {@link Hierarchy}.
020     *
021     * @author jhyde
022     * @since 6 August, 2001
023     * @version $Id: //open/mondrian/src/main/mondrian/olap/HierarchyBase.java#27 $
024     */
025    public abstract class HierarchyBase
026        extends OlapElementBase
027        implements Hierarchy {
028    
029        protected final Dimension dimension;
030        /**
031         * <code>name</code> and <code>subName</code> are the name of the
032         * hierarchy, respectively containing and not containing dimension
033         * name. For example:
034         * <table>
035         * <tr> <th>uniqueName</th>    <th>name</th>        <th>subName</th></tr>
036         * <tr> <td>[Time.Weekly]</td> <td>Time.Weekly</td> <td>Weekly</td></tr>
037         * <tr> <td>[Customers]</td>   <td>Customers</td>   <td>null</td></tr>
038         * </table>
039         */
040        protected final String subName;
041        protected final String name;
042        protected final String uniqueName;
043        protected String description;
044        protected Level[] levels;
045        protected final boolean hasAll;
046        protected String allMemberName;
047        protected String allLevelName;
048    
049        protected HierarchyBase(Dimension dimension,
050                                String subName,
051                                boolean hasAll) {
052            this.dimension = dimension;
053            this.hasAll = hasAll;
054            this.caption = dimension.getCaption();
055    
056            this.subName = subName;
057            String name = dimension.getName();
058            if (this.subName != null) {
059                // e.g. "Time.Weekly"
060                this.name = name + "." + subName;
061                // e.g. "[Time.Weekly]"
062                this.uniqueName = Util.makeFqName(this.name);
063            } else {
064                // e.g. "Time"
065                this.name = name;
066                // e.g. "[Time]"
067                this.uniqueName = dimension.getUniqueName();
068            }
069        }
070    
071    
072        /**
073         * Returns the name of the hierarchy sans dimension name.
074         *
075         * @return name of hierarchy sans dimension name
076         */
077        public String getSubName() {
078            return subName;
079        }
080    
081        // implement MdxElement
082        public String getUniqueName() {
083            return uniqueName;
084        }
085    
086        public String getName() {
087            return name;
088        }
089    
090        public String getQualifiedName() {
091            return MondrianResource.instance().MdxHierarchyName.str(getUniqueName());
092        }
093    
094        public abstract boolean isRagged();
095    
096        public String getDescription() {
097            return description;
098        }
099    
100        public Dimension getDimension() {
101            return dimension;
102        }
103    
104        public Level[] getLevels() {
105            return levels;
106        }
107    
108        public Hierarchy getHierarchy() {
109            return this;
110        }
111    
112        public boolean hasAll() {
113            return hasAll;
114        }
115    
116        public boolean equals(OlapElement mdxElement) {
117            // Use object identity, because a private hierarchy can have the same
118            // name as a public hierarchy.
119            return (this == mdxElement);
120        }
121    
122        public OlapElement lookupChild(SchemaReader schemaReader, Id.Segment s) {
123            return lookupChild(schemaReader, s, MatchType.EXACT);
124        }
125    
126        public OlapElement lookupChild(
127            SchemaReader schemaReader, Id.Segment s, MatchType matchType)
128        {
129            OlapElement oe = Util.lookupHierarchyLevel(this, s.name);
130            if (oe == null) {
131                oe = Util.lookupHierarchyRootMember(
132                    schemaReader, this, s, matchType);
133            }
134            if (getLogger().isDebugEnabled()) {
135                StringBuilder buf = new StringBuilder(64);
136                buf.append("HierarchyBase.lookupChild: ");
137                buf.append("name=");
138                buf.append(getName());
139                buf.append(", childname=");
140                buf.append(s);
141                if (oe == null) {
142                    buf.append(" returning null");
143                } else {
144                    buf.append(" returning elementname=" + oe.getName());
145                }
146                getLogger().debug(buf.toString());
147            }
148            return oe;
149        }
150    
151        public String getAllMemberName() {
152            return allMemberName;
153        }
154    
155        /**
156         * Returns the name of the 'all' level in this hierarchy.
157         *
158         * @return name of the 'all' level
159         */
160        public String getAllLevelName() {
161            return allLevelName;
162        }
163    }
164    
165    // End HierarchyBase.java