CreateFolderOperation.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. protected 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,
  53. mCreateFullPath);
  54. RemoteOperationResult result = operation.execute(client);
  55. if (result.isSuccess()) {
  56. saveFolderInDB();
  57. } else {
  58. Log_OC.e(TAG, mRemotePath + "hasn't been created");
  59. }
  60. return result;
  61. }
  62. @Override
  63. public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
  64. if (operation instanceof CreateRemoteFolderOperation) {
  65. onCreateRemoteFolderOperationFinish((CreateRemoteFolderOperation)operation, result);
  66. }
  67. }
  68. private void onCreateRemoteFolderOperationFinish(CreateRemoteFolderOperation operation,
  69. RemoteOperationResult result) {
  70. if (result.isSuccess()) {
  71. saveFolderInDB();
  72. } else {
  73. Log_OC.e(TAG, mRemotePath + "hasn't been created");
  74. }
  75. }
  76. /**
  77. * Save new directory in local database
  78. */
  79. public void saveFolderInDB() {
  80. if (mCreateFullPath && getStorageManager().
  81. getFileByPath(FileStorageUtils.getParentPath(mRemotePath)) == null){// When parent
  82. // of remote path
  83. // is not created
  84. String[] subFolders = mRemotePath.split("/");
  85. String composedRemotePath = "/";
  86. // For each antecesor folders create them recursively
  87. for (int i=0; i<subFolders.length; i++) {
  88. String subFolder = subFolders[i];
  89. if (!subFolder.isEmpty()) {
  90. composedRemotePath = composedRemotePath + subFolder + "/";
  91. mRemotePath = composedRemotePath;
  92. saveFolderInDB();
  93. }
  94. }
  95. } else { // Create directory on DB
  96. OCFile newDir = new OCFile(mRemotePath);
  97. newDir.setMimetype(MimeType.DIRECTORY);
  98. long parentId = getStorageManager().
  99. getFileByPath(FileStorageUtils.getParentPath(mRemotePath)).getFileId();
  100. newDir.setParentId(parentId);
  101. newDir.setModificationTimestamp(System.currentTimeMillis());
  102. getStorageManager().saveFile(newDir);
  103. Log_OC.d(TAG, "Create directory " + mRemotePath + " in Database");
  104. }
  105. }
  106. public String getRemotePath() {
  107. return mRemotePath;
  108. }
  109. }