001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/CubeBase.java#28 $
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-2007 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     * <code>CubeBase</code> is an abstract implementation of {@link Cube}.
020     *
021     * @author jhyde
022     * @since 6 August, 2001
023     * @version $Id: //open/mondrian/src/main/mondrian/olap/CubeBase.java#28 $
024     */
025    public abstract class CubeBase extends OlapElementBase implements Cube {
026    
027        /** constraints indexes for adSchemaMembers
028         *
029         * http://msdn.microsoft.com/library/psdk/dasdk/mdx8h4k.htm
030         * check "Restrictions in the MEMBER Rowset" under MEMBER Rowset section
031         */
032        public static final int CATALOG_NAME = 0;
033        public static final int SCHEMA_NAME = 1;
034        public static final int CUBE_NAME = 2;
035        public static final int DIMENSION_UNIQUE_NAME = 3;
036        public static final int HIERARCHY_UNIQUE_NAME = 4;
037        public static final int LEVEL_UNIQUE_NAME = 5;
038        public static final int LEVEL_NUMBER = 6;
039        public static final int MEMBER_NAME = 7;
040        public static final int MEMBER_UNIQUE_NAME = 8;
041        public static final int MEMBER_CAPTION = 9;
042        public static final int MEMBER_TYPE = 10;
043        public static final int Tree_Operator = 11;
044        public static final int maxNofConstraintsForAdSchemaMember = 12;
045        public static final int MDTREEOP_SELF = 0;
046        public static final int MDTREEOP_CHILDREN = 1;
047        public static final int MDPROP_USERDEFINED0 = 19;
048    
049        protected final String name;
050        protected Dimension[] dimensions;
051    
052        protected CubeBase(String name, Dimension[] dimensions) {
053            this.name = name;
054            this.dimensions = dimensions;
055        }
056    
057        // implement OlapElement
058        public String getName() {
059            return name;
060        }
061    
062        public String getUniqueName() {
063            // return e.g. '[Sales Ragged]'
064            return Util.quoteMdxIdentifier(name);
065        }
066    
067        public String getQualifiedName() {
068            return MondrianResource.instance().MdxCubeName.str(getName());
069        }
070    
071        public Dimension getDimension() {
072            return null;
073        }
074    
075        public Hierarchy getHierarchy() {
076            return null;
077        }
078    
079        public String getDescription() {
080            return null;
081        }
082    
083        public Dimension[] getDimensions() {
084            return dimensions;
085        }
086    
087        public Hierarchy lookupHierarchy(Id.Segment s, boolean unique) {
088            for (Dimension dimension : dimensions) {
089                Hierarchy[] hierarchies = dimension.getHierarchies();
090                for (Hierarchy hierarchy : hierarchies) {
091                    String name = unique
092                        ? hierarchy.getUniqueName() : hierarchy.getName();
093                    if (name.equals(s.name)) {
094                        return hierarchy;
095                    }
096                }
097            }
098            return null;
099        }
100    
101        public OlapElement lookupChild(SchemaReader schemaReader, Id.Segment s)
102        {
103            return lookupChild(schemaReader, s, MatchType.EXACT);
104        }
105    
106        public OlapElement lookupChild(
107            SchemaReader schemaReader, Id.Segment s, MatchType matchType)
108        {
109            Dimension mdxDimension = (Dimension)lookupDimension(s);
110            if (mdxDimension != null ||
111                MondrianProperties.instance().NeedDimensionPrefix.get()) {
112                return mdxDimension;
113            }
114    
115            //maybe this is not a dimension - maybe it's hierarchy, level or name
116            for (Dimension dimension : dimensions) {
117                OlapElement mdxElement = dimension.lookupChild(
118                    schemaReader, s, matchType);
119                if (mdxElement != null) {
120                    return mdxElement;
121                }
122            }
123            return null;
124        }
125    
126        public Dimension getTimeDimension() {
127            for (Dimension dimension : dimensions) {
128                if (dimension.getDimensionType() ==
129                    DimensionType.TimeDimension) {
130                    return dimension;
131                }
132            }
133    
134            return null;
135        }
136    
137        public OlapElement lookupDimension(Id.Segment s) {
138            for (Dimension dimension : dimensions) {
139                if (dimension.getName().equalsIgnoreCase(s.name)) {
140                    return dimension;
141                }
142            }
143            return null;
144        }
145    
146        // ------------------------------------------------------------------------
147    
148        private Level getTimeLevel(LevelType levelType) {
149            for (Dimension dimension : dimensions) {
150                if (dimension.getDimensionType() == DimensionType.TimeDimension) {
151                    Hierarchy[] hierarchies = dimension.getHierarchies();
152                    for (Hierarchy hierarchy : hierarchies) {
153                        Level[] levels = hierarchy.getLevels();
154                        for (Level level : levels) {
155                            if (level.getLevelType() == levelType) {
156                                return level;
157                            }
158                        }
159                    }
160                }
161            }
162            return null;
163        }
164    
165        public Level getYearLevel() {
166            return getTimeLevel(LevelType.TimeYears);
167        }
168    
169        public Level getQuarterLevel() {
170            return getTimeLevel(LevelType.TimeQuarters);
171        }
172    
173        public Level getMonthLevel() {
174            return getTimeLevel(LevelType.TimeMonths);
175        }
176    
177        public Level getWeekLevel() {
178            return getTimeLevel(LevelType.TimeWeeks);
179        }
180    
181    }
182    
183    
184    // End CubeBase.java