Merge branch 'share_link_show_shared_files' into share_link__new_share
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / CreateFolderOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.operations;
19
20 import com.owncloud.android.datamodel.FileDataStorageManager;
21 import com.owncloud.android.datamodel.OCFile;
22 import com.owncloud.android.lib.network.OwnCloudClient;
23 import com.owncloud.android.lib.operations.remote.CreateRemoteFolderOperation;
24 import com.owncloud.android.lib.operations.common.OnRemoteOperationListener;
25 import com.owncloud.android.lib.operations.common.RemoteOperation;
26 import com.owncloud.android.lib.operations.common.RemoteOperationResult;
27 import com.owncloud.android.utils.FileStorageUtils;
28 import com.owncloud.android.utils.Log_OC;
29
30
31 /**
32 * Access to remote operation performing the creation of a new folder in the ownCloud server.
33 * Save the new folder in Database
34 *
35 * @author David A. Velasco
36 * @author masensio
37 */
38 public class CreateFolderOperation extends RemoteOperation implements OnRemoteOperationListener{
39
40 private static final String TAG = CreateFolderOperation.class.getSimpleName();
41
42 protected String mRemotePath;
43 protected boolean mCreateFullPath;
44 protected FileDataStorageManager mStorageManager;
45
46 /**
47 * Constructor
48 *
49 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
50 * @param storageManager Reference to the local database corresponding to the account where the file is contained.
51 */
52 public CreateFolderOperation(String remotePath, boolean createFullPath, FileDataStorageManager storageManager) {
53 mRemotePath = remotePath;
54 mCreateFullPath = createFullPath;
55 mStorageManager = storageManager;
56
57 }
58
59
60 @Override
61 protected RemoteOperationResult run(OwnCloudClient client) {
62 CreateRemoteFolderOperation operation = new CreateRemoteFolderOperation(mRemotePath, mCreateFullPath);
63 RemoteOperationResult result = operation.execute(client);
64
65 if (result.isSuccess()) {
66 saveFolderInDB();
67 } else {
68 Log_OC.e(TAG, mRemotePath + "hasn't been created");
69 }
70
71 return result;
72 }
73
74 @Override
75 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
76 if (operation instanceof CreateRemoteFolderOperation) {
77 onCreateRemoteFolderOperationFinish((CreateRemoteFolderOperation)operation, result);
78 }
79 }
80
81
82 private void onCreateRemoteFolderOperationFinish(CreateRemoteFolderOperation operation, RemoteOperationResult result) {
83 if (result.isSuccess()) {
84 saveFolderInDB();
85 } else {
86 Log_OC.e(TAG, mRemotePath + "hasn't been created");
87 }
88 }
89
90
91 /**
92 * Save new directory in local database
93 */
94 public void saveFolderInDB() {
95 OCFile newDir = new OCFile(mRemotePath);
96 newDir.setMimetype("DIR");
97 long parentId = mStorageManager.getFileByPath(FileStorageUtils.getParentPath(mRemotePath)).getFileId();
98 newDir.setParentId(parentId);
99 newDir.setModificationTimestamp(System.currentTimeMillis());
100 mStorageManager.saveFile(newDir);
101
102 Log_OC.d(TAG, "Create directory " + mRemotePath + " in Database");
103
104 }
105
106
107 }