[LIB-8] Add Event Rounds and Seedings functional
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonSnookerScore.java
1 /*
2  * Copyright (c) 2017. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.snooker.json;
18
19 import com.google.gson.Gson;
20
21 import org.hedgecode.snooker.api.APIException;
22 import org.hedgecode.snooker.api.Event;
23 import org.hedgecode.snooker.api.Events;
24 import org.hedgecode.snooker.api.Match;
25 import org.hedgecode.snooker.api.Matches;
26 import org.hedgecode.snooker.api.OngoingMatches;
27 import org.hedgecode.snooker.api.Player;
28 import org.hedgecode.snooker.api.PlayerCategory;
29 import org.hedgecode.snooker.api.Players;
30 import org.hedgecode.snooker.api.RankingType;
31 import org.hedgecode.snooker.api.Rankings;
32 import org.hedgecode.snooker.api.Rounds;
33 import org.hedgecode.snooker.api.Season;
34 import org.hedgecode.snooker.api.Seedings;
35 import org.hedgecode.snooker.api.SnookerScoreAPI;
36 import org.hedgecode.snooker.gson.SnookerGsonBuilder;
37 import org.hedgecode.snooker.request.RequestException;
38 import org.hedgecode.snooker.request.RequestParams;
39 import org.hedgecode.snooker.request.RequestType;
40
41 /**
42  * Implementation of Interface {@link SnookerScoreAPI}
43  * with deserialize from input JSON strings.
44  *
45  * @author Dmitry Samoshin aka gotty
46  */
47 public class JsonSnookerScore implements SnookerScoreAPI {
48
49     public static final int UNKNOWN_PLAYER_ID = 376;
50
51     private final Gson GSON = SnookerGsonBuilder.build();
52
53     private static JsonSnookerScore _instance;
54
55     public static JsonSnookerScore getInstance() {
56         if (_instance == null)
57             _instance = new JsonSnookerScore();
58         return _instance;
59     }
60
61     protected JsonSnookerScore() {
62     }
63
64     @Override
65     public Event getEvent(int eventId) throws APIException {
66         Event[] events;
67         try {
68             events = GSON.fromJson(
69                     JsonStringToken.token(
70                             RequestType.request(RequestType.EVENT, eventId)
71                     ),
72                     JsonEvent[].class
73             );
74         } catch (RequestException e) {
75             throw new APIException(
76                     APIException.Type.REQUEST, e.getMessage()
77             );
78         }
79         return events.length > 0 ? events[0] : null;
80     }
81
82     @Override
83     public Match getMatch(int eventId, int roundId, int matchNumber) throws APIException {
84         Match[] matches;
85         try {
86             matches = GSON.fromJson(
87                     JsonStringToken.token(
88                             RequestType.request(
89                                     RequestType.MATCH,
90                                     new RequestParams(
91                                             eventId, roundId, matchNumber
92                                     )
93                             )
94                     ),
95                     JsonMatch[].class
96             );
97         } catch (RequestException e) {
98             throw new APIException(
99                     APIException.Type.REQUEST, e.getMessage()
100             );
101         }
102         return matches.length > 0 ? matches[0] : null;
103     }
104
105     @Override
106     public Player getPlayer(int playerId) throws APIException {
107         Player[] players;
108         try {
109             players = GSON.fromJson(
110                     JsonStringToken.token(
111                             RequestType.request(RequestType.PLAYER, playerId)
112                     ),
113                     JsonPlayer[].class
114             );
115         } catch (RequestException e) {
116             throw new APIException(
117                     APIException.Type.REQUEST, e.getMessage()
118             );
119         }
120         return players.length > 0 ? players[0] : null;
121     }
122
123     @Override
124     public Events getSeasonEvents(Season season) throws APIException {
125         Events events;
126         try {
127             events = new JsonEvents(
128                     GSON.fromJson(
129                             JsonStringToken.token(
130                                     RequestType.request(
131                                             RequestType.SEASON_EVENTS,
132                                             new RequestParams(
133                                                     RequestType.SEASON_EVENTS, 0, season
134                                             )
135                                     )
136                             ),
137                             JsonEvent[].class
138                     )
139             );
140         } catch (RequestException e) {
141             throw new APIException(
142                     APIException.Type.REQUEST, e.getMessage()
143             );
144         }
145         return events;
146     }
147
148     @Override
149     public Matches getEventMatches(int eventId) throws APIException {
150         Matches matches;
151         try {
152             matches = new JsonMatches(
153                     GSON.fromJson(
154                             JsonStringToken.token(
155                                     RequestType.request(
156                                             RequestType.EVENT_MATCHES,
157                                             eventId
158                                     )
159                             ),
160                             JsonMatch[].class
161                     )
162             );
163         } catch (RequestException e) {
164             throw new APIException(
165                     APIException.Type.REQUEST, e.getMessage()
166             );
167         }
168         return matches;
169     }
170
171     @Override
172     public OngoingMatches getOngoingMatches() throws APIException {
173         OngoingMatches ongoingMatches;
174         try {
175             ongoingMatches = new JsonOngoingMatches(
176                     GSON.fromJson(
177                             JsonStringToken.token(
178                                     RequestType.request(
179                                             RequestType.ONGOING_MATCHES,
180                                             null
181                                     )
182                             ),
183                             JsonOngoingMatch[].class
184                     )
185             );
186         } catch (RequestException e) {
187             throw new APIException(
188                     APIException.Type.REQUEST, e.getMessage()
189             );
190         }
191         return ongoingMatches;
192     }
193
194     @Override
195     public Matches getPlayerMatches(int playerId, Season season) throws APIException {
196         Matches matches;
197         try {
198             matches = new JsonMatches(
199                     GSON.fromJson(
200                             JsonStringToken.token(
201                                     RequestType.request(
202                                             RequestType.PLAYER_MATCHES,
203                                             new RequestParams(
204                                                     RequestType.PLAYER_MATCHES, playerId, season
205                                             )
206                                     )
207                             ),
208                             JsonMatch[].class
209                     )
210             );
211         } catch (RequestException e) {
212             throw new APIException(
213                     APIException.Type.REQUEST, e.getMessage()
214             );
215         }
216         return matches;
217     }
218
219     @Override
220     public Players getEventPlayers(int eventId) throws APIException {
221         Players players;
222         try {
223             players = new JsonPlayers(
224                     GSON.fromJson(
225                             JsonStringToken.token(
226                                     RequestType.request(
227                                             RequestType.EVENT_PLAYERS,
228                                             eventId
229                                     )
230                             ),
231                             JsonPlayer[].class
232                     )
233             );
234         } catch (RequestException e) {
235             throw new APIException(
236                     APIException.Type.REQUEST, e.getMessage()
237             );
238         }
239         return players;
240     }
241
242     @Override
243     public Players getPlayers(Season season, PlayerCategory category) throws APIException {
244         Players players;
245         try {
246             players = new JsonPlayers(
247                     GSON.fromJson(
248                             JsonStringToken.token(
249                                     RequestType.request(
250                                             RequestType.SEASON_PLAYERS,
251                                             new RequestParams(
252                                                     season, category
253                                             )
254                                     )
255                             ),
256                             JsonPlayer[].class
257                     )
258             );
259         } catch (RequestException e) {
260             throw new APIException(
261                     APIException.Type.REQUEST, e.getMessage()
262             );
263         }
264         return players;
265     }
266
267     @Override
268     public Rankings getRankings(Season season, RankingType type) throws APIException {
269         Rankings rankings;
270         try {
271             rankings = new JsonRankings(
272                     GSON.fromJson(
273                             JsonStringToken.token(
274                                     RequestType.request(
275                                             RequestType.RANKING,
276                                             new RequestParams(
277                                                     season, type
278                                             )
279                                     )
280                             ),
281                             JsonRanking[].class
282                     )
283             );
284         } catch (RequestException e) {
285             throw new APIException(
286                     APIException.Type.REQUEST, e.getMessage()
287             );
288         }
289         return rankings;
290     }
291
292     @Override
293     public Rounds getEventRounds(int eventId) throws APIException {
294         Rounds rounds;
295         try {
296             rounds = new JsonRounds(
297                     GSON.fromJson(
298                             JsonStringToken.token(
299                                     RequestType.request(
300                                             RequestType.EVENT_ROUNDS,
301                                             eventId
302                                     )
303                             ),
304                             JsonRound[].class
305                     )
306             );
307         } catch (RequestException e) {
308             throw new APIException(
309                     APIException.Type.REQUEST, e.getMessage()
310             );
311         }
312         return rounds;
313     }
314
315     @Override
316     public Seedings getEventSeedings(int eventId) throws APIException {
317         Seedings seedings;
318         try {
319             seedings = new JsonSeedings(
320                     GSON.fromJson(
321                             JsonStringToken.token(
322                                     RequestType.request(
323                                             RequestType.EVENT_SEEDING,
324                                             eventId
325                                     )
326                             ),
327                             JsonSeeding[].class
328                     )
329             );
330         } catch (RequestException e) {
331             throw new APIException(
332                     APIException.Type.REQUEST, e.getMessage()
333             );
334         }
335         return seedings;
336     }
337
338     @Override
339     public void setCurrentSeason(Season season) throws APIException {
340         throw new APIException(
341                 APIException.Type.INFO, "This feature is only available in Cached API"
342         );
343     }
344
345     @Override
346     public Season currentSeason() throws APIException {
347         throw new APIException(
348                 APIException.Type.INFO, "This feature is only available in Cached API"
349         );
350     }
351
352 }