[LIB-2] Add snooker-score-api source files
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonSnookerScore.java
diff --git a/src/main/java/org/hedgecode/snooker/json/JsonSnookerScore.java b/src/main/java/org/hedgecode/snooker/json/JsonSnookerScore.java
new file mode 100644 (file)
index 0000000..aa27161
--- /dev/null
@@ -0,0 +1,304 @@
+/*
+ * Copyright (c) 2017. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.snooker.json;
+
+import com.google.gson.Gson;
+
+import org.hedgecode.snooker.api.APIException;
+import org.hedgecode.snooker.api.Event;
+import org.hedgecode.snooker.api.Events;
+import org.hedgecode.snooker.api.Match;
+import org.hedgecode.snooker.api.Matches;
+import org.hedgecode.snooker.api.OngoingMatches;
+import org.hedgecode.snooker.api.Player;
+import org.hedgecode.snooker.api.PlayerCategory;
+import org.hedgecode.snooker.api.Players;
+import org.hedgecode.snooker.api.RankingType;
+import org.hedgecode.snooker.api.Rankings;
+import org.hedgecode.snooker.api.Season;
+import org.hedgecode.snooker.api.SnookerScoreAPI;
+import org.hedgecode.snooker.gson.SnookerGsonBuilder;
+import org.hedgecode.snooker.request.RequestException;
+import org.hedgecode.snooker.request.RequestParams;
+import org.hedgecode.snooker.request.RequestType;
+
+/**
+ * Implementation of Interface {@link SnookerScoreAPI}
+ * with deserialize from input JSON strings.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class JsonSnookerScore implements SnookerScoreAPI {
+
+    public static final int UNKNOWN_PLAYER_ID = 376;
+
+    private final Gson GSON = SnookerGsonBuilder.build();
+
+    private static JsonSnookerScore _instance;
+
+    public static JsonSnookerScore getInstance() {
+        if (_instance == null)
+            _instance = new JsonSnookerScore();
+        return _instance;
+    }
+
+    protected JsonSnookerScore() {
+    }
+
+    @Override
+    public Event getEvent(int eventId) throws APIException {
+        Event[] events;
+        try {
+            events = GSON.fromJson(
+                    JsonStringToken.token(
+                            RequestType.request(RequestType.EVENT, eventId)
+                    ),
+                    JsonEvent[].class
+            );
+        } catch (RequestException e) {
+            throw new APIException(
+                    APIException.Type.REQUEST, e.getMessage()
+            );
+        }
+        return events.length > 0 ? events[0] : null;
+    }
+
+    @Override
+    public Match getMatch(int eventId, int roundId, int matchNumber) throws APIException {
+        Match[] matches;
+        try {
+            matches = GSON.fromJson(
+                    JsonStringToken.token(
+                            RequestType.request(
+                                    RequestType.MATCH,
+                                    new RequestParams(
+                                            eventId, roundId, matchNumber
+                                    )
+                            )
+                    ),
+                    JsonMatch[].class
+            );
+        } catch (RequestException e) {
+            throw new APIException(
+                    APIException.Type.REQUEST, e.getMessage()
+            );
+        }
+        return matches.length > 0 ? matches[0] : null;
+    }
+
+    @Override
+    public Player getPlayer(int playerId) throws APIException {
+        Player[] players;
+        try {
+            players = GSON.fromJson(
+                    JsonStringToken.token(
+                            RequestType.request(RequestType.PLAYER, playerId)
+                    ),
+                    JsonPlayer[].class
+            );
+        } catch (RequestException e) {
+            throw new APIException(
+                    APIException.Type.REQUEST, e.getMessage()
+            );
+        }
+        return players.length > 0 ? players[0] : null;
+    }
+
+    @Override
+    public Events getSeasonEvents(Season season) throws APIException {
+        Events events;
+        try {
+            events = new JsonEvents(
+                    GSON.fromJson(
+                            JsonStringToken.token(
+                                    RequestType.request(
+                                            RequestType.SEASON_EVENTS,
+                                            new RequestParams(
+                                                    RequestType.SEASON_EVENTS, 0, season
+                                            )
+                                    )
+                            ),
+                            JsonEvent[].class
+                    )
+            );
+        } catch (RequestException e) {
+            throw new APIException(
+                    APIException.Type.REQUEST, e.getMessage()
+            );
+        }
+        return events;
+    }
+
+    @Override
+    public Matches getEventMatches(int eventId) throws APIException {
+        Matches matches;
+        try {
+            matches = new JsonMatches(
+                    GSON.fromJson(
+                            JsonStringToken.token(
+                                    RequestType.request(
+                                            RequestType.EVENT_MATCHES,
+                                            eventId
+                                    )
+                            ),
+                            JsonMatch[].class
+                    )
+            );
+        } catch (RequestException e) {
+            throw new APIException(
+                    APIException.Type.REQUEST, e.getMessage()
+            );
+        }
+        return matches;
+    }
+
+    @Override
+    public OngoingMatches getOngoingMatches() throws APIException {
+        OngoingMatches ongoingMatches;
+        try {
+            ongoingMatches = new JsonOngoingMatches(
+                    GSON.fromJson(
+                            JsonStringToken.token(
+                                    RequestType.request(
+                                            RequestType.ONGOING_MATCHES,
+                                            null
+                                    )
+                            ),
+                            JsonOngoingMatch[].class
+                    )
+            );
+        } catch (RequestException e) {
+            throw new APIException(
+                    APIException.Type.REQUEST, e.getMessage()
+            );
+        }
+        return ongoingMatches;
+    }
+
+    @Override
+    public Matches getPlayerMatches(int playerId, Season season) throws APIException {
+        Matches matches;
+        try {
+            matches = new JsonMatches(
+                    GSON.fromJson(
+                            JsonStringToken.token(
+                                    RequestType.request(
+                                            RequestType.PLAYER_MATCHES,
+                                            new RequestParams(
+                                                    RequestType.PLAYER_MATCHES, playerId, season
+                                            )
+                                    )
+                            ),
+                            JsonMatch[].class
+                    )
+            );
+        } catch (RequestException e) {
+            throw new APIException(
+                    APIException.Type.REQUEST, e.getMessage()
+            );
+        }
+        return matches;
+    }
+
+    @Override
+    public Players getEventPlayers(int eventId) throws APIException {
+        Players players;
+        try {
+            players = new JsonPlayers(
+                    GSON.fromJson(
+                            JsonStringToken.token(
+                                    RequestType.request(
+                                            RequestType.EVENT_PLAYERS,
+                                            eventId
+                                    )
+                            ),
+                            JsonPlayer[].class
+                    )
+            );
+        } catch (RequestException e) {
+            throw new APIException(
+                    APIException.Type.REQUEST, e.getMessage()
+            );
+        }
+        return players;
+    }
+
+    @Override
+    public Players getPlayers(Season season, PlayerCategory category) throws APIException {
+        Players players;
+        try {
+            players = new JsonPlayers(
+                    GSON.fromJson(
+                            JsonStringToken.token(
+                                    RequestType.request(
+                                            RequestType.SEASON_PLAYERS,
+                                            new RequestParams(
+                                                    season, category
+                                            )
+                                    )
+                            ),
+                            JsonPlayer[].class
+                    )
+            );
+        } catch (RequestException e) {
+            throw new APIException(
+                    APIException.Type.REQUEST, e.getMessage()
+            );
+        }
+        return players;
+    }
+
+    @Override
+    public Rankings getRankings(Season season, RankingType type) throws APIException {
+        Rankings rankings;
+        try {
+            rankings = new JsonRankings(
+                    GSON.fromJson(
+                            JsonStringToken.token(
+                                    RequestType.request(
+                                            RequestType.RANKING,
+                                            new RequestParams(
+                                                    season, type
+                                            )
+                                    )
+                            ),
+                            JsonRanking[].class
+                    )
+            );
+        } catch (RequestException e) {
+            throw new APIException(
+                    APIException.Type.REQUEST, e.getMessage()
+            );
+        }
+        return rankings;
+    }
+
+    @Override
+    public void setCurrentSeason(Season season) throws APIException {
+        throw new APIException(
+                APIException.Type.INFO, "This feature is only available in Cached API"
+        );
+    }
+
+    @Override
+    public Season currentSeason() throws APIException {
+        throw new APIException(
+                APIException.Type.INFO, "This feature is only available in Cached API"
+        );
+    }
+
+}