Skip to content

#3664 Stop using JsonObject on StructuredData #3672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions app/src/main/java/fr/free/nrw/commons/Media.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import androidx.annotation.Nullable;
import androidx.room.Entity;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.media.Depictions;
import fr.free.nrw.commons.utils.CommonsDateUtil;
import fr.free.nrw.commons.utils.MediaDataExtractorUtil;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.wikipedia.dataclient.mwapi.MwQueryPage;
import org.wikipedia.gallery.ExtMetadata;
Expand Down Expand Up @@ -56,7 +56,7 @@ public class Media implements Parcelable {
* Depicts is a feature part of Structured data. Multiple Depictions can be added for an image just like categories.
* However unlike categories depictions is multi-lingual
*/
private List<Map<String, String>> depictionList= new ArrayList<>();
private Depictions depictions;
private boolean requestedDeletion;
@Nullable private LatLng coordinates;

Expand Down Expand Up @@ -305,12 +305,10 @@ public String getCaption() {
/**
* @return depictions associated with the current media
*/
public List<Map<String, String>> getDepiction() {
return depictionList;
public Depictions getDepiction() {
return depictions;
}



/**
* Sets the file description.
* @param description the new description of the file
Expand Down Expand Up @@ -489,8 +487,8 @@ public void setCaption(String caption) {
}

/* Sets depictions for the current media obtained fro Wikibase API*/
public void setDepictionList(List<Map<String, String>> depictions) {
this.depictionList = depictions;
public void setDepictions(Depictions depictions) {
this.depictions = depictions;
}

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

public List<Map<String, String>> getDepictionList() {
return depictionList;
public Depictions getDepictions() {
return depictions;
}

@Override
Expand Down Expand Up @@ -542,7 +540,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.creator);
dest.writeString(this.pageId);
dest.writeStringList(this.categories);
dest.writeList(this.depictionList);
dest.writeParcelable(this.depictions, flags);
dest.writeByte(this.requestedDeletion ? (byte) 1 : (byte) 0);
dest.writeParcelable(this.coordinates, flags);
}
Expand All @@ -568,7 +566,7 @@ protected Media(Parcel in) {
final ArrayList<String> list = new ArrayList<>();
in.readStringList(list);
this.categories=list;
in.readList(depictionList,null);
in.readParcelable(Depictions.class.getClassLoader());
this.requestedDeletion = in.readByte() != 0;
this.coordinates = in.readParcelable(LatLng.class.getClassLoader());
}
Expand Down
49 changes: 7 additions & 42 deletions app/src/main/java/fr/free/nrw/commons/MediaDataExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
import static fr.free.nrw.commons.depictions.Media.DepictedImagesFragment.PAGE_ID_PREFIX;

import androidx.core.text.HtmlCompat;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import fr.free.nrw.commons.media.Depictions;
import fr.free.nrw.commons.media.MediaClient;
import io.reactivex.Single;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jetbrains.annotations.NotNull;
Expand All @@ -24,10 +20,6 @@
@Singleton
public class MediaDataExtractor {

private static final int LABEL_BEGIN_INDEX = 3;
private static final int LABEL_END_OFFSET = 3;
private static final int ID_BEGIN_INDEX = 1;
private static final int ID_END_OFFSET = 1;
private final MediaClient mediaClient;

@Inject
Expand All @@ -52,10 +44,10 @@ public Single<Media> fetchMediaDetails(final String filename, final String pageI

@NotNull
private Media combineToMedia(final Media media, final Boolean deletionStatus, final String discussion,
final String caption, final JsonObject depiction) {
final String caption, final Depictions depictions) {
media.setDiscussion(discussion);
media.setCaption(caption);
media.setDepictionList(formatDepictions(depiction));
media.setDepictions(depictions);
if (deletionStatus) {
media.setRequestedDeletion(true);
}
Expand All @@ -74,39 +66,12 @@ private Single<String> getCaption(final String wikibaseIdentifier) {
}

/**
* From the Json Object extract depictions into an array list
* @param mediaResponse
* @return List containing map for depictions, the map has two keys,
* first key is for the label and second is for the url of the item
*/
private ArrayList<Map<String, String>> formatDepictions(final JsonObject mediaResponse) {
try {
final JsonArray depictionArray = (JsonArray) mediaResponse.get("Depiction");
final ArrayList<Map<String, String>> depictedItemList = new ArrayList<>();
for (int i = 0; i <depictionArray.size() ; i++) {
final JsonObject depictedItem = (JsonObject) depictionArray.get(i);
final Map <String, String> depictedObject = new HashMap<>();
final String label = depictedItem.get("label").toString();
final String id = depictedItem.get("id").toString();
final String transformedLabel = label.substring(LABEL_BEGIN_INDEX, label.length()- LABEL_END_OFFSET);
final String transformedId = id.substring(ID_BEGIN_INDEX,id.length() - ID_END_OFFSET);
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)
depictedObject.put("id", transformedId);
depictedItemList.add(depictedObject);
}
return depictedItemList;
} catch (final ClassCastException | NullPointerException ignore) {
return new ArrayList<>();
}
}

/**
* Fetch caption and depictions from the MediaWiki API
* Fetch depictions from the MediaWiki API
* @param filename the filename we will return the caption for
* @return a map containing caption and depictions (empty string in the map if no caption/depictions)
* @return Depictions
*/
private Single<JsonObject> getDepictions(final String filename) {
return mediaClient.getCaptionAndDepictions(filename)
private Single<Depictions> getDepictions(final String filename) {
return mediaClient.getDepictions(filename)
.doOnError(throwable -> Timber.e(throwable, "error while fetching depictions"));
}

Expand Down
21 changes: 11 additions & 10 deletions app/src/main/java/fr/free/nrw/commons/db/Converters.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.di.ApplicationlessInjection;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.media.Depictions;
import fr.free.nrw.commons.upload.WikidataPlace;
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem;
import java.util.Date;
Expand Down Expand Up @@ -72,16 +73,6 @@ public static LatLng stringToLatLng(String objectList) {
return readObjectFromString(objectList,LatLng.class);
}

@TypeConverter
public static String listOfMapToString(List<Map<String,String>> listOfMaps) {
return writeObjectToString(listOfMaps);
}

@TypeConverter
public static List<Map<String,String>> stringToListOfMap(String listOfMaps) {
return readObjectWithTypeToken(listOfMaps, new TypeToken<List<Map<String, String>>>() {});
}

@TypeConverter
public static String wikidataPlaceToString(WikidataPlace wikidataPlace) {
return writeObjectToString(wikidataPlace);
Expand All @@ -102,6 +93,16 @@ public static List<DepictedItem> stringToList(String depictedItems) {
return readObjectWithTypeToken(depictedItems, new TypeToken<List<DepictedItem>>() {});
}

@TypeConverter
public static String depictionsToString(Depictions depictedItems) {
return writeObjectToString(depictedItems);
}

@TypeConverter
public static Depictions stringToDepictions(String depictedItems) {
return readObjectFromString(depictedItems, Depictions.class);
}

private static String writeObjectToString(Object object) {
return object == null ? null : getGson().toJson(object);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import dagger.Module;
import fr.free.nrw.commons.depictions.Media.DepictedImagesContract;
import fr.free.nrw.commons.depictions.Media.DepictedImagesPresenter;
import fr.free.nrw.commons.depictions.SubClass.SubDepictionListContract;
import fr.free.nrw.commons.depictions.SubClass.SubDepictionListPresenter;
import fr.free.nrw.commons.depictions.subClass.SubDepictionListContract;
import fr.free.nrw.commons.depictions.subClass.SubDepictionListPresenter;

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

This file was deleted.

This file was deleted.

This file was deleted.

Loading