Author: thomasobrien95
Date: Tue Jan  6 14:14:23 2009
New Revision: 2905

Modified:
   trunk/regress/ca/sqlpower/architect/TestSQLColumn.java
   trunk/regress/ca/sqlpower/architect/swingui/TestPlayPen.java
   trunk/src/ca/sqlpower/architect/ArchitectUtils.java
   trunk/src/ca/sqlpower/architect/SQLColumn.java
   trunk/src/ca/sqlpower/architect/swingui/PlayPen.java

Log:
Moved and modified the fix for d&d between sessions from Jonathan's comments.

The SQLColumn will now null out the source column if it is
from a different session.

There is currently a bug that columns dragged into the play
pen lose their source columns. A fix is in the works.

Modified: trunk/regress/ca/sqlpower/architect/TestSQLColumn.java
==============================================================================
--- trunk/regress/ca/sqlpower/architect/TestSQLColumn.java      (original)
+++ trunk/regress/ca/sqlpower/architect/TestSQLColumn.java Tue Jan 6 14:14:23 2009
@@ -23,6 +23,7 @@
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.sql.Types;
+import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashSet;
 import java.util.Map;
@@ -34,6 +35,8 @@

 import org.apache.commons.beanutils.BeanUtils;

+import ca.sqlpower.architect.swingui.ArchitectSwingSession;
+import ca.sqlpower.architect.swingui.TestingArchitectSwingSessionContext;
 import ca.sqlpower.sql.PlDotIni;
 import ca.sqlpower.sql.SPDataSource;
 import ca.sqlpower.sql.SPDataSourceType;
@@ -448,7 +451,7 @@
                assertEquals(cowCol, tmpCol.getSourceColumn());
                
                tmpCol = SQLColumn.getDerivedInstance(new SQLColumn(), 
table3pk);
-               assertNotNull(tmpCol.getSourceColumn());
+               assertNull(tmpCol.getSourceColumn());
        }

        /*
@@ -917,5 +920,47 @@

         assertTrue(pkcol.isAutoIncrement());
         assertFalse(normalcol.isAutoIncrement());
+    }
+
+    /**
+     * Test for bug in 0.9.13. If a column is dropped from one session
+     * to another the source will remain from the session it was dragged
+     * from. This can lead to problems with saving and loading.
+     */
+    public void testDnDAcrossSessionsRemovesSource() throws Exception {
+ TestingArchitectSwingSessionContext sourceContext = new TestingArchitectSwingSessionContext(); + final ArchitectSwingSession sourceSession = sourceContext.createSession(false);
+
+ final SQLTable sourceTable = new SQLTable(sourceSession.getTargetDatabase(), true); + SQLColumn sourceColumn = new SQLColumn(sourceTable, "Source column", Types.VARCHAR, 10, 0);
+        sourceTable.addColumn(sourceColumn);
+
+ TestingArchitectSwingSessionContext context = new TestingArchitectSwingSessionContext();
+        final ArchitectSwingSession session = context.createSession(false);
+
+        SQLTable table = new SQLTable(session.getTargetDatabase(), true);
+ SQLColumn column = SQLColumn.getDerivedInstance(sourceColumn, table);
+
+        assertNull(column.getSourceColumn());
+    }
+
+    /**
+     * Test for bug in 0.9.13. If a column is dropped from one database
+ * in a session to the play pen database the column should keep its lineage.
+     */
+    public void testDnDWithinSessionKeepsSource() throws Exception {
+ TestingArchitectSwingSessionContext context = new TestingArchitectSwingSessionContext();
+        final ArchitectSwingSession session = context.createSession(false);
+        SQLDatabase db = new SQLDatabase();
+        session.setSourceDatabaseList(Collections.singletonList(db));
+
+        final SQLTable sourceTable = new SQLTable(db, true);
+ SQLColumn sourceColumn = new SQLColumn(sourceTable, "Source column", Types.VARCHAR, 10, 0);
+        sourceTable.addColumn(sourceColumn);
+
+        SQLTable table = new SQLTable(session.getTargetDatabase(), true);
+ SQLColumn column = SQLColumn.getDerivedInstance(sourceColumn, table);
+
+        assertNull(column.getSourceColumn());
     }
 }

Modified: trunk/regress/ca/sqlpower/architect/swingui/TestPlayPen.java
==============================================================================
--- trunk/regress/ca/sqlpower/architect/swingui/TestPlayPen.java        
(original)
+++ trunk/regress/ca/sqlpower/architect/swingui/TestPlayPen.java Tue Jan 6 14:14:23 2009
@@ -401,35 +401,6 @@
                }
        }
        
-       /**
-        * Test for bug in 0.9.13. If a column is dropped from one session
-        * to another the source will remain from the session it was dragged
-        * from. This can lead to problems with saving and loading.
-        */
-       public void testDnDAcrossSessionsRemovesSource() throws Exception {
-           SQLTable table = new SQLTable(ppdb, true);
- SQLColumn column = new SQLColumn(table, "Test column", Types.VARCHAR, 10, 0);
-           table.addColumn(column);
-       
-           final SQLTable sourceTable = new SQLTable(ppdb, true);
- SQLColumn sourceColumn = new SQLColumn(sourceTable, "Source column", Types.VARCHAR, 10, 0);
-           sourceTable.addColumn(sourceColumn);
-       
-           column.setSourceColumn(sourceColumn);
-       
- TestingArchitectSwingSessionContext context = new TestingArchitectSwingSessionContext();
-        final ArchitectSwingSession session = context.createSession(false);
-           final PlayPen newPP = new PlayPen(session);
-       
-           newPP.importTableCopy(table, new Point(0, 0));
-
-           assertEquals(1, newPP.getTables().size());
-           assertEquals(1, newPP.getTables().get(0).getColumns().size());
- assertFalse(session.getSourceDatabases().getDatabaseList().contains(sourceTable.getParentDatabase()));
-           assertNull(newPP.getTables().get(0).getColumn(0).getSourceColumn());
-       
-       }
-
     /**
      * Returns a new value that is not equal to oldVal. The
      * returned object will be a new instance compatible with oldVal.

Modified: trunk/src/ca/sqlpower/architect/ArchitectUtils.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/ArchitectUtils.java (original)
+++ trunk/src/ca/sqlpower/architect/ArchitectUtils.java Tue Jan 6 14:14:23 2009
@@ -781,4 +781,23 @@
             return (f.canRead() && f.isFile());
         }
     }
+
+    /**
+     * This will check if the two objects given are contained in the same
+     * session by finding their top ancestors and comparing them. o1 and o2
+     * cannot be null.
+     */
+    public static boolean isInSameSession(SQLObject o1, SQLObject o2) {
+        SQLObject o1Parent = o1;
+        while (o1Parent.getParent() != null) {
+            o1Parent = o1Parent.getParent();
+        }
+        SQLObject o2Parent = o2;
+        while (o2Parent.getParent() != null) {
+            o2Parent = o2Parent.getParent();
+        }
+
+ logger.debug("Parent of " + o1 + " is " + o1Parent + ", parent of " + o2 + " is " + o2Parent);
+        return o1Parent == o2Parent;
+    }
 }

Modified: trunk/src/ca/sqlpower/architect/SQLColumn.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/SQLColumn.java      (original)
+++ trunk/src/ca/sqlpower/architect/SQLColumn.java      Tue Jan  6 14:14:23 2009
@@ -226,7 +226,8 @@
         * you get back will have a parent pointer of addTo.columnsFolder,
         * but will not be attached as a child (you will normally do that
         * right after calling this).  It will refer to source as its
-        * sourceColumn property, and otherwise be identical to source.
+        * sourceColumn property if the source is in the same session,
+        * and otherwise be identical to source.
         *
      * getDerivedInstance is used for reverse engineering.
      * Will not preserve listeners.
@@ -236,8 +237,12 @@
                logger.debug("derived instance SQLColumn constructor 
invocation.");
                SQLColumn c = new SQLColumn();
                copyProperties(c, source);
-               c.sourceColumn = source;
                c.parent = addTo.getColumnsFolder();
+               if (source != null && ArchitectUtils.isInSameSession(c, 
source)) {
+                   c.sourceColumn = source;
+               } else {
+                   c.sourceColumn = null;
+               }
                logger.debug("getDerivedInstance set ref count to 1");
                c.referenceCount = 1;
                return c;

Modified: trunk/src/ca/sqlpower/architect/swingui/PlayPen.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/PlayPen.java        (original)
+++ trunk/src/ca/sqlpower/architect/swingui/PlayPen.java Tue Jan 6 14:14:23 2009
@@ -1272,13 +1272,6 @@
         */
public synchronized TablePane importTableCopy(SQLTable source, Point preferredLocation) throws ArchitectException { SQLTable newTable = SQLTable.getDerivedInstance(source, session.getTargetDatabase()); // adds newTable to db
-               for (int i = 0; i < newTable.getColumns().size(); i++) {
-                   SQLColumn col = newTable.getColumn(i);
-                   if (col.getSourceColumn() != null
- && !session.getSourceDatabases().getDatabaseList().contains(col.getSourceColumn().getParentTable().getParentDatabase())) {
-                       col.setSourceColumn(null);
-                   }
-               }
                String key = source.getName().toLowerCase();
                boolean isAlreadyOnPlaypen = false;
                int newSuffix = 0;

Reply via email to