From: masensio Date: Wed, 6 Nov 2013 16:55:11 +0000 (+0100) Subject: OC-1865: Isolate code from CreateFolderOperation to remove dependencies on local... X-Git-Tag: oc-android-1.5.5~123^2~22 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/112b44188edbd419ed85761815a8cccad6b7d1fa OC-1865: Isolate code from CreateFolderOperation to remove dependencies on local storage --- diff --git a/src/com/owncloud/android/operations/CreateFolderOperation.java b/src/com/owncloud/android/operations/CreateFolderOperation.java index c86d2fc3..2b72b282 100644 --- a/src/com/owncloud/android/operations/CreateFolderOperation.java +++ b/src/com/owncloud/android/operations/CreateFolderOperation.java @@ -17,29 +17,24 @@ package com.owncloud.android.operations; -import java.io.File; - -import org.apache.commons.httpclient.HttpStatus; -import org.apache.jackrabbit.webdav.client.methods.MkColMethod; - import com.owncloud.android.Log_OC; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.operations.remote.CreateRemoteFolderOperation; +import com.owncloud.android.utils.FileStorageUtils; import eu.alefzero.webdav.WebdavClient; -import eu.alefzero.webdav.WebdavUtils; /** - * Remote operation performing the creation of a new folder in the ownCloud server. + * Access to remote operation performing the creation of a new folder in the ownCloud server. + * Save the new folder in Database * * @author David A. Velasco + * @author masensio */ -public class CreateFolderOperation extends RemoteOperation { +public class CreateFolderOperation extends RemoteOperation implements OnRemoteOperationListener{ private static final String TAG = CreateFolderOperation.class.getSimpleName(); - - private static final int READ_TIMEOUT = 10000; - private static final int CONNECTION_TIMEOUT = 5000; protected String mRemotePath; protected boolean mCreateFullPath; @@ -56,62 +51,55 @@ public class CreateFolderOperation extends RemoteOperation { mRemotePath = remotePath; mCreateFullPath = createFullPath; mStorageManager = storageManager; + } - - - /** - * Performs the operation - * - * @param client Client object to communicate with the remote ownCloud server. - */ + + @Override protected RemoteOperationResult run(WebdavClient client) { - RemoteOperationResult result = null; - MkColMethod mkcol = null; - try { - mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath)); - int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); - if (!mkcol.succeeded() && mkcol.getStatusCode() == HttpStatus.SC_CONFLICT && mCreateFullPath) { - result = createParentFolder(getParentPath(), client); - status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); // second (and last) try - } - if (mkcol.succeeded()) { - // Save new directory in local database - OCFile newDir = new OCFile(mRemotePath); - newDir.setMimetype("DIR"); - long parentId = mStorageManager.getFileByPath(getParentPath()).getFileId(); - newDir.setParentId(parentId); - newDir.setModificationTimestamp(System.currentTimeMillis()); - mStorageManager.saveFile(newDir); - } - result = new RemoteOperationResult(mkcol.succeeded(), status, mkcol.getResponseHeaders()); - Log_OC.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage()); - client.exhaustResponse(mkcol.getResponseBodyAsStream()); - - } catch (Exception e) { - result = new RemoteOperationResult(e); - Log_OC.e(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage(), e); - - } finally { - if (mkcol != null) - mkcol.releaseConnection(); + CreateRemoteFolderOperation operation = new CreateRemoteFolderOperation(mRemotePath, mCreateFullPath); + RemoteOperationResult result = operation.execute(client); + + if (result.isSuccess()) { + saveFolderInDB(); + } else { + Log_OC.e(TAG, mRemotePath + "hasn't been created"); } + return result; } - - private String getParentPath() { - String parentPath = new File(mRemotePath).getParent(); - parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR; - return parentPath; + @Override + public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) { + if (operation instanceof CreateRemoteFolderOperation) { + onCreateRemoteFolderOperationFinish((CreateRemoteFolderOperation)operation, result); + } } - - private RemoteOperationResult createParentFolder(String parentPath, WebdavClient client) { - RemoteOperation operation = new CreateFolderOperation( parentPath, - mCreateFullPath, - mStorageManager ); - return operation.execute(client); + + private void onCreateRemoteFolderOperationFinish(CreateRemoteFolderOperation operation, RemoteOperationResult result) { + if (result.isSuccess()) { + saveFolderInDB(); + } else { + Log_OC.e(TAG, mRemotePath + "hasn't been created"); + } + + } + /** + * Save new directory in local database + */ + public void saveFolderInDB() { + OCFile newDir = new OCFile(mRemotePath); + newDir.setMimetype("DIR"); + long parentId = mStorageManager.getFileByPath(FileStorageUtils.getParentPath(mRemotePath)).getFileId(); + newDir.setParentId(parentId); + newDir.setModificationTimestamp(System.currentTimeMillis()); + mStorageManager.saveFile(newDir); + + Log_OC.d(TAG, "Create directory " + mRemotePath + " in Database"); + + } + } diff --git a/src/com/owncloud/android/operations/remote/CreateRemoteFolderOperation.java b/src/com/owncloud/android/operations/remote/CreateRemoteFolderOperation.java index 6d417d08..8b0d802f 100644 --- a/src/com/owncloud/android/operations/remote/CreateRemoteFolderOperation.java +++ b/src/com/owncloud/android/operations/remote/CreateRemoteFolderOperation.java @@ -1,20 +1,81 @@ package com.owncloud.android.operations.remote; +import org.apache.commons.httpclient.HttpStatus; +import org.apache.jackrabbit.webdav.client.methods.MkColMethod; + +import com.owncloud.android.Log_OC; import com.owncloud.android.operations.RemoteOperation; import com.owncloud.android.operations.RemoteOperationResult; +import com.owncloud.android.utils.FileStorageUtils; import eu.alefzero.webdav.WebdavClient; +import eu.alefzero.webdav.WebdavUtils; +/** + * Remote operation performing the creation of a new folder in the ownCloud server. + * + * @author David A. Velasco + * @author masensio + * + */ public class CreateRemoteFolderOperation extends RemoteOperation { + + private static final String TAG = CreateRemoteFolderOperation.class.getSimpleName(); - public CreateRemoteFolderOperation() { - // TODO Auto-generated constructor stub + private static final int READ_TIMEOUT = 10000; + private static final int CONNECTION_TIMEOUT = 5000; + + protected String mRemotePath; + protected boolean mCreateFullPath; + + /** + * Contructor + * + * @param remotePath Full path to the new directory to create in the remote server. + * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet. + */ + public CreateRemoteFolderOperation(String remotePath, boolean createFullPath) { + mRemotePath = remotePath; + mCreateFullPath = createFullPath; } + /** + * Performs the operation + * + * @param client Client object to communicate with the remote ownCloud server. + */ @Override protected RemoteOperationResult run(WebdavClient client) { - // TODO Auto-generated method stub - return null; + RemoteOperationResult result = null; + MkColMethod mkcol = null; + + try { + mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath)); + int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); + if (!mkcol.succeeded() && mkcol.getStatusCode() == HttpStatus.SC_CONFLICT && mCreateFullPath) { + result = createParentFolder(FileStorageUtils.getParentPath(mRemotePath), client); + status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); // second (and last) try + } + + result = new RemoteOperationResult(mkcol.succeeded(), status, mkcol.getResponseHeaders()); + Log_OC.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage()); + client.exhaustResponse(mkcol.getResponseBodyAsStream()); + + } catch (Exception e) { + result = new RemoteOperationResult(e); + Log_OC.e(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage(), e); + + } finally { + if (mkcol != null) + mkcol.releaseConnection(); + } + return result; } + + private RemoteOperationResult createParentFolder(String parentPath, WebdavClient client) { + RemoteOperation operation = new CreateRemoteFolderOperation( parentPath, + mCreateFullPath); + return operation.execute(client); + } } diff --git a/src/com/owncloud/android/utils/FileStorageUtils.java b/src/com/owncloud/android/utils/FileStorageUtils.java index 55399525..e221de5f 100644 --- a/src/com/owncloud/android/utils/FileStorageUtils.java +++ b/src/com/owncloud/android/utils/FileStorageUtils.java @@ -75,5 +75,11 @@ public class FileStorageUtils { String value = uploadPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName); return value; } + + public static String getParentPath(String remotePath) { + String parentPath = new File(remotePath).getParent(); + parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR; + return parentPath; + } } \ No newline at end of file