[LIB-8] URL and HTML tag processing, new entities fields
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonPlayer.java
index 04ac5da..aa481ed 100644 (file)
 
 package org.hedgecode.snooker.json;
 
+import java.net.URL;
 import java.util.Date;
 
+import com.google.gson.annotations.Expose;
 import com.google.gson.annotations.SerializedName;
 
+import org.hedgecode.snooker.SnookerURLUtils;
+import org.hedgecode.snooker.annotation.IsURL;
+import org.hedgecode.snooker.api.APIException;
 import org.hedgecode.snooker.api.Player;
+import org.hedgecode.snooker.api.PlayerImage;
 
 /**
  * Player Entity to JSON deserialize.
@@ -47,10 +53,13 @@ public class JsonPlayer extends JsonIdEntity implements Player {
     private int teamSeason;
     @SerializedName("ShortName")
     private String shortName;
+    @Expose
+    private String fullName;
     @SerializedName("Nationality")
     private String nationality;
     @SerializedName("Sex")
     private String sex;
+    @IsURL
     @SerializedName("BioPage")
     private String bioPage;
     @SerializedName("Born")
@@ -63,10 +72,21 @@ public class JsonPlayer extends JsonIdEntity implements Player {
     private String license;
     @SerializedName("Club")
     private String club;
+    @IsURL
     @SerializedName("URL")
     private String url;
+    @IsURL
     @SerializedName("Photo")
     private String photo;
+    @Expose
+    private PlayerImage image;
+    @IsURL
+    @SerializedName("PhotoSource")
+    private String photoSource;
+    @SerializedName("FirstSeasonAsPro")
+    private int firstSeasonAsPro;
+    @SerializedName("LastSeasonAsPro")
+    private int lastSeasonAsPro;
     @SerializedName("Info")
     private String info;
 
@@ -119,6 +139,19 @@ public class JsonPlayer extends JsonIdEntity implements Player {
     }
 
     @Override
+    public String fullName() {
+        if (fullName == null) {
+            fullName = String.format(
+                    "%s %s %s",
+                    surnameFirst ? lastName : firstName,
+                    middleName,
+                    surnameFirst ? firstName : lastName
+            ).replaceAll("\\s+", " ").trim();
+        }
+        return fullName;
+    }
+
+    @Override
     public String nationality() {
         return nationality;
     }
@@ -145,6 +178,12 @@ public class JsonPlayer extends JsonIdEntity implements Player {
         return twitter;
     }
 
+    @IsURL
+    @Override
+    public String twitterUrl() {
+        return SnookerURLUtils.twitterUrl(twitter);
+    }
+
     @Override
     public boolean surnameFirst() {
         return surnameFirst;
@@ -171,6 +210,32 @@ public class JsonPlayer extends JsonIdEntity implements Player {
     }
 
     @Override
+    public PlayerImage image() throws APIException {
+        if (image == null) {
+            URL imageUrl = SnookerURLUtils.assignUrl(photo);
+            if (imageUrl != null) {
+                image = new PlayerImage(imageUrl, false);
+            }
+        }
+        return image;
+    }
+
+    @Override
+    public String photoSource() {
+        return photoSource;
+    }
+
+    @Override
+    public int firstSeasonAsPro() {
+        return firstSeasonAsPro;
+    }
+
+    @Override
+    public int lastSeasonAsPro() {
+        return lastSeasonAsPro;
+    }
+
+    @Override
     public String info() {
         return info;
     }