Author: kaiyi4
Date: Wed Aug 27 08:18:14 2008
New Revision: 2621

Modified:
   trunk/src/ca/sqlpower/architect/swingui/olap/HierarchyEditPanel.java

Log:
Added primaryKey property to hierarchy edit panel. This property is required according to mondrian documentation.

This primaryKey is retrieved from the selected table.

I also discovered that it's compulsory to have a source table for a hierarchy, it's specified in either hierarchy.relation or hierarchy.primaryKeyTable. Right now we only allow a user to edit relation property.

It might be good to have a validator watching for the table selection at a later time.

Modified: trunk/src/ca/sqlpower/architect/swingui/olap/HierarchyEditPanel.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/olap/HierarchyEditPanel.java (original) +++ trunk/src/ca/sqlpower/architect/swingui/olap/HierarchyEditPanel.java Wed Aug 27 08:18:14 2008
@@ -19,6 +19,7 @@

 package ca.sqlpower.architect.swingui.olap;

+import java.util.ArrayList;
 import java.util.List;
 import java.util.Vector;

@@ -29,6 +30,7 @@
 import javax.swing.JTextField;

 import ca.sqlpower.architect.ArchitectException;
+import ca.sqlpower.architect.SQLColumn;
 import ca.sqlpower.architect.SQLTable;
 import ca.sqlpower.architect.olap.OLAPUtil;
 import ca.sqlpower.architect.olap.MondrianModel.Hierarchy;
@@ -51,6 +53,7 @@
     private final JComboBox tableChooser;
     private final JCheckBox hasAll;
     private final JTextField allLevelName;
+    private final JComboBox primaryKeyName;

     /**
      * Validation handler for errors in the dialog
@@ -80,6 +83,22 @@
builder.append("Table", tableChooser = new JComboBox(new Vector<SQLTable>(tables))); builder.append("All Level Name", allLevelName = new JTextField(hierarchy.getAllLevelName() != null ? hierarchy.getAllLevelName() : "(All)")); tableChooser.setSelectedItem(OLAPUtil.tableForHierarchy(hierarchy)); // XXX this isn't quite right.. it would set the default as the local value
+        SQLTable selectedTable = (SQLTable) tableChooser.getSelectedItem();
+        List<SQLColumn> pk = new ArrayList<SQLColumn>();
+        if (selectedTable != null) {
+            for (SQLColumn col : selectedTable.getColumns()) {
+                if (col.isPrimaryKey()) {
+                    pk.add(col);
+                }
+            }
+            if (pk.isEmpty()) {
+ throw new IllegalStateException("Primary key in a hierarchy should always be " + + "specified from the selected table, but selected table contains no primary key");
+            }
+        } else {
+ throw new IllegalStateException("Selected source table must not be null");
+        }
+ builder.append("Primary Key", primaryKeyName = new JComboBox(new Vector<SQLColumn>(pk)));

         handler = new FormValidationHandler(status);
Validator validator = new OLAPObjectNameValidator(hierarchy.getParent(), hierarchy, true);
@@ -108,6 +127,8 @@
             hierarchy.setRelation(table);
         }
         hierarchy.setAllLevelName(allLevelName.getText());
+        SQLColumn column = (SQLColumn) primaryKeyName.getSelectedItem();
+        hierarchy.setPrimaryKey(column.getName());
         hierarchy.endCompoundEdit();
         return true;
     }

Reply via email to