001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap4j/MondrianOlap4jLevel.java#2 $
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) 2007-2008 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009    */
010    package mondrian.olap4j;
011    
012    import org.olap4j.metadata.*;
013    import org.olap4j.impl.ArrayNamedListImpl;
014    import org.olap4j.impl.Named;
015    
016    import java.util.*;
017    
018    /**
019     * Implementation of {@link Level}
020     * for the Mondrian OLAP engine.
021     *
022     * @author jhyde
023     * @version $Id: //open/mondrian/src/main/mondrian/olap4j/MondrianOlap4jLevel.java#2 $
024     * @since May 25, 2007
025     */
026    class MondrianOlap4jLevel implements Level, Named {
027        private final MondrianOlap4jSchema olap4jSchema;
028        private final mondrian.olap.Level level;
029    
030        MondrianOlap4jLevel(
031            MondrianOlap4jSchema olap4jSchema,
032            mondrian.olap.Level level)
033        {
034            this.olap4jSchema = olap4jSchema;
035            this.level = level;
036        }
037    
038        public boolean equals(Object obj) {
039            return obj instanceof MondrianOlap4jLevel &&
040                level.equals(((MondrianOlap4jLevel) obj).level);
041        }
042    
043        public int hashCode() {
044            return level.hashCode();
045        }
046    
047        public int getDepth() {
048            return level.getDepth();
049        }
050    
051        public Hierarchy getHierarchy() {
052            return new MondrianOlap4jHierarchy(olap4jSchema, level.getHierarchy());
053        }
054    
055        public Dimension getDimension() {
056            return new MondrianOlap4jDimension(olap4jSchema, level.getDimension());
057        }
058    
059        public Type getLevelType() {
060            throw new UnsupportedOperationException();
061        }
062    
063        public NamedList<Property> getProperties() {
064            final NamedList<Property> list = new ArrayNamedListImpl<Property>() {
065                protected String getName(Property property) {
066                    return property.getName();
067                }
068            };
069            // standard properties first
070            list.addAll(
071                Arrays.asList(Property.StandardMemberProperty.values()));
072            // then level-specific properties
073            for (mondrian.olap.Property property : level.getProperties()) {
074                list.add(new MondrianOlap4jProperty(property));
075            }
076            return list;
077        }
078    
079        public List<Member> getMembers() {
080            final MondrianOlap4jConnection olap4jConnection =
081                olap4jSchema.olap4jCatalog.olap4jDatabaseMetaData.olap4jConnection;
082            final mondrian.olap.SchemaReader schemaReader =
083                olap4jConnection.connection.getSchemaReader();
084            final List<mondrian.olap.Member> levelMembers =
085                schemaReader.getLevelMembers(level, true);
086            return new AbstractList<Member>() {
087                public Member get(int index) {
088                    return olap4jConnection.toOlap4j(levelMembers.get(index));
089                }
090    
091                public int size() {
092                    return levelMembers.size();
093                }
094            };
095        }
096    
097        public String getName() {
098            return level.getName();
099        }
100    
101        public String getUniqueName() {
102            return level.getUniqueName();
103        }
104    
105        public String getCaption(Locale locale) {
106            // todo: localized captions
107            return level.getCaption();
108        }
109    
110        public String getDescription(Locale locale) {
111            // todo: localize
112            return level.getDescription();
113        }
114    
115        public int getCardinality() {
116            return level.getApproxRowCount();
117        }
118    }
119    
120    // End MondrianOlap4jLevel.java