03c6868c345c0241099aba09601e1baaab26e89f
[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 org.apache.jackrabbit.webdav.client.methods.MkColMethod;
21
22 import com.owncloud.android.Log_OC;
23 import com.owncloud.android.datamodel.DataStorageManager;
24 import com.owncloud.android.datamodel.OCFile;
25
26 import eu.alefzero.webdav.WebdavClient;
27 import eu.alefzero.webdav.WebdavUtils;
28
29 /**
30 * Remote operation performing the creation of a new folder in the ownCloud server.
31 *
32 * @author David A. Velasco
33 */
34 public class CreateFolderOperation extends RemoteOperation {
35
36 private static final String TAG = CreateFolderOperation.class.getSimpleName();
37
38 private static final int READ_TIMEOUT = 10000;
39 private static final int CONNECTION_TIMEOUT = 5000;
40
41 protected String mRemotePath;
42 protected long mParentDirId;
43 protected DataStorageManager mStorageManager;
44
45 /**
46 * Constructor
47 *
48 * @param remoetPath Full path to the new directory to create in the remote server.
49 * @param parentDirId Local database id for the parent folder.
50 * @param storageManager Reference to the local database corresponding to the account where the file is contained.
51 */
52 public CreateFolderOperation(String remotePath, long parentDirId, DataStorageManager storageManager) {
53 mRemotePath = remotePath;
54 mParentDirId = parentDirId;
55 mStorageManager = storageManager;
56 }
57
58
59 /**
60 * Performs the remove operation
61 *
62 * @param client Client object to communicate with the remote ownCloud server.
63 */
64 @Override
65 protected RemoteOperationResult run(WebdavClient client) {
66 RemoteOperationResult result = null;
67 MkColMethod mkcol = null;
68 try {
69 mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
70 int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT);
71 if (mkcol.succeeded()) {
72 // Save new directory in local database
73 OCFile newDir = new OCFile(mRemotePath);
74 newDir.setMimetype("DIR");
75 newDir.setParentId(mParentDirId);
76 newDir.setModificationTimestamp(System.currentTimeMillis());
77 mStorageManager.saveFile(newDir);
78 }
79
80 result = new RemoteOperationResult(mkcol.succeeded(), status);
81 Log_OC.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage());
82 client.exhaustResponse(mkcol.getResponseBodyAsStream());
83
84 } catch (Exception e) {
85 result = new RemoteOperationResult(e);
86 Log_OC.e(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage(), e);
87
88 } finally {
89 if (mkcol != null)
90 mkcol.releaseConnection();
91 }
92 return result;
93 }
94
95 }