cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject cayenne git commit: Remove code duplication in SelectQuery and SQLTemplate prefetch tabs
Date Mon, 05 Mar 2018 09:00:02 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master ec5df09b7 -> 9c07e1826


Remove code duplication in SelectQuery and SQLTemplate prefetch tabs


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/9c07e182
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/9c07e182
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/9c07e182

Branch: refs/heads/master
Commit: 9c07e1826e48df4a86de9110b553bd98388ac581
Parents: ec5df09
Author: stariy <stariy95@gmail.com>
Authored: Mon Mar 5 11:59:55 2018 +0300
Committer: stariy <stariy95@gmail.com>
Committed: Mon Mar 5 11:59:55 2018 +0300

----------------------------------------------------------------------
 .../cayenne/modeler/editor/PrefetchModel.java   | 130 +++++++++++++++++
 .../modeler/editor/SQLTemplatePrefetchTab.java  | 139 +-----------------
 .../modeler/editor/SelectQueryPrefetchTab.java  | 141 +------------------
 3 files changed, 140 insertions(+), 270 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/9c07e182/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/PrefetchModel.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/PrefetchModel.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/PrefetchModel.java
new file mode 100644
index 0000000..c671c66
--- /dev/null
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/PrefetchModel.java
@@ -0,0 +1,130 @@
+package org.apache.cayenne.modeler.editor;
+
+import org.apache.cayenne.exp.Expression;
+import org.apache.cayenne.exp.ExpressionException;
+import org.apache.cayenne.exp.ExpressionFactory;
+import org.apache.cayenne.map.Relationship;
+
+import javax.swing.table.AbstractTableModel;
+import java.util.Map;
+
+/**
+ * A table model for the Prefetch table.
+ */
+class PrefetchModel extends AbstractTableModel {
+
+    private final Map<String, Integer> prefetchMap;
+
+    private final Object root;
+
+    private final String[] prefetches;
+
+    PrefetchModel(Map<String, Integer> prefetchMap, Object root) {
+        this.prefetchMap = prefetchMap;
+        this.prefetches = prefetchMap.keySet().toArray(new String[0]);
+        this.root = root;
+    }
+
+    @Override
+    public int getColumnCount() {
+        return 3;
+    }
+
+    @Override
+    public int getRowCount() {
+        return (prefetches != null) ? prefetches.length : 0;
+    }
+
+    @Override
+    public Object getValueAt(int row, int column) {
+        switch (column) {
+            case 0:
+                return prefetches[row];
+            case 1:
+                return isToMany(prefetches[row]) ? Boolean.TRUE : Boolean.FALSE;
+            case 2:
+                return getPrefetchTypeString(prefetchMap.get(prefetches[row]));
+            default:
+                throw new IndexOutOfBoundsException("Invalid column: " + column);
+        }
+    }
+
+    @Override
+    public Class getColumnClass(int column) {
+        switch (column) {
+            case 0:
+                return String.class;
+            case 1:
+                return Boolean.class;
+            case 2:
+                return String.class;
+            default:
+                throw new IndexOutOfBoundsException("Invalid column: " + column);
+        }
+    }
+
+    @Override
+    public String getColumnName(int column) {
+        switch (column) {
+            case 0:
+                return "Prefetch Path";
+            case 1:
+                return "To Many";
+            case 2:
+                return "Prefetch Type";
+            default:
+                throw new IndexOutOfBoundsException("Invalid column: " + column);
+        }
+    }
+
+    @Override
+    public boolean isCellEditable(int row, int column) {
+        return column == 2;
+    }
+
+    @Override
+    public void setValueAt(Object value, int row, int column) {
+        switch (column) {
+            case 2:
+                prefetchMap.put(prefetches[row], getPrefetchType((String)value));
+                break;
+        }
+    }
+
+    static int getPrefetchType(String semantics) {
+
+        //case 2: disjoint isn't use for SQLTemplate prefetch
+        switch (semantics){
+            case "Joint" :
+                return 1;
+            case "Disjoint":
+                return 2;
+            case "Disjoint by id":
+                return 3;
+            default: return 0;
+        }
+    }
+
+    private static String getPrefetchTypeString(int semantics) {
+        switch (semantics){
+            case 1 :
+                return SelectQueryPrefetchTab.JOINT_PREFETCH_SEMANTICS;
+            case 2:
+                return SelectQueryPrefetchTab.DISJOINT_PREFETCH_SEMANTICS;
+            case 3:
+                return SelectQueryPrefetchTab.DISJOINT_BY_ID_PREFETCH_SEMANTICS;
+        }
+        return SelectQueryPrefetchTab.UNDEFINED_SEMANTICS;
+    }
+
+    private boolean isToMany(String prefetch) {
+        // totally invalid path would result in ExpressionException
+        try {
+            Expression exp = ExpressionFactory.exp(prefetch);
+            Object object = exp.evaluate(root);
+            return object instanceof Relationship && ((Relationship) object).isToMany();
+        } catch (ExpressionException e) {
+            return false;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/9c07e182/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/SQLTemplatePrefetchTab.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/SQLTemplatePrefetchTab.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/SQLTemplatePrefetchTab.java
index 1375429..5136b86 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/SQLTemplatePrefetchTab.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/SQLTemplatePrefetchTab.java
@@ -20,9 +20,6 @@
 package org.apache.cayenne.modeler.editor;
 
 import org.apache.cayenne.configuration.event.QueryEvent;
-import org.apache.cayenne.exp.Expression;
-import org.apache.cayenne.exp.ExpressionException;
-import org.apache.cayenne.exp.ExpressionFactory;
 import org.apache.cayenne.map.Attribute;
 import org.apache.cayenne.map.Entity;
 import org.apache.cayenne.map.QueryDescriptor;
@@ -53,7 +50,6 @@ import javax.swing.JSplitPane;
 import javax.swing.JTable;
 import javax.swing.JToolBar;
 import javax.swing.ListSelectionModel;
-import javax.swing.table.AbstractTableModel;
 import javax.swing.table.DefaultTableCellRenderer;
 import javax.swing.table.TableColumn;
 import javax.swing.table.TableModel;
@@ -73,10 +69,6 @@ public class SQLTemplatePrefetchTab extends JPanel implements PropertyChangeList
     // property for split pane divider size
     private static final String SPLIT_DIVIDER_LOCATION_PROPERTY = "query.orderings.divider.location";
 
-    private static final String JOINT_PREFETCH_SEMANTICS = "Joint";
-    private static final String DISJOINT_BY_ID_PREFETCH_SEMANTICS = "Disjoint by id";
-    private static final String UNDEFINED_SEMANTICS = "Undefined semantics";
-
     private static final Dimension BROWSER_CELL_DIM = new Dimension(150, 100);
     private static final Dimension TABLE_DIM = new Dimension(460, 60);
 
@@ -168,8 +160,8 @@ public class SQLTemplatePrefetchTab extends JPanel implements PropertyChangeList
     protected void setUpPrefetchBox(TableColumn column) {
 
         JComboBox<String> prefetchBox = new JComboBox<>();
-        prefetchBox.addItem(JOINT_PREFETCH_SEMANTICS);
-        prefetchBox.addItem(DISJOINT_BY_ID_PREFETCH_SEMANTICS);
+        prefetchBox.addItem(SelectQueryPrefetchTab.JOINT_PREFETCH_SEMANTICS);
+        prefetchBox.addItem(SelectQueryPrefetchTab.DISJOINT_BY_ID_PREFETCH_SEMANTICS);
 
         prefetchBox.addActionListener(e -> Application.getInstance().getFrameController().getEditorView().getEventController().setDirty(true));
 
@@ -240,7 +232,6 @@ public class SQLTemplatePrefetchTab extends JPanel implements PropertyChangeList
         Icon removeIcon = ModelerUtil.buildIcon("icon-trash.png");
         remove.setIcon(removeIcon);
         remove.setDisabledIcon(FilteredIconFactory.createDisabledIcon(removeIcon));
-
         remove.addActionListener(e -> {
             int selection = table.getSelectedRow();
             if (selection < 0) {
@@ -299,7 +290,7 @@ public class SQLTemplatePrefetchTab extends JPanel implements PropertyChangeList
     }
 
     protected TableModel createTableModel() {
-        return new PrefetchModel();
+        return new PrefetchModel(sqlTemplate.getPrefetchesMap(), sqlTemplate.getRoot());
     }
 
     public void addPrefetch(String prefetch) {
@@ -310,7 +301,7 @@ public class SQLTemplatePrefetchTab extends JPanel implements PropertyChangeList
         }
 
         //default value is joint
-        sqlTemplate.addPrefetch(prefetch, getPrefetchType(DISJOINT_BY_ID_PREFETCH_SEMANTICS));
+        sqlTemplate.addPrefetch(prefetch, PrefetchModel.getPrefetchType(SelectQueryPrefetchTab.DISJOINT_BY_ID_PREFETCH_SEMANTICS));
 
         // reset the model, since it is immutable
         table.setModel(createTableModel());
@@ -329,27 +320,6 @@ public class SQLTemplatePrefetchTab extends JPanel implements PropertyChangeList
         mediator.fireQueryEvent(new QueryEvent(this, sqlTemplate));
     }
 
-    boolean isToMany(String prefetch) {
-        if (sqlTemplate == null) {
-            return false;
-        }
-
-        Object root = sqlTemplate.getRoot();
-
-        // totally invalid path would result in ExpressionException
-        try {
-            Expression exp = ExpressionFactory.exp(prefetch);
-            Object object = exp.evaluate(root);
-            if (object instanceof Relationship) {
-                return ((Relationship) object).isToMany();
-            } else {
-                return false;
-            }
-        } catch (ExpressionException e) {
-            return false;
-        }
-    }
-
     /**
      * Updates split pane divider location in properties
      */
@@ -369,105 +339,4 @@ public class SQLTemplatePrefetchTab extends JPanel implements PropertyChangeList
         return SPLIT_DIVIDER_LOCATION_PROPERTY;
     }
 
-    protected int getPrefetchType(String semantics) {
-
-        //case 2: disjoint isn't use for SQLTemplate prefetch
-        switch (semantics){
-            case "Joint" :
-                return 1;
-            case "Disjoint by id":
-                return 3;
-            default: return 0;
-        }
-    }
-
-    protected String getPrefetchTypeString(int semantics) {
-        switch (semantics){
-            case 1 :
-                return JOINT_PREFETCH_SEMANTICS;
-            case 3:
-                return DISJOINT_BY_ID_PREFETCH_SEMANTICS;
-            default: return UNDEFINED_SEMANTICS;
-        }
-    }
-
-    /**
-     * A table model for the Prefetch table.
-     */
-    final class PrefetchModel extends AbstractTableModel {
-
-        String[] prefetches;
-
-        PrefetchModel() {
-            if (sqlTemplate != null) {
-                prefetches = sqlTemplate.getPrefetchesMap().keySet().toArray(new String[0]);
-            }
-        }
-
-        @Override
-        public int getColumnCount() {
-            return 3;
-        }
-
-        @Override
-        public int getRowCount() {
-            return (prefetches != null) ? prefetches.length : 0;
-        }
-
-        @Override
-        public Object getValueAt(int row, int column) {
-            switch (column) {
-                case 0:
-                    return prefetches[row];
-                case 1:
-                    return isToMany(prefetches[row]) ? Boolean.TRUE : Boolean.FALSE;
-                case 2:
-                    return getPrefetchTypeString(sqlTemplate.getPrefetchesMap().get(prefetches[row]));
-                default:
-                    throw new IndexOutOfBoundsException("Invalid column: " + column);
-            }
-        }
-
-        @Override
-        public Class getColumnClass(int column) {
-            switch (column) {
-                case 0:
-                    return String.class;
-                case 1:
-                    return Boolean.class;
-                case 2:
-                    return String.class;
-                default:
-                    throw new IndexOutOfBoundsException("Invalid column: " + column);
-            }
-        }
-
-        @Override
-        public String getColumnName(int column) {
-            switch (column) {
-                case 0:
-                    return "Prefetch Path";
-                case 1:
-                    return "To Many";
-                case 2:
-                    return "Prefetch Type";
-                default:
-                    throw new IndexOutOfBoundsException("Invalid column: " + column);
-            }
-        }
-
-        @Override
-        public boolean isCellEditable(int row, int column) {
-            return column == 2;
-        }
-
-        @Override
-        public void setValueAt(Object value, int row, int column) {
-            switch (column) {
-                case 2:
-                    sqlTemplate.addPrefetch(prefetches[row], getPrefetchType((String)value));
-                    break;
-            }
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/9c07e182/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/SelectQueryPrefetchTab.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/SelectQueryPrefetchTab.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/SelectQueryPrefetchTab.java
index 1c4ac54..82719d8 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/SelectQueryPrefetchTab.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/SelectQueryPrefetchTab.java
@@ -20,9 +20,6 @@
 package org.apache.cayenne.modeler.editor;
 
 import org.apache.cayenne.configuration.event.QueryEvent;
-import org.apache.cayenne.exp.Expression;
-import org.apache.cayenne.exp.ExpressionException;
-import org.apache.cayenne.exp.ExpressionFactory;
 import org.apache.cayenne.map.Attribute;
 import org.apache.cayenne.map.Entity;
 import org.apache.cayenne.map.Relationship;
@@ -42,7 +39,6 @@ import javax.swing.JButton;
 import javax.swing.JComboBox;
 import javax.swing.JComponent;
 import javax.swing.JToolBar;
-import javax.swing.table.AbstractTableModel;
 import javax.swing.table.DefaultTableCellRenderer;
 import javax.swing.table.TableColumn;
 import javax.swing.table.TableModel;
@@ -54,10 +50,10 @@ import javax.swing.tree.TreeModel;
  */
 public class SelectQueryPrefetchTab extends SelectQueryOrderingTab {
 
-    private static final String JOINT_PREFETCH_SEMANTICS = "Joint";
-    private static final String DISJOINT_PREFETCH_SEMANTICS = "Disjoint";
-    private static final String DISJOINT_BY_ID_PREFETCH_SEMANTICS = "Disjoint by id";
-    private static final String UNDEFINED_SEMANTICS = "Undefined semantics";
+    static final String JOINT_PREFETCH_SEMANTICS = "Joint";
+    static final String DISJOINT_PREFETCH_SEMANTICS = "Disjoint";
+    static final String DISJOINT_BY_ID_PREFETCH_SEMANTICS = "Disjoint by id";
+    static final String UNDEFINED_SEMANTICS = "Undefined semantics";
 
     public SelectQueryPrefetchTab(ProjectController mediator) {
         super(mediator);
@@ -110,7 +106,6 @@ public class SelectQueryPrefetchTab extends SelectQueryOrderingTab {
         Icon removeIcon = ModelerUtil.buildIcon("icon-trash.png");
         remove.setIcon(removeIcon);
         remove.setDisabledIcon(FilteredIconFactory.createDisabledIcon(removeIcon));
-        
         remove.addActionListener(e -> {
             int selection = table.getSelectedRow();
 
@@ -147,7 +142,7 @@ public class SelectQueryPrefetchTab extends SelectQueryOrderingTab {
     }
 
     protected TableModel createTableModel() {
-        return new PrefetchModel();
+        return new PrefetchModel(selectQuery.getPrefetchesMap(), selectQuery.getRoot());
     }
 
     public void addPrefetch(String prefetch) {
@@ -158,7 +153,7 @@ public class SelectQueryPrefetchTab extends SelectQueryOrderingTab {
         }
 
         //default value id disjoint
-        selectQuery.addPrefetch(prefetch, getPrefetchType(DISJOINT_PREFETCH_SEMANTICS));
+        selectQuery.addPrefetch(prefetch, PrefetchModel.getPrefetchType(DISJOINT_PREFETCH_SEMANTICS));
        
         // reset the model, since it is immutable
         table.setModel(createTableModel());
@@ -177,128 +172,4 @@ public class SelectQueryPrefetchTab extends SelectQueryOrderingTab {
         mediator.fireQueryEvent(new QueryEvent(this, selectQuery));
     }
 
-    boolean isToMany(String prefetch) {
-        if (selectQuery == null) {
-            return false;
-        }
-
-        Object root = selectQuery.getRoot();
-
-        // totally invalid path would result in ExpressionException
-        try {
-            Expression exp = ExpressionFactory.exp(prefetch);
-            Object object = exp.evaluate(root);
-            if (object instanceof Relationship) {
-                return ((Relationship) object).isToMany();
-            } else {
-                return false;
-            }
-        } catch (ExpressionException e) {
-            return false;
-        }
-    }
-
-    protected int getPrefetchType(String semantics) {
-        switch (semantics){
-            case "Joint" :
-                return 1;
-            case "Disjoint":
-                return 2;
-            case "Disjoint by id":
-                return 3;
-            default: return 0;
-        }
-    }
-
-    protected String getPrefetchTypeString(int semantics) {
-        switch (semantics){
-            case 1 :
-                return JOINT_PREFETCH_SEMANTICS;
-            case 2:
-                return DISJOINT_PREFETCH_SEMANTICS;
-            case 3:
-                return DISJOINT_BY_ID_PREFETCH_SEMANTICS;
-            default: return UNDEFINED_SEMANTICS;
-        }
-    }
-
-    /**
-     * A table model for the Prefetch table.
-     */
-    final class PrefetchModel extends AbstractTableModel {
-
-        String[] prefetches;
-
-        PrefetchModel() {
-            if (selectQuery != null) {
-                prefetches = selectQuery.getPrefetchesMap().keySet().toArray(new String[0]);
-            }
-        }
-
-        @Override
-        public int getColumnCount() {
-            return 3;
-        }
-
-        @Override
-        public int getRowCount() {
-            return (prefetches != null) ? prefetches.length : 0;
-        }
-
-        @Override
-        public Object getValueAt(int row, int column) {
-            switch (column) {
-                case 0:
-                    return prefetches[row];
-                case 1:
-                    return isToMany(prefetches[row]) ? Boolean.TRUE : Boolean.FALSE;
-                case 2:
-                    return getPrefetchTypeString(selectQuery.getPrefetchesMap().get(prefetches[row]));
-                default:
-                    throw new IndexOutOfBoundsException("Invalid column: " + column);
-            }
-        }
-
-        @Override
-        public Class getColumnClass(int column) {
-            switch (column) {
-                case 0:
-                    return String.class;
-                case 1:
-                    return Boolean.class;
-                case 2:
-                    return String.class;
-                default:
-                    throw new IndexOutOfBoundsException("Invalid column: " + column);
-            }
-        }
-
-        @Override
-        public String getColumnName(int column) {
-            switch (column) {
-                case 0:
-                    return "Prefetch Path";
-                case 1:
-                    return "To Many";
-                case 2:
-                    return "Prefetch Type";
-                default:
-                    throw new IndexOutOfBoundsException("Invalid column: " + column);
-            }
-        }
-
-        @Override
-        public boolean isCellEditable(int row, int column) {
-            return column == 2;
-        }
-
-        @Override
-        public void setValueAt(Object value, int row, int column) {
-            switch (column) {
-                case 2:
-                    selectQuery.addPrefetch(prefetches[row], getPrefetchType((String)value));
-                    break;
-            }
-        }
-    }
 }


Mime
View raw message