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.XmlRootElement;
026import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
027
028import org.apache.commons.lang3.StringUtils;
029
030import com.restfb.types.CategorizedFacebookType;
031import com.restfb.types.Comments;
032import com.restfb.types.Video;
033
034import core.tut.pori.utils.ISODateAdapter;
035import core.tut.pori.utils.ListUtils;
036
037
038/**
039 * Video description retrieved from Facebook.
040 * 
041 * <h2>Optional Elements</h2>
042 * <ul>
043 *  <li>{@value service.tut.pori.facebookjazz.Definitions#ELEMENT_COMMENT_LIST}</li>
044 *  <li>{@value service.tut.pori.facebookjazz.Definitions#ELEMENT_LIKE_COUNT}</li>
045 *  <li>{@value service.tut.pori.facebookjazz.Definitions#ELEMENT_LOCATION}</li>
046 * </ul>
047 * 
048 * <h3>XML Example</h3>
049 * 
050 * {@doc.restlet service="[service.tut.pori.facebookjazz.reference.Definitions#SERVICE_FBJ_REFERENCE_EXAMPLE]" method="[service.tut.pori.facebookjazz.Definitions#ELEMENT_VIDEO_DESCRIPTION]" type="GET" query="" body_uri=""}
051 * 
052 * @see com.restfb.types.Video
053 */
054@XmlRootElement(name=Definitions.ELEMENT_VIDEO_DESCRIPTION)
055@XmlAccessorType(XmlAccessType.NONE)
056public class FacebookVideoDescription {
057  private List<FacebookComment> _comments = null;
058  @XmlElement(name = Definitions.ELEMENT_LIKE_COUNT)
059  private Long _likeCount = null;
060  private Integer _descriptionWeight = null;
061  private Video _video = null;
062
063  /**
064   * 
065   */
066  public FacebookVideoDescription(){
067    _video = new Video();
068  }
069
070  /**
071   * 
072   * @param video
073   * @throws IllegalArgumentException 
074   */
075  public FacebookVideoDescription(Video video) throws IllegalArgumentException{
076    if(video == null){
077      throw new IllegalArgumentException("Invalid video.");
078    }
079    _video = video;
080  }
081
082  /**
083   * 
084   * @return true if this is a valid photo description
085   * 
086   */
087  public boolean isValid(){
088    String description = _video.getDescription();
089    Comments comments = _video.getComments();
090    if(StringUtils.isBlank(description) && (comments == null || ListUtils.isEmpty(comments.getData()))){
091      return false;
092    }else{
093      return true;
094    }
095  }
096
097  /**
098   * 
099   * @param name 
100   * @see #getFromName()
101   */
102  public void setFromName(String name){
103    if(StringUtils.isBlank(name)){
104      _video.setFrom(null);
105    }else{
106      CategorizedFacebookType from = _video.getFrom();
107      if(from == null){
108        from = new CategorizedFacebookType();
109        _video.setFrom(from);
110      }
111      from.setName(name);
112    }
113  }
114
115  /**
116   * @see com.restfb.types.Video#getFrom()
117   * @see com.restfb.types.CategorizedFacebookType#getName()
118   * @see #setFromName(String)
119   * 
120   * @return sender name
121   */
122  @XmlElement(name = Definitions.ELEMENT_MESSAGE_POSTER)
123  public String getFromName() {
124    CategorizedFacebookType from = _video.getFrom();
125    return (from == null ? null : from.getName());
126  }
127
128  /**
129   * @see com.restfb.types.Video#getDescription()
130   * @see #getDescription()
131   * 
132   * @return description
133   */
134  @XmlElement(name = Definitions.ELEMENT_DESCRIPTION)
135  public WeightedStringElement getWDescription() {
136    String description = _video.getDescription();
137    if(description == null){
138      description = _video.getName();
139    }
140    if(StringUtils.isBlank(description)){
141      return null;
142    }else{
143      return new WeightedStringElement(description, _descriptionWeight);
144    }
145  }
146
147  /**
148   * for serialization
149   * 
150   * @param description
151   * @see #setDescription(String)
152   */
153  @SuppressWarnings("unused")
154  private void setWDescription(WeightedStringElement description) {
155    if(description == null){
156      setDescription(null);
157      setDescriptionWeight(null);
158    }else{
159      setDescription(description.getValue());
160      setDescriptionWeight(description.getWeight());
161    }
162  }
163
164  /**
165   * @see com.restfb.types.Video#getComments()
166   * @see #setDescriptionComments(List)
167   * 
168   * @return comments or null if none
169   */
170  @XmlElement(name = Definitions.ELEMENT_COMMENT_LIST)
171  public List<FacebookComment> getDescriptionComments() {
172    if(_comments == null){
173      Comments comments = _video.getComments();
174      return (comments == null ? null : FacebookComment.getCommentList(comments.getData()));
175    }else{
176      return _comments;
177    }
178  }
179
180  /**
181   * 
182   * @param comments replace currently set comments with the passed list of comments
183   * @see #getDescriptionComments()
184   */
185  public void setDescriptionComments(List<FacebookComment> comments){
186    _comments = comments;
187  }
188
189  /**
190   * @see com.restfb.types.Video#getCreatedTime()
191   * @see #setCreatedTime(Date)
192   * 
193   * @return created timestamp
194   */
195  @XmlJavaTypeAdapter(ISODateAdapter.class)
196  @XmlElement(name = Definitions.ELEMENT_CREATED_TIMESTAMP)
197  public Date getCreatedTime() {
198    return _video.getCreatedTime();
199  }
200
201  /**
202   * @see com.restfb.types.Video#getUpdatedTime()
203   * @see #setUpdatedTime(Date)
204   * 
205   * @return  updated timestamp
206   */
207  @XmlJavaTypeAdapter(ISODateAdapter.class)
208  @XmlElement(name = Definitions.ELEMENT_UPDATED_TIMESTAMP)
209  public Date getUpdatedTime() {
210    return _video.getUpdatedTime();
211  }
212
213  /**
214   * 
215   * @return like count
216   * @see #setLikeCount(Long)
217   */
218  public Long getLikeCount() {
219    return _likeCount;
220  }
221
222  /**
223   * 
224   * @param likeCount 
225   * @see #getLikeCount()
226   */
227  public void setLikeCount(Long likeCount) {
228    _likeCount = likeCount;
229  }
230
231  /**
232   * @return the descriptionWeight
233   * @see #setDescriptionWeight(Integer)
234   */
235  public Integer getDescriptionWeight() {
236    return _descriptionWeight;
237  }
238
239  /**
240   * @param descriptionWeight the descriptionWeight to set
241   * @see #getDescriptionWeight()
242   */
243  public void setDescriptionWeight(Integer descriptionWeight) {
244    _descriptionWeight = descriptionWeight;
245  }
246
247  /**
248   * @param date
249   * @see com.restfb.types.Video#setCreatedTime(java.util.Date)
250   * @see #getCreatedTime()
251   */
252  public void setCreatedTime(Date date) {
253    _video.setCreatedTime(date);
254  }
255
256  /**
257   * @param description
258   * @see com.restfb.types.Video#setDescription(java.lang.String)
259   * @see #getDescription()
260   */
261  public void setDescription(String description) {
262    _video.setDescription(description);
263  }
264
265  /**
266   * @param date
267   * @see com.restfb.types.Video#setUpdatedTime(java.util.Date)
268   * @see #getUpdatedTime()
269   */
270  public void setUpdatedTime(Date date) {
271    _video.setUpdatedTime(date);
272  }
273
274  /**
275   * @return id
276   * @see com.restfb.types.FacebookType#getId()
277   * @see #setId(String)
278   */
279  public String getId() {
280    return _video.getId();
281  }
282
283  /**
284   * @param id
285   * @see com.restfb.types.FacebookType#setId(java.lang.String)
286   * @see #getId()
287   */
288  public void setId(String id) {
289    _video.setId(id);
290  }
291
292  /**
293   * 
294   * @param videos
295   * @return videos wrapped to video descriptions or null if null or empty list was passed
296   */
297  public static List<FacebookVideoDescription> getFacebookVideoDescriptions(List<Video> videos){
298    if(videos == null || videos.isEmpty()){
299      return null;
300    }
301    List<FacebookVideoDescription> descriptions = new ArrayList<>(videos.size());
302    for(Video v : videos){
303      descriptions.add(new FacebookVideoDescription(v));
304    }
305    return descriptions;
306  }
307
308  /**
309   * @return description
310   * @see com.restfb.types.Video#getDescription()
311   * @see #setDescription(String)
312   */
313  public String getDescription() {
314    return _video.getDescription();
315  }
316}