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