X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/1070273dafa6b522a1ff47e4ca796e13f1652265..a032bdeebc51a6e81d1bd5c558944f96fc55eacb:/src/eu/alefzero/owncloud/datamodel/FileDataStorageManager.java diff --git a/src/eu/alefzero/owncloud/datamodel/FileDataStorageManager.java b/src/eu/alefzero/owncloud/datamodel/FileDataStorageManager.java index 3733234a..43af7468 100644 --- a/src/eu/alefzero/owncloud/datamodel/FileDataStorageManager.java +++ b/src/eu/alefzero/owncloud/datamodel/FileDataStorageManager.java @@ -19,16 +19,24 @@ package eu.alefzero.owncloud.datamodel; import java.io.File; +import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; +import java.util.List; import java.util.Vector; +import eu.alefzero.owncloud.db.ProviderMeta; import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta; import android.accounts.Account; import android.content.ContentProviderClient; +import android.content.ContentProviderOperation; +import android.content.ContentProviderResult; import android.content.ContentResolver; import android.content.ContentValues; +import android.content.OperationApplicationException; import android.database.Cursor; import android.net.Uri; +import android.os.Environment; import android.os.RemoteException; import android.util.Log; @@ -58,8 +66,8 @@ public class FileDataStorageManager implements DataStorageManager { OCFile file = null; if (c.moveToFirst()) { file = createFileInstance(c); - c.close(); } + c.close(); return file; } @@ -69,8 +77,8 @@ public class FileDataStorageManager implements DataStorageManager { OCFile file = null; if (c.moveToFirst()) { file = createFileInstance(c); - c.close(); } + c.close(); return file; } @@ -96,14 +104,17 @@ public class FileDataStorageManager implements DataStorageManager { if (file.getParentId() != 0) cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId()); cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath()); - cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); + if (!file.isDirectory()) + cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDate()); + cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0); if (fileExists(file.getRemotePath())) { OCFile tmpfile = getFileByPath(file.getRemotePath()); file.setStoragePath(tmpfile.getStoragePath()); - cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); + if (!file.isDirectory()); + cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); file.setFileId(tmpfile.getFileId()); overriden = true; @@ -151,6 +162,86 @@ public class FileDataStorageManager implements DataStorageManager { return overriden; } + + @Override + public void saveFiles(List files) { + + Iterator filesIt = files.iterator(); + ArrayList operations = new ArrayList(files.size()); + OCFile file = null; + + // prepare operations to perform + while (filesIt.hasNext()) { + file = filesIt.next(); + ContentValues cv = new ContentValues(); + cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp()); + cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp()); + cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength()); + cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype()); + cv.put(ProviderTableMeta.FILE_NAME, file.getFileName()); + if (file.getParentId() != 0) + cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId()); + cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath()); + if (!file.isDirectory()) + cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); + cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name); + cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDate()); + cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0); + + if (fileExists(file.getRemotePath())) { + OCFile tmpfile = getFileByPath(file.getRemotePath()); + file.setStoragePath(tmpfile.getStoragePath()); + if (!file.isDirectory()); + cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); + file.setFileId(tmpfile.getFileId()); + + operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). + withValues(cv). + withSelection( ProviderTableMeta._ID + "=?", + new String[] { String.valueOf(file.getFileId()) }) + .build()); + + } else { + operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).withValues(cv).build()); + } + } + + // apply operations in batch + ContentProviderResult[] results = null; + try { + if (getContentResolver() != null) { + results = getContentResolver().applyBatch(ProviderMeta.AUTHORITY_FILES, operations); + + } else { + results = getContentProvider().applyBatch(operations); + } + + } catch (OperationApplicationException e) { + Log.e(TAG, "Fail to update/insert list of files to database " + e.getMessage()); + + } catch (RemoteException e) { + Log.e(TAG, "Fail to update/insert list of files to database " + e.getMessage()); + } + + // update new id in file objects for insertions + if (results != null) { + long newId; + for (int i=0; i