[LIB-8] URL and HTML tag processing, new entities fields
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonEvent.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 java.util.Date;
20
21 import com.google.gson.annotations.Expose;
22 import com.google.gson.annotations.SerializedName;
23
24 import org.hedgecode.snooker.SnookerURLUtils;
25 import org.hedgecode.snooker.annotation.IsURL;
26 import org.hedgecode.snooker.annotation.WithHTMLTags;
27 import org.hedgecode.snooker.api.Event;
28 import org.hedgecode.snooker.api.EventFormat;
29 import org.hedgecode.snooker.api.Player;
30 import org.hedgecode.snooker.api.Season;
31
32 /**
33  * Event Entity to JSON deserialize.
34  *
35  * @author Dmitry Samoshin aka gotty
36  */
37 public class JsonEvent extends JsonIdEntity implements Event {
38
39     @SerializedName("ID")
40     private int eventId;
41     @SerializedName("Name")
42     private String name;
43     @SerializedName("StartDate")
44     private Date startDate;
45     @SerializedName("EndDate")
46     private Date endDate;
47     @SerializedName("Sponsor")
48     private String sponsor;
49     @SerializedName("Season")
50     private int seasonYear;
51     @Expose
52     private Season season;
53     @SerializedName("Type")
54     private String type;
55     @SerializedName("Num")
56     private int num;
57     @SerializedName("Venue")
58     private String venue;
59     @SerializedName("City")
60     private String city;
61     @SerializedName("Country")
62     private String country;
63     @SerializedName("Discipline")
64     private String discipline;
65     @SerializedName("Main")
66     private int mainEventId;
67     @Expose
68     private Event mainEvent;
69     @SerializedName("Sex")
70     private String sex;
71     @SerializedName("AgeGroup")
72     private String ageGroup;
73     @IsURL
74     @SerializedName("Url")
75     private String url;
76     @SerializedName("Related")
77     private String related;
78     @SerializedName("Stage")
79     private String stage;
80     @SerializedName("ValueType")
81     private String valueType;
82     @SerializedName("ShortName")
83     private String shortName;
84     @SerializedName("WorldSnookerId")
85     private int worldSnookerId;
86     @SerializedName("RankingType")
87     private String rankingType;
88     @SerializedName("EventPredictionID")
89     private int eventPredictionId;
90     @SerializedName("Team")
91     private boolean team;
92     @SerializedName("Format")
93     private int format;
94     @Expose
95     private EventFormat formatType;
96     @SerializedName("Twitter")
97     private String twitter;
98     @SerializedName("HashTag")
99     private String hashTag;
100     @SerializedName("ConversionRate")
101     private float conversionRate;
102     @SerializedName("AllRoundsAdded")
103     private boolean allRoundsAdded;
104     @IsURL
105     @SerializedName("PhotoURLs")
106     private String photoUrls;
107     @SerializedName("NumCompetitors")
108     private int numCompetitors;
109     @SerializedName("NumUpcoming")
110     private int numUpcoming;
111     @SerializedName("NumActive")
112     private int numActive;
113     @SerializedName("NumResults")
114     private int numResults;
115     @WithHTMLTags
116     @SerializedName("Note")
117     private String note;
118     @WithHTMLTags
119     @SerializedName("CommonNote")
120     private String commonNote;
121     @SerializedName("DefendingChampion")
122     private int defendingChampionId;
123     @Expose
124     private Player defendingChampion;
125     @SerializedName("PreviousEdition")
126     private int previousEditionId;
127     @Expose
128     private Event previousEdition;
129
130     protected JsonEvent() {
131     }
132
133     @Override
134     public int eventId() {
135         return eventId;
136     }
137
138     @Override
139     public String name() {
140         return name;
141     }
142
143     @Override
144     public Date startDate() {
145         return startDate == null
146                 ? null
147                 : new Date(startDate.getTime());
148     }
149
150     @Override
151     public Date endDate() {
152         return endDate == null
153                 ? null
154                 : new Date(endDate.getTime());
155     }
156
157     @Override
158     public String sponsor() {
159         return sponsor;
160     }
161
162     @Override
163     public Season season() {
164         if (season == null)
165             season = Season.getSeason(seasonYear);
166         return season;
167     }
168
169     @Override
170     public String type() {
171         return type;
172     }
173
174     @Override
175     public int num() {
176         return num;
177     }
178
179     @Override
180     public String venue() {
181         return venue;
182     }
183
184     @Override
185     public String city() {
186         return city;
187     }
188
189     @Override
190     public String country() {
191         return country;
192     }
193
194     @Override
195     public String discipline() {
196         return discipline;
197     }
198
199     @Override
200     public int mainEventId() {
201         return mainEventId;
202     }
203
204     @Override
205     public Event mainEvent() {
206         if (mainEvent == null && mainEventId == eventId) {
207             mainEvent = this;
208         }
209         return mainEvent;
210     }
211
212     public void setMainEvent(Event event) {
213         if (event != null && eventId == event.eventId()) {
214             mainEvent = event;
215         }
216     }
217
218     @Override
219     public String sex() {
220         return sex;
221     }
222
223     @Override
224     public String ageGroup() {
225         return ageGroup;
226     }
227
228     @Override
229     public String url() {
230         return url;
231     }
232
233     @Override
234     public String related() {
235         return related;
236     }
237
238     @Override
239     public String stage() {
240         return stage;
241     }
242
243     @Override
244     public String valueType() {
245         return valueType;
246     }
247
248     @Override
249     public String shortName() {
250         return shortName;
251     }
252
253     @Override
254     public int worldSnookerId() {
255         return worldSnookerId;
256     }
257
258     @Override
259     public String rankingType() {
260         return rankingType;
261     }
262
263     @Override
264     public int eventPredictionId() {
265         return eventPredictionId;
266     }
267
268     @Override
269     public boolean team() {
270         return team;
271     }
272
273     @Override
274     public int format() {
275         return format;
276     }
277
278     @Override
279     public EventFormat formatType() {
280         if (formatType == null)
281             formatType = EventFormat.byNumber(format);
282         return formatType;
283     }
284
285     @Override
286     public String twitter() {
287         return twitter;
288     }
289
290     @IsURL
291     @Override
292     public String twitterUrl() {
293         return SnookerURLUtils.twitterUrl(twitter);
294     }
295
296     @Override
297     public String hashTag() {
298         return hashTag;
299     }
300
301     @IsURL
302     @Override
303     public String hashTagUrl() {
304         return SnookerURLUtils.hashtagUrl(hashTag);
305     }
306
307     @Override
308     public float conversionRate() {
309         return conversionRate;
310     }
311
312     @Override
313     public boolean allRoundsAdded() {
314         return allRoundsAdded;
315     }
316
317     @Override
318     public String photoUrls() {
319         return photoUrls;
320     }
321
322     @Override
323     public int numCompetitors() {
324         return numCompetitors;
325     }
326
327     @Override
328     public int numUpcoming() {
329         return numUpcoming;
330     }
331
332     @Override
333     public int numActive() {
334         return numActive;
335     }
336
337     @Override
338     public int numResults() {
339         return numResults;
340     }
341
342     @Override
343     public String note() {
344         return note;
345     }
346
347     @Override
348     public String commonNote() {
349         return commonNote;
350     }
351
352     @Override
353     public int defendingChampionId() {
354         return defendingChampionId;
355     }
356
357     @Override
358     public Player defendingChampion() {
359         return defendingChampion;
360     }
361
362     public void setDefendingChampion(Player champion) {
363         if (champion != null && defendingChampionId == champion.playerId()) {
364             defendingChampion = champion;
365         }
366     }
367
368     @Override
369     public int previousEditionId() {
370         return previousEditionId;
371     }
372
373     @Override
374     public Event previousEdition() {
375         return previousEdition;
376     }
377
378     public void setPreviousEdition(Event event) {
379         if (event != null && previousEditionId == event.eventId()) {
380             previousEdition = event;
381         }
382     }
383
384     @Override
385     public int getId() {
386         return eventId;
387     }
388
389 }