Fix, creation of subdirectories
[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 mFolderName;
43 protected String mRemotePath;
44 protected boolean mCreateFullPath;
45 protected FileDataStorageManager mStorageManager;
46
47 /**
48 * Constructor
49 *
50 * @param remotePath Full path to the new directory to create in the remote server.
51 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
52 * @param storageManager Reference to the local database corresponding to the account where the file is contained.
53 */
54 public CreateFolderOperation(String folderName, String remotePath, boolean createFullPath, FileDataStorageManager storageManager) {
55 mFolderName = folderName;
56 mRemotePath = remotePath;
57 mCreateFullPath = createFullPath;
58 mStorageManager = storageManager;
59
60 }
61
62
63 @Override
64 protected RemoteOperationResult run(WebdavClient client) {
65 CreateRemoteFolderOperation operation = new CreateRemoteFolderOperation(mFolderName, mRemotePath, mCreateFullPath);
66 RemoteOperationResult result = operation.execute(client);
67
68 if (result.isSuccess()) {
69 saveFolderInDB();
70 } else {
71 Log_OC.e(TAG, mRemotePath + "hasn't been created");
72 }
73
74 return result;
75 }
76
77 @Override
78 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
79 if (operation instanceof CreateRemoteFolderOperation) {
80 onCreateRemoteFolderOperationFinish((CreateRemoteFolderOperation)operation, result);
81 }
82 }
83
84
85 private void onCreateRemoteFolderOperationFinish(CreateRemoteFolderOperation operation, RemoteOperationResult result) {
86 if (result.isSuccess()) {
87 saveFolderInDB();
88 } else {
89 Log_OC.e(TAG, mRemotePath + "hasn't been created");
90 }
91 }
92
93
94 /**
95 * Save new directory in local database
96 */
97 public void saveFolderInDB() {
98 OCFile newDir = new OCFile(mRemotePath);
99 newDir.setMimetype("DIR");
100 long parentId = mStorageManager.getFileByPath(FileStorageUtils.getParentPath(mRemotePath)).getFileId();
101 newDir.setParentId(parentId);
102 newDir.setModificationTimestamp(System.currentTimeMillis());
103 mStorageManager.saveFile(newDir);
104
105 Log_OC.d(TAG, "Create directory " + mRemotePath + " in Database");
106
107 }
108
109
110 }