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.Date;
019
020import javax.xml.bind.annotation.XmlAccessType;
021import javax.xml.bind.annotation.XmlAccessorType;
022import javax.xml.bind.annotation.XmlElement;
023import javax.xml.bind.annotation.XmlRootElement;
024import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
025
026import org.apache.commons.lang3.ArrayUtils;
027import org.apache.log4j.Logger;
028
029import core.tut.pori.utils.ISODateAdapter;
030import twitter4j.MediaEntity;
031import twitter4j.Status;
032
033
034/**
035 * Video description retrieved from Twitter.
036 * 
037 * <h2>Optional Elements</h2>
038 * <ul>
039 *  <li>{@value service.tut.pori.twitterjazz.Definitions#ELEMENT_LOCATION}</li>
040 * </ul>
041 * 
042 * <h3>XML Example</h3>
043 * 
044 * {@doc.restlet service="[service.tut.pori.twitterjazz.reference.Definitions#SERVICE_TJ_REFERENCE_EXAMPLE]" method="[service.tut.pori.twitterjazz.Definitions#ELEMENT_VIDEO_DESCRIPTION]" type="GET" query="" body_uri=""}
045 * 
046 * @see twitter4j.Status
047 */
048@XmlRootElement(name=Definitions.ELEMENT_VIDEO_DESCRIPTION)
049@XmlAccessorType(XmlAccessType.NONE)
050public class TwitterVideoDescription {
051  private static final Logger LOGGER = Logger.getLogger(TwitterVideoDescription.class);
052  @XmlJavaTypeAdapter(ISODateAdapter.class)
053  @XmlElement(name = Definitions.ELEMENT_CREATED_TIMESTAMP)
054  private Date _createdTime = null;
055  @XmlElement(name = Definitions.ELEMENT_LOCATION)
056  private TwitterLocation _location = null;
057  @XmlElement(name = Definitions.ELEMENT_DESCRIPTION)
058  private String _description = null;
059  @XmlElement(name = Definitions.ELEMENT_MESSAGE_POSTER)
060  private String _fromScreenName = null;
061  
062  /**
063   * 
064   * @param status
065   * @return video description or null if the passes status was null or not a valid video description
066   */
067  public static TwitterVideoDescription getTwitterVideoDescription(Status status){
068    if(status == null){
069      return null;
070    }
071    
072    MediaEntity[] entityArray = status.getMediaEntities();
073    if(ArrayUtils.isEmpty(entityArray)){
074      LOGGER.debug("The status object has no entities.");
075      return null;
076    }
077    
078    boolean isVideoDescription = false;
079    for(MediaEntity e : entityArray){
080      if(Definitions.TWITTER_TYPE_VIDEO.equalsIgnoreCase(e.getType())){
081        isVideoDescription = true;
082        break;
083      }
084    }
085    
086    if(!isVideoDescription){
087      LOGGER.debug("The status object contains no valid video URLs.");
088      return null;
089    }
090    
091    TwitterVideoDescription d = new TwitterVideoDescription();
092    d._createdTime = status.getCreatedAt();
093    d._location = TwitterLocation.getTwitterLocation(status.getGeoLocation());
094    d._description = status.getText();
095    d._fromScreenName = status.getUser().getScreenName();
096    return d;
097  }
098
099  /**
100   * @see twitter4j.Status#getCreatedAt()
101   * 
102   * @return the createdTime
103   */
104  public Date getCreatedTime() {
105    return _createdTime;
106  }
107
108  /**
109   * 
110   * @param createdTime the createdTime to set
111   */
112  public void setCreatedTime(Date createdTime) {
113    _createdTime = createdTime;
114  }
115
116  /**
117   * @see twitter4j.Status#getGeoLocation()
118   * 
119   * @return the location
120   */
121  public TwitterLocation getLocation() {
122    return _location;
123  }
124
125  /**
126   * @param location the location to set
127   */
128  public void setLocation(TwitterLocation location) {
129    _location = location;
130  }
131
132  /**
133   * @see twitter4j.Status#getText()
134   * 
135   * @return the description
136   */
137  public String getDescription() {
138    return _description;
139  }
140
141  /**
142   * @param description the description to set
143   */
144  public void setDescription(String description) {
145    _description = description;
146  }
147
148  /**
149   * @see twitter4j.Status#getUser()
150   * @see twitter4j.User#getScreenName()
151   * 
152   * @return the fromScreenName
153   */
154  public String getFromScreenName() {
155    return _fromScreenName;
156  }
157
158  /**
159   * @param fromScreenName the fromScreenName to set
160   */
161  public void setFromScreenName(String fromScreenName) {
162    _fromScreenName = fromScreenName;
163  }
164}