changeset 29:fc8f3ac948df

implement resolving the conflict
author dirk
date Sun, 18 Sep 2011 19:06:59 +0200
parents 0bed77e3608e
children 8e7f61e14e4d
files conflict-editor/src/main/java/de/codedo/conflicteditor/CouchDb.java conflict-editor/src/main/java/de/codedo/conflicteditor/HttpAccess.java conflict-editor/src/main/java/de/codedo/conflicteditor/UrlConnectionHttpAccess.java conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java
diffstat 5 files changed, 94 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/CouchDb.java	Sun Sep 18 11:28:20 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/CouchDb.java	Sun Sep 18 19:06:59 2011 +0200
@@ -48,6 +48,11 @@
         return new URL(path);
     }
 
+    public URL urlForDocument(String id) throws MalformedURLException
+    {
+        return new URL(appendPathToBaseUrl(id));
+    }
+
     private String appendPathToBaseUrl(String path)
     {
         if (path.startsWith("/"))
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/HttpAccess.java	Sun Sep 18 11:28:20 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/HttpAccess.java	Sun Sep 18 19:06:59 2011 +0200
@@ -6,7 +6,11 @@
 
 public interface HttpAccess
 {
+    Reader delete() throws IOException;
+
+    Reader get() throws IOException;
+
     Reader post(String content) throws IOException;
 
-    Reader get() throws IOException;
+    Reader put(String content) throws IOException;
 }
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/UrlConnectionHttpAccess.java	Sun Sep 18 11:28:20 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/UrlConnectionHttpAccess.java	Sun Sep 18 19:06:59 2011 +0200
@@ -14,10 +14,10 @@
 public class UrlConnectionHttpAccess extends Object implements HttpAccess
 {
     private static final String HEADER_CONTENT_LENGTH = "Content-Length";
-
     private static final String HEADER_CONTENT_TYPE = "Content-Type";
-
+    private static final String HTTP_METHOD_DELETE = "DELETE";
     private static final String HTTP_METHOD_POST = "POST";
+    private static final String HTTP_METHOD_PUT = "PUT";
 
     private URL _url;
 
@@ -30,18 +30,40 @@
     @Override
     public Reader post(String content) throws IOException
     {
-        HttpURLConnection connection = createUrlConnection(content);
+        HttpURLConnection connection = createUrlConnection(HTTP_METHOD_POST, content);
+        send(connection, content);
+        return createReader(connection);
+    }
+
+    @Override
+    public Reader put(String content) throws IOException
+    {
+        HttpURLConnection connection = createUrlConnection(HTTP_METHOD_PUT, content);
         send(connection, content);
         return createReader(connection);
     }
 
-    private HttpURLConnection createUrlConnection(String content) throws IOException, ProtocolException
+    @Override
+    public Reader delete() throws IOException
+    {
+        HttpURLConnection connection = createUrlConnection(HTTP_METHOD_DELETE);
+        connection.connect();
+        return createReader(connection);
+    }
+
+    private HttpURLConnection createUrlConnection(String method, String content) throws IOException, ProtocolException
+    {
+        HttpURLConnection connection = createUrlConnection(method);
+        connection.setDoInput(true);
+        connection.setRequestProperty(HEADER_CONTENT_TYPE, "application/json");
+        connection.setRequestProperty(HEADER_CONTENT_LENGTH, Integer.toString(content.length()));
+        return connection;
+    }
+
+    private HttpURLConnection createUrlConnection(String method) throws IOException
     {
         HttpURLConnection connection = (HttpURLConnection)_url.openConnection();
-        connection.setRequestMethod(HTTP_METHOD_POST);
-        connection.setRequestProperty(HEADER_CONTENT_TYPE, "application/json");
-        connection.setRequestProperty(HEADER_CONTENT_LENGTH, Integer.toString(content.length()));
-        connection.setDoInput(true);
+        connection.setRequestMethod(method);
         connection.setDoOutput(true);
         return connection;
     }
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java	Sun Sep 18 11:28:20 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java	Sun Sep 18 19:06:59 2011 +0200
@@ -53,6 +53,7 @@
         connectCurrentValueButton();
         connectConflictValueButton();
         connectPreviewButton();
+        connectSaveButton();
     }
 
     private void connectFindConflictsButton()
@@ -141,6 +142,20 @@
         _frame.getPreviewButton().addActionListener(listener);
     }
 
+    private void connectSaveButton()
+    {
+        Executable<ActionEvent> executable = new Executable<ActionEvent>()
+        {
+            @Override
+            public void execute(ActionEvent event) throws Exception
+            {
+                save();
+            }
+        };
+        ExceptionHandlingActionListener listener = new ExceptionHandlingActionListener(executable);
+        _frame.getSaveButton().addActionListener(listener);
+    }
+
     protected void findConflicts() throws IOException
     {
         String dbUrl = _frame.getDatabaseUrlTextField().getText();
@@ -202,4 +217,9 @@
         preview.pack();
         preview.setVisible(true);
     }
+
+    protected void save() throws Exception
+    {
+        _controller.save();
+    }
 }
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java	Sun Sep 18 11:28:20 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java	Sun Sep 18 19:06:59 2011 +0200
@@ -22,6 +22,7 @@
     private CouchDb _database;
     private ObjectNode _currentConflict;
     private Difference _currentDifference;
+    private String _conflictRevision;
 
     public JsonNode findConflicts(String dbUrl) throws IOException
     {
@@ -78,6 +79,13 @@
     public void setCurrentConflict(JsonNode conflictNode) throws Exception
     {
         JsonNode node = conflictNode.get("key");
+        JsonNode conflicts = node.get("_conflicts");
+        if (conflicts.size() > 1)
+        {
+            throw new UnsupportedOperationException("there is more than one conflict");
+        }
+        _conflictRevision = conflicts.get(0).getValueAsText();
+
         _currentConflict = deepCopyWithoutConflict(node);
     }
 
@@ -94,4 +102,30 @@
     {
         _currentDifference = difference;
     }
+
+    public void save() throws Exception
+    {
+        updateCurrentVersion();
+        deleteConflictVersion();
+    }
+
+    private void updateCurrentVersion() throws Exception
+    {
+        String id = _currentConflict.get("_id").getValueAsText();
+        URL updateUrl = _database.urlForDocument(id);
+        Reader reader = new UrlConnectionHttpAccess(updateUrl).put(_currentConflict.toString());
+
+        // TODO take care of the return value
+        System.out.println(new ObjectMapper().readTree(reader));
+    }
+
+    private void deleteConflictVersion() throws Exception
+    {
+        String id = _currentConflict.get("_id").getValueAsText();
+        URL deleteUrl = _database.urlForDocumentAndRevision(id, _conflictRevision);
+        Reader reader = new UrlConnectionHttpAccess(deleteUrl).delete();
+
+        // TODO take care of the return value
+        System.out.println(new ObjectMapper().readTree(reader));
+    }
 }