explicitrules.java

来自「数据仓库展示程序」· Java 代码 · 共 1,597 行 · 第 1/4 页

JAVA
1,597
字号
/*
// $Id: //open/mondrian/src/main/mondrian/rolap/aggmatcher/ExplicitRules.java#6 $
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2001-2005 Kana Software, Inc. and others.
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
//
// jhyde, 12 August, 2001
*/

package mondrian.rolap.aggmatcher;

import mondrian.olap.*;
import mondrian.rolap.*;
import mondrian.recorder.MessageRecorder;
import mondrian.resource.MondrianResource;

import org.apache.log4j.Logger;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;
import java.util.regex.Pattern;

/**
 * A class containing a RolapCube's Aggregate tables exclude/include
 * criteria.
 *
 * @author <a>Richard M. Emberson</a>
 * @version
 */
public class ExplicitRules {
    private static final Logger LOGGER = Logger.getLogger(ExplicitRules.class);

    private static final MondrianResource mres = MondrianResource.instance();

    /**
     * Is the given tableName explicitly excluded from consideration as a
     * candidate aggregate table? return true if it is to be excluded and false
     * otherwise.
     *
     * @param tableName
     * @param aggGroups
     * @return
     */
    public static boolean excludeTable(final String tableName,
                                       final List aggGroups) {
        for (Iterator it = aggGroups.iterator(); it.hasNext(); ) {
            ExplicitRules.Group group = (ExplicitRules.Group) it.next();
            if (group.excludeTable(tableName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Return the ExplicitRules.TableDef for a tableName that is a candidate
     * aggregate table. If null is returned, then the default rules are used
     * otherwise if not null, then the ExplicitRules.TableDef is used.
     *
     * @param tableName
     * @param aggGroups
     * @return
     */
    public static ExplicitRules.TableDef getIncludeByTableDef(
                                                    final String tableName,
                                                    final List aggGroups) {
        for (Iterator it = aggGroups.iterator(); it.hasNext(); ) {
            ExplicitRules.Group group = (ExplicitRules.Group) it.next();
            ExplicitRules.TableDef tableDef = group.getIncludeByTableDef(tableName);
            if (tableDef != null) {
                return tableDef;
            }
        }
        return null;
    }

    /**
     * This class forms a collection of aggregate table explicit rules for a
     * given cube.
     *
     */
    public static class Group {

        /**
         * Make an ExplicitRules.Group for a given RolapCube given the
         * MondrianDef.Cube associated with that cube.
         *
         * @param cube
         * @param xmlCube
         * @return
         */
        public static ExplicitRules.Group make(final RolapCube cube,
                                             final MondrianDef.Cube xmlCube) {
            Group group = new Group(cube);

            MondrianDef.Relation relation = xmlCube.fact;

            if (relation instanceof MondrianDef.Table) {
                MondrianDef.AggExclude[] aggExcludes =
                    ((MondrianDef.Table) relation).getAggExcludes();
                if (aggExcludes != null) {
                    for (int i = 0; i < aggExcludes.length; i++) {
                        ExplicitRules.Exclude exclude =
                            ExplicitRules.make(aggExcludes[i]);
                        group.addExclude(exclude);
                    }
                }
                MondrianDef.AggTable[] aggTables =
                    ((MondrianDef.Table) relation).getAggTables();
                if (aggTables != null) {
                    for (int i = 0; i < aggTables.length; i++) {
                        ExplicitRules.TableDef tableDef =
                            ExplicitRules.TableDef.make(aggTables[i], group);
                        group.addTableDef(tableDef);
                    }
                }
            } else {
                String msg = mres.CubeRelationNotTable.str(
                        cube.getName(),
                        relation.getClass().getName());
                LOGGER.warn(msg);
            }

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(Util.nl+group);
            }
            return group;
        }

        private final RolapCube cube;
        private List tableDefs;
        private List excludes;

        public Group(final RolapCube cube) {
            this.cube = cube;
            this.excludes = Collections.EMPTY_LIST;
            this.tableDefs = Collections.EMPTY_LIST;
        }

        /**
         * Get the RolapCube associated with this Group.
         *
         * @return
         */
        public RolapCube getCube() {
            return cube;
        }
        /**
         * Get the RolapStar associated with this Group's RolapCube.
         *
         * @return
         */
        public RolapStar getStar() {
            return getCube().getStar();
        }

        /**
         * Get the name of this Group (its the name of its RolapCube).
         *
         * @return
         */
        public String getName() {
            return getCube().getName();
        }

        /**
         * Are there any rules associated with this Group.
         *
         * @return
         */
        public boolean hasRules() {
            return (excludes != Collections.EMPTY_LIST) ||
                (tableDefs != Collections.EMPTY_LIST);
        }

        /**
         * Add an exclude rule.
         *
         * @param exclude
         */
        public void addExclude(final ExplicitRules.Exclude exclude) {
            if (excludes == Collections.EMPTY_LIST) {
                excludes = new ArrayList();
            }
            excludes.add(exclude);
        }

        /**
         * Add a name or pattern (table) rule.
         *
         * @param tableDef
         */
        public void addTableDef(final ExplicitRules.TableDef tableDef) {
            if (tableDefs == Collections.EMPTY_LIST) {
                tableDefs = new ArrayList();
            }
            tableDefs.add(tableDef);
        }

        /**
         * Is the given tableName excluded.
         *
         * @param tableName
         * @return
         */
        public boolean excludeTable(final String tableName) {
            // See if the table is explicitly, by name, excluded
            for (Iterator it = excludes.iterator(); it.hasNext(); ) {
                ExplicitRules.Exclude exclude = (ExplicitRules.Exclude) it.next();
                if (exclude.isExcluded(tableName)) {
                    return true;
                }
            }
            return false;
        }

        /**
         * Is the given tableName included either by exact name or by pattern.
         *
         * @param tableName
         * @return
         */
        public ExplicitRules.TableDef getIncludeByTableDef(final String tableName) {
            // An exact match on a NameTableDef takes precedences over a
            // fuzzy match on a PatternTableDef, so
            // first look throught NameTableDef then PatternTableDef
            for (Iterator it = tableDefs.iterator(); it.hasNext(); ) {
                ExplicitRules.TableDef tableDef = (ExplicitRules.TableDef) it.next();
                if (tableDef instanceof NameTableDef) {
                    if (tableDef.matches(tableName)) {
                        return tableDef;
                    }
                }
            }
            for (Iterator it = tableDefs.iterator(); it.hasNext(); ) {
                ExplicitRules.TableDef tableDef = (ExplicitRules.TableDef) it.next();
                if (tableDef instanceof PatternTableDef) {
                    if (tableDef.matches(tableName)) {
                        return tableDef;
                    }
                }
            }
            return null;
        }

        /**
         * Get the database table name associated with this Group's RolapStar's
         * fact table.
         *
         * @return
         */
        public String getTableName() {
            RolapStar.Table table = getStar().getFactTable();
            MondrianDef.Relation relation = table.getRelation();
            return relation.getAlias();
        }

        /**
         * Get the database schema name associated with this Group's RolapStar's
         * fact table.
         *
         * @return
         */
        public String getSchemaName() {
            String schema = null;

            RolapStar.Table table = getStar().getFactTable();
            MondrianDef.Relation relation = table.getRelation();

            if (relation instanceof MondrianDef.Table) {
                MondrianDef.Table mtable = (MondrianDef.Table) relation;
                schema = mtable.schema;
            }
            return schema;
        }
        /**
         * Get the database catalog name associated with this Group's
         * RolapStar's fact table.
         * Note: this currently this always returns null.
         *
         * @return
         */
        public String getCatalogName() {
            return null;
        }

        /**
         * Validate the content and structure of this Group.
         *
         * @param msgRecorder
         */
        public void validate(final MessageRecorder msgRecorder) {
            msgRecorder.pushContextName(getName());
            try {
                for (Iterator it = this.tableDefs.iterator(); it.hasNext(); ) {
                    ExplicitRules.TableDef tableDef =
                        (ExplicitRules.TableDef) it.next();
                    tableDef.validate(msgRecorder);
                }
            } finally {
                msgRecorder.popContextName();
            }
        }

        public String toString() {
            StringWriter sw = new StringWriter(256);
            PrintWriter pw = new PrintWriter(sw);
            print(pw, "");
            pw.flush();
            return sw.toString();
        }

        public void print(final PrintWriter pw, final String prefix) {
            pw.print(prefix);
            pw.println("ExplicitRules.Group:");
            String subprefix = prefix + "  ";
            String subsubprefix = subprefix + "  ";

            pw.print(subprefix);
            pw.print("name=");
            pw.println(getStar().getFactTable().getRelation());

            pw.print(subprefix);
            pw.println("TableDefs: [");
            Iterator it = this.tableDefs.iterator();
            while (it.hasNext()) {
                ExplicitRules.TableDef tableDef =
                    (ExplicitRules.TableDef) it.next();
                tableDef.print(pw, subsubprefix);
            }
            pw.print(subprefix);
            pw.println("]");

        }

        public class Table {
            private final String tableName;
            private final ExplicitRules.TableDef tableDef;

            private Table(final String tableName,
                          final ExplicitRules.TableDef tableDef) {
                this.tableName = tableName;
                this.tableDef = tableDef;
            }

            /**
             * Get the database name of the table.
             *
             * @return
             */
            public String getTableName() {
                return this.tableName;
            }

            /**
             * Get the TableDef instance.
             *
             * @return
             */
            public ExplicitRules.TableDef getTableDef() {
                return this.tableDef;
            }

            public String toString() {
                StringWriter sw = new StringWriter(256);
                PrintWriter pw = new PrintWriter(sw);
                print(pw, "");
                pw.flush();
                return sw.toString();
            }
            public void validate(final MessageRecorder msgRecorder) {
                msgRecorder.pushContextName(tableName);
                try {
                } finally {
                    msgRecorder.popContextName();
                }
            }
            public void print(final PrintWriter pw, final String prefix) {
                pw.print(prefix);
                pw.println("ExplicitRules.Table:");
                String subprefix = prefix + "  ";

                pw.print(subprefix);
                pw.print("TableName=");
                pw.println(this.tableName);

                pw.print(subprefix);
                pw.print("TableDef.id=");
                pw.println(this.tableDef.getId());
            }
        }

    }
    private static Exclude make(final MondrianDef.AggExclude aggExclude) {
        return (aggExclude.getNameAttribute() != null)
                ? new ExcludeName(aggExclude.getNameAttribute(),

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?