Merge remote-tracking branch 'origin/refactor_remote_operation_to_create_folder'...
[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.oc_framework.network.webdav.WebdavClient;
23 import com.owncloud.android.oc_framework.operations.remote.CreateRemoteFolderOperation;
24 import com.owncloud.android.oc_framework.operations.OnRemoteOperationListener;
25 import com.owncloud.android.oc_framework.operations.RemoteOperation;
26 import com.owncloud.android.oc_framework.operations.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 remotePath Full path to the new directory to create in the remote server.
50 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
51 * @param storageManager Reference to the local database corresponding to the account where the file is contained.
52 */
53 public CreateFolderOperation(String remotePath, boolean createFullPath, FileDataStorageManager storageManager) {
54 mRemotePath = remotePath;
55 mCreateFullPath = createFullPath;
56 mStorageManager = storageManager;
57
58 }
59
60
61 @Override
62 protected RemoteOperationResult run(WebdavClient client) {
63 CreateRemoteFolderOperation operation = new CreateRemoteFolderOperation(mRemotePath, mCreateFullPath);
64 RemoteOperationResult result = operation.execute(client);
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, RemoteOperationResult result) {
84 if (result.isSuccess()) {
85 saveFolderInDB();
86 } else {
87 Log_OC.e(TAG, mRemotePath + "hasn't been created");
88 }
89 }
90
91
92 /**
93 * Save new directory in local database
94 */
95 public void saveFolderInDB() {
96 OCFile newDir = new OCFile(mRemotePath);
97 newDir.setMimetype("DIR");
98 long parentId = mStorageManager.getFileByPath(FileStorageUtils.getParentPath(mRemotePath)).getFileId();
99 newDir.setParentId(parentId);
100 newDir.setModificationTimestamp(System.currentTimeMillis());
101 mStorageManager.saveFile(newDir);
102
103 Log_OC.d(TAG, "Create directory " + mRemotePath + " in Database");
104
105 }
106
107
108 }