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.XmlElementWrapper; 026import javax.xml.bind.annotation.XmlRootElement; 027import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 028 029import org.apache.commons.lang3.StringUtils; 030import org.apache.log4j.Logger; 031 032import com.restfb.types.NamedFacebookType; 033import com.restfb.types.StatusMessage; 034 035import core.tut.pori.utils.ISODateAdapter; 036 037 038/** 039 * Status message 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 * </ul> 046 * 047 * <h3>XML Example</h3> 048 * 049 * {@doc.restlet service="[service.tut.pori.facebookjazz.reference.Definitions#SERVICE_FBJ_REFERENCE_EXAMPLE]" method="[service.tut.pori.facebookjazz.Definitions#ELEMENT_STATUS_MESSAGE]" type="GET" query="" body_uri=""} 050 * 051 * @see com.restfb.types.StatusMessage 052 */ 053@XmlRootElement(name=Definitions.ELEMENT_STATUS_MESSAGE) 054@XmlAccessorType(XmlAccessType.NONE) 055public class FacebookStatusMessage { 056 private static final Logger LOGGER = Logger.getLogger(FacebookStatusMessage.class); 057 private List<FacebookComment> _comments = null; 058 private Integer _messageWeight = null; 059 private StatusMessage _statusMessage = null; 060 061 /** 062 * 063 */ 064 public FacebookStatusMessage() { 065 _statusMessage = new StatusMessage(); 066 } 067 068 /** 069 * 070 * @param statusMessage 071 * @throws IllegalArgumentException 072 */ 073 public FacebookStatusMessage(StatusMessage statusMessage) throws IllegalArgumentException{ 074 if(statusMessage == null){ 075 throw new IllegalArgumentException("Invalid status message."); 076 } 077 _statusMessage = statusMessage; 078 } 079 080 /** 081 * @see com.restfb.types.StatusMessage#getComments() 082 * @see #setMessageComments(List) 083 * 084 * @return a list of facebook comments based on contained list of comments (if any) 085 */ 086 @XmlElementWrapper(name = Definitions.ELEMENT_COMMENT_LIST) 087 @XmlElement(name = Definitions.ELEMENT_COMMENT) 088 public List<FacebookComment> getMessageComments() { 089 if(_comments == null){ 090 _comments = FacebookComment.getCommentList(_statusMessage.getComments()); 091 } 092 return _comments; 093 } 094 095 /** 096 * 097 * @param comments 098 * @see #getMessageComments() 099 */ 100 public void setMessageComments(List<FacebookComment> comments) { 101 _comments = comments; 102 } 103 104 /** 105 * @see com.restfb.types.StatusMessage#getLikes() 106 * 107 * @return like count 108 */ 109 @XmlElement(name = Definitions.ELEMENT_LIKE_COUNT) 110 public Long getLikeCount(){ 111 long count = _statusMessage.getLikes().size(); // create long as it is also long in the FacebookComment 112 return count; 113 } 114 115 116 /** 117 * This method does nothing and exists only to enable unmarshalling/marshalling. 118 * 119 * If you want to add likes use addLike() 120 * 121 * @see #addLike(NamedFacebookType) 122 * @see #getLikeCount() 123 * 124 * @param likeCount 125 */ 126 @SuppressWarnings("unused") 127 private void setLikeCount(Long likeCount){ 128 LOGGER.warn("Ignored "+Definitions.ELEMENT_LIKE_COUNT+" : "+likeCount); 129 } 130 131 /** 132 * @see com.restfb.types.StatusMessage#getFrom() 133 * @see com.restfb.types.NamedFacebookType#getName() 134 * @see #setFromName(String) 135 * 136 * @return sender name 137 */ 138 @XmlElement(name = Definitions.ELEMENT_MESSAGE_POSTER) 139 public String getFromName() { 140 NamedFacebookType from = _statusMessage.getFrom(); 141 return (from == null ? null : from.getName()); 142 } 143 144 /** 145 * @see com.restfb.types.StatusMessage#setFrom(com.restfb.types.NamedFacebookType) 146 * @see #getFromName() 147 * 148 * @param name 149 */ 150 public void setFromName(String name){ 151 if(StringUtils.isBlank(name)){ 152 _statusMessage.setFrom(null); 153 }else{ 154 NamedFacebookType from = _statusMessage.getFrom(); 155 if(from == null){ 156 from = new NamedFacebookType(); 157 _statusMessage.setFrom(from); 158 } 159 from.setName(name); 160 } 161 } 162 163 /** 164 * @see com.restfb.types.StatusMessage#getMessage() 165 * @see #getMessage() 166 * 167 * @return message 168 */ 169 @XmlElement(name = Definitions.ELEMENT_MESSAGE) 170 public WeightedStringElement getWMessage() { 171 String message = _statusMessage.getMessage(); 172 if(StringUtils.isBlank(message)){ 173 return null; 174 }else{ 175 return new WeightedStringElement(message, _messageWeight); 176 } 177 } 178 179 /** 180 * for serialization 181 * 182 * @param message 183 * @see com.restfb.types.StatusMessage#setMessage(String) 184 * @see #setMessage(String) 185 */ 186 @SuppressWarnings("unused") 187 private void setWMessage(WeightedStringElement message) { 188 if(message == null){ 189 _statusMessage.setMessage(null); 190 setMessageWeight(null); 191 }else{ 192 _statusMessage.setMessage(message.getValue()); 193 setMessageWeight(message.getWeight()); 194 } 195 } 196 197 /** 198 * @see com.restfb.types.StatusMessage#getUpdatedTime() 199 * @see #setUpdatedTime(Date) 200 * 201 * @return updated timestamp 202 */ 203 @XmlElement(name = Definitions.ELEMENT_UPDATED_TIMESTAMP) 204 @XmlJavaTypeAdapter(ISODateAdapter.class) 205 public Date getUpdatedTime() { 206 return _statusMessage.getUpdatedTime(); 207 } 208 209 /** 210 * 211 * @return message weight 212 * @see #setMessageWeight(Integer) 213 */ 214 public Integer getMessageWeight() { 215 return _messageWeight; 216 } 217 218 /** 219 * 220 * @param messageWeight 221 * @see #getMessageWeight() 222 */ 223 public void setMessageWeight(Integer messageWeight) { 224 _messageWeight = messageWeight; 225 } 226 227 /** 228 * @param like 229 * @return true on success 230 * @see com.restfb.types.StatusMessage#addLike(com.restfb.types.NamedFacebookType) 231 * @see #getLikeCount() 232 */ 233 public boolean addLike(NamedFacebookType like) { 234 return _statusMessage.addLike(like); 235 } 236 237 /** 238 * @param message 239 * @see com.restfb.types.StatusMessage#setMessage(java.lang.String) 240 * @see #getMessage() 241 */ 242 public void setMessage(String message) { 243 _statusMessage.setMessage(message); 244 } 245 246 /** 247 * @param date 248 * @see com.restfb.types.StatusMessage#setUpdatedTime(java.util.Date) 249 * @see #getUpdatedTime() 250 */ 251 public void setUpdatedTime(Date date) { 252 _statusMessage.setUpdatedTime(date); 253 } 254 255 /** 256 * 257 * @param statuses 258 * @return the given statuses wrapped to status messages or null if null or empty list was passed 259 */ 260 public static List<FacebookStatusMessage> getFacebookStatusMessages(List<StatusMessage> statuses){ 261 if(statuses == null || statuses.isEmpty()){ 262 return null; 263 } 264 List<FacebookStatusMessage> fsm = new ArrayList<>(statuses.size()); 265 for(StatusMessage sm : statuses){ 266 fsm.add(new FacebookStatusMessage(sm)); 267 } 268 return fsm; 269 } 270 271 /** 272 * @return message 273 * @see com.restfb.types.StatusMessage#getMessage() 274 * @see #setMessage(String) 275 */ 276 public String getMessage() { 277 return _statusMessage.getMessage(); 278 } 279}