Merge tag 'oc-android-1-3-22' into oauth_login
[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 as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.operations;
20
21 import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
22
23 import com.owncloud.android.datamodel.DataStorageManager;
24 import com.owncloud.android.datamodel.OCFile;
25
26 import android.util.Log;
27
28 import eu.alefzero.webdav.WebdavClient;
29 import eu.alefzero.webdav.WebdavUtils;
30
31 /**
32 * Remote operation performing the creation of a new folder in the ownCloud server.
33 *
34 * @author David A. Velasco
35 */
36 public class CreateFolderOperation extends RemoteOperation {
37
38 private static final String TAG = CreateFolderOperation.class.getSimpleName();
39
40 private static final int READ_TIMEOUT = 10000;
41 private static final int CONNECTION_TIMEOUT = 5000;
42
43 protected String mRemotePath;
44 protected long mParentDirId;
45 protected DataStorageManager mStorageManager;
46
47 /**
48 * Constructor
49 *
50 * @param remoetPath Full path to the new directory to create in the remote server.
51 * @param parentDirId Local database id for the parent folder.
52 * @param storageManager Reference to the local database corresponding to the account where the file is contained.
53 */
54 public CreateFolderOperation(String remotePath, long parentDirId, DataStorageManager storageManager) {
55 mRemotePath = remotePath;
56 mParentDirId = parentDirId;
57 mStorageManager = storageManager;
58 }
59
60
61 /**
62 * Performs the remove operation
63 *
64 * @param client Client object to communicate with the remote ownCloud server.
65 */
66 @Override
67 protected RemoteOperationResult run(WebdavClient client) {
68 RemoteOperationResult result = null;
69 MkColMethod mkcol = null;
70 try {
71 mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
72 int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT);
73 if (mkcol.succeeded()) {
74 // Save new directory in local database
75 OCFile newDir = new OCFile(mRemotePath);
76 newDir.setMimetype("DIR");
77 newDir.setParentId(mParentDirId);
78 mStorageManager.saveFile(newDir);
79 }
80
81 result = new RemoteOperationResult(mkcol.succeeded(), status);
82 Log.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage());
83 client.exhaustResponse(mkcol.getResponseBodyAsStream());
84
85 } catch (Exception e) {
86 result = new RemoteOperationResult(e);
87 Log.e(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage(), e);
88
89 } finally {
90 if (mkcol != null)
91 mkcol.releaseConnection();
92 }
93 return result;
94 }
95
96 }