CreateFolderOperation.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * @author masensio
  6. * Copyright (C) 2015 ownCloud Inc.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. package com.owncloud.android.operations;
  22. import com.owncloud.android.datamodel.OCFile;
  23. import com.owncloud.android.lib.common.OwnCloudClient;
  24. import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
  25. import com.owncloud.android.lib.common.operations.RemoteOperation;
  26. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  27. import com.owncloud.android.lib.common.utils.Log_OC;
  28. import com.owncloud.android.lib.resources.files.CreateRemoteFolderOperation;
  29. import com.owncloud.android.operations.common.SyncOperation;
  30. import com.owncloud.android.utils.FileStorageUtils;
  31. import com.owncloud.android.utils.MimeType;
  32. /**
  33. * Access to remote operation performing the creation of a new folder in the ownCloud server.
  34. * Save the new folder in Database
  35. */
  36. public class CreateFolderOperation extends SyncOperation implements OnRemoteOperationListener{
  37. private static final String TAG = CreateFolderOperation.class.getSimpleName();
  38. protected String mRemotePath;
  39. private boolean mCreateFullPath;
  40. /**
  41. * Constructor
  42. *
  43. * @param createFullPath 'True' means that all the ancestor folders should be created
  44. * if don't exist yet.
  45. */
  46. public CreateFolderOperation(String remotePath, boolean createFullPath) {
  47. mRemotePath = remotePath;
  48. mCreateFullPath = createFullPath;
  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(result);
  65. }
  66. }
  67. private void onCreateRemoteFolderOperationFinish(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. if (mCreateFullPath && getStorageManager().
  79. getFileByPath(FileStorageUtils.getParentPath(mRemotePath)) == null){// When parent
  80. // of remote path
  81. // is not created
  82. String[] subFolders = mRemotePath.split("/");
  83. String composedRemotePath = "/";
  84. // For each antecesor folders create them recursively
  85. for (String subFolder : subFolders) {
  86. if (!subFolder.isEmpty()) {
  87. composedRemotePath = composedRemotePath + subFolder + "/";
  88. mRemotePath = composedRemotePath;
  89. saveFolderInDB();
  90. }
  91. }
  92. } else { // Create directory on DB
  93. OCFile newDir = new OCFile(mRemotePath);
  94. newDir.setMimetype(MimeType.DIRECTORY);
  95. long parentId = getStorageManager().getFileByPath(FileStorageUtils.getParentPath(mRemotePath)).getFileId();
  96. newDir.setParentId(parentId);
  97. newDir.setModificationTimestamp(System.currentTimeMillis());
  98. getStorageManager().saveFile(newDir);
  99. Log_OC.d(TAG, "Create directory " + mRemotePath + " in Database");
  100. }
  101. }
  102. public String getRemotePath() {
  103. return mRemotePath;
  104. }
  105. }