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.Iterator;
021import java.util.List;
022
023import javax.xml.bind.annotation.XmlAccessType;
024import javax.xml.bind.annotation.XmlAccessorType;
025import javax.xml.bind.annotation.XmlElement;
026import javax.xml.bind.annotation.XmlRootElement;
027import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
028
029import org.apache.commons.lang3.StringUtils;
030
031import com.restfb.types.CategorizedFacebookType;
032import com.restfb.types.Comment;
033
034import core.tut.pori.utils.ISODateAdapter;
035
036
037/**
038 * A user comment retrieved from Facebook.
039 * 
040 * <h2>Optional Elements</h2>
041 * <ul>
042 *  <li>{@value service.tut.pori.facebookjazz.Definitions#ELEMENT_LIKE_COUNT}</li>
043 * </ul>
044 * 
045 * <h3>XML Example</h3>
046 * 
047 * {@doc.restlet service="[service.tut.pori.facebookjazz.reference.Definitions#SERVICE_FBJ_REFERENCE_EXAMPLE]" method="[service.tut.pori.facebookjazz.Definitions#ELEMENT_COMMENT]" type="GET" query="" body_uri=""}
048 *
049 * @see com.restfb.types.Comment
050 */
051@XmlRootElement(name=Definitions.ELEMENT_COMMENT)
052@XmlAccessorType(XmlAccessType.NONE)
053public class FacebookComment {
054  private Comment _comment = null;
055  private Integer _messageWeight = null;
056  
057  /**
058   * 
059   */
060  public FacebookComment(){
061    _comment = new Comment();
062  }
063  
064  /**
065   * 
066   * @param comment
067   * @throws IllegalArgumentException
068   */
069  public FacebookComment(Comment comment) throws IllegalArgumentException{
070    if(comment == null){
071      throw new IllegalArgumentException("Invalid comment.");
072    }
073    _comment = comment;
074  }
075
076  /**
077   * @param date
078   * @see com.restfb.types.Comment#setCreatedTime(java.util.Date)
079   * @see #getCreatedTime()
080   */
081  public void setCreatedTime(Date date) {
082    _comment.setCreatedTime(date);
083  }
084
085  /**
086   * @param count
087   * @see com.restfb.types.Comment#setLikeCount(java.lang.Long)
088   * @see #getLikeCount()
089   */
090  public void setLikeCount(Long count) {
091    _comment.setLikeCount(count);
092  }
093
094  /**
095   * @param message
096   * @see com.restfb.types.Comment#setMessage(java.lang.String)
097   * @see #getMessage()
098   */
099  public void setMessage(String message) {
100    _comment.setMessage(message);
101  }
102
103  /**
104   * convert the given list of comments to FacebookComments
105   * 
106   * @param comments
107   * @return the given comments converted to facebook comment or null if null or empty list was passed
108   */
109  public static List<FacebookComment> getCommentList(List<Comment> comments){
110    List<FacebookComment> list = null;
111    if(comments != null && !comments.isEmpty()){
112      list = new ArrayList<>();
113      for(Iterator<Comment> iter = comments.iterator();iter.hasNext();){
114        FacebookComment comment = new FacebookComment(iter.next());
115        if(comment != null){
116          list.add(comment);
117        }   
118      }  // for
119      if(list.isEmpty()){
120        list = null;
121      }
122    }
123    return list;
124  }
125
126  /**
127   * @see com.restfb.types.Comment#getCreatedTime()
128   * 
129   * @return created time
130   * @see #setCreatedTime(Date)
131   */
132  @XmlJavaTypeAdapter(ISODateAdapter.class)
133  @XmlElement(name = Definitions.ELEMENT_CREATED_TIMESTAMP)
134  public Date getCreatedTime() {
135    return _comment.getCreatedTime();
136  }
137
138  /**
139   * @see com.restfb.types.Comment#getFrom()
140   * @see com.restfb.types.CategorizedFacebookType#getName()
141   * @see #setFromName(String)
142   * 
143   * @return sender name
144   */
145  @XmlElement(name = Definitions.ELEMENT_MESSAGE_POSTER)
146  public String getFromName() {
147    return _comment.getFrom().getName();
148  }
149  
150  /**
151   * @param fromName the fromName to set
152   * @see #getFromName()
153   */
154  public void setFromName(String fromName) {
155    if(StringUtils.isBlank(fromName)){
156      _comment.setFrom(null);
157    }else{
158      CategorizedFacebookType from = _comment.getFrom();
159      if(from == null){
160        from = new CategorizedFacebookType();
161        _comment.setFrom(from);
162      }
163      from.setName(fromName);
164    }
165  }
166  
167  /**
168   * @see com.restfb.types.Comment#getMessage()
169   * 
170   * @return message
171   * @see #getMessage()
172   */
173  @XmlElement(name = Definitions.ELEMENT_MESSAGE)
174  public WeightedStringElement getWMessage() {
175    String message = _comment.getMessage();
176    if(StringUtils.isBlank(message)){
177      return null;
178    }else{
179      return new WeightedStringElement(message, _messageWeight);
180    }
181  }
182  
183  /**
184   * for serialization
185   * @param message 
186   * @see #setMessage(String)
187   */
188  @SuppressWarnings("unused")
189  private void setWMessage(WeightedStringElement message) {
190    if(message == null){
191      setMessage(null);
192      setMessageWeight(null);
193    }else{
194      setMessage(message.getValue());
195      setMessageWeight(message.getWeight());
196    }
197  }
198
199  /**
200   * @see com.restfb.types.Comment#getLikeCount()
201   * 
202   * @return like count
203   * @see #setLikeCount(Long)
204   */
205  @XmlElement(name = Definitions.ELEMENT_LIKE_COUNT)
206  public Long getLikeCount() {
207    return _comment.getLikeCount();
208  }
209
210  /**
211   * @return the commentWeight
212   * @see #setMessageWeight(Integer)
213   */
214  public Integer getMessageWeight() {
215    return _messageWeight;
216  }
217
218  /**
219   * @param messageWeight the commentWeight to set
220   * @see #getMessageWeight()
221   */
222  public void setMessageWeight(Integer messageWeight) {
223    _messageWeight = messageWeight;
224  }
225
226  /**
227   * @return message
228   * @see com.restfb.types.Comment#getMessage()
229   * @see #setMessage(String)
230   */
231  public String getMessage() {
232    return _comment.getMessage();
233  }
234}