001    /*
002    // $Id: //open/mondrian-release/3.2/src/main/mondrian/rolap/RolapCalculatedMember.java#2 $
003    // This software is subject to the terms of the Eclipse Public License v1.0
004    // Agreement, available at the following URL:
005    // http://www.eclipse.org/legal/epl-v10.html.
006    // Copyright (C) 2001-2002 Kana Software, Inc.
007    // Copyright (C) 2001-2010 Julian Hyde and others
008    // All Rights Reserved.
009    // You must accept the terms of that agreement to use this software.
010    //
011    // jhyde, 26 August, 2001
012    */
013    
014    package mondrian.rolap;
015    
016    import mondrian.olap.*;
017    
018    import java.util.Map;
019    import java.util.Collections;
020    
021    /**
022     * A <code>RolapCalculatedMember</code> is a member based upon a
023     * {@link Formula}.
024     *
025     * <p>It is created before the formula has been resolved; the formula is
026     * responsible for setting the "format_string" property.
027     *
028     * @author jhyde
029     * @since 26 August, 2001
030     * @version $Id: //open/mondrian-release/3.2/src/main/mondrian/rolap/RolapCalculatedMember.java#2 $
031     */
032    public class RolapCalculatedMember extends RolapMemberBase {
033        private final Formula formula;
034        private Map<String, Annotation> annotationMap;
035    
036        /**
037         * Creates a RolapCalculatedMember.
038         *
039         * @param parentMember Parent member
040         * @param level Level
041         * @param name Name
042         * @param formula Formula
043         */
044        RolapCalculatedMember(
045            RolapMember parentMember,
046            RolapLevel level,
047            String name,
048            Formula formula)
049        {
050            // A calculated measure has MemberType.FORMULA because FORMULA
051            // overrides MEASURE.
052            super(parentMember, level, name, null, MemberType.FORMULA);
053            this.formula = formula;
054            this.annotationMap = Collections.emptyMap();
055        }
056    
057        // override RolapMember
058        public int getSolveOrder() {
059            final Number solveOrder = formula.getSolveOrder();
060            return solveOrder == null ? 0 : solveOrder.intValue();
061        }
062    
063        public Object getPropertyValue(String propertyName, boolean matchCase) {
064            if (Util.equal(propertyName, Property.FORMULA.name, matchCase)) {
065                return formula;
066            } else if (Util.equal(
067                propertyName, Property.CHILDREN_CARDINALITY.name, matchCase))
068            {
069                // Looking up children is unnecessary for calculated member.
070                // If do that, SQLException will be thrown.
071                return 0;
072            } else {
073                return super.getPropertyValue(propertyName, matchCase);
074            }
075        }
076    
077        protected boolean computeCalculated(final MemberType memberType) {
078            return true;
079        }
080    
081        public boolean isCalculatedInQuery() {
082            final String memberScope =
083                (String) getPropertyValue(Property.MEMBER_SCOPE.name);
084            return memberScope == null
085                || memberScope.equals("QUERY");
086        }
087    
088        public Exp getExpression() {
089            return formula.getExpression();
090        }
091    
092        public Formula getFormula() {
093            return formula;
094        }
095    
096        @Override
097        public Map<String, Annotation> getAnnotationMap() {
098            return annotationMap;
099        }
100    
101        void setAnnotationMap(Map<String, Annotation> annotationMap) {
102            assert annotationMap != null;
103            this.annotationMap = annotationMap;
104        }
105    }
106    
107    // End RolapCalculatedMember.java