001/**
002 * Copyright 2014 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package service.tut.pori.facebookjazz;
017
018import java.util.ArrayList;
019import java.util.Date;
020import java.util.List;
021
022import javax.xml.bind.annotation.XmlAccessType;
023import javax.xml.bind.annotation.XmlAccessorType;
024import javax.xml.bind.annotation.XmlElement;
025import javax.xml.bind.annotation.XmlEnum;
026import javax.xml.bind.annotation.XmlRootElement;
027import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
028
029import org.apache.commons.lang3.StringUtils;
030
031import service.tut.pori.facebookjazz.FacebookGroup.Privacy;
032
033import com.restfb.types.Event;
034import com.restfb.types.Event.Owner;
035
036import core.tut.pori.utils.ISODateAdapter;
037
038
039/**
040 * Details of an event retrieved from Facebook.
041 * 
042 * <h2>Optional Elements</h2>
043 * <ul>
044 *  <li>{@value service.tut.pori.facebookjazz.Definitions#ELEMENT_DESCRIPTION}</li>
045 *  <li>{@value service.tut.pori.facebookjazz.Definitions#ELEMENT_PRIVACY}</li>
046 *  <li>{@value service.tut.pori.facebookjazz.Definitions#ELEMENT_LOCATION}</li>
047 * </ul>
048 * 
049 * <h3>XML Example</h3>
050 * 
051 * {@doc.restlet service="[service.tut.pori.facebookjazz.reference.Definitions#SERVICE_FBJ_REFERENCE_EXAMPLE]" method="[service.tut.pori.facebookjazz.Definitions#ELEMENT_EVENT]" type="GET" query="" body_uri=""}
052 *
053 * @see com.restfb.types.Event
054 */
055@XmlRootElement(name=Definitions.ELEMENT_EVENT)
056@XmlAccessorType(XmlAccessType.NONE)
057public class FacebookEvent {
058  private Event _event = null;
059  private Integer _nameWeight = null;
060  private Integer _descriptionWeight = null;
061
062  /**
063   * Reply status for a Facebook event.
064   * 
065   * Note that the values returned by Facebook are not clearly defined, and the values listed here are most likely only a subset of the available values.
066   * 
067   */
068  @XmlEnum
069  public enum RSVPStatus{
070    /** user has not replied to the invitation */
071    NOT_REPLIED,
072    /** user has nor decided */
073    UNSURE,
074    /** user is attending to the event */
075    ATTENDING,
076    /** user has declined the invitation */
077    DECLINED;
078
079    /**
080     * 
081     * @return status as string
082     */
083    public String toRSVPStatusString(){
084      return name();
085    }
086
087    /**
088     * 
089     * @param statusString
090     * @return RSVPStatus or null if invalid string
091     */
092    public static RSVPStatus fromRSVPStatusString(String statusString){
093      if(statusString != null){
094        for(RSVPStatus r : RSVPStatus.values()){
095          if(r.toRSVPStatusString().equalsIgnoreCase(statusString)){
096            return r;
097          }
098        }
099      }
100      return null;
101    }
102  }  // enum RSVPStatus
103  
104  /**
105   * 
106   */
107  public FacebookEvent() {
108    _event = new Event();
109  }
110
111  /**
112   * 
113   * @param event
114   * @throws IllegalArgumentException 
115   */
116  public FacebookEvent(Event event) throws IllegalArgumentException{
117    if(event == null){
118      throw new IllegalArgumentException("Invalid event.");
119    }
120    _event = event;
121  }
122
123  /**
124   * @see com.restfb.types.Event#getOwner()
125   * @see #setEventOwnerName(String)
126   * 
127   * @return owner name
128   */
129  @XmlElement(name = Definitions.ELEMENT_OWNER)
130  public String getEventOwnerName() {
131    Owner owner = _event.getOwner();
132    return (owner == null ? null : owner.getName());
133  }
134  
135  /**
136   * @param name 
137   * @see com.restfb.types.Event#setOwner(com.restfb.types.Event.Owner)
138   * @see #getEventOwnerName()
139   */
140  public void setEventOwnerName(String name) {
141    if(StringUtils.isBlank(name)){
142      _event.setOwner(null);
143    }else{
144      Owner owner = _event.getOwner();
145      if(owner == null){
146        owner = new Owner();
147        _event.setOwner(owner);
148      }
149      owner.setName(name);
150    }
151  }
152
153  /**
154   * @see com.restfb.types.Event#getDescription()
155   * @see #getDescription()
156   * 
157   * @return description
158   */
159  @XmlElement(name = Definitions.ELEMENT_DESCRIPTION)
160  public WeightedStringElement getWDescription() {
161    String description = _event.getDescription();
162    if(StringUtils.isBlank(description)){
163      return null;
164    }else{
165      return new WeightedStringElement(description, _descriptionWeight);
166    }
167  }
168  
169  /**
170   * for serialization
171   * @param description 
172   * @see #setDescription(String)
173   */
174  @SuppressWarnings("unused")
175  private void setWDescription(WeightedStringElement description) {
176    if(description == null){
177      setDescription(null);
178      setDescriptionWeight(null);
179    }else{
180      setDescription(description.getValue());
181      setDescriptionWeight(description.getWeight());
182    }
183  }
184  
185  /**
186   * @see com.restfb.types.Event#getStartTime()
187   * @see #setStartTime(Date)
188   * 
189   * @return event start time
190   */
191  @XmlJavaTypeAdapter(ISODateAdapter.class)
192  @XmlElement(name = Definitions.ELEMENT_START_TIMESTAMP)
193  public Date getStartTime() {
194    return _event.getStartTime();
195  }
196
197  /**
198   * @see com.restfb.types.Event#getEndTime()
199   * @see #setEndTime(Date)
200   * 
201   * @return event end time
202   */
203  @XmlJavaTypeAdapter(ISODateAdapter.class)
204  @XmlElement(name = Definitions.ELEMENT_END_TIMESTAMP)
205  public Date getEndTime() {
206    return _event.getEndTime();
207  }
208
209  /**
210   * @see com.restfb.types.Event#getVenue()
211   * @see com.restfb.types.Event#getLocation()
212   * @see #setFacebookLocation(FacebookLocation)
213   * 
214   * @return location
215   */
216  @XmlElement(name = Definitions.ELEMENT_LOCATION)
217  public FacebookLocation getFacebookLocation() {
218    FacebookLocation l = FacebookLocation.getFacebookLocation(_event.getVenue());
219    if(l == null){
220      l = FacebookLocation.getFacebookLocation(_event.getLocation());
221    }
222    return l;
223  }
224  
225  /**
226   * @param location
227   * @see com.restfb.types.Event#setLocation(java.lang.String)
228   * @see #getFacebookLocation()
229   */
230  public void setFacebookLocation(FacebookLocation location) {
231    _event.setLocation((location == null ? null : location.getName()));
232  }
233
234  /**
235   * @see com.restfb.types.Event#getRsvpStatus()
236   * @see #setEventStatus(RSVPStatus)
237   * 
238   * @return status
239   */
240  @XmlElement(name = Definitions.ELEMENT_RSVP_STATUS)
241  public RSVPStatus getEventStatus(){
242    return RSVPStatus.fromRSVPStatusString(_event.getRsvpStatus());
243  }
244  
245  /**
246   * @param status
247   * @see com.restfb.types.Event#setRsvpStatus(java.lang.String)
248   * @see #getEventStatus()
249   */
250  public void setEventStatus(RSVPStatus status) {
251    _event.setRsvpStatus((status == null ? null : status.toRSVPStatusString()));
252  }
253
254  /**
255   * @see com.restfb.types.Event#getPrivacy()
256   * @see #setPrivacy(service.tut.pori.facebookjazz.FacebookGroup.Privacy)
257   * 
258   * @return privacy
259   */
260  @XmlElement(name = Definitions.ELEMENT_PRIVACY)
261  public Privacy getPrivacy() {
262    return Privacy.fromPrivacyString(_event.getPrivacy()); 
263  }
264  
265  /**
266   * @param privacy
267   * @see com.restfb.types.Event#setPrivacy(java.lang.String)
268   * @see #getPrivacy()
269   */
270  public void setPrivacy(Privacy privacy) {
271    _event.setPrivacy((privacy == null ? null : privacy.toPrivacyString()));
272  }
273
274  /**
275   * @see com.restfb.types.Event#getUpdatedTime()
276   * @see #setUpdatedTime(Date)
277   * 
278   * @return updated time
279   */
280  @XmlJavaTypeAdapter(ISODateAdapter.class)
281  @XmlElement(name = Definitions.ELEMENT_UPDATED_TIMESTAMP)
282  public Date getUpdatedTime() {
283    return _event.getUpdatedTime();
284  }
285
286  /**
287   * @see com.restfb.types.Event#getName()
288   * @see #getName()
289   * 
290   * @return name
291   */
292  @XmlElement(name = Definitions.ELEMENT_NAME)
293  public WeightedStringElement getWName() {
294    String name = _event.getName();
295    if(StringUtils.isBlank(name)){
296      return null;
297    }else{
298      return new WeightedStringElement(name, _nameWeight);
299    }
300  }
301  
302  /**
303   * for serialization
304   * @param name 
305   * @see #setName(String)
306   */
307  @SuppressWarnings("unused")
308  private void setWName(WeightedStringElement name) {
309    if(name == null){
310      setName(null);
311      setNameWeight(null);
312    }else{
313      setName(name.getValue());
314      setNameWeight(name.getWeight());
315    }
316  }
317
318  /**
319   * @return the nameWeight
320   * @see #setNameWeight(Integer)
321   */
322  public Integer getNameWeight() {
323    return _nameWeight;
324  }
325
326  /**
327   * @param nameWeight the nameWeight to set
328   * @see #getNameWeight()
329   */
330  public void setNameWeight(Integer nameWeight) {
331    _nameWeight = nameWeight;
332  }
333
334  /**
335   * @return the descriptionWeight
336   * @see #setDescriptionWeight(Integer)
337   */
338  public Integer getDescriptionWeight() {
339    return _descriptionWeight;
340  }
341
342  /**
343   * @param descriptionWeight the descriptionWeight to set
344   * @see #getDescriptionWeight()
345   */
346  public void setDescriptionWeight(Integer descriptionWeight) {
347    _descriptionWeight = descriptionWeight;
348  }
349
350  /**
351   * @param description
352   * @see com.restfb.types.Event#setDescription(java.lang.String)
353   * @see #getDescription()
354   */
355  public void setDescription(String description) {
356    _event.setDescription(description);
357  }
358
359  /**
360   * @param date
361   * @see com.restfb.types.Event#setEndTime(java.util.Date)
362   * @see #getEndTime()
363   */
364  public void setEndTime(Date date) {
365    _event.setEndTime(date);
366  }
367
368  /**
369   * @param name
370   * @see com.restfb.types.NamedFacebookType#setName(java.lang.String)
371   * @see #getName()
372   */
373  public void setName(String name) {
374    _event.setName(name);
375  }
376
377  /**
378   * @param date
379   * @see com.restfb.types.Event#setStartTime(java.util.Date)
380   * @see #getStartTime()
381   */
382  public void setStartTime(Date date) {
383    _event.setStartTime(date);
384  }
385
386  /**
387   * @param date
388   * @see com.restfb.types.Event#setUpdatedTime(java.util.Date)
389   * @see #getUpdatedTime()
390   */
391  public void setUpdatedTime(Date date) {
392    _event.setUpdatedTime(date);
393  }
394  
395  /**
396   * 
397   * @param events
398   * @return the given events wrapped as facebook events or null if null or empty list was passed
399   */
400  public static List<FacebookEvent> getFacebookEvents(List<Event> events){
401    if(events == null || events.isEmpty()){
402      return null;
403    }
404    List<FacebookEvent> fEvents = new ArrayList<>(events.size());
405    for(Event e : events){
406      fEvents.add(new FacebookEvent(e));
407    }
408    return fEvents;
409  }
410
411  /**
412   * @return description
413   * @see com.restfb.types.Event#getDescription()
414   * @see #setDescription(String)
415   */
416  public String getDescription() {
417    return _event.getDescription();
418  }
419
420  /**
421   * @return name
422   * @see com.restfb.types.NamedFacebookType#getName()
423   * @see #setName(String)
424   */
425  public String getName() {
426    return _event.getName();
427  }
428}