diff options
author | steckbrief <steckbrief@chefmail.de> | 2016-09-29 11:57:16 +0200 |
---|---|---|
committer | steckbrief <steckbrief@chefmail.de> | 2016-09-29 11:57:16 +0200 |
commit | 34fcdda53fa8ae1174909b62860534d2d874eb72 (patch) | |
tree | 50564c1d946ef1779567c67ad25a646b446a6403 /src/main/java/de/thedevstack/conversationsplus/persistance/db | |
parent | 297bed106efdfa619d700ae44ce047d7f2663c93 (diff) |
Implements FS#235: Deletion of remote files uploaded via httpupload
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/thedevstack/conversationsplus/persistance/db/access/AbstractDatabaseAccess.java | 21 | ||||
-rw-r--r-- | src/main/java/de/thedevstack/conversationsplus/persistance/db/access/CursorHelper.java (renamed from src/main/java/de/thedevstack/conversationsplus/persistance/CursorHelper.java) | 32 | ||||
-rw-r--r-- | src/main/java/de/thedevstack/conversationsplus/persistance/db/access/MessageDatabaseAccess.java | 180 | ||||
-rw-r--r-- | src/main/java/de/thedevstack/conversationsplus/persistance/db/migrations/FileParamsBodyToDatabaseFieldsMigration.java | 101 |
4 files changed, 318 insertions, 16 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/persistance/db/access/AbstractDatabaseAccess.java b/src/main/java/de/thedevstack/conversationsplus/persistance/db/access/AbstractDatabaseAccess.java new file mode 100644 index 00000000..407859c7 --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/persistance/db/access/AbstractDatabaseAccess.java @@ -0,0 +1,21 @@ +package de.thedevstack.conversationsplus.persistance.db.access; + +import android.database.sqlite.SQLiteDatabase; + +/** + * Created by steckbrief on 24.08.2016. + */ +public abstract class AbstractDatabaseAccess { + static void executeAlterStatements(SQLiteDatabase db, String... alterStatements) { + for (String alterStatement : alterStatements) { + db.execSQL(alterStatement); + } + } + + static void addNewColumns(SQLiteDatabase db, String tableName, String... columnDefinitions) { + for (String columnDefinition : columnDefinitions) { + String alterStatement = "ALTER TABLE " + tableName + " ADD COLUMN " + columnDefinition; + db.execSQL(alterStatement); + } + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/persistance/CursorHelper.java b/src/main/java/de/thedevstack/conversationsplus/persistance/db/access/CursorHelper.java index 7e3fdab0..122e5160 100644 --- a/src/main/java/de/thedevstack/conversationsplus/persistance/CursorHelper.java +++ b/src/main/java/de/thedevstack/conversationsplus/persistance/db/access/CursorHelper.java @@ -1,4 +1,4 @@ -package de.thedevstack.conversationsplus.persistance; +package de.thedevstack.conversationsplus.persistance.db.access; import android.database.Cursor; @@ -7,7 +7,7 @@ import android.database.Cursor; */ public abstract class CursorHelper { - static double getDouble(Cursor cursor, String columnName) { + public static double getDouble(Cursor cursor, String columnName) { if (null == cursor) { return Double.MIN_VALUE; } @@ -18,7 +18,7 @@ public abstract class CursorHelper { return getDouble(cursor, columnIndex); } - static String getString(Cursor cursor, String columnName) { + public static String getString(Cursor cursor, String columnName) { if (null == cursor) { return null; } @@ -29,7 +29,7 @@ public abstract class CursorHelper { return getString(cursor, columnIndex); } - static float getFloat(Cursor cursor, String columnName) { + public static float getFloat(Cursor cursor, String columnName) { if (null == cursor) { return Float.MIN_VALUE; } @@ -40,7 +40,7 @@ public abstract class CursorHelper { return getFloat(cursor, columnIndex); } - static int getInt(Cursor cursor, String columnName) { + public static int getInt(Cursor cursor, String columnName) { if (null == cursor) { return Integer.MIN_VALUE; } @@ -51,7 +51,7 @@ public abstract class CursorHelper { return getInt(cursor, columnIndex); } - static long getLong(Cursor cursor, String columnName) { + public static long getLong(Cursor cursor, String columnName) { if (null == cursor) { return Long.MIN_VALUE; } @@ -62,7 +62,7 @@ public abstract class CursorHelper { return getLong(cursor, columnIndex); } - static int getShort(Cursor cursor, String columnName) { + public static int getShort(Cursor cursor, String columnName) { if (null == cursor) { return Short.MIN_VALUE; } @@ -73,7 +73,7 @@ public abstract class CursorHelper { return getShort(cursor, columnIndex); } - static byte[] getBlob(Cursor cursor, String columnName) { + public static byte[] getBlob(Cursor cursor, String columnName) { if (null == cursor) { return null; } @@ -94,7 +94,7 @@ public abstract class CursorHelper { * the column name does not exist. * @see Cursor#getColumnIndexOrThrow(String) */ - static int getColumnIndex(Cursor cursor, String columnName) { + public static int getColumnIndex(Cursor cursor, String columnName) { return cursor.getColumnIndex(columnName); } @@ -108,7 +108,7 @@ public abstract class CursorHelper { * @param columnIndex the zero-based index of the target column. * @return the value of that column as a byte array. */ - static byte[] getBlob(Cursor cursor, int columnIndex) { + public static byte[] getBlob(Cursor cursor, int columnIndex) { return cursor.getBlob(columnIndex); } @@ -122,7 +122,7 @@ public abstract class CursorHelper { * @param columnIndex the zero-based index of the target column. * @return the value of that column as a String. */ - static String getString(Cursor cursor, int columnIndex) { + public static String getString(Cursor cursor, int columnIndex) { return cursor.getString(columnIndex); } @@ -137,7 +137,7 @@ public abstract class CursorHelper { * @param columnIndex the zero-based index of the target column. * @return the value of that column as a short. */ - static short getShort(Cursor cursor, int columnIndex) { + public static short getShort(Cursor cursor, int columnIndex) { return cursor.getShort(columnIndex); } @@ -152,7 +152,7 @@ public abstract class CursorHelper { * @param columnIndex the zero-based index of the target column. * @return the value of that column as an int. */ - static int getInt(Cursor cursor, int columnIndex) { + public static int getInt(Cursor cursor, int columnIndex) { return cursor.getInt(columnIndex); } @@ -167,7 +167,7 @@ public abstract class CursorHelper { * @param columnIndex the zero-based index of the target column. * @return the value of that column as a long. */ - static long getLong(Cursor cursor, int columnIndex) { + public static long getLong(Cursor cursor, int columnIndex) { return cursor.getLong(columnIndex); } @@ -182,7 +182,7 @@ public abstract class CursorHelper { * @param columnIndex the zero-based index of the target column. * @return the value of that column as a float. */ - static float getFloat(Cursor cursor, int columnIndex) { + public static float getFloat(Cursor cursor, int columnIndex) { return cursor.getFloat(columnIndex); } @@ -197,7 +197,7 @@ public abstract class CursorHelper { * @param columnIndex the zero-based index of the target column. * @return the value of that column as a double. */ - static double getDouble(Cursor cursor, int columnIndex) { + public static double getDouble(Cursor cursor, int columnIndex) { return cursor.getDouble(columnIndex); } } diff --git a/src/main/java/de/thedevstack/conversationsplus/persistance/db/access/MessageDatabaseAccess.java b/src/main/java/de/thedevstack/conversationsplus/persistance/db/access/MessageDatabaseAccess.java new file mode 100644 index 00000000..51ad99a4 --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/persistance/db/access/MessageDatabaseAccess.java @@ -0,0 +1,180 @@ +package de.thedevstack.conversationsplus.persistance.db.access; + +import android.content.ContentValues; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; + +import de.thedevstack.android.logcat.Logging; +import de.thedevstack.conversationsplus.entities.FileParams; +import de.thedevstack.conversationsplus.entities.Message; +import de.thedevstack.conversationsplus.enums.FileStatus; +import de.thedevstack.conversationsplus.persistance.db.migrations.FileParamsBodyToDatabaseFieldsMigration; + +/** + * + */ +public class MessageDatabaseAccess extends AbstractDatabaseAccess { + // since cplus db version 1 + public static final String TABLE_NAME_ADDITIONAL_PARAMETERS = "message_parameters"; + private static final String COLUMN_NAME_MSG_PARAMS_HTTPUPLOAD = "httpupload"; + private static final String COLUMN_NAME_MSG_PARAMS_MSGUUID = "message_uuid"; + private static final String COLUMN_NAME_MSG_PARAMS_TREATASDOWNLOADABLE_DECISION = "treatasdownloadable_decision"; + // since cplus db version 3 + private static final String COLUMN_NAME_MSG_PARAMS_FILE_SIZE = "file_size"; + private static final String COLUMN_NAME_MSG_PARAMS_FILE_TYPE = "file_type"; + private static final String COLUMN_NAME_MSG_PARAMS_FILE_WIDTH = "file_width"; + private static final String COLUMN_NAME_MSG_PARAMS_FILE_HEIGHT = "file_height"; + private static final String COLUMN_NAME_MSG_PARAMS_FILE_STATUS = "file_status"; + private static final String COLUMN_NAME_MSG_PARAMS_FILE_NAME = "file_name"; + private static final String COLUMN_NAME_MSG_PARAMS_FILE_PATH = "file_path"; + private static final String COLUMN_NAME_MSG_PARAMS_FILE_URL = "file_url"; + + private static final String TABLE_ADDITIONAL_PARAMETERS_CREATE_V1 = "CREATE TABLE " + MessageDatabaseAccess.TABLE_NAME_ADDITIONAL_PARAMETERS + " (" + + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_MSGUUID + " TEXT, " + + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_HTTPUPLOAD + " INTEGER DEFAULT 0, " + + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_TREATASDOWNLOADABLE_DECISION + " TEXT DEFAULT 'NOT_DECIDED', " + + "FOREIGN KEY(" + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_MSGUUID + ") REFERENCES " + Message.TABLENAME + "(" + Message.UUID + ") ON DELETE CASCADE)"; + + private static final String TABLE_ADDITIONAL_PARAMETERS_CREATE = "CREATE TABLE " + MessageDatabaseAccess.TABLE_NAME_ADDITIONAL_PARAMETERS + " (" + + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_MSGUUID + " TEXT, " + + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_HTTPUPLOAD + " INTEGER DEFAULT 0, " + + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_TREATASDOWNLOADABLE_DECISION + " TEXT DEFAULT 'NOT_DECIDED', " + + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_TYPE + " TEXT DEFAULT NULL, " + + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_SIZE + " INTEGER DEFAULT 0, " + + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_HEIGHT + " INTEGER DEFAULT 0, " + + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_WIDTH + " INTEGER DEFAULT 0, " + + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_STATUS + " TEXT DEFAULT 'UNDEFINED', " + + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_NAME + " TEXT DEFAULT NULL, " + + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_PATH + " TEXT DEFAULT NULL, " + + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_URL + " TEXT DEFAULT NULL, " + + "FOREIGN KEY(" + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_MSGUUID + ") REFERENCES " + Message.TABLENAME + "(" + Message.UUID + ") ON DELETE CASCADE)"; + + public static void updateMessageParameters(SQLiteDatabase db, Message message, String uuid) { + String[] args = {uuid}; + db.update(MessageDatabaseAccess.TABLE_NAME_ADDITIONAL_PARAMETERS, MessageDatabaseAccess.getAdditionalParametersContentValues(message), MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_MSGUUID + "=?", args); + } + + public static ContentValues getAdditionalParametersContentValues(Message message) { + ContentValues additionalParameters = new ContentValues(); + additionalParameters.put(MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_MSGUUID, message.getUuid()); + additionalParameters.put(MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_HTTPUPLOAD, message.isHttpUploaded() ? 1 : 0); + additionalParameters.put(MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_TREATASDOWNLOADABLE_DECISION, message.treatAsDownloadable().name()); + if (null != message.getFileParams()) { + FileParams fileParams = message.getFileParams(); + additionalParameters.put(MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_TYPE, fileParams.getMimeType()); + additionalParameters.put(MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_SIZE, fileParams.getSize()); + additionalParameters.put(MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_HEIGHT, fileParams.getHeight()); + additionalParameters.put(MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_WIDTH, fileParams.getWidth()); + additionalParameters.put(MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_PATH, fileParams.getPath()); + additionalParameters.put(MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_URL, fileParams.getUrl()); + if (null != fileParams.getFileStatus()) { + additionalParameters.put(MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_STATUS, fileParams.getFileStatus().name()); + } + } + + return additionalParameters; + } + + private static void populateMessageParametersFromCursor(Cursor cursor, Message message) { + boolean isHttpUploaded = CursorHelper.getInt(cursor, COLUMN_NAME_MSG_PARAMS_HTTPUPLOAD) == 1; + message.setHttpUploaded(isHttpUploaded); + String downloadable = CursorHelper.getString(cursor, COLUMN_NAME_MSG_PARAMS_TREATASDOWNLOADABLE_DECISION); + Message.Decision treatAsDownloadable = Message.Decision.NOT_DECIDED; + try { + treatAsDownloadable = Message.Decision.valueOf(downloadable); + } catch (IllegalArgumentException e) { + // Should only happen if the database is corrupted, but to be on the save side catch it here + Logging.e("db.msg", "Unknown Decision for treatAsDownloadable found: '" + downloadable + "'"); + } + + message.setTreatAsDownloadable(treatAsDownloadable); + + if (message.hasFileAttached()) { + FileParams fileParams = new FileParams(message.getBody()); + String fileType = CursorHelper.getString(cursor, COLUMN_NAME_MSG_PARAMS_FILE_TYPE); + fileParams.setMimeType(fileType); + String name = CursorHelper.getString(cursor, COLUMN_NAME_MSG_PARAMS_FILE_NAME); + fileParams.setName(name); + String path = CursorHelper.getString(cursor, COLUMN_NAME_MSG_PARAMS_FILE_PATH); + fileParams.setPath(path); + String url = CursorHelper.getString(cursor, COLUMN_NAME_MSG_PARAMS_FILE_URL); + fileParams.setUrl(url); + long fileSize = CursorHelper.getLong(cursor, COLUMN_NAME_MSG_PARAMS_FILE_SIZE); + fileParams.setSize(fileSize); + int imageHeight = CursorHelper.getInt(cursor, COLUMN_NAME_MSG_PARAMS_FILE_HEIGHT); + fileParams.setHeight(imageHeight); + int imageWidth = CursorHelper.getInt(cursor, COLUMN_NAME_MSG_PARAMS_FILE_WIDTH); + fileParams.setWidth(imageWidth); + String status = CursorHelper.getString(cursor, COLUMN_NAME_MSG_PARAMS_FILE_STATUS); + if (null != status && !status.isEmpty()) { + FileStatus fileStatus = FileStatus.UNDEFINED; + try { + fileStatus = FileStatus.valueOf(status); + } catch (IllegalArgumentException e) { + // Should only happen if the database is corrupted, but to be on the save side catch it here + Logging.e("db.msg", "Unknown FileStatus for fileParams.fileStatus found: '" + status + "'"); + } + fileParams.setFileStatus(fileStatus); + } + message.setFileParams(fileParams); + } + } + + public static void populateMessageParameters(SQLiteDatabase db, Message message) { + Cursor paramsCursor = db.query(MessageDatabaseAccess.TABLE_NAME_ADDITIONAL_PARAMETERS, + null, MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_MSGUUID + "=?", + new String[] {message.getUuid()}, + null, null, null, null); + paramsCursor.moveToNext(); + MessageDatabaseAccess.populateMessageParametersFromCursor(paramsCursor, message); + paramsCursor.close(); + } + + public static void upgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + if (oldVersion < 1 && newVersion >= 1) { + Logging.d("db.upgrade.cplus", "Creating additional parameters table for messages."); + db.execSQL(MessageDatabaseAccess.TABLE_ADDITIONAL_PARAMETERS_CREATE_V1); + db.execSQL("INSERT INTO " + MessageDatabaseAccess.TABLE_NAME_ADDITIONAL_PARAMETERS + "(" + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_MSGUUID + ") " + + " SELECT " + Message.UUID + " FROM " + Message.TABLENAME); + } + if (oldVersion < 3 && newVersion >= 3) { + Logging.d("db.upgrade.cplus", "Upgrade " + MessageDatabaseAccess.TABLE_NAME_ADDITIONAL_PARAMETERS); + String[] columnDefinitions = { + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_TYPE + " TEXT DEFAULT NULL", + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_SIZE + " INTEGER DEFAULT 0", + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_HEIGHT + " INTEGER DEFAULT 0", + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_WIDTH + " INTEGER DEFAULT 0", + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_STATUS + " TEXT DEFAULT 'UNDEFINED'", + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_NAME + " TEXT DEFAULT NULL", + MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_PATH + " TEXT DEFAULT NULL", + }; + + addNewColumns(db, MessageDatabaseAccess.TABLE_NAME_ADDITIONAL_PARAMETERS, columnDefinitions); + + Logging.d("db.upgrade.cplus", "Migrate file params from message body to database fields"); + Cursor cursor = db.rawQuery("SELECT " + Message.UUID + ", " + Message.BODY + " FROM " + Message.TABLENAME + " WHERE " + Message.TYPE + "=? OR " + Message.TYPE + "=?", new String[] {String.valueOf(Message.TYPE_FILE), String.valueOf(Message.TYPE_IMAGE)}); + while (cursor.moveToNext()) { + String uuid = CursorHelper.getString(cursor, Message.UUID); + String body = CursorHelper.getString(cursor, Message.BODY); + FileParams fileParams = FileParamsBodyToDatabaseFieldsMigration.getFileParams(body); + String newBody = fileParams.getUrl(); + ContentValues values = new ContentValues(); + values.put(Message.BODY, newBody); + db.update(Message.TABLENAME, values, Message.UUID + "=?", new String[] {uuid}); + + ContentValues parameterValues = new ContentValues(); + parameterValues.put(MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_SIZE, fileParams.getSize()); + parameterValues.put(MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_HEIGHT, fileParams.getHeight()); + parameterValues.put(MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_FILE_WIDTH, fileParams.getWidth()); + + db.update(MessageDatabaseAccess.TABLE_NAME_ADDITIONAL_PARAMETERS, parameterValues, MessageDatabaseAccess.COLUMN_NAME_MSG_PARAMS_MSGUUID + "=?", new String[] {uuid}); + } + cursor.close(); + } + } + + public static void create(SQLiteDatabase db) { + // Create Conversations+ related tables + db.execSQL(MessageDatabaseAccess.TABLE_ADDITIONAL_PARAMETERS_CREATE); + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/persistance/db/migrations/FileParamsBodyToDatabaseFieldsMigration.java b/src/main/java/de/thedevstack/conversationsplus/persistance/db/migrations/FileParamsBodyToDatabaseFieldsMigration.java new file mode 100644 index 00000000..c0aa63c0 --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/persistance/db/migrations/FileParamsBodyToDatabaseFieldsMigration.java @@ -0,0 +1,101 @@ +package de.thedevstack.conversationsplus.persistance.db.migrations; + +import java.net.MalformedURLException; +import java.net.URL; + +import de.thedevstack.conversationsplus.entities.FileParams; + +/** + * Created by steckbrief on 24.08.2016. + */ +public class FileParamsBodyToDatabaseFieldsMigration { + + public static FileParams getFileParams(String body) { + FileParams params = getLegacyFileParams(body); + if (params != null) { + return params; + } + params = new FileParams(); + if (body == null) { + return params; + } + String parts[] = body.split("\\|"); + switch (parts.length) { + case 1: + try { + params.setSize(Long.parseLong(parts[0])); + } catch (NumberFormatException e) { + try { + URL url = new URL(parts[0]); + params.setUrl(url.toString()); + } catch (MalformedURLException e1) { + } + } + break; + case 2: + case 4: + try { + URL url = new URL(parts[0]); + params.setUrl(url.toString()); + } catch (MalformedURLException e1) { + } + try { + params.setSize(Long.parseLong(parts[1])); + } catch (NumberFormatException e) { + } + try { + params.setWidth(Integer.parseInt(parts[2])); + } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { + } + try { + params.setHeight(Integer.parseInt(parts[3])); + } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { + } + break; + case 3: + try { + params.setSize(Long.parseLong(parts[0])); + } catch (NumberFormatException e) { + } + try { + params.setWidth(Integer.parseInt(parts[1])); + } catch (NumberFormatException e) { + } + try { + params.setHeight(Integer.parseInt(parts[2])); + } catch (NumberFormatException e) { + } + break; + } + return params; + } + + private static FileParams getLegacyFileParams(String body) { + FileParams params = new FileParams(); + if (body == null) { + return params; + } + String parts[] = body.split(","); + if (parts.length == 3) { + try { + params.setSize(Long.parseLong(parts[0])); + } catch (NumberFormatException e) { + return null; + } + try { + params.setWidth(Integer.parseInt(parts[1])); + } catch (NumberFormatException e) { + return null; + } + try { + params.setHeight(Integer.parseInt(parts[2])); + } catch (NumberFormatException e) { + return null; + } + return params; + } else { + return null; + } + } + +} |