Merge pull request #266 from owncloud/fix_fake_creation_of_InstantUploads_folder_when...
[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 java.io.File;
21
22 import org.apache.commons.httpclient.HttpStatus;
23 import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
24
25 import com.owncloud.android.Log_OC;
26 import com.owncloud.android.datamodel.DataStorageManager;
27 import com.owncloud.android.datamodel.OCFile;
28
29 import eu.alefzero.webdav.WebdavClient;
30 import eu.alefzero.webdav.WebdavUtils;
31
32 /**
33 * Remote operation performing the creation of a new folder in the ownCloud server.
34 *
35 * @author David A. Velasco
36 */
37 public class CreateFolderOperation extends RemoteOperation {
38
39 private static final String TAG = CreateFolderOperation.class.getSimpleName();
40
41 private static final int READ_TIMEOUT = 10000;
42 private static final int CONNECTION_TIMEOUT = 5000;
43
44 protected String mRemotePath;
45 protected boolean mCreateFullPath;
46 protected DataStorageManager mStorageManager;
47
48 /**
49 * Constructor
50 *
51 * @param remotePath Full path to the new directory to create in the remote server.
52 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
53 * @param storageManager Reference to the local database corresponding to the account where the file is contained.
54 */
55 public CreateFolderOperation(String remotePath, boolean createFullPath, DataStorageManager storageManager) {
56 mRemotePath = remotePath;
57 mCreateFullPath = createFullPath;
58 mStorageManager = storageManager;
59 }
60
61
62 /**
63 * Performs the operation
64 *
65 * @param client Client object to communicate with the remote ownCloud server.
66 */
67 @Override
68 protected RemoteOperationResult run(WebdavClient client) {
69 RemoteOperationResult result = null;
70 MkColMethod mkcol = null;
71 try {
72 mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
73 int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT);
74 if (!mkcol.succeeded() && mkcol.getStatusCode() == HttpStatus.SC_CONFLICT && mCreateFullPath) {
75 result = createParentFolder(getParentPath(), client);
76 status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); // second (and last) try
77 }
78 if (mkcol.succeeded()) {
79 // Save new directory in local database
80 OCFile newDir = new OCFile(mRemotePath);
81 newDir.setMimetype("DIR");
82 long parentId = mStorageManager.getFileByPath(getParentPath()).getFileId();
83 newDir.setParentId(parentId);
84 newDir.setModificationTimestamp(System.currentTimeMillis());
85 mStorageManager.saveFile(newDir);
86 }
87 result = new RemoteOperationResult(mkcol.succeeded(), status, mkcol.getResponseHeaders());
88 Log_OC.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage());
89 client.exhaustResponse(mkcol.getResponseBodyAsStream());
90
91 } catch (Exception e) {
92 result = new RemoteOperationResult(e);
93 Log_OC.e(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage(), e);
94
95 } finally {
96 if (mkcol != null)
97 mkcol.releaseConnection();
98 }
99 return result;
100 }
101
102
103 private String getParentPath() {
104 String parentPath = new File(mRemotePath).getParent();
105 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
106 return parentPath;
107 }
108
109
110 private RemoteOperationResult createParentFolder(String parentPath, WebdavClient client) {
111 RemoteOperation operation = new CreateFolderOperation( parentPath,
112 mCreateFullPath,
113 mStorageManager );
114 return operation.execute(client);
115 }
116
117 }