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.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.ArrayUtils;
030import org.apache.log4j.Logger;
031
032import service.tut.pori.contentanalysis.CAContentCore.ServiceType;
033import twitter4j.MediaEntity;
034import twitter4j.Status;
035import core.tut.pori.utils.ISODateAdapter;
036
037/**
038 * A photo description received from Twitter.
039 * 
040 * <h2>Optional Elements</h2>
041 * <ul>
042 *  <li>{@value service.tut.pori.twitterjazz.Definitions#ELEMENT_LOCATION}</li>
043 *  <li>{@value service.tut.pori.twitterjazz.Definitions#ELEMENT_PHOTO_GUID}, missing if the photo is not known by the system.</li>
044 *  <li>{@value service.tut.pori.twitterjazz.Definitions#ELEMENT_PHOTO_TAG_LIST}</li>
045 *  <li>{@value service.tut.pori.contentanalysis.Definitions#ELEMENT_SERVICE_ID}, missing if the photo is not known by the system.</li>
046 * </ul>
047 * 
048 * <h3>XML Example</h3>
049 * 
050 * {@doc.restlet service="[service.tut.pori.twitterjazz.reference.Definitions#SERVICE_TJ_REFERENCE_EXAMPLE]" method="[service.tut.pori.twitterjazz.Definitions#ELEMENT_PHOTO_DESCRIPTION]" type="GET" query="" body_uri=""}
051 * 
052 * @see twitter4j.Status
053 */
054@XmlRootElement(name=Definitions.ELEMENT_PHOTO_DESCRIPTION)
055@XmlAccessorType(XmlAccessType.NONE)
056public class TwitterPhotoDescription {
057  private static final Logger LOGGER = Logger.getLogger(TwitterPhotoDescription.class);
058  @XmlJavaTypeAdapter(ISODateAdapter.class)
059  @XmlElement(name = Definitions.ELEMENT_CREATED_TIMESTAMP)
060  private Date _createdTime = null;
061  @XmlElement(name = Definitions.ELEMENT_DESCRIPTION)
062  private String _description = null;
063  private String _entityId = null;
064  private String _entityUrl = null;
065  @XmlElement(name = Definitions.ELEMENT_MESSAGE_POSTER)
066  private String _fromName = null;
067  @XmlElement(name = Definitions.ELEMENT_LOCATION)
068  private TwitterLocation _location = null;
069  @XmlElement(name = Definitions.ELEMENT_PHOTO_GUID)
070  private String _photoGUID = null;
071  @XmlElement(name = service.tut.pori.contentanalysis.Definitions.ELEMENT_SERVICE_ID)
072  private ServiceType _serviceType = null;
073  @XmlElementWrapper(name = Definitions.ELEMENT_PHOTO_TAG_LIST)
074  @XmlElement(name = Definitions.ELEMENT_PHOTO_TAG)
075  private List<TwitterPhotoTag> _tags = null;
076  
077  /**
078   * 
079   * @param twitterTag
080   * @see #getTags()
081   */
082  public void addTag(TwitterPhotoTag twitterTag) {
083    if(_tags == null){
084      _tags = new ArrayList<>();
085    }
086    _tags.add(twitterTag);
087  }
088
089  /**
090   * @return the tags
091   * @see #setTags(List)
092   */
093  public List<TwitterPhotoTag> getTags() {
094    return _tags;
095  }
096
097  /**
098   * @param tags the tags to set
099   * @see #getTags()
100   */
101  public void setTags(List<TwitterPhotoTag> tags) {
102    _tags = tags;
103  }
104
105  /**
106   * @return the photoGUID
107   * @see #setPhotoGUID(String)
108   */
109  public String getPhotoGUID() {
110    return _photoGUID;
111  }
112
113  /**
114   * @param photoGUID the photoGUID to set
115   * @see #getPhotoGUID()
116   */
117  public void setPhotoGUID(String photoGUID) {
118    _photoGUID  = photoGUID;
119  }
120
121  /**
122   * @return the entityId
123   * @see #setEntityId(String)
124   */
125  public String getEntityId() {
126    return _entityId;
127  }
128
129  /**
130   * @return the entityUrl
131   * @see #setEntityUrl(String)
132   */
133  public String getEntityUrl() {
134    return _entityUrl;
135  }
136
137  /**
138   * 
139   * @param status
140   * @return photo descriptions or null if the passes status was null or did not contain valid photo descriptions
141   */
142  public static List<TwitterPhotoDescription> getTwitterPhotoDescriptions(Status status){
143    if(status == null){
144      LOGGER.warn("null status.");
145      return null;
146    }
147
148    MediaEntity[] entities = status.getMediaEntities();
149    if(ArrayUtils.isEmpty(entities)){
150      LOGGER.debug("No entities.");
151      return null;
152    }
153
154    TwitterLocation location = TwitterLocation.getTwitterLocation(status.getGeoLocation());
155    ArrayList<TwitterPhotoDescription> descriptions = new ArrayList<>(entities.length);
156    for(MediaEntity e : entities){
157      if(Definitions.TWITTER_TYPE_PHOTO.equalsIgnoreCase(e.getType())){
158        TwitterPhotoDescription d = new TwitterPhotoDescription();
159        d._createdTime = status.getCreatedAt();
160        d._description = status.getText();
161        d._fromName = status.getUser().getScreenName();
162        d._location = location;
163        d._entityUrl = e.getMediaURL();
164        d._entityId = String.valueOf(e.getId());
165        descriptions.add(d);
166      }
167    }
168
169    return (descriptions.isEmpty() ? null : descriptions);
170  }
171
172  /**
173   * @return the serviceType
174   * @see #setServiceType(service.tut.pori.contentanalysis.CAContentCore.ServiceType)
175   */
176  public ServiceType getServiceType() {
177    return _serviceType;
178  }
179
180  /**
181   * @param serviceType the serviceType to set
182   * @see #getServiceType()
183   */
184  public void setServiceType(ServiceType serviceType) {
185    _serviceType = serviceType;
186  }
187
188  /**
189   * @see twitter4j.Status#getCreatedAt()
190   * @see #setCreatedTime(Date)
191   * 
192   * @return the createdTime
193   */
194  public Date getCreatedTime() {
195    return _createdTime;
196  }
197
198  /**
199   * @param createdTime the createdAt to set
200   * @see #getCreatedTime()
201   */
202  public void setCreatedTime(Date createdTime) {
203    _createdTime = createdTime;
204  }
205
206  /**
207   * @see twitter4j.Status#getText()
208   * @see #setDescription(String)
209   * 
210   * @return the description
211   */
212  public String getDescription() {
213    return _description;
214  }
215
216  /**
217   * @param description the description to set
218   * @see #getDescription()
219   */
220  public void setDescription(String description) {
221    _description = description;
222  }
223
224  /**
225   * @see twitter4j.Status#getUser()
226   * @see twitter4j.User#getScreenName()
227   * @see #setFromName(String)
228   * 
229   * @return the fromName
230   */
231  public String getFromName() {
232    return _fromName;
233  }
234
235  /**
236   * @param fromName the fromName to set
237   * @see #getFromName()
238   */
239  public void setFromName(String fromName) {
240    _fromName = fromName;
241  }
242
243  /**
244   * @see twitter4j.Status#getGeoLocation()
245   * @see #setLocation(TwitterLocation)
246   * 
247   * @return the location
248   */
249  public TwitterLocation getLocation() {
250    return _location;
251  }
252
253  /**
254   * @param location the location to set
255   * @see #getLocation()
256   */
257  public void setLocation(TwitterLocation location) {
258    _location = location;
259  }
260
261  /**
262   * @param entityId the entityId to set
263   * @see #getEntityId()
264   */
265  public void setEntityId(String entityId) {
266    _entityId = entityId;
267  }
268
269  /**
270   * @param entityUrl the entityUrl to set
271   * @see #getEntityUrl()
272   */
273  public void setEntityUrl(String entityUrl) {
274    _entityUrl = entityUrl;
275  }
276}