Revision: 3472
Author: silva.josemanuel1
Date: Fri Apr 23 14:24:33 2010
Log: Projects will now close when they have been deleted from the workspace. The user has the option of saving the project before it is closed.

Related system changes: JSONMessage now takes a status code instead of a success boolean, and UpdateListener now has a workspaceDeleted method.
http://code.google.com/p/power-architect/source/detail?r=3472

Modified:
 /trunk/src/main/java/ca/sqlpower/architect/enterprise/JSONMessage.java
/trunk/src/main/java/ca/sqlpower/architect/enterprise/JSONResponseHandler.java /trunk/src/main/java/ca/sqlpower/architect/enterprise/NetworkConflictResolver.java /trunk/src/main/java/ca/sqlpower/architect/swingui/ArchitectSwingSessionContextImpl.java /trunk/src/main/java/ca/sqlpower/architect/swingui/ArchitectSwingSessionImpl.java
 /trunk/src/main/java/ca/sqlpower/architect/swingui/PlayPenComponent.java
/trunk/src/main/java/ca/sqlpower/architect/swingui/enterprise/ServerProjectsManagerPanel.java

=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/enterprise/JSONMessage.java Fri Apr 23 13:58:12 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/enterprise/JSONMessage.java Fri Apr 23 14:24:33 2010
@@ -25,40 +25,22 @@
  */
 public class JSONMessage {
     private final String message;
-    private final boolean successful;

     /**
- * The status code of the response if it had one. If the response is just
-     * text this may be null.
+     * The status code of the response.
      */
-    private final Integer statusCode;
+    private final int statusCode;

     /**
      * @param message
* The body of the response from the server. This may be but is
      *            not limited to: an error message, exception, or JSON of
      *            persist calls.
-     * @param successful
-     *            True if the response was successful, false otherwise.
-     */
-    public JSONMessage(String message, boolean successful) {
-        this(message, successful, null);
-    }
-
-    /**
-     * @param message
- * The body of the response from the server. This may be but is
-     *            not limited to: an error message, exception, or JSON of
-     *            persist calls.
-     * @param successful
-     *            True if the response was successful, false otherwise.
      * @param statusCode
- * The code that was returned in the response if the response
-     *            contained one.
+     *            The code that was returned in the response.
      */
- public JSONMessage(String message, boolean successful, Integer statusCode) {
+    public JSONMessage(String message, int statusCode) {
         this.message = message;
-        this.successful = successful;
         this.statusCode = statusCode;
     }

@@ -66,11 +48,14 @@
         return message;
     }

-    public boolean isSuccessful() {
-        return successful;
-    }
-
-    public Integer getStatusCode() {
+    public int getStatusCode() {
         return statusCode;
     }
-}
+
+    /**
+     * Returns true if the status code is in the 200s.
+     */
+    public boolean isSuccessful() {
+        return (statusCode >= 200 && statusCode < 300);
+    }
+}
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/enterprise/JSONResponseHandler.java Fri Apr 23 13:58:12 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/enterprise/JSONResponseHandler.java Fri Apr 23 14:24:33 2010
@@ -38,7 +38,8 @@
     public JSONMessage handleResponse(HttpResponse response) {
         try {

-            if (response.getStatusLine().getStatusCode() == 401) {
+            int status = response.getStatusLine().getStatusCode();
+            if (status == 401) {
                 throw new AccessDeniedException("Access Denied");
             }

@@ -50,13 +51,7 @@
             while ((line = reader.readLine()) != null) {
                 buffer.append(line).append("\n");
             }
-
-            if (response.getStatusLine().getStatusCode() == 412) {
-                JSONObject message = new JSONObject(buffer.toString());
- return new JSONMessage(message.getString("data"), false, 412);
-            }
-
-            return handleResponse(buffer.toString());
+            return handleResponse(buffer.toString(), status);
         } catch (AccessDeniedException e) {
             throw e;
         } catch (Exception ex) {
@@ -64,18 +59,18 @@
         }
     }

-    public JSONMessage handleResponse(String json) {
+    public JSONMessage handleResponse(String json, int status) {
         try {
             JSONObject message = new JSONObject(json);

// Does the response contain data? If so, return it. Communication
             // with the resource has been successful.
             if (message.getString("responseKind").equals("data")) {
-                return new JSONMessage(message.getString("data"), true);
+                return new JSONMessage(message.getString("data"), status);
             } else {
                 // Has the request been unsuccessful?
if (message.getString("responseKind").equals("unsuccessful")) { - return new JSONMessage(message.getString("data"), false); + return new JSONMessage(message.getString("data"), status);
                 } else {
// Does the response contain an exception? If so, reconstruct, and then // re-throw it. There has been an exception on the server.
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/enterprise/NetworkConflictResolver.java Fri Apr 23 13:58:12 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/enterprise/NetworkConflictResolver.java Fri Apr 23 14:24:33 2010
@@ -196,7 +196,7 @@

//If the preconditions failed which caused the persist to fail don't try to
                 //push the persist forward again.
- if (!response.isSuccessful() && new Integer(412).equals(response.getStatusCode())) { + if (!response.isSuccessful() && response.getStatusCode() == 412) {
                     logger.info("Friendly error occurred, " + response);
                     if (promptSession != null) {
                         promptSession.createUserPrompter(
@@ -282,14 +282,22 @@
                        synchronized (this) {
                            wait();
                        }
-                   }
-
-                   updating = true;
-
- // Request an update from the server using the current revision number.
+                   }
+                   updating = true;
+ // Request an update from the server using the current revision number.
                    JSONMessage message = getJsonArray(inboundHttpClient);
- // The updater may have been interrupted/closed while waiting for an update.
+
+                   // Status 410 (Gone) means the workspace was deleted
+                   if (message.getStatusCode() == 410) {
+                       for (UpdateListener listener : updateListeners) {
+                           listener.workspaceDeleted();
+                       }
+                       updateListeners.clear();
+                       interrupt();
+                   }
+ // The updater may have been interrupted/closed/deleted while waiting for an update.
                    if (this.isInterrupted() || cancelled) break;
+
final JSONObject json = new JSONObject(message.getBody());
                    session.runInForeground(new Runnable() {
                        public void run() {
@@ -539,6 +547,13 @@
          */
         public boolean updatePerformed(NetworkConflictResolver resolver);
         public boolean updateException(NetworkConflictResolver resolver);
+
+        /**
+         * Notifies listeners that the workspace was deleted.
+ * Swing sessions should listen for this to disable the enterprise session.
+         * The listener is removed after this method is called.
+         */
+        public void workspaceDeleted();

         /**
          * Called just before an update will be performed by the
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/ArchitectSwingSessionContextImpl.java Wed Apr 21 09:00:48 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/ArchitectSwingSessionContextImpl.java Fri Apr 23 14:24:33 2010
@@ -34,6 +34,8 @@
 import java.util.prefs.Preferences;

 import javax.swing.ImageIcon;
+import javax.swing.JOptionPane;
+import javax.swing.SwingUtilities;

 import org.apache.log4j.Logger;
 import org.springframework.security.AccessDeniedException;
@@ -230,21 +232,59 @@

clientSession.getUpdater().addListener(new NetworkConflictResolver.UpdateListener() {

+            boolean loading = true;
+
public void preUpdatePerformed(NetworkConflictResolver resolver) {
-                swingSession.getUndoManager().setLoading(true);
+                if (loading) {
+                    swingSession.getUndoManager().setLoading(true);
+                }
             }

public boolean updatePerformed(NetworkConflictResolver resolver) { //On the first update from the server we must discard all edits //as it is equivalent to loading from a file and undoing does not
                 //make sense.
-                swingSession.getUndoManager().setLoading(false);
-                return true;
+                if (loading) {
+                    swingSession.getUndoManager().setLoading(false);
+                    loading = false;
+                }
+                return false;
             }

- public boolean updateException(NetworkConflictResolver resolver) {
-                swingSession.getUndoManager().setLoading(false);
-                return true;
+ public boolean updateException(NetworkConflictResolver resolver) {
+                if (loading) {
+                    swingSession.getUndoManager().setLoading(false);
+                    loading = false;
+                }
+                return false;
+            }
+
+            public void workspaceDeleted() {
+ int response = JOptionPane.showConfirmDialog(swingSession.getArchitectFrame(),
+                        "This project has been deleted from the server. " +
+ "\nWould you like to save it to a file before it closes?",
+                        "Workspace deleted...", JOptionPane.YES_NO_OPTION);
+                if (response == JOptionPane.YES_OPTION) {
+                    swingSession.saveOrSaveAs(true, false);
+                }
+                exitAfterAllSessionsClosed = false;
+                swingSession.close();
+                exitAfterAllSessionsClosed = true;
+                if (getSessions().size() == 0) {
+                    try {
+                        SwingUtilities.invokeAndWait(new Runnable() {
+                            public void run() {
+                                try {
+                                    createSession();
+                                } catch (Exception e) {
+ throw new RuntimeException("Error occurred when trying to open new project", e);
+                                }
+                            }
+                        });
+                    }  catch (Exception e) {
+ throw new RuntimeException("Error occurred when trying to open new project", e);
+                    }
+                }
             }
         });

@@ -290,6 +330,10 @@
public void preUpdatePerformed(NetworkConflictResolver resolver) {
                         //do nothing
                     }
+
+                    public void workspaceDeleted() {
+                        // do nothing
+                    }
                 });

                 newSecuritySession.startUpdaterThread();
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/ArchitectSwingSessionImpl.java Tue Apr 20 15:12:12 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/ArchitectSwingSessionImpl.java Fri Apr 23 14:24:33 2010
@@ -1196,6 +1196,10 @@
public void preUpdatePerformed(NetworkConflictResolver resolver) {
                         //do nothing
                     }
+
+                    public void workspaceDeleted() {
+                        // do nothing
+                    }
                 });

((ArchitectClientSideSession) ((ArchitectSwingSessionImpl) newSession).getDelegateSession()).startUpdaterThread();
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/PlayPenComponent.java Fri Apr 9 11:56:35 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/PlayPenComponent.java Fri Apr 23 14:24:33 2010
@@ -104,6 +104,10 @@
         public void preUpdatePerformed(NetworkConflictResolver resolver) {
             //do nothing
         }
+
+        public void workspaceDeleted() {
+            // do nothing
+        }
     };

     protected PlayPenComponent(String name) {
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/enterprise/ServerProjectsManagerPanel.java Tue Apr 6 14:37:05 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/enterprise/ServerProjectsManagerPanel.java Fri Apr 23 14:24:33 2010
@@ -149,6 +149,10 @@
public void preUpdatePerformed(NetworkConflictResolver resolver) {
                                         //do nothing
                                     }
+
+                                    public void workspaceDeleted() {
+                                        refreshInfoList();
+                                    }
                                 });

((ArchitectClientSideSession) ((ArchitectSwingSessionImpl) newSession).getDelegateSession()).startUpdaterThread();


--
Subscription settings: 
http://groups.google.com/group/architect-commits/subscribe?hl=en

Reply via email to