1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.operations
;
20 import com
.owncloud
.android
.datamodel
.OCFile
;
21 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
22 import com
.owncloud
.android
.lib
.resources
.files
.CreateRemoteFolderOperation
;
23 import com
.owncloud
.android
.lib
.common
.operations
.OnRemoteOperationListener
;
24 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
25 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
26 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
27 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
28 import com
.owncloud
.android
.utils
.FileStorageUtils
;
32 * Access to remote operation performing the creation of a new folder in the ownCloud server.
33 * Save the new folder in Database
35 * @author David A. Velasco
38 public class CreateFolderOperation
extends SyncOperation
implements OnRemoteOperationListener
{
40 private static final String TAG
= CreateFolderOperation
.class.getSimpleName();
42 protected String mRemotePath
;
43 protected boolean mCreateFullPath
;
48 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
50 public CreateFolderOperation(String remotePath
, boolean createFullPath
) {
51 mRemotePath
= remotePath
;
52 mCreateFullPath
= createFullPath
;
58 protected RemoteOperationResult
run(OwnCloudClient client
) {
59 CreateRemoteFolderOperation operation
= new CreateRemoteFolderOperation(mRemotePath
, mCreateFullPath
);
60 RemoteOperationResult result
= operation
.execute(client
);
62 if (result
.isSuccess()) {
65 Log_OC
.e(TAG
, mRemotePath
+ "hasn't been created");
72 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
73 if (operation
instanceof CreateRemoteFolderOperation
) {
74 onCreateRemoteFolderOperationFinish((CreateRemoteFolderOperation
)operation
, result
);
79 private void onCreateRemoteFolderOperationFinish(CreateRemoteFolderOperation operation
, RemoteOperationResult result
) {
80 if (result
.isSuccess()) {
83 Log_OC
.e(TAG
, mRemotePath
+ "hasn't been created");
88 * Save new directory in local database
90 public void saveFolderInDB() {
91 if (mCreateFullPath
&& getStorageManager().
92 getFileByPath(FileStorageUtils
.getParentPath(mRemotePath
)) == null
){// When parent
95 String
[] subFolders
= mRemotePath
.split("/");
96 String composedRemotePath
= "/";
98 // For each antecesor folders create them recursively
99 for (int i
=0; i
<subFolders
.length
; i
++) {
100 String subFolder
= subFolders
[i
];
101 if (!subFolder
.isEmpty()) {
102 composedRemotePath
= composedRemotePath
+ subFolder
+ "/";
103 mRemotePath
= composedRemotePath
;
107 } else { // Create directory on DB
108 OCFile newDir
= new OCFile(mRemotePath
);
109 newDir
.setMimetype("DIR");
110 long parentId
= getStorageManager().
111 getFileByPath(FileStorageUtils
.getParentPath(mRemotePath
)).getFileId();
112 newDir
.setParentId(parentId
);
113 newDir
.setModificationTimestamp(System
.currentTimeMillis());
114 getStorageManager().saveFile(newDir
);
116 Log_OC
.d(TAG
, "Create directory " + mRemotePath
+ " in Database");