KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > rolap > aggmatcher > DefaultRecognizer


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/rolap/aggmatcher/DefaultRecognizer.java#13 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2005-2007 Julian Hyde and others
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10
11 package mondrian.rolap.aggmatcher;
12
13 import mondrian.olap.Hierarchy;
14 import mondrian.resource.MondrianResource;
15 import mondrian.recorder.MessageRecorder;
16 import mondrian.rolap.RolapStar;
17 import mondrian.rolap.RolapLevel;
18 import mondrian.rolap.HierarchyUsage;
19
20 import java.util.Iterator JavaDoc;
21
22 /**
23  * This is the default Recognizer. It uses the rules found in the file
24  * DefaultRules.xml to find aggregate tables and there columns.
25  *
26  * @author Richard M. Emberson
27  * @version $Id: //open/mondrian/src/main/mondrian/rolap/aggmatcher/DefaultRecognizer.java#13 $
28  */

29 class DefaultRecognizer extends Recognizer {
30
31     private static final MondrianResource mres = MondrianResource.instance();
32
33     private final DefaultRules aggDefault;
34
35     DefaultRecognizer(final DefaultRules aggDefault,
36                       final RolapStar star,
37                       final JdbcSchema.Table dbFactTable,
38                       final JdbcSchema.Table aggTable,
39                       final MessageRecorder msgRecorder) {
40         super(star, dbFactTable, aggTable, msgRecorder);
41         this.aggDefault = aggDefault;
42     }
43
44     /**
45      * Get the DefaultRules instance associated with this object.
46      */

47     DefaultRules getRules() {
48         return aggDefault;
49     }
50
51     /**
52      * Get the Matcher to be used to match columns to be ignored.
53      */

54     protected Recognizer.Matcher getIgnoreMatcher() {
55         return getRules().getIgnoreMatcher();
56     }
57
58     /**
59      * Get the Matcher to be used to match the column which is the fact count
60      * column.
61      */

62     protected Recognizer.Matcher getFactCountMatcher() {
63         return getRules().getFactCountMatcher();
64     }
65
66     /**
67      * Get the Match used to identify columns that are measures.
68      */

69     protected Recognizer.Matcher getMeasureMatcher(
70             JdbcSchema.Table.Column.Usage factUsage) {
71
72         String JavaDoc measureName = factUsage.getSymbolicName();
73         String JavaDoc measureColumnName = factUsage.getColumn().getName();
74         String JavaDoc aggregateName = factUsage.getAggregator().getName();
75
76         return getRules().getMeasureMatcher(
77             measureName,
78             measureColumnName,
79             aggregateName);
80     }
81
82     /**
83      * Create measures for an aggregate table.
84      * <p>
85      * First, iterator through all fact table measure usages.
86      * Create a Matcher for each such usage.
87      * Iterate through all aggregate table columns.
88      * For each column that matches create a measure usage.
89      * <p>
90      * Per fact table measure usage, at most only one aggregate measure should
91      * be created.
92      *
93      * @return number of measures created.
94      */

95     protected int checkMeasures() {
96         msgRecorder.pushContextName("DefaultRecognizer.checkMeasures");
97
98         try {
99             int measureCountCount = 0;
100
101             for (Iterator JavaDoc<JdbcSchema.Table.Column.Usage> it =
102                     dbFactTable.getColumnUsages(JdbcSchema.UsageType.MEASURE);
103                     it.hasNext(); ) {
104                 JdbcSchema.Table.Column.Usage factUsage = it.next();
105
106                 Matcher matcher = getMeasureMatcher(factUsage);
107
108                 int matchCount = 0;
109                 for (JdbcSchema.Table.Column aggColumn : aggTable.getColumns()) {
110                     // if marked as ignore, then do not consider
111
if (aggColumn.hasUsage(JdbcSchema.UsageType.IGNORE)) {
112                         continue;
113                     }
114
115                     if (matcher.matches(aggColumn.getName())) {
116                         makeMeasure(factUsage, aggColumn);
117
118                         measureCountCount++;
119                         matchCount++;
120                     }
121                 }
122
123                 if (matchCount > 1) {
124                     String JavaDoc msg = mres.AggMultipleMatchingMeasure.str(
125                         msgRecorder.getContext(),
126                         aggTable.getName(),
127                         dbFactTable.getName(),
128                         matchCount,
129                         factUsage.getSymbolicName(),
130                         factUsage.getColumn().getName(),
131                         factUsage.getAggregator().getName());
132                     msgRecorder.reportError(msg);
133
134                     returnValue = false;
135                 }
136             }
137             return measureCountCount;
138
139         } finally {
140             msgRecorder.popContextName();
141         }
142     }
143
144     /**
145      * This creates a foreign key usage.
146      *
147      * <p>Using the foreign key Matcher with the fact usage's column name the
148      * aggregate table's columns are searched for one that matches. For each
149      * that matches a foreign key usage is created (thought if more than one is
150      * created its is an error which is handled in the calling code.
151      */

152     protected int matchForeignKey(JdbcSchema.Table.Column.Usage factUsage) {
153         JdbcSchema.Table.Column factColumn = factUsage.getColumn();
154
155         // search to see if any of the aggTable's columns match
156
Recognizer.Matcher matcher =
157             getRules().getForeignKeyMatcher(factColumn.getName());
158
159         int matchCount = 0;
160         for (JdbcSchema.Table.Column aggColumn : aggTable.getColumns()) {
161             // if marked as ignore, then do not consider
162
if (aggColumn.hasUsage(JdbcSchema.UsageType.IGNORE)) {
163                 continue;
164             }
165
166             if (matcher.matches(aggColumn.getName())) {
167                 makeForeignKey(factUsage, aggColumn, null);
168                 matchCount++;
169             }
170         }
171         return matchCount;
172     }
173
174     /**
175      * Create level usages.
176      *
177      * <p> A Matcher is created using the Hierarchy's name, the RolapLevel
178      * name, and the column name associated with the RolapLevel's key
179      * expression. The aggregate table columns are search for the first match
180      * and, if found, a level usage is created for that column and true is
181      * returned.
182      */

183     protected boolean matchLevel(
184             final Hierarchy hierarchy,
185             final HierarchyUsage hierarchyUsage,
186             final RolapLevel level) {
187
188         msgRecorder.pushContextName("DefaultRecognizer.matchLevel");
189         try {
190
191             String JavaDoc usagePrefix = hierarchyUsage.getUsagePrefix();
192             String JavaDoc hierName = hierarchy.getName();
193             String JavaDoc levelName = level.getName();
194             String JavaDoc levelColumnName = getColumnName(level.getKeyExp());
195
196             Recognizer.Matcher matcher = getRules().getLevelMatcher(
197                 usagePrefix, hierName, levelName, levelColumnName);
198
199             for (JdbcSchema.Table.Column aggColumn : aggTable.getColumns()) {
200                 if (matcher.matches(aggColumn.getName())) {
201                     makeLevel(
202                         aggColumn,
203                         hierarchy,
204                         hierarchyUsage,
205                         getColumnName(level.getKeyExp()),
206                         getColumnName(level.getKeyExp()),
207                         level.getName());
208                     return true;
209                 }
210             }
211             return false;
212
213         } finally {
214             msgRecorder.popContextName();
215         }
216     }
217 }
218
219 // End DefaultRecognizer.java
220
Popular Tags