Revision: 3908
Author: [email protected]
Date: Fri Aug 20 11:26:13 2010
Log: NEW - bug 2896: Import function
http://trillian.sqlpower.ca/bugzilla/show_bug.cgi?id=2896

Added the ability to upload a project file to the server.
http://code.google.com/p/power-architect/source/detail?r=3908

Modified:
/trunk/src/main/java/ca/sqlpower/architect/enterprise/ArchitectClientSideSession.java /trunk/src/main/java/ca/sqlpower/architect/enterprise/JSONResponseHandler.java
 /trunk/src/main/java/ca/sqlpower/architect/swingui/ArchitectFrame.java
/trunk/src/main/java/ca/sqlpower/architect/swingui/ArchitectSwingSessionImpl.java /trunk/src/main/java/ca/sqlpower/architect/swingui/action/OpenProjectAction.java /trunk/src/main/java/ca/sqlpower/architect/swingui/enterprise/ServerProjectsManagerPanel.java

=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/enterprise/ArchitectClientSideSession.java Thu Aug 19 11:02:29 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/enterprise/ArchitectClientSideSession.java Fri Aug 20 11:26:13 2010
@@ -2,10 +2,8 @@

 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.security.MessageDigest;
@@ -41,6 +39,10 @@
 import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.entity.FileEntity;
 import org.apache.http.entity.StringEntity;
+import org.apache.http.entity.mime.MultipartEntity;
+import org.apache.http.entity.mime.content.ContentBody;
+import org.apache.http.entity.mime.content.FileBody;
+import org.apache.http.entity.mime.content.StringBody;
 import org.apache.http.impl.client.BasicCookieStore;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.message.BasicNameValuePair;
@@ -579,21 +581,18 @@
        }
     }

- public static ProjectLocation uploadProject(SPServerInfo serviceInfo, String name, InputStream project, ArchitectSession session) + public static ProjectLocation uploadProject(SPServerInfo serviceInfo, String name, File project, ArchitectSession session) throws URISyntaxException, ClientProtocolException, IOException, JSONException {
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        SQLPowerUtils.copyStream(project, out);
-        String data = out.toString();
-
         HttpClient httpClient = createHttpClient(serviceInfo);
         try {
- List<NameValuePair> properties = new ArrayList<NameValuePair>();
-            properties.add(new BasicNameValuePair("name", name));
-            properties.add(new BasicNameValuePair("file", data));
-            properties.add(new BasicNameValuePair("randomUUID", "true"));
+            MultipartEntity entity = new MultipartEntity();
+            ContentBody fileBody = new FileBody(project);
+            ContentBody nameBody = new StringBody(name);
+            entity.addPart("file", fileBody);
+            entity.addPart("name", nameBody);

HttpPost request = new HttpPost(getServerURI(serviceInfo, "/" + REST_TAG + "/jcr", "name=" + name));
-            request.setEntity(new UrlEncodedFormEntity(properties));
+            request.setEntity(entity);
JSONMessage message = httpClient.execute(request, new JSONResponseHandler());
             JSONObject response = new JSONObject(message.getBody());
             return new ProjectLocation(
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/enterprise/JSONResponseHandler.java Wed Jul 28 08:41:01 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/enterprise/JSONResponseHandler.java Fri Aug 20 11:26:13 2010
@@ -63,10 +63,7 @@
     }

     public JSONMessage handleResponse(String json, int status) {
-        if (status == 500) {
-            logger.error("Internal server error: " + json);
- throw new RuntimeException("Internal Server Error. See logs for more details.");
-        } else if (status == 404) {
+        if (status == 404) {
throw new RuntimeException("Server resource is not available.");
         }

@@ -101,7 +98,7 @@
                 }
             }
         } catch (Exception ex) {
-            throw new RuntimeException(ex);
+ throw new RuntimeException("Server returned status " + status + "\n" + json, ex);
         }
     }
 }
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/ArchitectFrame.java Tue Aug 17 15:54:26 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/ArchitectFrame.java Fri Aug 20 11:26:13 2010
@@ -912,7 +912,7 @@
             public void loadFile(String fileName) throws IOException {
                 File f = new File(fileName);
                 try {
- OpenProjectAction.getFileLoader().openAsynchronously(context.createSession(), f, currentSession); + OpenProjectAction.getFileLoader().open(context.createSession(), f, currentSession, false);
                 } catch (SQLObjectException ex) {
SPSUtils.showExceptionDialogNoReport(ArchitectFrame.this, Messages.getString("ArchitectSwingSessionImpl.openProjectFileFailed"), ex); //$NON-NLS-1$
                 }
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/ArchitectSwingSessionImpl.java Thu Aug 19 11:10:37 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/ArchitectSwingSessionImpl.java Fri Aug 20 11:26:13 2010
@@ -341,7 +341,7 @@
             public void loadFile(String fileName) throws IOException {
                 File f = new File(fileName);
                 try {
- OpenProjectAction.getFileLoader().openAsynchronously(getContext().createSession(), f, ArchitectSwingSessionImpl.this); + OpenProjectAction.getFileLoader().open(getContext().createSession(), f, ArchitectSwingSessionImpl.this, true);
                 } catch (SQLObjectException ex) {
SPSUtils.showExceptionDialogNoReport(getArchitectFrame(), Messages.getString("ArchitectSwingSessionImpl.openProjectFileFailed"), ex); //$NON-NLS-1$
                 }
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/action/OpenProjectAction.java Thu Aug 12 08:27:21 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/action/OpenProjectAction.java Fri Aug 20 11:26:13 2010
@@ -51,7 +51,8 @@
private static final Logger logger = Logger.getLogger(OpenProjectAction.class);

     public static interface FileLoader {
- public void openAsynchronously(ArchitectSwingSession newSession, File f, ArchitectSwingSession openingSession); + public void open(ArchitectSwingSession newSession, File f, ArchitectSwingSession openingSession, boolean separateThread); + public void open(ArchitectSwingSession newSession, InputStream in, ArchitectSwingSession openingSession, boolean separateThread);
     }

     /**
@@ -76,15 +77,32 @@
      *            called once the project is finished loading.
      */
     private static FileLoader fileLoader = new FileLoader() {
- public void openAsynchronously(ArchitectSwingSession newSession, File f, ArchitectSwingSession openingSession) { + public void open(ArchitectSwingSession newSession, File f, ArchitectSwingSession openingSession, boolean separateThread) {
             LoadFileWorker worker;
             try {
                 worker = new LoadFileWorker(f, newSession, openingSession);
-                new Thread(worker).start();
+                if (separateThread) {
+                    new Thread(worker).start();
+                } else {
+                    worker.run();
+                }
             } catch (Exception e1) {
ASUtils.showExceptionDialogNoReport(Messages.getString("OpenProjectAction.errorLoadingFile"), e1); //$NON-NLS-1$
             }
-
+        }
+
+ public void open(ArchitectSwingSession newSession, InputStream in, ArchitectSwingSession openingSession, boolean separateThread) {
+            LoadFileWorker worker;
+            try {
+ worker = new LoadFileWorker(in, newSession, openingSession);
+                if (separateThread) {
+                    new Thread(worker).start();
+                } else {
+                    worker.run();
+                }
+            } catch (Exception e1) {
+ ASUtils.showExceptionDialogNoReport(Messages.getString("OpenProjectAction.errorLoadingFile"), e1); //$NON-NLS-1$
+            }
         }
     };

@@ -117,7 +135,7 @@
             f = new File(e.getActionCommand().substring("file:".length()));
         }
         try {
- fileLoader.openAsynchronously(getSession().getContext().createSession(), f, getSession()); + fileLoader.open(getSession().getContext().createSession(), f, getSession(), true);
         } catch (SQLObjectException ex) {
SPSUtils.showExceptionDialogNoReport(getSession().getArchitectFrame(), Messages.getString("OpenProjectAction.failedToOpenProjectFile"), ex); //$NON-NLS-1$
@@ -182,6 +200,18 @@
in = new BufferedInputStream(new ProgressMonitorInputStream(openingSession.getArchitectFrame(), Messages.getString("OpenProjectAction.reading") + file.getName(), new FileInputStream(file))); //$NON-NLS-1$
         }
+
+ public LoadFileWorker(InputStream in, ArchitectSwingSession newSession, ArchitectSwingSession openingSession) {
+            super(newSession);
+            this.context = newSession.getContext();
+            file = null;
+            this.recent = newSession.getRecentMenu();
+            this.openingSession = openingSession;
+
+            this.session = newSession;
+
+            this.in = in;
+        }

         @Override
         public void doStuff() throws Exception {
@@ -212,9 +242,13 @@
                     session.close();
                 }
             } else {
-                recent.putRecentFileName(file.getAbsolutePath());
-                openingSession.getArchitectFrame().addSession(session);
- openingSession.getArchitectFrame().setCurrentSession(session);
+                if (file != null) {
+                    recent.putRecentFileName(file.getAbsolutePath());
+                }
+                if (openingSession != null) {
+                    openingSession.getArchitectFrame().addSession(session);
+ openingSession.getArchitectFrame().setCurrentSession(session);
+                }
((DBTreeModel) session.getDBTree().getModel()).refreshTreeStructure();
             }

=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/enterprise/ServerProjectsManagerPanel.java Thu Aug 19 11:02:29 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/enterprise/ServerProjectsManagerPanel.java Fri Aug 20 11:26:13 2010
@@ -27,7 +27,6 @@
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.io.File;
-import java.io.FileInputStream;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
@@ -234,7 +233,7 @@
                         return;
                     }
                     try {
- ArchitectClientSideSession.uploadProject(getSelectedServerInfo(), nameField.getText(), new FileInputStream(f), session); + ArchitectClientSideSession.uploadProject(getSelectedServerInfo(), nameField.getText(), f, session);
                         dialog.dispose();
                         refreshInfoList();
                     } catch (Exception ex) {

Reply via email to