KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > gui > SchemaTreeCellRenderer


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

12 package mondrian.gui;
13
14 import javax.swing.tree.TreePath JavaDoc;
15
16 import javax.swing.*;
17 import java.awt.*;
18 import java.lang.reflect.Field JavaDoc;
19 import java.util.ResourceBundle JavaDoc;
20
21 import org.eigenbase.xom.ElementDef;
22
23 /**
24  * Render an entry for the tree.
25  *
26  * @author sean
27  * @version $Id: //open/mondrian/src/main/mondrian/gui/SchemaTreeCellRenderer.java#13 $
28  */

29 public class SchemaTreeCellRenderer extends javax.swing.tree.DefaultTreeCellRenderer JavaDoc {
30
31     private final ClassLoader JavaDoc myClassLoader;
32     public boolean invalidFlag;
33     private JDBCMetaData jdbcMetaData;
34
35     /** Creates a new instance of SchemaTreeCellRenderer */
36
37     public SchemaTreeCellRenderer(JDBCMetaData jdbcMetaData ){
38         this();
39         this.jdbcMetaData = jdbcMetaData;
40     }
41     public SchemaTreeCellRenderer() {
42         super();
43         myClassLoader = this.getClass().getClassLoader();
44
45     }
46
47     public Component getTreeCellRendererComponent(JTree tree, Object JavaDoc value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
48         super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
49         ResourceBundle JavaDoc resources = ResourceBundle.getBundle("mondrian.gui.resources.gui");
50
51         invalidFlag = isInvalid(tree, value, row);
52
53         this.setPreferredSize(null); // This allows the layout mgr to calculate the pref size of renderer.
54
if (value instanceof MondrianGuiDef.Cube) {
55             setText(invalidFlag, ((MondrianGuiDef.Cube) value).name);
56             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("cube"))));
57         } else if (value instanceof MondrianGuiDef.Column) {
58             setText(invalidFlag, ((MondrianGuiDef.Column) value).name);
59         } else if (value instanceof MondrianGuiDef.Dimension) {
60             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("dimension"))));
61             setText(invalidFlag, ((MondrianGuiDef.CubeDimension) value).name);
62             /* Do not remove this line.
63              * This sets the preferred width of tree cell displaying dimension name.
64              * This resolves the ambiguous problem of last char or last word truncated from dimension name in the tree cell.
65              * This problem was there with only Dimension objects, while all other objects display their names
66              * without any truncation of characters. Therefore, we have to force the setting of preferred width to desired width
67              * so that characters do not truncate from dimension name.
68              * Along with this the preferred size of other objects should be set to null, so that the layout mgr can calculate the
69              * preferred width in case of other objects.
70              */

71             this.setPreferredSize(new java.awt.Dimension JavaDoc(this.getPreferredSize().width+1, 25)); //Do not remove this
72
} else if (value instanceof MondrianGuiDef.DimensionUsage) {
73             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("dimensionUsage"))));
74             setText(invalidFlag, ((MondrianGuiDef.CubeDimension) value).name);
75         } else if (value instanceof MondrianGuiDef.KeyExpression) {
76             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("key"))));
77             setText("Key Expression");
78         } else if (value instanceof MondrianGuiDef.NameExpression) {
79             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("name"))));
80             setText("Name Expression");
81         } else if (value instanceof MondrianGuiDef.OrdinalExpression) {
82             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("ordinal"))));
83             setText("Ordinal Expression");
84         } else if (value instanceof MondrianGuiDef.ParentExpression) {
85             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("parent"))));
86             setText("Parent Expression");
87         } else if (value instanceof MondrianGuiDef.Expression) {
88             setText("Expression");
89         } else if (value instanceof MondrianGuiDef.ExpressionView) {
90             setText("ExpressionView");
91         } else if (value instanceof MondrianGuiDef.Hierarchy) {
92             setText(invalidFlag, "Hierarchy");
93             //setText(((MondrianGuiDef.Hierarchy) value).name); // hierarchies do not have names
94
super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("hierarchy"))));
95             this.setPreferredSize(new java.awt.Dimension JavaDoc(this.getPreferredSize().width+1, 25)); //Do not remove this
96

97 // } else if (value instanceof MondrianGuiDef.Relation) {
98
} else if ((value instanceof MondrianGuiDef.Relation) ||
99                 (value instanceof MondrianGuiDef.Table)) {
100             TreePath JavaDoc tpath = tree.getPathForRow(row);
101             String JavaDoc prefix = "";
102             if (tpath != null) {
103                 TreePath JavaDoc parentpath = tpath.getParentPath();
104                 if (parentpath != null) {
105                     Object JavaDoc parent = parentpath.getLastPathComponent();
106                     if (parent instanceof MondrianGuiDef.Join) {
107                         int indexOfChild = tree.getModel().getIndexOfChild(parent, value);
108                         switch (indexOfChild) {
109                             case 0: prefix="Left : "; break;
110                             case 1: prefix="Right : "; break;
111                         }
112                     }
113                 }
114             }
115             if (value instanceof MondrianGuiDef.Join) {
116                 setText(prefix+"Join");
117                 super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("join"))));
118             } else if (value instanceof MondrianGuiDef.Table) {
119                 setText(prefix+"Table: "+ ((MondrianGuiDef.Table) value).name);
120                 super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("table"))));
121             }
122             this.getPreferredSize();
123             this.setPreferredSize(new Dimension(this.getPreferredSize().width+35, 24)); //Do not remove this
124
//this.setSize(new Dimension(this.getPreferredSize().width, 24)); //Do not remove this
125
//this.setPreferredSize(new Dimension(170, 24)); //Do not remove this
126
//setText("Relation");
127

128         } else if (value instanceof MondrianGuiDef.Level) {
129             setText(invalidFlag, ((MondrianGuiDef.Level) value).name);
130             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("level"))));
131             /* Do not remove this line.
132              * This sets the preferred width of tree cell displaying Level name.
133              * This resolves the ambiguous problem of last char or last word truncated from Level name in the tree cell.
134              * This problem was there with Level objects, while all other objects display their names
135              * without any truncation of characters. Therefore, we have to force the setting of preferred width to desired width
136              * so that characters do not truncate from dimension name.
137              * Along with this the preferred size of other objects should be set to null, so that the layout mgr can calculate the
138              * preferred width in case of other objects.
139              */

140             this.setPreferredSize(new java.awt.Dimension JavaDoc(this.getPreferredSize().width+1, 25)); //Do not remove this
141
} else if (value instanceof MondrianGuiDef.Measure) {
142
143             setText(invalidFlag, ((MondrianGuiDef.Measure) value).name);
144             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("measure"))));
145         } else if (value instanceof MondrianGuiDef.MemberReaderParameter) {
146             setText(invalidFlag, ((MondrianGuiDef.MemberReaderParameter) value).name);
147         } else if (value instanceof MondrianGuiDef.Property) {
148             setText(invalidFlag, ((MondrianGuiDef.Property) value).name);
149             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("property"))));
150         } else if (value instanceof MondrianGuiDef.Schema) {
151             setText(invalidFlag, "Schema");
152             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("schema"))));
153         } else if (value instanceof MondrianGuiDef.NamedSet) {
154             setText(invalidFlag, ((MondrianGuiDef.NamedSet) value).name);
155             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("namedSet"))));
156         } else if (value instanceof MondrianGuiDef.CalculatedMember) {
157             setText(invalidFlag, ((MondrianGuiDef.CalculatedMember) value).name);
158             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("calculatedMember"))));
159         } else if (value instanceof MondrianGuiDef.CalculatedMemberProperty) {
160             setText(invalidFlag, ((MondrianGuiDef.CalculatedMemberProperty) value).name);
161             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("nopic"))));
162         } else if (value instanceof MondrianGuiDef.UserDefinedFunction) {
163             setText(invalidFlag, ((MondrianGuiDef.UserDefinedFunction) value).name);
164             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("userDefinedFunction"))));
165         } else if (value instanceof MondrianGuiDef.Role) {
166             setText(invalidFlag, ((MondrianGuiDef.Role) value).name);
167             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("role"))));
168         } else if (value instanceof MondrianGuiDef.SchemaGrant) {
169             setText(invalidFlag, "Schema Grant");
170             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("schemaGrant"))));
171         } else if (value instanceof MondrianGuiDef.CubeGrant) {
172             setText(invalidFlag, "Cube Grant");
173             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("cubeGrant"))));
174         } else if (value instanceof MondrianGuiDef.DimensionGrant) {
175             setText(invalidFlag, "Dimension Grant");
176             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("dimensionGrant"))));
177         } else if (value instanceof MondrianGuiDef.HierarchyGrant) {
178             setText(invalidFlag, "Hierarchy Grant");
179             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("hierarchyGrant"))));
180         } else if (value instanceof MondrianGuiDef.MemberGrant) {
181             setText(invalidFlag, "Member Grant");
182             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("memberGrant"))));
183         } else if (value instanceof MondrianGuiDef.SQL) {
184             setText("SQL");
185             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("sql"))));
186         } else if (value instanceof MondrianGuiDef.View) {
187             setText("View");
188         } else if (value instanceof MondrianGuiDef.VirtualCube) {
189             setText(invalidFlag, ((MondrianGuiDef.VirtualCube) value).name);
190             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("virtualCube"))));
191         } else if (value instanceof MondrianGuiDef.VirtualCubeDimension) {
192             setText(invalidFlag, ((MondrianGuiDef.VirtualCubeDimension) value).name);
193             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("virtualCubeDimension"))));
194         } else if (value instanceof MondrianGuiDef.VirtualCubeMeasure) {
195             setText(invalidFlag, ((MondrianGuiDef.VirtualCubeMeasure) value).name);
196             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("virtualCubeMeasure"))));
197         } else if (value instanceof MondrianGuiDef.AggName) {
198             setText(invalidFlag, "Aggregate Name");
199             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("aggTable"))));
200         } else if (value instanceof MondrianGuiDef.AggForeignKey) {
201             setText(invalidFlag, "Aggregate Foreign Key");
202             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("aggForeignKey"))));
203         } else if (value instanceof MondrianGuiDef.AggIgnoreColumn) {
204             setText(invalidFlag, "Aggregate Ignore Column");
205             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("aggIgnoreColumn"))));
206         } else if (value instanceof MondrianGuiDef.AggLevel) {
207             setText(invalidFlag, "Aggregate Level");
208             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("aggLevel"))));
209         } else if (value instanceof MondrianGuiDef.AggMeasure) {
210             setText(invalidFlag, "Aggregate Measure");
211             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("aggMeasure"))));
212         } else if (value instanceof MondrianGuiDef.AggPattern) {
213             setText(invalidFlag, "Aggregate Pattern");
214             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("aggPattern"))));
215         } else if (value instanceof MondrianGuiDef.AggExclude) {
216             setText(invalidFlag, "Aggregate Exclude");
217             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("aggExclude"))));
218         } else if (value instanceof MondrianGuiDef.Closure) {
219             setText(invalidFlag, "Closure");
220             super.setIcon(new ImageIcon(myClassLoader.getResource(resources.getString("closure"))));
221         } else if (value instanceof ElementDef) {
222             setText(((ElementDef) value).getName());
223         } else {
224             super.setText("");
225         }
226
227         return this;
228
229     }
230
231     // called from external methods
232
public String JavaDoc invalid(JTree tree, TreePath JavaDoc tpath, Object JavaDoc value) {
233         return this.invalid(tree, tpath, value, null, null, null, null);
234     }
235
236     public String JavaDoc invalid(JTree tree, TreePath JavaDoc tpath, Object JavaDoc value, Object JavaDoc icube, Object JavaDoc iparentDimension, Object JavaDoc iparentHierarchy, Object JavaDoc iparentLevel) {
237         String JavaDoc errMsg = null;
238         String JavaDoc nameLiteral = "Name";
239         //String valueClass = value.getClass().getSimpleName();
240
String JavaDoc emptyMsg = " must be set";
241
242         MondrianGuiDef.Cube cube = (MondrianGuiDef.Cube) icube ; //null;
243
MondrianGuiDef.Dimension parentDimension = (MondrianGuiDef.Dimension) iparentDimension; // null // used only by level to check for leveltype value
244
MondrianGuiDef.Hierarchy parentHierarchy = (MondrianGuiDef.Hierarchy) iparentHierarchy ; //null; // used only by level validation
245
MondrianGuiDef.Level parentLevel = (MondrianGuiDef.Level) iparentLevel ; // null // used only by property validation
246

247         if (tpath != null ) {
248             int pathcount = tpath.getPathCount();
249             for(int i=0; i<pathcount && (cube==null || parentDimension==null || parentHierarchy==null || parentLevel==null) ;i++) {
250                 //System.out.println("path element "+i+" ="+tree.getSelectionPath().getPathComponent(i).getClass().toString());
251
if (tpath.getPathComponent(i) instanceof MondrianGuiDef.Cube && cube==null) {
252                     cube = (MondrianGuiDef.Cube) tpath.getPathComponent(i);
253                 }
254                 if (tpath.getPathComponent(i) instanceof MondrianGuiDef.Dimension && parentDimension == null) {
255                     parentDimension = (MondrianGuiDef.Dimension) tpath.getPathComponent(i);
256                 }
257                 if (tpath.getPathComponent(i) instanceof MondrianGuiDef.Hierarchy && parentHierarchy == null) {
258                     parentHierarchy = (MondrianGuiDef.Hierarchy) tpath.getPathComponent(i);
259                 }
260                 if (tpath.getPathComponent(i) instanceof MondrianGuiDef.Level && parentLevel == null) {
261                     parentLevel = (MondrianGuiDef.Level) tpath.getPathComponent(i);
262                 }
263                 //System.out.println("Cube fact table name ="+((MondrianGuiDef.Table) ((MondrianGuiDef.Cube) e.getPath().getPathComponent(i)).fact).name);
264
}
265         }
266
267         //Step 1: check validity of this value object
268
if (value instanceof MondrianGuiDef.Schema) {
269             if ( isEmpty(((MondrianGuiDef.Schema) value).name) ) {
270                 return nameLiteral + emptyMsg;
271             }
272         } else if(value instanceof MondrianGuiDef.VirtualCube) {
273             if ( isEmpty(((MondrianGuiDef.VirtualCube) value).name) ) {
274                 return nameLiteral + emptyMsg;
275             }
276         } else if(value instanceof MondrianGuiDef.VirtualCubeDimension) {
277             if ( isEmpty(((MondrianGuiDef.VirtualCubeDimension) value).name) ) {
278                 return nameLiteral + emptyMsg;
279             }
280         } else if(value instanceof MondrianGuiDef.VirtualCubeMeasure) {
281             if ( isEmpty(((MondrianGuiDef.VirtualCubeMeasure) value).name) ) {
282                 return nameLiteral + emptyMsg;
283             }
284         } else if (value instanceof MondrianGuiDef.Cube) {
285             if ( isEmpty(((MondrianGuiDef.Cube) value).name) ) {
286                 return nameLiteral + emptyMsg;}
287             if ( ((MondrianGuiDef.Cube) value).fact == null || isEmpty(((MondrianGuiDef.Table) ((MondrianGuiDef.Cube) value).fact).name) ) //check name is not blank
288
{ return "Fact name" + emptyMsg;}
289
290             // database validity check, if database connection is successful
291
if (jdbcMetaData.getErrMsg() == null) {
292
293                 //Vector allTables = jdbcMetaData.getAllTables(((MondrianGuiDef.Table) ((MondrianGuiDef.Cube) value).fact).schema);
294
String JavaDoc schemaName = ((MondrianGuiDef.Table) ((MondrianGuiDef.Cube) value).fact).schema;
295                 String JavaDoc factTable = ((MondrianGuiDef.Table) ((MondrianGuiDef.Cube) value).fact).name;
296                 if (! jdbcMetaData.isTableExists(schemaName, factTable)) {
297                     return "Fact table '"+factTable+"' does not exist in database "+((schemaName==null || schemaName.equals(""))?".":"schema "+schemaName);
298                 }
299             }
300         } else if (value instanceof MondrianGuiDef.CubeDimension) {
301             if (isEmpty(((MondrianGuiDef.CubeDimension) value).name)) //check name is not blank
302
{ return nameLiteral + emptyMsg;}
303             if (value instanceof MondrianGuiDef.DimensionUsage) {
304                 if(isEmpty(((MondrianGuiDef.DimensionUsage) value).source)) //check source is not blank
305
{ return "Source" + emptyMsg;}
306                 // check source is name of one of dimensions of schema (shared dimensions)
307
MondrianGuiDef.Schema s = (MondrianGuiDef.Schema) tree.getModel().getRoot();
308                 MondrianGuiDef.Dimension ds[] = s.dimensions;
309                 String JavaDoc sourcename = ((MondrianGuiDef.DimensionUsage) value).source;
310                 boolean notfound = true;
311                 for(int j=0; j<ds.length; j++) {
312                     if (ds[j].name.equalsIgnoreCase(sourcename)) {
313                         notfound = false;
314                         break;
315                     }
316                 }
317                 if (notfound) {
318                     return "Source '"+sourcename+"' does not exist as Shared Dimension of Schema";
319                 }
320             }
321             if (value instanceof MondrianGuiDef.Dimension && cube != null) {
322                 /* //foreignkey can be blank if hierarchy relation is null
323                  * // this check moved to child hierarchies relation check below
324                 if(isEmpty(((MondrianGuiDef.Dimension) value).foreignKey)) //check foreignkey is not blank
325                 { return "ForeignKey" + emptyMsg;}
326                  */

327                 if(! isEmpty(((MondrianGuiDef.Dimension) value).foreignKey)) {
328                     // database validity check, if database connection is successful
329
if (jdbcMetaData.getErrMsg() == null) {
330
331                         //Vector allcols = jdbcMetaData.getAllColumns(((MondrianGuiDef.Table) cube.fact).schema, ((MondrianGuiDef.Table) cube.fact).name);
332
String JavaDoc foreignKey = ((MondrianGuiDef.Dimension) value).foreignKey;
333                         if (! jdbcMetaData.isColExists(((MondrianGuiDef.Table) cube.fact).schema, ((MondrianGuiDef.Table) cube.fact).name, foreignKey)) {
334                             return "foreignKey '"+foreignKey+"' does not exist in fact table.";
335                         }
336                     /*
337                     if (! allcols.contains(foreignKey)) // check foreignKey is a fact table column
338                     { return "ForeignKey '"+foreignKey+"' does not exist in fact table.";}
339                      */

340                     }
341                 }
342             }
343         } else if (value instanceof MondrianGuiDef.Level) {
344             /*
345             // check 'column' exists in 'table' if table is specified otherwise :: case of join
346             // it should exist in relation table if it is specified otherwise :: case of table
347             // it should exist in fact table :: case of degenerate dimension where dimension columns exist in fact table
348             // and there is no separate table
349              */

350             MondrianGuiDef.Level l = (MondrianGuiDef.Level) value;
351             if (! isEmpty(l.levelType)) {
352                 // empty leveltype is treated as default value of "Regular"" which is ok with standard/time dimension
353
if (parentDimension != null) {
354                     if ((isEmpty(parentDimension.type) || parentDimension.type.equals("StandardDimension")) &&
355                             (! l.levelType.equals(MondrianGuiDef.Level._levelType_values[0]))) {
356                      // if dimension type is 'standard' then leveltype should be 'regular'
357
return "levelType '"+l.levelType+"' can only be used with a TimeDimension.";
358                     }
359                     if ((parentDimension.type.equals("TimeDimension")) &&
360                      // if dimension type is 'time' then leveltype value could be 'timeyears', 'timedays' etc'
361
(l.levelType.equals(MondrianGuiDef.Level._levelType_values[0]))) {
362                         return "levelType '"+l.levelType+"' can only be used with a StandardDimension.";
363                     }
364                 }
365             }
366             String JavaDoc column = l.column; // check level's column is in fact table'
367
/* // level column may be blank, if it has properties defined with cols.
368             if (isEmpty(column)) {
369                 return "Column" + emptyMsg;
370             }
371              */

372             if (isEmpty(column)) {
373                 if (l.properties == null || l.properties.length == 0) {
374                     return "column" + emptyMsg;
375                 }
376             } else {
377                 // database validity check, if database connection is successful
378
if (jdbcMetaData.getErrMsg() == null) {
379                     String JavaDoc table = l.table; // specified table for level's column'
380
if (isEmpty(table)) {
381                         if (parentHierarchy != null ) {
382                             if (parentHierarchy.relation == null && cube != null) { // case of degenerate dimension within cube, hierarchy table not specified
383
if (! jdbcMetaData.isColExists(((MondrianGuiDef.Table) cube.fact).schema, ((MondrianGuiDef.Table) cube.fact).name, column)) {
384                                     return "Degenerate dimension validation check - Column '"+column+"' does not exist in fact table.";
385                                 }
386                             } else if (parentHierarchy.relation instanceof MondrianGuiDef.Table){
387                                 if (! jdbcMetaData.isColExists(((MondrianGuiDef.Table) parentHierarchy.relation).schema, ((MondrianGuiDef.Table) parentHierarchy.relation).name, column)) {
388                                     return "column '"+column+"' does not exist in Dimension table '"+((MondrianGuiDef.Table) parentHierarchy.relation).name+"'.";
389                                 }
390                             } else if (parentHierarchy.relation instanceof MondrianGuiDef.Join){ // relation is join, table should be specified
391
return "table" + emptyMsg;
392                             }
393                         }
394                     } else {
395                         if (! jdbcMetaData.isColExists(null, table, column)) {
396                             return "column '"+column+"' does not exist in table '"+table+"'.";
397                         }
398                     }
399                 }
400             }
401         } else if (value instanceof MondrianGuiDef.Property) {
402             /*
403             // check 'column' exists in 'table' if [level table] is specified otherwise :: case of join
404             // it should exist in [hierarchy relation table] if it is specified otherwise :: case of table
405             // it should exist in [fact table] :: case of degenerate dimension where dimension columns exist in fact table
406             // and there is no separate table
407              */

408             MondrianGuiDef.Property p = (MondrianGuiDef.Property) value;
409             String JavaDoc column = p.column; // check property's column is in table'
410
if (isEmpty(column)) {
411                 return "column" + emptyMsg;
412             }
413             // database validity check, if database connection is successful
414
if (jdbcMetaData.getErrMsg() == null) {
415                 String JavaDoc table = null;
416                 if (parentLevel != null) {
417                     table = parentLevel.table; // specified table for level's column'
418
}
419                 if (isEmpty(table)) {
420                     if (parentHierarchy != null ) {
421                         if (parentHierarchy.relation == null && cube != null) { // case of degenerate dimension within cube, hierarchy table not specified
422
if (! jdbcMetaData.isColExists(((MondrianGuiDef.Table) cube.fact).schema, ((MondrianGuiDef.Table) cube.fact).name, column)) {
423                                 return "Degenerate dimension validation check - Column '"+column+"' does not exist in fact table.";
424                             }
425                         } else if (parentHierarchy.relation instanceof MondrianGuiDef.Table){
426                             if (! jdbcMetaData.isColExists(((MondrianGuiDef.Table) parentHierarchy.relation).schema, ((MondrianGuiDef.Table) parentHierarchy.relation).name, column)) {
427                                 return "column '"+column+"' does not exist in Dimension table '"+((MondrianGuiDef.Table) parentHierarchy.relation).name+"'.";
428                             }
429                         }
430                     }
431                 } else {
432                     if (! jdbcMetaData.isColExists(null, table, column)) {
433                         return "column '"+column+"' does not exist in Level table '"+table+"'.";
434                     }
435                 }
436             }
437         } else if (value instanceof MondrianGuiDef.Measure) {
438             if ( isEmpty(((MondrianGuiDef.Measure) value).name) ) {
439                 return nameLiteral + emptyMsg;}
440             if (isEmpty(((MondrianGuiDef.Measure) value).aggregator) ) {
441                 return "aggregator" + emptyMsg;}
442             if (isEmpty(((MondrianGuiDef.Measure) value).column) ) {
443                 return "column" + emptyMsg;}
444             if (cube != null && cube.fact != null) {
445
446                 // database validity check, if database connection is successful
447
if (jdbcMetaData.getErrMsg() == null) {
448
449                     //Vector allcols = jdbcMetaData.getAllColumns(((MondrianGuiDef.Table) cube.fact).schema, ((MondrianGuiDef.Table) cube.fact).name);
450

451                     String JavaDoc column = ((MondrianGuiDef.Measure) value).column;
452                     if (jdbcMetaData.isColExists(((MondrianGuiDef.Table) cube.fact).schema, ((MondrianGuiDef.Table) cube.fact).name, column)) {
453                         /* disabled check that the column value should exist in table because column could also be an expression
454                     if (! jdbcMetaData.isColExists(((MondrianGuiDef.Table) cube.fact).schema, ((MondrianGuiDef.Table) cube.fact).name, column)) {
455                         return "Column '"+column+"' does not exist in fact table.";
456                     }
457                          */

458                     /*
459                     if (! allcols.contains(column)) // check foreignKey is a fact table column
460                     { return "Column '"+column+"' does not exist in fact table.";}
461                      */

462                         // check for aggregator type only if column exists in table
463
// check if aggregator selected is valid on the data type of the column selected.
464
int colType = jdbcMetaData.getColumnDataType(((MondrianGuiDef.Table) cube.fact).schema, ((MondrianGuiDef.Table) cube.fact).name, ((MondrianGuiDef.Measure) value).column);
465                         // colType of 2, 4,5, 7,8 is numeric types whereas 1, 12 are char varchar string and 91 is date type
466
int agIndex = -1;
467                         if ("sum".equals(((MondrianGuiDef.Measure) value).aggregator) || "avg".equals(((MondrianGuiDef.Measure) value).aggregator)) {
468                             agIndex=0; // aggregator = sum or avg, column should be numeric
469
}
470                         if (! (agIndex == -1 || (colType >=2 && colType <=8))) {
471                             return "aggregator '"+((MondrianGuiDef.Measure) value).aggregator+"' is not valid on the data type of the column '"+((MondrianGuiDef.Measure) value).column+"'";
472                         }
473                     }
474                 }
475             }
476         } else if (value instanceof MondrianGuiDef.Hierarchy) {
477             if (((MondrianGuiDef.Hierarchy)value).relation instanceof MondrianGuiDef.Join) {
478                 String JavaDoc returnMsg = "";
479                 if ( isEmpty(((MondrianGuiDef.Hierarchy) value).primaryKeyTable)) {
480                     returnMsg = "primaryKeyTable ";
481                 }
482                 if ( isEmpty(((MondrianGuiDef.Hierarchy) value).primaryKey)) {
483                     if (returnMsg.length() > 0) {
484                         returnMsg = returnMsg + "and";
485                     }
486                     returnMsg = returnMsg + " primaryKey ";
487                 }
488                 if (returnMsg.length() > 0) {
489                     return (returnMsg + emptyMsg + " for Join");
490                 }
491             }
492         } else if (value instanceof MondrianGuiDef.NamedSet) {
493             if ( isEmpty(((MondrianGuiDef.NamedSet) value).name) ) {
494                 return nameLiteral + emptyMsg;}
495             if ( isEmpty(((MondrianGuiDef.NamedSet) value).formula) ) {
496                 return "formula " + emptyMsg;}
497         } else if (value instanceof MondrianGuiDef.UserDefinedFunction) {
498             if ( isEmpty(((MondrianGuiDef.UserDefinedFunction) value).name) ) {
499                 return nameLiteral + emptyMsg;}
500             if( isEmpty(((MondrianGuiDef.UserDefinedFunction) value).className)) {
501                 return "className" + emptyMsg;}
502         } else if (value instanceof MondrianGuiDef.CalculatedMember) {
503             if ( isEmpty(((MondrianGuiDef.CalculatedMember) value).name) ) {
504                 return nameLiteral + emptyMsg;}
505             if (isEmpty(((MondrianGuiDef.CalculatedMember) value).dimension) ) {
506                 return "dimension" + emptyMsg;}
507         } else if (value instanceof MondrianGuiDef.Join) {
508             if ( isEmpty(((MondrianGuiDef.Join) value).leftKey) ) {
509                 return "leftKey" + emptyMsg;}
510             if (isEmpty(((MondrianGuiDef.Join) value).rightKey) ) {
511                 return "rightKey" + emptyMsg;}
512         }
513
514         // Step 2: check validity of all child objects for this value object.
515
SchemaTreeModel model = (SchemaTreeModel) tree.getModel();
516         int childCnt = model.getChildCount(value);
517         for (int i=0; i<childCnt; i++) {
518             Object JavaDoc child = model.getChild(value, i);
519             String JavaDoc childErrMsg;
520             if (child instanceof MondrianGuiDef.Cube) {
521                 childErrMsg = invalid(tree, tpath, child, child, parentDimension, parentHierarchy, parentLevel); //check current cube child and its children
522
} else if (child instanceof MondrianGuiDef.Dimension) {
523                 childErrMsg = invalid(tree, tpath, child, cube, child, parentHierarchy, parentLevel); //check the current hierarchy and its children
524
} else if (child instanceof MondrianGuiDef.Hierarchy) {
525                 // special check for cube dimension where foreign key is blank : allowed /not allowed
526
if (value instanceof MondrianGuiDef.Dimension && cube != null && ((MondrianGuiDef.Hierarchy)child).relation != null) {
527                     if(isEmpty(((MondrianGuiDef.Dimension) value).foreignKey)) //check foreignkey is not blank
528
{ return "foreignKey" + emptyMsg; // if relation is null, foreignkey must be specified
529
}
530                 }
531                 childErrMsg = invalid(tree, tpath, child, cube, parentDimension, child, parentLevel); //check the current hierarchy and its children
532
} else if (child instanceof MondrianGuiDef.Level) {
533                 childErrMsg = invalid(tree, tpath, child, cube, parentDimension, parentHierarchy, child); //check the current hierarchy and its children
534
} else {
535                 childErrMsg = invalid(tree, tpath, child, cube, parentDimension, parentHierarchy, parentLevel); //check this child and all its children objects with incoming cube and hierarchy
536
}
537
538             /* If all children are valid then do a special check.
539              * Special check for cubes to see if their child dimensions have foreign key set and set the childErrMsg with error msg
540              */

541             /* === Begin : disabled
542             if (childErrMsg == null) { // all children are valid
543                 if (child instanceof MondrianGuiDef.Cube) {
544                     MondrianGuiDef.Cube c = (MondrianGuiDef.Cube) child;
545                     MondrianGuiDef.CubeDimension [] ds = c.dimensions;
546                     for (int j=0; j<ds.length; j++) {
547                         MondrianGuiDef.CubeDimension d = (MondrianGuiDef.CubeDimension) ds[j];
548                         if (d instanceof MondrianGuiDef.DimensionUsage) {
549                             continue; // check the next dimension.
550                         }
551
552                         if(isEmpty(d.foreignKey)) //check foreignkey is not blank
553                         { childErrMsg = "ForeignKey" + emptyMsg;
554                           break;
555                         }
556
557                         // database validity check, if database connection is successful
558                         if (jdbcMetaData.getErrMsg() == null) {
559
560                             //Vector allcols = jdbcMetaData.getAllColumns(((MondrianGuiDef.Table) c.fact).schema, ((MondrianGuiDef.Table) c.fact).name);
561                             String foreignKey = d.foreignKey;
562                             if (! jdbcMetaData.isColExists(((MondrianGuiDef.Table) c.fact).schema, ((MondrianGuiDef.Table) c.fact).name, foreignKey)) {
563                                 childErrMsg = "ForeignKey '"+foreignKey+"' does not exist in fact table.";
564                                 break;
565                             }
566                             /*
567                             if (! allcols.contains(foreignKey)) // check foreignKey is a fact table column
568                             { childErrMsg = "ForeignKey '"+foreignKey+"' does not exist in fact table.";
569                                 break;
570                             }
571              * /
572                         }
573                     }
574                 }
575             }
576              * === End : disabled
577              */

578             // Now set the final errormsg
579
if (childErrMsg != null) {
580                 String JavaDoc childClassName = child.getClass().getName();
581                 String JavaDoc simpleName[] = childClassName.split("[$.]",0);
582                 String JavaDoc childName;
583                 try {
584                     Field JavaDoc f = child.getClass().getField("name");
585                     childName = (String JavaDoc) f.get(child) ;
586                     if (childName == null) {
587                         childName="";
588                     } else {
589                         childName = " '" + childName + "'";
590                     }
591                     childErrMsg = simpleName[simpleName.length-1] + " " +
592                         childName +" is invalid.";
593                 } catch(Exception JavaDoc ex) {
594                     childErrMsg = simpleName[simpleName.length-1] + " is invalid.";
595                 }
596                 return childErrMsg;
597             }
598         }
599
600         return errMsg;
601     }
602
603     private boolean isEmpty(Object JavaDoc v) {
604         if ((v == null) || v.equals("") ) {
605             return true;
606         } else {
607             return false;
608         }
609     }
610     private boolean isInvalid(JTree tree, Object JavaDoc value, int row) {
611         //return (invalid(tree.getSelectionPath(), value) ==null)?false:true;
612
/* (TreePath) tree.getPathForRow(row) returns null for new objects added to tree in the first run of rendering.
613          * Check for null before calling methods on Treepath returned.
614          */

615         return (invalid(tree, tree.getPathForRow(row), value) ==null)?false:true;
616         //return (invalid(null, value) ==null)?false:true;
617
}
618
619     public void setText(boolean invalidFlag, String JavaDoc myText) {
620         if (invalidFlag) {
621             myText = "<html><FONT COLOR=RED><b>x</b></FONT><FONT COLOR="+ getForeground().hashCode()+">"+myText+"</FONT></html>";
622         }
623         setText(myText);
624     }
625 }
626
627 // End SchemaTreeCellRenderer.java
628
Popular Tags