Skip to content

Commit f7b1b14

Browse files
authored
#3664 Stop using JsonObject on StructuredData (#3672)
* #3664 Stop using JsonObject on StructuredData - remove usage in Media classes - remove from depicts client - create partial network models * #3664 Stop using JsonObject on StructuredData - allow partial mapping of polymorphic models by returning null in typeadapter * #3664 Stop using JsonObject on StructuredData - use models for editing depicts property * #3664 Stop using JsonObject on StructuredData - use models for sparql parent query * #3664 Stop using JsonObject on StructuredData - fix unit test compilation * #3664 Stop using JsonObject on StructuredData - unify sparql responses * #3664 Stop using JsonObject on StructuredData - minor cleanup of misnamed/unused/too broad visibility * #3664 Stop using JsonObject on StructuredData - share variable names and logic for the Sarql queries * #3664 Stop using JsonObject on StructuredData - add error logging
1 parent b20dec0 commit f7b1b14

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+871
-985
lines changed

app/src/main/java/fr/free/nrw/commons/Media.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
import androidx.annotation.Nullable;
88
import androidx.room.Entity;
99
import fr.free.nrw.commons.location.LatLng;
10+
import fr.free.nrw.commons.media.Depictions;
1011
import fr.free.nrw.commons.utils.CommonsDateUtil;
1112
import fr.free.nrw.commons.utils.MediaDataExtractorUtil;
1213
import java.text.ParseException;
1314
import java.util.ArrayList;
1415
import java.util.Date;
1516
import java.util.List;
1617
import java.util.Locale;
17-
import java.util.Map;
1818
import org.apache.commons.lang3.StringUtils;
1919
import org.wikipedia.dataclient.mwapi.MwQueryPage;
2020
import org.wikipedia.gallery.ExtMetadata;
@@ -56,7 +56,7 @@ public class Media implements Parcelable {
5656
* Depicts is a feature part of Structured data. Multiple Depictions can be added for an image just like categories.
5757
* However unlike categories depictions is multi-lingual
5858
*/
59-
private List<Map<String, String>> depictionList= new ArrayList<>();
59+
private Depictions depictions;
6060
private boolean requestedDeletion;
6161
@Nullable private LatLng coordinates;
6262

@@ -305,12 +305,10 @@ public String getCaption() {
305305
/**
306306
* @return depictions associated with the current media
307307
*/
308-
public List<Map<String, String>> getDepiction() {
309-
return depictionList;
308+
public Depictions getDepiction() {
309+
return depictions;
310310
}
311311

312-
313-
314312
/**
315313
* Sets the file description.
316314
* @param description the new description of the file
@@ -489,8 +487,8 @@ public void setCaption(String caption) {
489487
}
490488

491489
/* Sets depictions for the current media obtained fro Wikibase API*/
492-
public void setDepictionList(List<Map<String, String>> depictions) {
493-
this.depictionList = depictions;
490+
public void setDepictions(Depictions depictions) {
491+
this.depictions = depictions;
494492
}
495493

496494
public void setLocalUri(@Nullable final Uri localUri) {
@@ -509,8 +507,8 @@ public void setLicenseUrl(final String licenseUrl) {
509507
this.licenseUrl = licenseUrl;
510508
}
511509

512-
public List<Map<String, String>> getDepictionList() {
513-
return depictionList;
510+
public Depictions getDepictions() {
511+
return depictions;
514512
}
515513

516514
@Override
@@ -542,7 +540,7 @@ public void writeToParcel(Parcel dest, int flags) {
542540
dest.writeString(this.creator);
543541
dest.writeString(this.pageId);
544542
dest.writeStringList(this.categories);
545-
dest.writeList(this.depictionList);
543+
dest.writeParcelable(this.depictions, flags);
546544
dest.writeByte(this.requestedDeletion ? (byte) 1 : (byte) 0);
547545
dest.writeParcelable(this.coordinates, flags);
548546
}
@@ -568,7 +566,7 @@ protected Media(Parcel in) {
568566
final ArrayList<String> list = new ArrayList<>();
569567
in.readStringList(list);
570568
this.categories=list;
571-
in.readList(depictionList,null);
569+
in.readParcelable(Depictions.class.getClassLoader());
572570
this.requestedDeletion = in.readByte() != 0;
573571
this.coordinates = in.readParcelable(LatLng.class.getClassLoader());
574572
}

app/src/main/java/fr/free/nrw/commons/MediaDataExtractor.java

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
import static fr.free.nrw.commons.depictions.Media.DepictedImagesFragment.PAGE_ID_PREFIX;
44

55
import androidx.core.text.HtmlCompat;
6-
import com.google.gson.JsonArray;
7-
import com.google.gson.JsonObject;
6+
import fr.free.nrw.commons.media.Depictions;
87
import fr.free.nrw.commons.media.MediaClient;
98
import io.reactivex.Single;
10-
import java.util.ArrayList;
11-
import java.util.HashMap;
12-
import java.util.Map;
139
import javax.inject.Inject;
1410
import javax.inject.Singleton;
1511
import org.jetbrains.annotations.NotNull;
@@ -24,10 +20,6 @@
2420
@Singleton
2521
public class MediaDataExtractor {
2622

27-
private static final int LABEL_BEGIN_INDEX = 3;
28-
private static final int LABEL_END_OFFSET = 3;
29-
private static final int ID_BEGIN_INDEX = 1;
30-
private static final int ID_END_OFFSET = 1;
3123
private final MediaClient mediaClient;
3224

3325
@Inject
@@ -53,10 +45,10 @@ public Single<Media> fetchMediaDetails(final String filename, final String pageI
5345

5446
@NotNull
5547
private Media combineToMedia(final Media media, final Boolean deletionStatus, final String discussion,
56-
final String caption, final JsonObject depiction) {
48+
final String caption, final Depictions depictions) {
5749
media.setDiscussion(discussion);
5850
media.setCaption(caption);
59-
media.setDepictionList(formatDepictions(depiction));
51+
media.setDepictions(depictions);
6052
if (deletionStatus) {
6153
media.setRequestedDeletion(true);
6254
}
@@ -75,39 +67,12 @@ private Single<String> getCaption(final String wikibaseIdentifier) {
7567
}
7668

7769
/**
78-
* From the Json Object extract depictions into an array list
79-
* @param mediaResponse
80-
* @return List containing map for depictions, the map has two keys,
81-
* first key is for the label and second is for the url of the item
82-
*/
83-
private ArrayList<Map<String, String>> formatDepictions(final JsonObject mediaResponse) {
84-
try {
85-
final JsonArray depictionArray = (JsonArray) mediaResponse.get("Depiction");
86-
final ArrayList<Map<String, String>> depictedItemList = new ArrayList<>();
87-
for (int i = 0; i <depictionArray.size() ; i++) {
88-
final JsonObject depictedItem = (JsonObject) depictionArray.get(i);
89-
final Map <String, String> depictedObject = new HashMap<>();
90-
final String label = depictedItem.get("label").toString();
91-
final String id = depictedItem.get("id").toString();
92-
final String transformedLabel = label.substring(LABEL_BEGIN_INDEX, label.length()- LABEL_END_OFFSET);
93-
final String transformedId = id.substring(ID_BEGIN_INDEX,id.length() - ID_END_OFFSET);
94-
depictedObject.put("label", transformedLabel); //remove the additional characters obtained in label and ID object to extract the relevant string (since the string also contains extra quites that are not required)
95-
depictedObject.put("id", transformedId);
96-
depictedItemList.add(depictedObject);
97-
}
98-
return depictedItemList;
99-
} catch (final ClassCastException | NullPointerException ignore) {
100-
return new ArrayList<>();
101-
}
102-
}
103-
104-
/**
105-
* Fetch caption and depictions from the MediaWiki API
70+
* Fetch depictions from the MediaWiki API
10671
* @param filename the filename we will return the caption for
107-
* @return a map containing caption and depictions (empty string in the map if no caption/depictions)
72+
* @return Depictions
10873
*/
109-
private Single<JsonObject> getDepictions(final String filename) {
110-
return mediaClient.getCaptionAndDepictions(filename)
74+
private Single<Depictions> getDepictions(final String filename) {
75+
return mediaClient.getDepictions(filename)
11176
.doOnError(throwable -> Timber.e(throwable, "error while fetching depictions"));
11277
}
11378

app/src/main/java/fr/free/nrw/commons/db/Converters.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import fr.free.nrw.commons.CommonsApplication;
88
import fr.free.nrw.commons.di.ApplicationlessInjection;
99
import fr.free.nrw.commons.location.LatLng;
10+
import fr.free.nrw.commons.media.Depictions;
1011
import fr.free.nrw.commons.upload.WikidataPlace;
1112
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem;
1213
import java.util.Date;
@@ -72,16 +73,6 @@ public static LatLng stringToLatLng(String objectList) {
7273
return readObjectFromString(objectList,LatLng.class);
7374
}
7475

75-
@TypeConverter
76-
public static String listOfMapToString(List<Map<String,String>> listOfMaps) {
77-
return writeObjectToString(listOfMaps);
78-
}
79-
80-
@TypeConverter
81-
public static List<Map<String,String>> stringToListOfMap(String listOfMaps) {
82-
return readObjectWithTypeToken(listOfMaps, new TypeToken<List<Map<String, String>>>() {});
83-
}
84-
8576
@TypeConverter
8677
public static String wikidataPlaceToString(WikidataPlace wikidataPlace) {
8778
return writeObjectToString(wikidataPlace);
@@ -102,6 +93,16 @@ public static List<DepictedItem> stringToList(String depictedItems) {
10293
return readObjectWithTypeToken(depictedItems, new TypeToken<List<DepictedItem>>() {});
10394
}
10495

96+
@TypeConverter
97+
public static String depictionsToString(Depictions depictedItems) {
98+
return writeObjectToString(depictedItems);
99+
}
100+
101+
@TypeConverter
102+
public static Depictions stringToDepictions(String depictedItems) {
103+
return readObjectFromString(depictedItems, Depictions.class);
104+
}
105+
105106
private static String writeObjectToString(Object object) {
106107
return object == null ? null : getGson().toJson(object);
107108
}

app/src/main/java/fr/free/nrw/commons/depictions/DepictionModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import dagger.Module;
55
import fr.free.nrw.commons.depictions.Media.DepictedImagesContract;
66
import fr.free.nrw.commons.depictions.Media.DepictedImagesPresenter;
7-
import fr.free.nrw.commons.depictions.SubClass.SubDepictionListContract;
8-
import fr.free.nrw.commons.depictions.SubClass.SubDepictionListPresenter;
7+
import fr.free.nrw.commons.depictions.subClass.SubDepictionListContract;
8+
import fr.free.nrw.commons.depictions.subClass.SubDepictionListPresenter;
99

1010
/**
1111
* The Dagger Module for explore:depictions related presenters and (some other objects maybe in future)

app/src/main/java/fr/free/nrw/commons/depictions/SubClass/models/Binding.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

app/src/main/java/fr/free/nrw/commons/depictions/SubClass/models/Head.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

app/src/main/java/fr/free/nrw/commons/depictions/SubClass/models/Results.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)