Merge branch 'develop' into refactor_remote_operation_to_read_folder
authormasensio <masensio@solidgear.es>
Wed, 27 Nov 2013 13:50:16 +0000 (14:50 +0100)
committermasensio <masensio@solidgear.es>
Wed, 27 Nov 2013 13:50:16 +0000 (14:50 +0100)
Conflicts:
oc_framework-test-project/src/com/owncloud/android/oc_framework_test_project/TestActivity.java

oc_framework-test-project/oc_framework-test-test/src/com/owncloud/android/oc_framework_test_project/test/ReadFileTest.java [new file with mode: 0644]
oc_framework-test-project/src/com/owncloud/android/oc_framework_test_project/TestActivity.java
oc_framework/.classpath
oc_framework/src/com/owncloud/android/oc_framework/operations/remote/ReadRemoteFileOperation.java [new file with mode: 0644]
src/com/owncloud/android/operations/SynchronizeFolderOperation.java

diff --git a/oc_framework-test-project/oc_framework-test-test/src/com/owncloud/android/oc_framework_test_project/test/ReadFileTest.java b/oc_framework-test-project/oc_framework-test-test/src/com/owncloud/android/oc_framework_test_project/test/ReadFileTest.java
new file mode 100644 (file)
index 0000000..465e82c
--- /dev/null
@@ -0,0 +1,48 @@
+package com.owncloud.android.oc_framework_test_project.test;
+
+
+import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
+import com.owncloud.android.oc_framework_test_project.TestActivity;
+
+import android.test.ActivityInstrumentationTestCase2;
+
+public class ReadFileTest extends      ActivityInstrumentationTestCase2<TestActivity> {
+       
+
+       /* Folder data to read. This folder must exist on the account */
+       private final String mRemoteFolderPath = "/folderToRead";
+       
+       /* File data to rename. This file must exist on the account */
+       private final String mRemoteFilePath = "/fileToRead.txt";
+       
+       private TestActivity mActivity;
+       
+       public ReadFileTest() {
+           super(TestActivity.class);
+       }
+       
+       @Override
+         protected void setUp() throws Exception {
+           super.setUp();
+           setActivityInitialTouchMode(false);
+           mActivity = getActivity();
+       }
+
+       /**
+        * Test Read Folder
+        */
+       public void testReadFolder() {
+
+               RemoteOperationResult result = mActivity.readFile(mRemoteFolderPath);
+               assertTrue(result.isSuccess());
+       }
+       
+       /**
+        * Test Read File
+        */
+       public void testReadFile() {
+
+               RemoteOperationResult result = mActivity.readFile(mRemoteFilePath);
+               assertTrue(result.isSuccess());
+       }
+}
index 12330a9..1d7b485 100644 (file)
@@ -4,6 +4,7 @@ import com.owncloud.android.oc_framework.network.webdav.OwnCloudClientFactory;
 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
 import com.owncloud.android.oc_framework.operations.remote.CreateRemoteFolderOperation;
+import com.owncloud.android.oc_framework.operations.remote.ReadRemoteFileOperation;
 import com.owncloud.android.oc_framework.operations.remote.RemoveRemoteFileOperation;
 import com.owncloud.android.oc_framework.operations.remote.RenameRemoteFileOperation;
 
@@ -90,5 +91,18 @@ public class TestActivity extends Activity {
                return result;
        }
        
+       /**
+        * Access to the library method to Read a File or Folder (PROPFIND DEPTH 1)
+        * @param remotePath
+        * 
+        * @return
+        */
+       public RemoteOperationResult readFile(String remotePath) {
+               
+               ReadRemoteFileOperation readOperation= new ReadRemoteFileOperation(remotePath);
+               RemoteOperationResult result = readOperation.execute(mClient);
+
+               return result;
+       }
        
 }
index 5176974..72e286b 100644 (file)
@@ -5,5 +5,6 @@
        <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
        <classpathentry kind="src" path="src"/>
        <classpathentry kind="src" path="gen"/>
+       <classpathentry exported="true" kind="lib" path="D:/solidgear/OwnCloud/android/libs/jackrabbit-webdav-2.2.5-jar-with-dependencies.jar"/>
        <classpathentry kind="output" path="bin/classes"/>
 </classpath>
diff --git a/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/ReadRemoteFileOperation.java b/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/ReadRemoteFileOperation.java
new file mode 100644 (file)
index 0000000..a73bad0
--- /dev/null
@@ -0,0 +1,97 @@
+package com.owncloud.android.oc_framework.operations.remote;
+
+import org.apache.http.HttpStatus;
+import org.apache.jackrabbit.webdav.DavConstants;
+import org.apache.jackrabbit.webdav.MultiStatus;
+import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
+
+import android.util.Log;
+
+import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
+import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
+import com.owncloud.android.oc_framework.operations.RemoteOperation;
+import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
+
+/**
+ * Remote operation performing the read of remote file or folder in the ownCloud server.
+ * 
+ * @author David A. Velasco
+ * @author masensio
+ */
+
+public class ReadRemoteFileOperation extends RemoteOperation {
+
+       private static final String TAG = ReadRemoteFileOperation.class.getSimpleName();
+
+       private String mRemotePath;
+       private MultiStatus mDataInServer;
+
+       public MultiStatus getDataInServer() {
+               return mDataInServer;
+       }
+       
+       
+       /**
+     * Constructor
+     * 
+     * @param remotePath               Remote path of the file. 
+     */
+       public ReadRemoteFileOperation(String remotePath) {
+               mRemotePath = remotePath;
+               mDataInServer = null;
+       }
+
+       /**
+     * Performs the read operation.
+     * 
+     * @param   client      Client object to communicate with the remote ownCloud server.
+     */
+       @Override
+       protected RemoteOperationResult run(WebdavClient client) {
+               RemoteOperationResult result = null;
+        PropFindMethod query = null;
+        
+        try {
+            // remote request 
+            query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath),
+                    DavConstants.PROPFIND_ALL_PROP,
+                    DavConstants.DEPTH_1);
+            int status = client.executeMethod(query);
+
+            // check and process response
+            if (isMultiStatus(status)) {
+               // get data from remote folder 
+               mDataInServer = query.getResponseBodyAsMultiStatus();
+               result = new RemoteOperationResult(true, status, query.getResponseHeaders());
+            } else {
+                // synchronization failed
+                client.exhaustResponse(query.getResponseBodyAsStream());
+                result = new RemoteOperationResult(false, status, query.getResponseHeaders());
+            }
+
+        } catch (Exception e) {
+            result = new RemoteOperationResult(e);
+            
+
+        } finally {
+            if (query != null)
+                query.releaseConnection();  // let the connection available for other methods
+            if (result.isSuccess()) {
+                Log.i(TAG, "Synchronized "  + mRemotePath + ": " + result.getLogMessage());
+            } else {
+                if (result.isException()) {
+                    Log.e(TAG, "Synchronized " + mRemotePath  + ": " + result.getLogMessage(), result.getException());
+                } else {
+                    Log.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage());
+                }
+            }
+            
+        }
+        return result;
+       }
+
+    public boolean isMultiStatus(int status) {
+        return (status == HttpStatus.SC_MULTI_STATUS); 
+    }
+       
+}
index 780fd42..450cc3e 100644 (file)
@@ -45,6 +45,7 @@ import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
 import com.owncloud.android.oc_framework.operations.RemoteOperation;
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
 import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
+import com.owncloud.android.oc_framework.operations.remote.ReadRemoteFileOperation;
 import com.owncloud.android.syncadapter.FileSyncService;
 import com.owncloud.android.utils.FileStorageUtils;
 import com.owncloud.android.utils.Log_OC;
@@ -241,59 +242,89 @@ public class SynchronizeFolderOperation extends RemoteOperation {
 
 
     private RemoteOperationResult fetchAndSyncRemoteFolder(WebdavClient client) {
-        RemoteOperationResult result = null;
-        String remotePath = null;
-        PropFindMethod query = null;
-        try {
-            remotePath = mLocalFolder.getRemotePath();
-            Log_OC.d(TAG, "Synchronizing " + mAccount.name + remotePath);
-
-            // remote request 
-            query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(remotePath),
-                    DavConstants.PROPFIND_ALL_PROP,
-                    DavConstants.DEPTH_1);
-            int status = client.executeMethod(query);
-
-            // check and process response
-            if (isMultiStatus(status)) {
-                synchronizeData(query.getResponseBodyAsMultiStatus(), client);
-                if (mConflictsFound > 0  || mFailsInFavouritesFound > 0) { 
-                    result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);   // should be different result, but will do the job
-                } else {
-                    result = new RemoteOperationResult(true, status, query.getResponseHeaders());
-                }
-                
-            } else {
-                // synchronization failed
-                client.exhaustResponse(query.getResponseBodyAsStream());
-                if (status == HttpStatus.SC_NOT_FOUND) {
-                    removeLocalFolder();
-                }
-                result = new RemoteOperationResult(false, status, query.getResponseHeaders());
-            }
-
-        } catch (Exception e) {
-            result = new RemoteOperationResult(e);
-            
-
-        } finally {
-            if (query != null)
-                query.releaseConnection();  // let the connection available for other methods
-            if (result.isSuccess()) {
-                Log_OC.i(TAG, "Synchronized " + mAccount.name + remotePath + ": " + result.getLogMessage());
-            } else {
-                if (result.isException()) {
-                    Log_OC.e(TAG, "Synchronized " + mAccount.name + remotePath  + ": " + result.getLogMessage(), result.getException());
-                } else {
-                    Log_OC.e(TAG, "Synchronized " + mAccount.name + remotePath + ": " + result.getLogMessage());
-                }
+        String remotePath = mLocalFolder.getRemotePath();
+        ReadRemoteFileOperation operation = new ReadRemoteFileOperation(remotePath);
+        RemoteOperationResult result = operation.execute(client);
+        Log_OC.d(TAG, "Synchronizing " + mAccount.name + remotePath);
+        
+        if (result.isSuccess()) {
+            MultiStatus dataInServer = ((ReadRemoteFileOperation) operation).getDataInServer();
+            synchronizeData(dataInServer, client);
+            if (mConflictsFound > 0  || mFailsInFavouritesFound > 0) { 
+                result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);   // should be different result, but will do the job
             }
-            
+        } else {
+            if (result.getCode() == ResultCode.FILE_NOT_FOUND)
+                removeLocalFolder();
         }
+        
+//        RemoteOperationResult result = null;
+//        String remotePath = null;
+//        PropFindMethod query = null;
+//        try {
+//            remotePath = mLocalFolder.getRemotePath();
+//            Log_OC.d(TAG, "Synchronizing " + mAccount.name + remotePath);
+//
+//            // remote request 
+//            query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(remotePath),
+//                    DavConstants.PROPFIND_ALL_PROP,
+//                    DavConstants.DEPTH_1);
+//            int status = client.executeMethod(query);
+//
+//            // check and process response
+//            if (isMultiStatus(status)) {
+//                synchronizeData(query.getResponseBodyAsMultiStatus(), client);
+//                if (mConflictsFound > 0  || mFailsInFavouritesFound > 0) { 
+//                    result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);   // should be different result, but will do the job
+//                } else {
+//                    result = new RemoteOperationResult(true, status, query.getResponseHeaders());
+//                }
+//                
+//            } else {
+//                // synchronization failed
+//                client.exhaustResponse(query.getResponseBodyAsStream());
+//                if (status == HttpStatus.SC_NOT_FOUND) {
+//                    removeLocalFolder();
+//                }
+//                result = new RemoteOperationResult(false, status, query.getResponseHeaders());
+//            }
+//
+//        } catch (Exception e) {
+//            result = new RemoteOperationResult(e);
+//            
+//
+//        } finally {
+//            if (query != null)
+//                query.releaseConnection();  // let the connection available for other methods
+//            if (result.isSuccess()) {
+//                Log_OC.i(TAG, "Synchronized " + mAccount.name + remotePath + ": " + result.getLogMessage());
+//            } else {
+//                if (result.isException()) {
+//                    Log_OC.e(TAG, "Synchronized " + mAccount.name + remotePath  + ": " + result.getLogMessage(), result.getException());
+//                } else {
+//                    Log_OC.e(TAG, "Synchronized " + mAccount.name + remotePath + ": " + result.getLogMessage());
+//                }
+//            }
+//            
+//        }
         return result;
     }
 
 
+//    @Override
+//    public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
+//        if (operation instanceof ReadRemoteFileOperation) {
+//            if (result.isSuccess()) {
+//                MultiStatus dataInServer = ((ReadRemoteFileOperation) operation).getDataInServer();
+//                synchronizeData(dataInServer, client)
+//            } else {
+//                
+//            }
+//                
+//        }
+//        
+//    }
+    
     private void removeLocalFolder() {
         if (mStorageManager.fileExists(mLocalFolder.getFileId())) {
             String currentSavePath = FileStorageUtils.getSavePath(mAccount.name);