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 javax.xml.bind.annotation.XmlAccessType;
019import javax.xml.bind.annotation.XmlAccessorType;
020import javax.xml.bind.annotation.XmlElement;
021import javax.xml.bind.annotation.XmlRootElement;
022
023import org.apache.commons.lang3.StringUtils;
024import org.apache.log4j.Logger;
025
026import twitter4j.GeoLocation;
027
028
029/**
030 * A location received from Twitter.
031 * 
032 * <h2>Conditional Elements</h2>
033 * <ul>
034 *  <li>{@value service.tut.pori.twitterjazz.Definitions#ELEMENT_LATITUDE}</li>
035 *  <li>{@value service.tut.pori.twitterjazz.Definitions#ELEMENT_LONGITUDE}</li>
036 *  <li>{@value service.tut.pori.twitterjazz.Definitions#ELEMENT_NAME}</li>
037 * </ul>
038 * 
039 * Any of the conditional fields can be omitted, but at least one of the fields must be present. If latitude is given, longitude must also be given, and vice versa.
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_LOCATION]" type="GET" query="" body_uri=""}
044 * 
045 */
046@XmlRootElement(name=Definitions.ELEMENT_LOCATION)
047@XmlAccessorType(XmlAccessType.NONE)
048public class TwitterLocation {
049  private static final Logger LOGGER = Logger.getLogger(TwitterLocation.class);
050  @XmlElement(name=Definitions.ELEMENT_NAME)
051  private String _name = null;
052  @XmlElement(name=Definitions.ELEMENT_LATITUDE)
053  private Double _latitude = null;
054  @XmlElement(name=Definitions.ELEMENT_LONGITUDE)
055  private Double _longitude = null;
056  
057  /**
058   * 
059   * @param location
060   * @return the location or null if the given string was null or empty
061   */
062  public static TwitterLocation getTwitterLocation(String location){
063    if(StringUtils.isBlank(location)){
064      LOGGER.warn("Bad location string: "+location);
065      return null;
066    }
067    TwitterLocation tl = new TwitterLocation();
068    tl._name = location;
069    return tl;
070  }
071  
072  /**
073   * 
074   * @param location
075   * @return the location or null if location was null
076   */
077  public static TwitterLocation getTwitterLocation(GeoLocation location) {
078    if(location == null){
079      return null;
080    }
081    
082    TwitterLocation tl = new TwitterLocation();
083    tl._latitude = location.getLatitude();
084    tl._longitude = location.getLongitude();
085    return tl;
086  }
087  
088  /**
089   * 
090   */
091  public TwitterLocation() {
092    // nothing needed
093  }
094
095  /**
096   * @return the name
097   * @see #setName(String)
098   */
099  public String getName() {
100    return _name;
101  }
102
103  /**
104   * @param name the name to set
105   * @see #getName()
106   */
107  public void setName(String name) {
108    _name = name;
109  }
110
111  /**
112   * @return the latitude
113   * @see #setLatitude(Double)
114   */
115  public Double getLatitude() {
116    return _latitude;
117  }
118
119  /**
120   * @param latitude the latitude to set
121   * @see #getLatitude()
122   */
123  public void setLatitude(Double latitude) {
124    _latitude = latitude;
125  }
126
127  /**
128   * @return the longitude
129   * @see #setLongitude(Double)
130   */
131  public Double getLongitude() {
132    return _longitude;
133  }
134
135  /**
136   * @param longitude the longitude to set
137   * @see #getLongitude()
138   */
139  public void setLongitude(Double longitude) {
140    _longitude = longitude;
141  }
142}