Removed legacy JSON (de)serialization for event media

This commit is contained in:
Steffen Schaefer committed 2016-03-02 11:10:49 +01:00
1 parent fdb59c3ca5
commit 4b1d2e1567
2 files changed
-91

No files matched your search

@@ -3,10 +3,8 @@ package com.sap.sailing.server.gateway.deserialization.impl;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import org.json.simple.JSONArray;
@@ -16,14 +14,11 @@ import com.sap.sailing.domain.base.EventBase;
import com.sap.sailing.domain.base.LeaderboardGroupBase;
import com.sap.sailing.domain.base.Venue;
import com.sap.sailing.domain.base.impl.StrippedEventImpl;
import com.sap.sailing.domain.common.impl.ImageSizeImpl;
import com.sap.sailing.server.gateway.deserialization.JsonDeserializationException;
import com.sap.sailing.server.gateway.deserialization.JsonDeserializer;
import com.sap.sailing.server.gateway.serialization.impl.EventBaseJsonSerializer;
import com.sap.sse.common.TimePoint;
import com.sap.sse.common.Util;
import com.sap.sse.common.impl.MillisecondsTimePoint;
import com.sap.sse.common.media.ImageSize;
import com.sap.sse.common.media.MimeType;
import com.sap.sse.shared.media.ImageDescriptor;
import com.sap.sse.shared.media.VideoDescriptor;
@@ -88,10 +83,6 @@ public class EventBaseJsonDeserializer implements JsonDeserializer<EventBase> {
}
}
}
// if we read an event with data in the old image/video format we need migrate them
if(Util.size(result.getImages()) == 0 && Util.size(result.getVideos()) == 0) {
readImageAndVideoURLsFromLegacyServer(eventJson, result);
}
JSONArray sailorsInfoWebsiteURLsJson = (JSONArray) eventJson.get(EventBaseJsonSerializer.FIELD_SAILORS_INFO_WEBSITE_URLS);
if (sailorsInfoWebsiteURLsJson != null) {
for (Object sailorsInfoWebsiteURLJson : sailorsInfoWebsiteURLsJson) {
@@ -103,61 +94,6 @@ public class EventBaseJsonDeserializer implements JsonDeserializer<EventBase> {
}
return result;
}
private void readImageAndVideoURLsFromLegacyServer(JSONObject eventJson, StrippedEventImpl event) throws JsonDeserializationException {
URL logoImageURL = null;
List<URL> imageURLs = new ArrayList<URL>();
List<URL> sponsorImageURLs = new ArrayList<URL>();
List<URL> videoURLs = new ArrayList<URL>();
String logoImageURLAsString = (String) eventJson.get(EventBaseJsonSerializer.FIELD_LOGO_IMAGE_URL);
if (logoImageURLAsString != null) {
try {
logoImageURL = new URL(logoImageURLAsString);
} catch (MalformedURLException e) {
throw new JsonDeserializationException("Error deserializing logo image URL for event "+event.getName(), e);
}
}
if (eventJson.get(EventBaseJsonSerializer.FIELD_IMAGE_URLS) != null) {
try {
imageURLs = getURLsFromStrings(Helpers.getNestedArraySafe(eventJson, EventBaseJsonSerializer.FIELD_IMAGE_URLS));
} catch (MalformedURLException e) {
throw new JsonDeserializationException("Error deserializing image URLs for event "+event.getName(), e);
}
}
if (eventJson.get(EventBaseJsonSerializer.FIELD_VIDEO_URLS) != null) {
try {
videoURLs = getURLsFromStrings(Helpers.getNestedArraySafe(eventJson, EventBaseJsonSerializer.FIELD_VIDEO_URLS));
} catch (MalformedURLException e) {
throw new JsonDeserializationException("Error deserializing video URLs for event "+event.getName(), e);
}
}
if (eventJson.get(EventBaseJsonSerializer.FIELD_SPONSOR_IMAGE_URLS) != null) {
try {
sponsorImageURLs = getURLsFromStrings(Helpers.getNestedArraySafe(eventJson, EventBaseJsonSerializer.FIELD_SPONSOR_IMAGE_URLS));
} catch (MalformedURLException e) {
throw new JsonDeserializationException("Error deserializing sponsor image URLs for event "+event.getName(), e);
}
}
// handle images sizes coming from an old server instance not having images and videos in the rich format
Map<URL, ImageSize> imageSizes = new HashMap<URL, ImageSize>();;
JSONArray imageSizesJson = (JSONArray) eventJson.get(EventBaseJsonSerializer.FIELD_IMAGE_SIZES);
if (imageSizesJson != null) {
for (Object imageURLAndSizeObject : imageSizesJson) {
JSONObject imageURLAndSizeJson = (JSONObject) imageURLAndSizeObject;
try {
imageSizes.put(
new URL((String) imageURLAndSizeJson.get(EventBaseJsonSerializer.FIELD_IMAGE_URL)),
new ImageSizeImpl(
((Number) imageURLAndSizeJson.get(EventBaseJsonSerializer.FIELD_IMAGE_WIDTH)).intValue(),
((Number) imageURLAndSizeJson.get(EventBaseJsonSerializer.FIELD_IMAGE_HEIGHT)).intValue()));
} catch (MalformedURLException e) {
throw new JsonDeserializationException(e);
}
}
}
event.setMediaURLs(imageURLs, sponsorImageURLs, videoURLs, logoImageURL, imageSizes);
}
private ImageDescriptor loadImage(JSONObject imageJson) {
ImageDescriptor image = null;
@@ -227,14 +163,4 @@ public class EventBaseJsonDeserializer implements JsonDeserializer<EventBase> {
}
return video;
}
private List<URL> getURLsFromStrings(JSONArray strings) throws MalformedURLException {
List<URL> result = new ArrayList<URL>();
if (strings != null) {
for (Object string : strings) {
result.add(new URL(string.toString()));
}
}
return result;
}
}
@@ -21,10 +21,6 @@ public class EventBaseJsonSerializer implements JsonSerializer<EventBase> {
public static final String FIELD_START_DATE = "startDate";
public static final String FIELD_END_DATE = "endDate";
public static final String FIELD_VENUE = "venue";
public static final String FIELD_IMAGE_URLS = "imageURLs";
public static final String FIELD_VIDEO_URLS = "videoURLs";
public static final String FIELD_SPONSOR_IMAGE_URLS = "sponsorImageURLs";
public static final String FIELD_LOGO_IMAGE_URL = "logoImageURL";
public static final String FIELD_IMAGE_SIZES = "imageSizes";
public static final String FIELD_IMAGE_URL = "imageURL";
public static final String FIELD_IMAGE_WIDTH = "imageWidth";
@@ -61,7 +57,6 @@ public class EventBaseJsonSerializer implements JsonSerializer<EventBase> {
this.venueSerializer = venueSerializer;
}
@SuppressWarnings("deprecation")
public JSONObject serialize(EventBase event) {
JSONObject result = new JSONObject();
result.put(FIELD_ID, event.getId().toString());
@@ -71,10 +66,6 @@ public class EventBaseJsonSerializer implements JsonSerializer<EventBase> {
result.put(FIELD_START_DATE, event.getStartDate() != null ? event.getStartDate().asMillis() : null);
result.put(FIELD_END_DATE, event.getStartDate() != null ? event.getEndDate().asMillis() : null);
result.put(FIELD_VENUE, venueSerializer.serialize(event.getVenue()));
result.put(FIELD_LOGO_IMAGE_URL, event.getLogoImageURL() != null ? event.getLogoImageURL().toString() : null);
result.put(FIELD_IMAGE_URLS, getURLsAsStringArray(event.getImageURLs()));
result.put(FIELD_VIDEO_URLS, getURLsAsStringArray(event.getVideoURLs()));
result.put(FIELD_SPONSOR_IMAGE_URLS, getURLsAsStringArray(event.getSponsorImageURLs()));
JSONArray leaderboardGroups = new JSONArray();
result.put(FIELDS_LEADERBOARD_GROUPS, leaderboardGroups);
for (LeaderboardGroupBase lg : event.getLeaderboardGroups()) {
@@ -157,14 +148,6 @@ public class EventBaseJsonSerializer implements JsonSerializer<EventBase> {
}
}
private JSONArray getURLsAsStringArray(Iterable<URL> urls) {
JSONArray jsonImageURLs = new JSONArray();
for (URL url : urls) {
jsonImageURLs.add(url.toString());
}
return jsonImageURLs;
}
private void addSailorsInfoWebsiteURL(Locale locale, URL url, JSONArray jsonSailorsInfoWebsiteURLs) {
JSONObject jsonSailorsInfoWebsiteURL = new JSONObject();
// TODO use toLanguageTag() when deserializer can use Locale.forLanguageTag()