2  *   ownCloud Android client application 
   4  *   @author David A. Velasco 
   6  *   Copyright (C) 2015 ownCloud Inc. 
   8  *   This program is free software: you can redistribute it and/or modify 
   9  *   it under the terms of the GNU General Public License version 2, 
  10  *   as published by the Free Software Foundation. 
  12  *   This program is distributed in the hope that it will be useful, 
  13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  15  *   GNU General Public License for more details. 
  17  *   You should have received a copy of the GNU General Public License 
  18  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  22 package com
.owncloud
.android
.operations
; 
  24 import com
.owncloud
.android
.MainApp
; 
  25 import com
.owncloud
.android
.datamodel
.OCFile
; 
  26 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
; 
  27 import com
.owncloud
.android
.lib
.resources
.files
.CreateRemoteFolderOperation
; 
  28 import com
.owncloud
.android
.lib
.common
.operations
.OnRemoteOperationListener
; 
  29 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
; 
  30 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
; 
  31 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
; 
  32 import com
.owncloud
.android
.operations
.common
.SyncOperation
; 
  33 import com
.owncloud
.android
.utils
.FileStorageUtils
; 
  37  * Access to remote operation performing the creation of a new folder in the ownCloud server. 
  38  * Save the new folder in Database 
  40 public class CreateFolderOperation 
extends SyncOperation 
implements OnRemoteOperationListener
{ 
  42     private static final String TAG 
= CreateFolderOperation
.class.getSimpleName(); 
  44     protected String mRemotePath
; 
  45     protected boolean mCreateFullPath
; 
  50      * @param createFullPath        'True' means that all the ancestor folders should be created 
  53     public CreateFolderOperation(String remotePath
, boolean createFullPath
) { 
  54         mRemotePath 
= remotePath
; 
  55         mCreateFullPath 
= createFullPath
; 
  61     protected RemoteOperationResult 
run(OwnCloudClient client
) { 
  62         CreateRemoteFolderOperation operation 
= new CreateRemoteFolderOperation(mRemotePath
, 
  64         RemoteOperationResult result 
=  operation
.execute(client
, MainApp
.getUserAgent()); 
  66         if (result
.isSuccess()) { 
  69             Log_OC
.e(TAG
, mRemotePath 
+ "hasn't been created"); 
  76     public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) { 
  77         if (operation 
instanceof CreateRemoteFolderOperation
) { 
  78             onCreateRemoteFolderOperationFinish((CreateRemoteFolderOperation
)operation
, result
); 
  83     private void onCreateRemoteFolderOperationFinish(CreateRemoteFolderOperation operation
, 
  84                                                      RemoteOperationResult result
) { 
  85        if (result
.isSuccess()) { 
  88            Log_OC
.e(TAG
, mRemotePath 
+ "hasn't been created"); 
  93      * Save new directory in local database 
  95     public void saveFolderInDB() { 
  96         if (mCreateFullPath 
&& getStorageManager(). 
  97                 getFileByPath(FileStorageUtils
.getParentPath(mRemotePath
)) == null
){// When parent 
 100             String
[] subFolders 
= mRemotePath
.split("/"); 
 101             String composedRemotePath 
= "/"; 
 103             // For each antecesor folders create them recursively 
 104             for (int i
=0; i
<subFolders
.length
; i
++) { 
 105                 String subFolder 
=  subFolders
[i
]; 
 106                 if (!subFolder
.isEmpty()) { 
 107                     composedRemotePath 
= composedRemotePath 
+ subFolder 
+ "/"; 
 108                     mRemotePath 
= composedRemotePath
; 
 112         } else { // Create directory on DB 
 113             OCFile newDir 
= new OCFile(mRemotePath
); 
 114             newDir
.setMimetype("DIR"); 
 115             long parentId 
= getStorageManager(). 
 116                     getFileByPath(FileStorageUtils
.getParentPath(mRemotePath
)).getFileId(); 
 117             newDir
.setParentId(parentId
); 
 118             newDir
.setModificationTimestamp(System
.currentTimeMillis()); 
 119             getStorageManager().saveFile(newDir
); 
 121             Log_OC
.d(TAG
, "Create directory " + mRemotePath 
+ " in Database");