diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionDao.java b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionDao.java index 4c83af393..2c064deed 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionDao.java +++ b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionDao.java @@ -16,8 +16,10 @@ import javax.inject.Named; import javax.inject.Provider; import fr.free.nrw.commons.settings.Prefs; +import fr.free.nrw.commons.utils.StringUtils; import static fr.free.nrw.commons.contributions.ContributionDao.Table.ALL_FIELDS; +import static fr.free.nrw.commons.contributions.ContributionDao.Table.COLUMN_WIKI_DATA_ENTITY_ID; import static fr.free.nrw.commons.contributions.ContributionsContentProvider.BASE_URI; import static fr.free.nrw.commons.contributions.ContributionsContentProvider.uriForId; @@ -109,6 +111,7 @@ public class ContributionDao { cv.put(Table.COLUMN_WIDTH, contribution.getWidth()); cv.put(Table.COLUMN_HEIGHT, contribution.getHeight()); cv.put(Table.COLUMN_LICENSE, contribution.getLicense()); + cv.put(Table.COLUMN_WIKI_DATA_ENTITY_ID, contribution.getWikiDataEntityId()); return cv; } @@ -122,7 +125,7 @@ public class ContributionDao { } else { index = cursor.getColumnIndex(Table.COLUMN_LICENSE); } - return new Contribution( + Contribution contribution = new Contribution( uriForId(cursor.getInt(cursor.getColumnIndex(Table.COLUMN_ID))), cursor.getString(cursor.getColumnIndex(Table.COLUMN_FILENAME)), parseUri(cursor.getString(cursor.getColumnIndex(Table.COLUMN_LOCAL_URI))), @@ -140,6 +143,13 @@ public class ContributionDao { cursor.getInt(cursor.getColumnIndex(Table.COLUMN_HEIGHT)), cursor.getString(index) ); + + String wikidataEntityId = cursor.getString(cursor.getColumnIndex(COLUMN_WIKI_DATA_ENTITY_ID)); + if (!StringUtils.isNullOrWhiteSpace(wikidataEntityId)) { + contribution.setWikiDataEntityId(wikidataEntityId); + } + + return contribution; } return null; @@ -174,6 +184,7 @@ public class ContributionDao { public static final String COLUMN_WIDTH = "width"; public static final String COLUMN_HEIGHT = "height"; public static final String COLUMN_LICENSE = "license"; + public static final String COLUMN_WIKI_DATA_ENTITY_ID = "wikidataEntityID"; // NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES. public static final String[] ALL_FIELDS = { @@ -192,7 +203,8 @@ public class ContributionDao { COLUMN_MULTIPLE, COLUMN_WIDTH, COLUMN_HEIGHT, - COLUMN_LICENSE + COLUMN_LICENSE, + COLUMN_WIKI_DATA_ENTITY_ID }; public static final String DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS " + TABLE_NAME; @@ -213,7 +225,8 @@ public class ContributionDao { + "multiple INTEGER," + "width INTEGER," + "height INTEGER," - + "LICENSE STRING" + + "LICENSE STRING," + + "wikidataEntityID STRING" + ");"; // Upgrade from version 1 -> @@ -232,6 +245,9 @@ public class ContributionDao { static final String ADD_LICENSE_FIELD = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN license STRING;"; static final String SET_DEFAULT_LICENSE = "UPDATE " + TABLE_NAME + " SET license='" + Prefs.Licenses.CC_BY_SA_3 + "';"; + // Upgrade from version 8 -> + static final String ADD_WIKI_DATA_ENTITY_ID_FIELD = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN wikidataEntityID STRING;"; + public static void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_STATEMENT); @@ -284,6 +300,13 @@ public class ContributionDao { onUpdate(db, from, to); return; } + if (from == 8) { + // Added place field + db.execSQL(ADD_WIKI_DATA_ENTITY_ID_FIELD); + from++; + onUpdate(db, from, to); + return; + } } } } diff --git a/app/src/main/java/fr/free/nrw/commons/data/DBOpenHelper.java b/app/src/main/java/fr/free/nrw/commons/data/DBOpenHelper.java index 076eacf75..c9ecce289 100644 --- a/app/src/main/java/fr/free/nrw/commons/data/DBOpenHelper.java +++ b/app/src/main/java/fr/free/nrw/commons/data/DBOpenHelper.java @@ -14,7 +14,7 @@ import fr.free.nrw.commons.modifications.ModifierSequenceDao; public class DBOpenHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "commons.db"; - private static final int DATABASE_VERSION = 8; + private static final int DATABASE_VERSION = 9; /** * Do not use directly - @Inject an instance where it's needed and let diff --git a/app/src/test/kotlin/fr/free/nrw/commons/contributions/ContributionDaoTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/contributions/ContributionDaoTest.kt index 00bb92830..124886b74 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/contributions/ContributionDaoTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/contributions/ContributionDaoTest.kt @@ -119,6 +119,15 @@ class ContributionDaoTest { verifyZeroInteractions(database) } + @Test + fun migrateTableVersionFrom_v8_to_v9() { + Table.onUpdate(database, 8, 9) + // Table changed in version 9 + inOrder(database) { + verify(database).execSQL(Table.ADD_WIKI_DATA_ENTITY_ID_FIELD) + } + } + @Test fun saveNewContribution_nonNullFields() { whenever(client.insert(isA(), isA())).thenReturn(contentUri) @@ -328,19 +337,22 @@ class ContributionDaoTest { MatrixCursor(Table.ALL_FIELDS, 1).apply { addRow(listOf("111", "file", localUri, "image", created, STATE_QUEUED, 222L, uploaded, 88L, SOURCE_GALLERY, "desc", - "create", if (multiple) 1 else 0, 640, 480, "007")) + "create", if (multiple) 1 else 0, 640, 480, "007", "Q1")) moveToFirst() } - private fun createContribution(isMultiple: Boolean, localUri: Uri?, imageUrl: String?, dateUploaded: Date?, filename: String?) = - Contribution(localUri, imageUrl, filename, "desc", 222L, Date(321L), dateUploaded, - "create", "edit", "coords").apply { - state = STATE_COMPLETED - transferred = 333L - source = SOURCE_CAMERA - license = "007" - multiple = isMultiple - width = 640 - height = 480 // VGA should be enough for anyone, right? - } + private fun createContribution(isMultiple: Boolean, localUri: Uri?, imageUrl: String?, dateUploaded: Date?, filename: String?): Contribution { + val contribution = Contribution(localUri, imageUrl, filename, "desc", 222L, Date(321L), dateUploaded, + "create", "edit", "coords").apply { + state = STATE_COMPLETED + transferred = 333L + source = SOURCE_CAMERA + license = "007" + multiple = isMultiple + width = 640 + height = 480 // VGA should be enough for anyone, right? + } + contribution.wikiDataEntityId = "Q1" + return contribution + } } \ No newline at end of file