From a28117397d1dcb5e6d5b56b970727dde11ff5074 Mon Sep 17 00:00:00 2001 From: Kanahia Date: Mon, 8 Jul 2024 06:56:09 +0530 Subject: [PATCH] Changed primary key from location to entity id --- .../locations/BookmarkLocationsDao.java | 16 ++++++++++++++-- .../fr/free/nrw/commons/nearby/Place.java | 19 +++++++++++++++---- .../fr/free/nrw/commons/nearby/PlaceDao.java | 4 ++-- .../commons/nearby/PlacesLocalDataSource.java | 4 ++-- .../nrw/commons/nearby/PlacesRepository.java | 4 ++-- .../fragments/NearbyParentFragment.java | 2 +- .../fr/free/nrw/commons/utils/PlaceUtils.java | 3 ++- 7 files changed, 38 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/bookmarks/locations/BookmarkLocationsDao.java b/app/src/main/java/fr/free/nrw/commons/bookmarks/locations/BookmarkLocationsDao.java index a55ae5e0d..be2af065f 100644 --- a/app/src/main/java/fr/free/nrw/commons/bookmarks/locations/BookmarkLocationsDao.java +++ b/app/src/main/java/fr/free/nrw/commons/bookmarks/locations/BookmarkLocationsDao.java @@ -165,7 +165,8 @@ public class BookmarkLocationsDao { cursor.getString(cursor.getColumnIndex(Table.COLUMN_CATEGORY)), builder.build(), cursor.getString(cursor.getColumnIndex(Table.COLUMN_PIC)), - Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(Table.COLUMN_EXISTS))) + Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(Table.COLUMN_EXISTS))), + cursor.getString(cursor.getColumnIndex(Table.COLUMN_ENTITY_ID)) ); } @@ -184,6 +185,7 @@ public class BookmarkLocationsDao { cv.put(BookmarkLocationsDao.Table.COLUMN_LONG, bookmarkLocation.location.getLongitude()); cv.put(BookmarkLocationsDao.Table.COLUMN_PIC, bookmarkLocation.pic); cv.put(BookmarkLocationsDao.Table.COLUMN_EXISTS, bookmarkLocation.exists.toString()); + cv.put(BookmarkLocationsDao.Table.COLUMN_ENTITY_ID, bookmarkLocation.entityID); return cv; } @@ -204,6 +206,7 @@ public class BookmarkLocationsDao { static final String COLUMN_COMMONS_LINK = "location_commons_link"; static final String COLUMN_PIC = "location_pic"; static final String COLUMN_EXISTS = "location_exists"; + static final String COLUMN_ENTITY_ID = "location_entity_id"; // NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES. public static final String[] ALL_FIELDS = { @@ -221,6 +224,7 @@ public class BookmarkLocationsDao { COLUMN_COMMONS_LINK, COLUMN_PIC, COLUMN_EXISTS, + COLUMN_ENTITY_ID }; static final String DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS " + TABLE_NAME; @@ -239,7 +243,8 @@ public class BookmarkLocationsDao { + COLUMN_WIKIDATA_LINK + " STRING," + COLUMN_COMMONS_LINK + " STRING," + COLUMN_PIC + " STRING," - + COLUMN_EXISTS + " STRING" + + COLUMN_EXISTS + " STRING," + + COLUMN_ENTITY_ID + " STRING" + ");"; public static void onCreate(SQLiteDatabase db) { @@ -306,6 +311,13 @@ public class BookmarkLocationsDao { Timber.e(exception); } } + if (from >= 15){ + try { + db.execSQL("ALTER TABLE bookmarksLocations ADD COLUMN location_entity_id STRING;"); + } catch (SQLiteException exception){ + Timber.e(exception); + } + } } } } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/Place.java b/app/src/main/java/fr/free/nrw/commons/nearby/Place.java index dfa367938..717f74d29 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/Place.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/Place.java @@ -24,8 +24,9 @@ public class Place implements Parcelable { public String name; private Label label; private String longDescription; - @PrimaryKey @NonNull public LatLng location; + @PrimaryKey @NonNull + public String entityID; private String category; public String pic; // exists boolean will tell whether the place exists or not, @@ -47,10 +48,11 @@ public class Place implements Parcelable { pic = null; exists = null; siteLinks = null; + entityID = null; } public Place(String language, String name, Label label, String longDescription, LatLng location, - String category, Sitelinks siteLinks, String pic, Boolean exists) { + String category, Sitelinks siteLinks, String pic, Boolean exists, String entityID) { this.language = language; this.name = name; this.label = label; @@ -60,10 +62,11 @@ public class Place implements Parcelable { this.siteLinks = siteLinks; this.pic = (pic == null) ? "" : pic; this.exists = exists; + this.entityID = entityID; } public Place(String name, String longDescription, LatLng location, String category, - Sitelinks siteLinks, String pic, String thumb) { + Sitelinks siteLinks, String pic, String thumb, String entityID) { this.name = name; this.longDescription = longDescription; this.location = location; @@ -74,6 +77,7 @@ public class Place implements Parcelable { this.language = null; this.label = null; this.exists = true; + this.entityID = entityID; } public Place(Parcel in) { @@ -89,6 +93,7 @@ public class Place implements Parcelable { String existString = in.readString(); this.exists = Boolean.parseBoolean(existString); this.isMonument = in.readInt() == 1; + this.entityID = in.readString(); } public static Place from(NearbyResultItem item) { @@ -97,6 +102,10 @@ public class Place implements Parcelable { if (!StringUtils.isBlank(itemClass)) { classEntityId = itemClass.replace("http://www.wikidata.org/entity/", ""); } + String entityId = ""; + if (!StringUtils.isBlank(item.getItem().getValue())){ + entityId = item.getItem().getValue().replace("http://www.wikidata.org/entity/", ""); + } // Set description when not null and not empty String description = (item.getDescription().getValue() != null && !item.getDescription().getValue() @@ -129,7 +138,7 @@ public class Place implements Parcelable { .build(), item.getPic().getValue(), // Checking if the place exists or not - (item.getDestroyed().getValue() == "") && (item.getEndTime().getValue() == "")); + (item.getDestroyed().getValue() == "") && (item.getEndTime().getValue() == ""), entityId); } /** @@ -296,6 +305,7 @@ public class Place implements Parcelable { ", siteLinks='" + siteLinks.toString() + '\'' + ", pic='" + pic + '\'' + ", exists='" + exists.toString() + '\'' + + ", entityID='" + entityID + '\'' + '}'; } @@ -314,6 +324,7 @@ public class Place implements Parcelable { dest.writeString(category); dest.writeParcelable(siteLinks, 0); dest.writeString(pic); + dest.writeString(entityID); dest.writeString(exists.toString()); dest.writeInt(isMonument ? 1 : 0); } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/PlaceDao.java b/app/src/main/java/fr/free/nrw/commons/nearby/PlaceDao.java index 777ac5e41..1431dc2eb 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/PlaceDao.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/PlaceDao.java @@ -12,8 +12,8 @@ public abstract class PlaceDao { @Insert(onConflict = OnConflictStrategy.REPLACE) public abstract void saveSynchronous(Place place); - @Query("SELECT * from place WHERE location=:l") - public abstract Place getPlace(LatLng l); + @Query("SELECT * from place WHERE entityID=:entity") + public abstract Place getPlace(String entity); public Completable save(final Place place) { return Completable diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/PlacesLocalDataSource.java b/app/src/main/java/fr/free/nrw/commons/nearby/PlacesLocalDataSource.java index 985331ee4..099b7e5d3 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/PlacesLocalDataSource.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/PlacesLocalDataSource.java @@ -14,8 +14,8 @@ public class PlacesLocalDataSource { this.placeDao = placeDao; } - public Place fetchPlace(LatLng latLng){ - return placeDao.getPlace(latLng); + public Place fetchPlace(String entityID){ + return placeDao.getPlace(entityID); } public Completable savePlace(Place place) { diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/PlacesRepository.java b/app/src/main/java/fr/free/nrw/commons/nearby/PlacesRepository.java index e6a485ca9..40fc9eb4d 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/PlacesRepository.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/PlacesRepository.java @@ -18,8 +18,8 @@ public class PlacesRepository { return localDataSource.savePlace(place); } - public Place fetchPlace(LatLng latLng){ - return localDataSource.fetchPlace(latLng); + public Place fetchPlace(String entityID){ + return localDataSource.fetchPlace(entityID); } } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java index daa57d61d..0bdebad61 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java @@ -1409,7 +1409,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment for (Place place : batch) { Observable placeObservable = Observable .fromCallable(() -> { - Place fetchedPlace = placesRepository.fetchPlace(place.location); + Place fetchedPlace = placesRepository.fetchPlace(place.entityID); return fetchedPlace != null ? fetchedPlace : place; }) .subscribeOn(Schedulers.io()) diff --git a/app/src/main/java/fr/free/nrw/commons/utils/PlaceUtils.java b/app/src/main/java/fr/free/nrw/commons/utils/PlaceUtils.java index b9da196fe..f1022a041 100644 --- a/app/src/main/java/fr/free/nrw/commons/utils/PlaceUtils.java +++ b/app/src/main/java/fr/free/nrw/commons/utils/PlaceUtils.java @@ -47,7 +47,8 @@ public class PlaceUtils { .setWikidataLink("") // we don't necessarily have them, can be fetched later .build(), media.getImageUrl(), - media.getThumbUrl())); + media.getThumbUrl(), + "")); } return explorePlaceList; }