a2e79d63417f94eff8a98c42b1d9be282b910753
[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.Log_OC;
21 import com.owncloud.android.datamodel.FileDataStorageManager;
22 import com.owncloud.android.datamodel.OCFile;
23 import com.owncloud.android.network.webdav.WebdavClient;
24 import com.owncloud.android.operations.remote.CreateRemoteFolderOperation;
25 import com.owncloud.android.utils.FileStorageUtils;
26
27
28 /**
29 * Access to remote operation performing the creation of a new folder in the ownCloud server.
30 * Save the new folder in Database
31 *
32 * @author David A. Velasco
33 * @author masensio
34 */
35 public class CreateFolderOperation extends RemoteOperation implements OnRemoteOperationListener{
36
37 private static final String TAG = CreateFolderOperation.class.getSimpleName();
38
39 protected String mRemotePath;
40 protected boolean mCreateFullPath;
41 protected FileDataStorageManager mStorageManager;
42
43 /**
44 * Constructor
45 *
46 * @param remotePath Full path to the new directory to create in the remote server.
47 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
48 * @param storageManager Reference to the local database corresponding to the account where the file is contained.
49 */
50 public CreateFolderOperation(String remotePath, boolean createFullPath, FileDataStorageManager storageManager) {
51 mRemotePath = remotePath;
52 mCreateFullPath = createFullPath;
53 mStorageManager = storageManager;
54
55 }
56
57
58 @Override
59 protected RemoteOperationResult run(WebdavClient 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
90 /**
91 * Save new directory in local database
92 */
93 public void saveFolderInDB() {
94 OCFile newDir = new OCFile(mRemotePath);
95 newDir.setMimetype("DIR");
96 long parentId = mStorageManager.getFileByPath(FileStorageUtils.getParentPath(mRemotePath)).getFileId();
97 newDir.setParentId(parentId);
98 newDir.setModificationTimestamp(System.currentTimeMillis());
99 mStorageManager.saveFile(newDir);
100
101 Log_OC.d(TAG, "Create directory " + mRemotePath + " in Database");
102
103 }
104
105 }