Merge pull request #740 from owncloud/revert-669-master
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / SynchronizeFileOperation.java
index 2b94885..45a7305 100644 (file)
@@ -1,6 +1,6 @@
 /* ownCloud Android client application
  *   Copyright (C) 2012 Bartek Przybylski
 /* ownCloud Android client application
  *   Copyright (C) 2012 Bartek Przybylski
- *   Copyright (C) 2012-2013 ownCloud Inc.
+ *   Copyright (C) 2012-2014 ownCloud Inc.
  *
  *   This program is free software: you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License version 2,
  *
  *   This program is free software: you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License version 2,
 
 package com.owncloud.android.operations;
 
 
 package com.owncloud.android.operations;
 
-import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.files.services.FileUploader;
-import com.owncloud.android.lib.network.OwnCloudClient;
-import com.owncloud.android.lib.operations.common.RemoteFile;
-import com.owncloud.android.lib.operations.common.RemoteOperation;
-import com.owncloud.android.lib.operations.common.RemoteOperationResult;
-import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode;
-import com.owncloud.android.lib.operations.remote.ReadRemoteFileOperation;
+import com.owncloud.android.lib.common.OwnCloudClient;
+import com.owncloud.android.lib.resources.files.RemoteFile;
+import com.owncloud.android.lib.common.operations.RemoteOperationResult;
+import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
+import com.owncloud.android.lib.common.utils.Log_OC;
+import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation;
+import com.owncloud.android.operations.common.SyncOperation;
 import com.owncloud.android.utils.FileStorageUtils;
 import com.owncloud.android.utils.FileStorageUtils;
-import com.owncloud.android.utils.Log_OC;
 
 import android.accounts.Account;
 import android.content.Context;
 
 import android.accounts.Account;
 import android.content.Context;
@@ -42,41 +41,87 @@ import android.content.Intent;
  * @author masensio
  */
 
  * @author masensio
  */
 
-public class SynchronizeFileOperation extends RemoteOperation {
+public class SynchronizeFileOperation extends SyncOperation {
 
     private String TAG = SynchronizeFileOperation.class.getSimpleName();
     
     private OCFile mLocalFile;
 
     private String TAG = SynchronizeFileOperation.class.getSimpleName();
     
     private OCFile mLocalFile;
+    private String mRemotePath;
     private OCFile mServerFile;
     private OCFile mServerFile;
-    private FileDataStorageManager mStorageManager;
     private Account mAccount;
     private boolean mSyncFileContents;
     private Context mContext;
     
     private boolean mTransferWasRequested = false;
     private Account mAccount;
     private boolean mSyncFileContents;
     private Context mContext;
     
     private boolean mTransferWasRequested = false;
+
     
     
+    /**
+     * Constructor.
+     * 
+     * Uses remotePath to retrieve all the data in local cache and remote server when the operation
+     * is executed, instead of reusing {@link OCFile} instances.
+     * 
+     * @param 
+     * @param account               ownCloud account holding the file.
+     * @param syncFileContents      When 'true', transference of data will be started by the 
+     *                              operation if needed and no conflict is detected.
+     * @param context               Android context; needed to start transfers.
+     */
+    public SynchronizeFileOperation(
+            String remotePath,  
+            Account account, 
+            boolean syncFileContents,
+            Context context) {
+        
+        mRemotePath = remotePath;
+        mLocalFile = null;
+        mServerFile = null;
+        mAccount = account;
+        mSyncFileContents = syncFileContents;
+        mContext = context;
+    }
+
+    
+    /**
+     * Constructor allowing to reuse {@link OCFile} instances just queried from cache or network.
+     * 
+     * Useful for folder / account synchronizations.
+     * 
+     * @param localFile             Data of file currently hold in device cache. MUSTN't be null.
+     * @param serverFile            Data of file just retrieved from network. If null, will be
+     *                              retrieved from network by the operation when executed.
+     * @param account               ownCloud account holding the file.
+     * @param syncFileContents      When 'true', transference of data will be started by the 
+     *                              operation if needed and no conflict is detected.
+     * @param context               Android context; needed to start transfers.
+     */
     public SynchronizeFileOperation(
             OCFile localFile,
     public SynchronizeFileOperation(
             OCFile localFile,
-            OCFile serverFile,          // make this null to let the operation checks the server; added to reuse info from SynchronizeFolderOperation 
-            FileDataStorageManager storageManager, 
+            OCFile serverFile, 
             Account account, 
             boolean syncFileContents,
             Context context) {
         
         mLocalFile = localFile;
         mServerFile = serverFile;
             Account account, 
             boolean syncFileContents,
             Context context) {
         
         mLocalFile = localFile;
         mServerFile = serverFile;
-        mStorageManager = storageManager;
+        mRemotePath = localFile.getRemotePath();
         mAccount = account;
         mSyncFileContents = syncFileContents;
         mContext = context;
     }
         mAccount = account;
         mSyncFileContents = syncFileContents;
         mContext = context;
     }
-
+    
 
     @Override
     protected RemoteOperationResult run(OwnCloudClient client) {
 
         RemoteOperationResult result = null;
         mTransferWasRequested = false;
 
     @Override
     protected RemoteOperationResult run(OwnCloudClient client) {
 
         RemoteOperationResult result = null;
         mTransferWasRequested = false;
+        
+        if (mLocalFile == null) {
+            // Get local file from the DB
+            mLocalFile = getStorageManager().getFileByPath(mRemotePath);
+        }
+        
         if (!mLocalFile.isDown()) {
             /// easy decision
             requestForDownload(mLocalFile);
         if (!mLocalFile.isDown()) {
             /// easy decision
             requestForDownload(mLocalFile);
@@ -86,8 +131,7 @@ public class SynchronizeFileOperation extends RemoteOperation {
             /// local copy in the device -> need to think a bit more before do anything
 
             if (mServerFile == null) {
             /// local copy in the device -> need to think a bit more before do anything
 
             if (mServerFile == null) {
-                String remotePath = mLocalFile.getRemotePath();
-                ReadRemoteFileOperation operation = new ReadRemoteFileOperation(remotePath);
+                ReadRemoteFileOperation operation = new ReadRemoteFileOperation(mRemotePath);
                 result = operation.execute(client);
                 if (result.isSuccess()){
                     mServerFile = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
                 result = operation.execute(client);
                 if (result.isSuccess()){
                     mServerFile = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
@@ -126,6 +170,8 @@ public class SynchronizeFileOperation extends RemoteOperation {
                     result = new RemoteOperationResult(ResultCode.OK);
 
                 } else if (serverChanged) {
                     result = new RemoteOperationResult(ResultCode.OK);
 
                 } else if (serverChanged) {
+                    mLocalFile.setRemoteId(mServerFile.getRemoteId());
+                    
                     if (mSyncFileContents) {
                         requestForDownload(mLocalFile); // local, not server; we won't to keep the value of keepInSync!
                         // the update of local data will be done later by the FileUploader service when the upload finishes
                     if (mSyncFileContents) {
                         requestForDownload(mLocalFile); // local, not server; we won't to keep the value of keepInSync!
                         // the update of local data will be done later by the FileUploader service when the upload finishes
@@ -135,7 +181,7 @@ public class SynchronizeFileOperation extends RemoteOperation {
                         mServerFile.setLastSyncDateForData(mLocalFile.getLastSyncDateForData());
                         mServerFile.setStoragePath(mLocalFile.getStoragePath());
                         mServerFile.setParentId(mLocalFile.getParentId());
                         mServerFile.setLastSyncDateForData(mLocalFile.getLastSyncDateForData());
                         mServerFile.setStoragePath(mLocalFile.getStoragePath());
                         mServerFile.setParentId(mLocalFile.getParentId());
-                        mStorageManager.saveFile(mServerFile);
+                        getStorageManager().saveFile(mServerFile);
 
                     }
                     result = new RemoteOperationResult(ResultCode.OK);
 
                     }
                     result = new RemoteOperationResult(ResultCode.OK);