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 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 com.restfb.types.CategorizedFacebookType;
027import com.restfb.types.User;
028
029
030/**
031 * Container for a relationship information retrieved from Facebook.
032 * 
033 * <h2>Optional Elements</h2>
034 * <ul>
035 *  <li>{@value service.tut.pori.facebookjazz.Definitions#ELEMENT_RELATIONSHIP_WITH}</li>
036 * </ul>
037 * 
038 * <h3>XML Example</h3>
039 * 
040 * {@doc.restlet service="[service.tut.pori.facebookjazz.reference.Definitions#SERVICE_FBJ_REFERENCE_EXAMPLE]" method="[service.tut.pori.facebookjazz.Definitions#ELEMENT_RELATIONSHIP]" type="GET" query="" body_uri=""}
041 *
042 */
043@XmlRootElement(name=Definitions.ELEMENT_RELATIONSHIP)
044@XmlAccessorType(XmlAccessType.NONE)
045public class FacebookRelationship {
046  private static final Logger LOGGER = Logger.getLogger(FacebookRelationship.class);
047  @XmlElement(name = Definitions.ELEMENT_RELATIONSHIP_WITH)
048  private String _with = null;
049  @XmlElement(name = Definitions.ELEMENT_RELATIONSHIP_TYPE)
050  private String _type = null;
051
052
053  /**
054   * 
055   * @param user
056   * @return facebook relationship or null if none
057   */
058  public static FacebookRelationship getFacebookRelationShip(User user){
059    String type = user.getRelationshipStatus();
060    if(StringUtils.isBlank(type)){
061      return null;
062    }
063    FacebookRelationship fr = new FacebookRelationship();
064    fr._with = user.getSignificantOther().getName();
065    fr._type = type;
066    return fr;
067  }
068  
069  /**
070   * set values from this relationship to the given user
071   * 
072   * @param user
073   */
074  public void toUser(User user) {
075    CategorizedFacebookType with = null;
076    if(StringUtils.isBlank(_with)){
077      LOGGER.warn("Invalid with.");
078      user.setSignificantOther(null);
079      user.setRelationshipStatus(null);
080      return;
081    }else{
082      with = new CategorizedFacebookType();
083      with.setName(_with);
084    }
085    user.setSignificantOther(with);
086    user.setRelationshipStatus(_type);
087  }
088
089  /**
090   * The type is assigned by the content service (e.g. Facebook), and may (in practice) have enumerated values, but the element value should not be considered to be "enumerated".
091   * 
092   * @return type
093   * @see #setType(String)
094   */
095  public String getType() {
096    return _type;
097  }
098
099  /**
100   * 
101   * @return the other half
102   * @see #setWith(String)
103   */
104  public String getWith() {
105    return _with;
106  }
107
108  /**
109   * @param type the type to set
110   * @see #getWith()
111   */
112  public void setType(String type) {
113    _type = type;
114  }
115
116  /**
117   * @param with the with to set
118   * @see #getWith()
119   */
120  public void setWith(String with) {
121    _with = with;
122  }
123}