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.twitterjazz; 017 018import java.util.ArrayList; 019import java.util.Collection; 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; 026 027import org.apache.log4j.Logger; 028 029import twitter4j.User; 030import core.tut.pori.users.UserIdentity; 031 032/** 033 * Details of a Twitter user. 034 * 035 * <h2>Optional Elements</h2> 036 * <ul> 037 * <li>{@value service.tut.pori.twitterjazz.Definitions#ELEMENT_BIO}</li> 038 * <li>{@value service.tut.pori.twitterjazz.Definitions#ELEMENT_LOCATION}</li> 039 * </ul> 040 * 041 * <h3>XML Example</h3> 042 * 043 * {@doc.restlet service="[service.tut.pori.twitterjazz.reference.Definitions#SERVICE_TJ_REFERENCE_EXAMPLE]" method="[service.tut.pori.twitterjazz.Definitions#ELEMENT_USER_DETAILS]" type="GET" query="" body_uri=""} 044 * 045 * @see twitter4j.User 046 */ 047@XmlRootElement(name=Definitions.ELEMENT_USER_DETAILS) 048@XmlAccessorType(XmlAccessType.NONE) 049public class TwitterUserDetails { 050 private static final Logger LOGGER = Logger.getLogger(TwitterUserDetails.class); 051 @XmlElement(name=Definitions.ELEMENT_BIO) 052 private String _bio = null; 053 @XmlElement(name=Definitions.ELEMENT_FAVORITES_COUNT) 054 private Integer _favoritesCount = null; 055 @XmlElement(name=Definitions.ELEMENT_FRIENDS_COUNT) 056 private Integer _friendsCount = null; 057 @XmlElement(name=Definitions.ELEMENT_FOLLOWERS_COUNT) 058 private Integer _followersCount = null; 059 @XmlElement(name=Definitions.ELEMENT_NAME) 060 private String _name = null; 061 private Boolean _protected = null; 062 @XmlElement(name=Definitions.ELEMENT_SCREEN_NAME) 063 private String _screenName = null; 064 @XmlElement(name=Definitions.ELEMENT_TWITTER_ID) 065 private String _twitterId = null; 066 private UserIdentity _userId = null; 067 @XmlElement(name=Definitions.ELEMENT_LOCATION) 068 private TwitterLocation _location = null; 069 070 /** 071 * 072 * @param user 073 * @return twitter user details or null if the passes user was null 074 */ 075 public static TwitterUserDetails getTwitterUserDetails(User user){ 076 if(user == null){ 077 LOGGER.warn("Null user."); 078 return null; 079 } 080 TwitterUserDetails tud = new TwitterUserDetails(); 081 tud._bio = user.getDescription(); 082 tud._favoritesCount = user.getFavouritesCount(); 083 tud._friendsCount = user.getFriendsCount(); 084 tud._followersCount = user.getFollowersCount(); 085 tud._name = user.getName(); 086 tud._screenName = user.getScreenName(); 087 Long id = user.getId(); 088 tud._twitterId = (id == null ? null : String.valueOf(id)); 089 tud._location = TwitterLocation.getTwitterLocation(user.getLocation()); 090 tud._protected = user.isProtected(); 091 return tud; 092 } 093 094 /** 095 * @see #getUserId() 096 * 097 * @return user identity value 098 */ 099 @XmlElement(name = core.tut.pori.users.Definitions.ELEMENT_USER_ID) 100 public Long getUserIdValue(){ 101 return (_userId == null ? null : _userId.getUserId()); 102 } 103 104 /** 105 * For serialization 106 * 107 * @see #setUserId(UserIdentity) 108 * 109 * @param userId 110 */ 111 @SuppressWarnings("unused") 112 private void setUserIdValue(Long userId){ 113 _userId = (userId == null ? null : new UserIdentity(userId)); 114 } 115 116 /** 117 * 118 * @return user identity 119 */ 120 public UserIdentity getUserId() { 121 return _userId; 122 } 123 124 /** 125 * 126 * @param userId 127 */ 128 public void setUserId(UserIdentity userId) { 129 _userId = userId; 130 } 131 132 /** 133 * 134 * @param users 135 * @return the collection of user converted to twitter user details or null if null or empty collection was passed 136 */ 137 public static List<TwitterUserDetails> getTwitterUserDetails(Collection<User> users) { 138 if(users == null || users.isEmpty()){ 139 LOGGER.warn("Null or empty user list."); 140 return null; 141 } 142 143 List<TwitterUserDetails> details = new ArrayList<>(users.size()); 144 for(User user : users){ 145 TwitterUserDetails ud = getTwitterUserDetails(user); 146 if(ud == null){ 147 LOGGER.warn("Ignored bad user object."); 148 }else{ 149 details.add(ud); 150 } 151 } 152 153 return (details.isEmpty() ? null : details); 154 } 155 156 /** 157 * @see twitter4j.User#getDescription() 158 * 159 * @return the bio 160 */ 161 public String getBio() { 162 return _bio; 163 } 164 165 /** 166 * @param bio the bio to set 167 */ 168 public void setBio(String bio) { 169 _bio = bio; 170 } 171 172 /** 173 * @see twitter4j.User#getFavouritesCount() 174 * 175 * @return the favoritesCount 176 */ 177 public Integer getFavoritesCount() { 178 return _favoritesCount; 179 } 180 181 /** 182 * @param favoritesCount the favoritesCount to set 183 */ 184 public void setFavoritesCount(Integer favoritesCount) { 185 _favoritesCount = favoritesCount; 186 } 187 188 /** 189 * @see twitter4j.User#getFriendsCount() 190 * 191 * @return the friendsCount 192 */ 193 public Integer getFriendsCount() { 194 return _friendsCount; 195 } 196 197 /** 198 * @param friendsCount the friendsCount to set 199 */ 200 public void setFriendsCount(Integer friendsCount) { 201 _friendsCount = friendsCount; 202 } 203 204 /** 205 * @see twitter4j.User#getFollowersCount() 206 * 207 * @return the followersCount 208 */ 209 public Integer getFollowersCount() { 210 return _followersCount; 211 } 212 213 /** 214 * @param followersCount the followersCount to set 215 */ 216 public void setFollowersCount(Integer followersCount) { 217 _followersCount = followersCount; 218 } 219 220 /** 221 * @see twitter4j.User#getName() 222 * 223 * @return the name 224 */ 225 public String getName() { 226 return _name; 227 } 228 229 /** 230 * @param name the name to set 231 */ 232 public void setName(String name) { 233 _name = name; 234 } 235 236 /** 237 * @see twitter4j.User#isProtected() 238 * 239 * @return the protected 240 */ 241 public Boolean isProtected() { 242 return _protected; 243 } 244 245 /** 246 * @param protected1 the protected to set 247 */ 248 public void setProtected(Boolean protected1) { 249 _protected = protected1; 250 } 251 252 /** 253 * @see twitter4j.User#getScreenName() 254 * 255 * @return the screenName 256 */ 257 public String getScreenName() { 258 return _screenName; 259 } 260 261 /** 262 * @param screenName the screenName to set 263 */ 264 public void setScreenName(String screenName) { 265 _screenName = screenName; 266 } 267 268 /** 269 * @see twitter4j.User#getId() 270 * 271 * @return the twitterId 272 */ 273 public String getTwitterId() { 274 return _twitterId; 275 } 276 277 /** 278 * @param twitterId the twitterId to set 279 */ 280 public void setTwitterId(String twitterId) { 281 _twitterId = twitterId; 282 } 283 284 /** 285 * @see twitter4j.User#getLocation() 286 * 287 * @return the location 288 */ 289 public TwitterLocation getLocation() { 290 return _location; 291 } 292 293 /** 294 * @param location the location to set 295 */ 296 public void setLocation(TwitterLocation location) { 297 _location = location; 298 } 299}