Add user agent in android project
[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.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;
34
35
36 /**
37 * Access to remote operation performing the creation of a new folder in the ownCloud server.
38 * Save the new folder in Database
39 */
40 public class CreateFolderOperation extends SyncOperation implements OnRemoteOperationListener{
41
42 private static final String TAG = CreateFolderOperation.class.getSimpleName();
43
44 protected String mRemotePath;
45 protected boolean mCreateFullPath;
46
47 /**
48 * Constructor
49 *
50 * @param createFullPath 'True' means that all the ancestor folders should be created
51 * if don't exist yet.
52 */
53 public CreateFolderOperation(String remotePath, boolean createFullPath) {
54 mRemotePath = remotePath;
55 mCreateFullPath = createFullPath;
56
57 }
58
59
60 @Override
61 protected RemoteOperationResult run(OwnCloudClient client) {
62 CreateRemoteFolderOperation operation = new CreateRemoteFolderOperation(mRemotePath,
63 mCreateFullPath);
64 RemoteOperationResult result = operation.execute(client, MainApp.getUserAgent());
65
66 if (result.isSuccess()) {
67 saveFolderInDB();
68 } else {
69 Log_OC.e(TAG, mRemotePath + "hasn't been created");
70 }
71
72 return result;
73 }
74
75 @Override
76 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
77 if (operation instanceof CreateRemoteFolderOperation) {
78 onCreateRemoteFolderOperationFinish((CreateRemoteFolderOperation)operation, result);
79 }
80 }
81
82
83 private void onCreateRemoteFolderOperationFinish(CreateRemoteFolderOperation operation,
84 RemoteOperationResult result) {
85 if (result.isSuccess()) {
86 saveFolderInDB();
87 } else {
88 Log_OC.e(TAG, mRemotePath + "hasn't been created");
89 }
90 }
91
92 /**
93 * Save new directory in local database
94 */
95 public void saveFolderInDB() {
96 if (mCreateFullPath && getStorageManager().
97 getFileByPath(FileStorageUtils.getParentPath(mRemotePath)) == null){// When parent
98 // of remote path
99 // is not created
100 String[] subFolders = mRemotePath.split("/");
101 String composedRemotePath = "/";
102
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;
109 saveFolderInDB();
110 }
111 }
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);
120
121 Log_OC.d(TAG, "Create directory " + mRemotePath + " in Database");
122 }
123 }
124 }