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
.datamodel
.OCFile
;
25 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
26 import com
.owncloud
.android
.lib
.resources
.files
.CreateRemoteFolderOperation
;
27 import com
.owncloud
.android
.lib
.common
.operations
.OnRemoteOperationListener
;
28 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
29 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
30 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
31 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
32 import com
.owncloud
.android
.utils
.FileStorageUtils
;
36 * Access to remote operation performing the creation of a new folder in the ownCloud server.
37 * Save the new folder in Database
39 public class CreateFolderOperation
extends SyncOperation
implements OnRemoteOperationListener
{
41 private static final String TAG
= CreateFolderOperation
.class.getSimpleName();
43 protected String mRemotePath
;
44 protected boolean mCreateFullPath
;
49 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
51 public CreateFolderOperation(String remotePath
, boolean createFullPath
) {
52 mRemotePath
= remotePath
;
53 mCreateFullPath
= createFullPath
;
59 protected RemoteOperationResult
run(OwnCloudClient client
) {
60 CreateRemoteFolderOperation operation
= new CreateRemoteFolderOperation(mRemotePath
, mCreateFullPath
);
61 RemoteOperationResult result
= operation
.execute(client
);
63 if (result
.isSuccess()) {
66 Log_OC
.e(TAG
, mRemotePath
+ "hasn't been created");
73 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
74 if (operation
instanceof CreateRemoteFolderOperation
) {
75 onCreateRemoteFolderOperationFinish((CreateRemoteFolderOperation
)operation
, result
);
80 private void onCreateRemoteFolderOperationFinish(CreateRemoteFolderOperation operation
, RemoteOperationResult result
) {
81 if (result
.isSuccess()) {
84 Log_OC
.e(TAG
, mRemotePath
+ "hasn't been created");
89 * Save new directory in local database
91 public void saveFolderInDB() {
92 if (mCreateFullPath
&& getStorageManager().
93 getFileByPath(FileStorageUtils
.getParentPath(mRemotePath
)) == null
){// When parent
96 String
[] subFolders
= mRemotePath
.split("/");
97 String composedRemotePath
= "/";
99 // For each antecesor folders create them recursively
100 for (int i
=0; i
<subFolders
.length
; i
++) {
101 String subFolder
= subFolders
[i
];
102 if (!subFolder
.isEmpty()) {
103 composedRemotePath
= composedRemotePath
+ subFolder
+ "/";
104 mRemotePath
= composedRemotePath
;
108 } else { // Create directory on DB
109 OCFile newDir
= new OCFile(mRemotePath
);
110 newDir
.setMimetype("DIR");
111 long parentId
= getStorageManager().
112 getFileByPath(FileStorageUtils
.getParentPath(mRemotePath
)).getFileId();
113 newDir
.setParentId(parentId
);
114 newDir
.setModificationTimestamp(System
.currentTimeMillis());
115 getStorageManager().saveFile(newDir
);
117 Log_OC
.d(TAG
, "Create directory " + mRemotePath
+ " in Database");