Process share with password
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / CreateFolderOperation.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * @author masensio
6 * Copyright (C) 2015 ownCloud Inc.
7 *
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.
11 *
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.
16 *
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/>.
19 *
20 */
21
22 package com.owncloud.android.operations;
23
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;
33
34
35 /**
36 * Access to remote operation performing the creation of a new folder in the ownCloud server.
37 * Save the new folder in Database
38 */
39 public class CreateFolderOperation extends SyncOperation implements OnRemoteOperationListener{
40
41 private static final String TAG = CreateFolderOperation.class.getSimpleName();
42
43 protected String mRemotePath;
44 protected boolean mCreateFullPath;
45
46 /**
47 * Constructor
48 *
49 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
50 */
51 public CreateFolderOperation(String remotePath, boolean createFullPath) {
52 mRemotePath = remotePath;
53 mCreateFullPath = createFullPath;
54
55 }
56
57
58 @Override
59 protected RemoteOperationResult run(OwnCloudClient client) {
60 CreateRemoteFolderOperation operation = new CreateRemoteFolderOperation(mRemotePath, mCreateFullPath);
61 RemoteOperationResult result = operation.execute(client);
62
63 if (result.isSuccess()) {
64 saveFolderInDB();
65 } else {
66 Log_OC.e(TAG, mRemotePath + "hasn't been created");
67 }
68
69 return result;
70 }
71
72 @Override
73 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
74 if (operation instanceof CreateRemoteFolderOperation) {
75 onCreateRemoteFolderOperationFinish((CreateRemoteFolderOperation)operation, result);
76 }
77 }
78
79
80 private void onCreateRemoteFolderOperationFinish(CreateRemoteFolderOperation operation, RemoteOperationResult result) {
81 if (result.isSuccess()) {
82 saveFolderInDB();
83 } else {
84 Log_OC.e(TAG, mRemotePath + "hasn't been created");
85 }
86 }
87
88 /**
89 * Save new directory in local database
90 */
91 public void saveFolderInDB() {
92 if (mCreateFullPath && getStorageManager().
93 getFileByPath(FileStorageUtils.getParentPath(mRemotePath)) == null){// When parent
94 // of remote path
95 // is not created
96 String[] subFolders = mRemotePath.split("/");
97 String composedRemotePath = "/";
98
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;
105 saveFolderInDB();
106 }
107 }
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);
116
117 Log_OC.d(TAG, "Create directory " + mRemotePath + " in Database");
118 }
119 }
120 }