Filter only '/' character in user input when version of server is 8.1 or later
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / CreateFolderOperation.java
index 9ba118e..b020b92 100644 (file)
@@ -1,5 +1,9 @@
-/* ownCloud Android client application
- *   Copyright (C) 2012 ownCloud Inc.
+/**
+ *   ownCloud Android client application
+ *
+ *   @author David A. Velasco
+ *   @author masensio
+ *   Copyright (C) 2015 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.MainApp;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.lib.network.OwnCloudClient;
-import com.owncloud.android.lib.operations.remote.CreateRemoteFolderOperation;
-import com.owncloud.android.lib.operations.common.OnRemoteOperationListener;
-import com.owncloud.android.lib.operations.common.RemoteOperation;
-import com.owncloud.android.lib.operations.common.RemoteOperationResult;
+import com.owncloud.android.lib.common.OwnCloudClient;
+import com.owncloud.android.lib.resources.files.CreateRemoteFolderOperation;
+import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
+import com.owncloud.android.lib.common.operations.RemoteOperation;
+import com.owncloud.android.lib.common.operations.RemoteOperationResult;
+import com.owncloud.android.lib.common.utils.Log_OC;
+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;
 
 
 /**
  * Access to remote operation performing the creation of a new folder in the ownCloud server.
  * Save the new folder in Database
 
 
 /**
  * Access to remote operation performing the creation of a new folder in the ownCloud server.
  * Save the new folder in Database
- * 
- * @author David A. Velasco 
- * @author masensio
  */
  */
-public class CreateFolderOperation extends RemoteOperation implements OnRemoteOperationListener{
+public class CreateFolderOperation extends SyncOperation implements OnRemoteOperationListener{
     
     private static final String TAG = CreateFolderOperation.class.getSimpleName();
     
     protected String mRemotePath;
     protected boolean mCreateFullPath;
     
     private static final String TAG = CreateFolderOperation.class.getSimpleName();
     
     protected String mRemotePath;
     protected boolean mCreateFullPath;
-    protected FileDataStorageManager mStorageManager;
     
     /**
      * Constructor
      * 
     
     /**
      * Constructor
      * 
-     * @param createFullPath        'True' means that all the ancestor folders should be created if don't exist yet.
-     * @param storageManager        Reference to the local database corresponding to the account where the file is contained. 
+     * @param createFullPath        'True' means that all the ancestor folders should be created
+     *                              if don't exist yet.
      */
      */
-    public CreateFolderOperation(String remotePath, boolean createFullPath, FileDataStorageManager storageManager) {
+    public CreateFolderOperation(String remotePath, boolean createFullPath) {
         mRemotePath = remotePath;
         mCreateFullPath = createFullPath;
         mRemotePath = remotePath;
         mCreateFullPath = createFullPath;
-        mStorageManager = storageManager;
         
     }
 
 
     @Override
     protected RemoteOperationResult run(OwnCloudClient client) {
         
     }
 
 
     @Override
     protected RemoteOperationResult run(OwnCloudClient client) {
-        CreateRemoteFolderOperation operation = new CreateRemoteFolderOperation(mRemotePath, mCreateFullPath);
+        CreateRemoteFolderOperation operation = new CreateRemoteFolderOperation(mRemotePath,
+                mCreateFullPath);
         RemoteOperationResult result =  operation.execute(client);
         
         if (result.isSuccess()) {
         RemoteOperationResult result =  operation.execute(client);
         
         if (result.isSuccess()) {
@@ -79,7 +80,8 @@ public class CreateFolderOperation extends RemoteOperation implements OnRemoteOp
     }
     
     
     }
     
     
-    private void onCreateRemoteFolderOperationFinish(CreateRemoteFolderOperation operation, RemoteOperationResult result) {
+    private void onCreateRemoteFolderOperationFinish(CreateRemoteFolderOperation operation,
+                                                     RemoteOperationResult result) {
        if (result.isSuccess()) {
            saveFolderInDB();
        } else {
        if (result.isSuccess()) {
            saveFolderInDB();
        } else {
@@ -87,21 +89,36 @@ public class CreateFolderOperation extends RemoteOperation implements OnRemoteOp
        }
     }
 
        }
     }
 
-    
     /**
      * Save new directory in local database
      */
     public void saveFolderInDB() {
     /**
      * Save new directory in local database
      */
     public void saveFolderInDB() {
-        OCFile newDir = new OCFile(mRemotePath);
-        newDir.setMimetype("DIR");
-        long parentId = mStorageManager.getFileByPath(FileStorageUtils.getParentPath(mRemotePath)).getFileId();
-        newDir.setParentId(parentId);
-        newDir.setModificationTimestamp(System.currentTimeMillis());
-        mStorageManager.saveFile(newDir);
+        if (mCreateFullPath && getStorageManager().
+                getFileByPath(FileStorageUtils.getParentPath(mRemotePath)) == null){// When parent
+                                                                                    // of remote path
+                                                                                    // is not created 
+            String[] subFolders = mRemotePath.split("/");
+            String composedRemotePath = "/";
 
 
-        Log_OC.d(TAG, "Create directory " + mRemotePath + " in Database");
+            // For each antecesor folders create them recursively
+            for (int i=0; i<subFolders.length; i++) {
+                String subFolder =  subFolders[i];
+                if (!subFolder.isEmpty()) {
+                    composedRemotePath = composedRemotePath + subFolder + "/";
+                    mRemotePath = composedRemotePath;
+                    saveFolderInDB();
+                }
+            }
+        } else { // Create directory on DB
+            OCFile newDir = new OCFile(mRemotePath);
+            newDir.setMimetype("DIR");
+            long parentId = getStorageManager().
+                    getFileByPath(FileStorageUtils.getParentPath(mRemotePath)).getFileId();
+            newDir.setParentId(parentId);
+            newDir.setModificationTimestamp(System.currentTimeMillis());
+            getStorageManager().saveFile(newDir);
 
 
+            Log_OC.d(TAG, "Create directory " + mRemotePath + " in Database");
+        }
     }
     }
-
-
 }
 }