Refactoring: removed old FileDataStorageManager#moveFolder method, redirected to newer FileDataStorageManager#moveLocalFile

This commit is contained in:
David A. Velasco 2014-12-02 13:33:57 +01:00
parent 0f39ff841a
commit 5167e646ad
3 changed files with 25 additions and 120 deletions

View file

@ -586,109 +586,25 @@ public class FileDataStorageManager {
return success;
}
public void moveFolder(OCFile folder, String newPath) {
// TODO check newPath
}
/**
* Updates database for a folder that was moved to a different location.
* Updates database and file system for a file or folder that was moved to a different location.
*
* TODO explore better (faster) implementations
* TODO throw exceptions up !
*/
public void moveFolder(OCFile folder, String newPath) {
// TODO check newPath
if ( folder != null && folder.isFolder() &&
folder.fileExists() && !OCFile.ROOT_PATH.equals(folder.getFileName())
) {
/// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
Cursor c = null;
if (getContentProviderClient() != null) {
try {
c = getContentProviderClient().query (
ProviderTableMeta.CONTENT_URI,
null,
ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
ProviderTableMeta.FILE_PATH + " LIKE ? ",
new String[] { mAccount.name, folder.getRemotePath() + "%" },
ProviderTableMeta.FILE_PATH + " ASC "
);
} catch (RemoteException e) {
Log_OC.e(TAG, e.getMessage());
}
} else {
c = getContentResolver().query (
ProviderTableMeta.CONTENT_URI,
null,
ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
ProviderTableMeta.FILE_PATH + " LIKE ? ",
new String[] { mAccount.name, folder.getRemotePath() + "%" },
ProviderTableMeta.FILE_PATH + " ASC "
);
}
/// 2. prepare a batch of update operations to change all the descendants
ArrayList<ContentProviderOperation> operations =
new ArrayList<ContentProviderOperation>(c.getCount());
int lengthOfOldPath = folder.getRemotePath().length();
String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath;
if (c.moveToFirst()) {
do {
ContentValues cv = new ContentValues(); // keep the constructor in the loop
OCFile child = createFileInstance(c);
cv.put(
ProviderTableMeta.FILE_PATH,
newPath + child.getRemotePath().substring(lengthOfOldPath)
);
if ( child.getStoragePath() != null &&
child.getStoragePath().startsWith(defaultSavePath) ) {
cv.put(
ProviderTableMeta.FILE_STORAGE_PATH,
defaultSavePath + newPath +
child.getStoragePath().substring(lengthOfOldStoragePath)
);
}
operations.add(
ContentProviderOperation.
newUpdate(ProviderTableMeta.CONTENT_URI).
withValues(cv).
withSelection(
ProviderTableMeta._ID + "=?",
new String[] { String.valueOf(child.getFileId()) }
).
build()
);
} while (c.moveToNext());
}
c.close();
/// 3. apply updates in batch
try {
if (getContentResolver() != null) {
getContentResolver().applyBatch(MainApp.getAuthority(), operations);
} else {
getContentProviderClient().applyBatch(operations);
}
} catch (OperationApplicationException e) {
Log_OC.e(TAG, "Fail to update descendants of " +
folder.getFileId() + " in database", e);
} catch (RemoteException e) {
Log_OC.e(TAG, "Fail to update desendants of " +
folder.getFileId() + " in database", e);
}
}
}
public void moveLocalFile(OCFile file, String targetPath, String targetParentPath) {
if (file != null && file.fileExists() && !OCFile.ROOT_PATH.equals(file.getFileName())) {
OCFile targetParent = getFileByPath(targetParentPath);
if (targetParent == null) {
// TODO panic
throw new IllegalStateException("Parent folder of the target path does not exist!!");
}
/// 1. get all the descendants of the moved element in a single QUERY
@ -776,11 +692,7 @@ public class FileDataStorageManager {
}
} catch (Exception e) {
Log_OC.e(
TAG,
"Fail to update " + file.getFileId() + " and descendants in database",
e
);
Log_OC.e(TAG, "Fail to update " + file.getFileId() + " and descendants in database", e);
}
/// 4. move in local file system
@ -795,13 +707,18 @@ public class FileDataStorageManager {
}
renamed = localFile.renameTo(targetFile);
}
Log_OC.d(TAG, "Local file RENAMED : " + renamed);
// Notify MediaScanner about removed file
triggerMediaScan(file.getStoragePath());
// Notify MediaScanner about new file/folder
triggerMediaScan(defaultSavePath + targetPath);
if (renamed) {
if (file.isFolder()) {
} else {
// Notify MediaScanner about removed file
triggerMediaScan(file.getStoragePath());
// Notify MediaScanner about new file/folder
triggerMediaScan(defaultSavePath + targetPath);
}
}
Log_OC.d(TAG, "uri old: " + file.getStoragePath());
Log_OC.d(TAG, "uri new: " + defaultSavePath + targetPath);

View file

@ -44,7 +44,6 @@ public class RenameFileOperation extends SyncOperation {
private OCFile mFile;
private String mRemotePath;
private Account mAccount;
private String mNewName;
private String mNewRemotePath;
@ -57,9 +56,8 @@ public class RenameFileOperation extends SyncOperation {
* @param account OwnCloud account containing the remote file
* @param newName New name to set as the name of file.
*/
public RenameFileOperation(String remotePath, Account account, String newName) {
public RenameFileOperation(String remotePath, String newName) {
mRemotePath = remotePath;
mAccount = account;
mNewName = newName;
mNewRemotePath = null;
}
@ -103,7 +101,8 @@ public class RenameFileOperation extends SyncOperation {
if (result.isSuccess()) {
if (mFile.isFolder()) {
saveLocalDirectory();
getStorageManager().moveLocalFile(mFile, mNewRemotePath, parent);
//saveLocalDirectory();
} else {
saveLocalFile();
@ -118,17 +117,6 @@ public class RenameFileOperation extends SyncOperation {
return result;
}
private void saveLocalDirectory() {
getStorageManager().moveFolder(mFile, mNewRemotePath);
String localPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
File localDir = new File(localPath);
if (localDir.exists()) {
localDir.renameTo(new File(FileStorageUtils.getSavePath(mAccount.name) + mNewRemotePath));
// TODO - if renameTo fails, children files that are already down will result unlinked
}
}
private void saveLocalFile() {
mFile.setFileName(mNewName);

View file

@ -359,7 +359,7 @@ public class OperationsService extends Service {
// Rename file or folder
String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
String newName = operationIntent.getStringExtra(EXTRA_NEWNAME);
operation = new RenameFileOperation(remotePath, account, newName);
operation = new RenameFileOperation(remotePath, newName);
} else if (action.equals(ACTION_REMOVE)) {
// Remove file or folder