CreateFolderOperation.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. package com.owncloud.android.operations;
  18. import com.owncloud.android.datamodel.FileDataStorageManager;
  19. import com.owncloud.android.datamodel.OCFile;
  20. import com.owncloud.android.lib.common.OwnCloudClient;
  21. import com.owncloud.android.lib.resources.files.CreateRemoteFolderOperation;
  22. import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
  23. import com.owncloud.android.lib.common.operations.RemoteOperation;
  24. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  25. import com.owncloud.android.utils.FileStorageUtils;
  26. import com.owncloud.android.utils.Log_OC;
  27. /**
  28. * Access to remote operation performing the creation of a new folder in the ownCloud server.
  29. * Save the new folder in Database
  30. *
  31. * @author David A. Velasco
  32. * @author masensio
  33. */
  34. public class CreateFolderOperation extends RemoteOperation implements OnRemoteOperationListener{
  35. private static final String TAG = CreateFolderOperation.class.getSimpleName();
  36. protected String mRemotePath;
  37. protected boolean mCreateFullPath;
  38. protected FileDataStorageManager mStorageManager;
  39. /**
  40. * Constructor
  41. *
  42. * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
  43. * @param storageManager Reference to the local database corresponding to the account where the file is contained.
  44. */
  45. public CreateFolderOperation(String remotePath, boolean createFullPath, FileDataStorageManager storageManager) {
  46. mRemotePath = remotePath;
  47. mCreateFullPath = createFullPath;
  48. mStorageManager = storageManager;
  49. }
  50. @Override
  51. protected RemoteOperationResult run(OwnCloudClient client) {
  52. CreateRemoteFolderOperation operation = new CreateRemoteFolderOperation(mRemotePath, mCreateFullPath);
  53. RemoteOperationResult result = operation.execute(client);
  54. if (result.isSuccess()) {
  55. saveFolderInDB();
  56. } else {
  57. Log_OC.e(TAG, mRemotePath + "hasn't been created");
  58. }
  59. return result;
  60. }
  61. @Override
  62. public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
  63. if (operation instanceof CreateRemoteFolderOperation) {
  64. onCreateRemoteFolderOperationFinish((CreateRemoteFolderOperation)operation, result);
  65. }
  66. }
  67. private void onCreateRemoteFolderOperationFinish(CreateRemoteFolderOperation operation, RemoteOperationResult result) {
  68. if (result.isSuccess()) {
  69. saveFolderInDB();
  70. } else {
  71. Log_OC.e(TAG, mRemotePath + "hasn't been created");
  72. }
  73. }
  74. /**
  75. * Save new directory in local database
  76. */
  77. public void saveFolderInDB() {
  78. OCFile newDir = new OCFile(mRemotePath);
  79. newDir.setMimetype("DIR");
  80. long parentId = mStorageManager.getFileByPath(FileStorageUtils.getParentPath(mRemotePath)).getFileId();
  81. newDir.setParentId(parentId);
  82. newDir.setModificationTimestamp(System.currentTimeMillis());
  83. mStorageManager.saveFile(newDir);
  84. Log_OC.d(TAG, "Create directory " + mRemotePath + " in Database");
  85. }
  86. }