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 service.tut.pori.contentanalysis.CAContentCore.ServiceType;
027import service.tut.pori.contentanalysis.MediaObject;
028import service.tut.pori.contentanalysis.MediaObject.MediaObjectType;
029import service.tut.pori.contentstorage.FacebookPhotoStorage;
030
031import com.restfb.types.Photo.Tag;
032
033/**
034 * Tag of a photo retrieved from Facebook.
035 * 
036 * <h2>Optional Elements</h2>
037 * <ul>
038 *  <li>{@value service.tut.pori.contentanalysis.Definitions#ELEMENT_SERVICE_ID}. Not present if the tag was created by the user or internally by the service.</li>
039 * </ul>
040 * 
041 * <h3>XML Example</h3>
042 * 
043 * {@doc.restlet service="[service.tut.pori.facebookjazz.reference.Definitions#SERVICE_FBJ_REFERENCE_EXAMPLE]" method="[service.tut.pori.facebookjazz.Definitions#ELEMENT_PHOTO_TAG]" type="GET" query="" body_uri=""}
044 *
045 * @see com.restfb.types.Photo.Tag
046 */
047@XmlRootElement(name=Definitions.ELEMENT_PHOTO_TAG)
048@XmlAccessorType(XmlAccessType.NONE)
049public class FacebookPhotoTag {
050  private static final Logger LOGGER = Logger.getLogger(FacebookPhotoTag.class);
051  @XmlElement(name = service.tut.pori.contentanalysis.Definitions.ELEMENT_SERVICE_ID)
052  private ServiceType _serviceType = FacebookPhotoStorage.SERVICE_TYPE;
053  private Tag _tag = null;
054
055  /**
056   * 
057   */
058  public FacebookPhotoTag() {
059    _tag = new Tag();
060  }
061  
062  /**
063   * 
064   * @param tag
065   * @throws IllegalArgumentException
066   */
067  public FacebookPhotoTag(Tag tag) throws IllegalArgumentException {
068    if(tag == null){
069      throw new IllegalArgumentException("Invalid tag.");
070    }
071    _tag = tag;
072  }
073  
074  /**
075   * @see com.restfb.types.NamedFacebookType#getName()
076   * @see #setName(String)
077   * 
078   * @return tag name
079   */
080  @XmlElement(name = Definitions.ELEMENT_VALUE)
081  public String getName() {
082    return _tag.getName();
083  }
084  
085  /**
086   * 
087   * @param tag
088   * @return new tag or null if the given tag was invalid
089   */
090  public static FacebookPhotoTag getFacebookTag(Tag tag){
091    if(tag == null){
092      LOGGER.debug("null tag.");
093      return null;
094    }
095    String name = tag.getName();
096    if(StringUtils.isBlank(name)){
097      LOGGER.debug("Invalid name.");
098      return null;
099    }
100    return new FacebookPhotoTag(tag);
101  }
102  
103  /**
104   * 
105   * @param object
106   * @return the object as a tag or null if null was passed
107   * @throws IllegalArgumentException on bad object
108   */
109  public static FacebookPhotoTag getFacebookTag(MediaObject object) throws IllegalArgumentException {
110    if(object == null){
111      LOGGER.debug("null tag.");
112      return null;
113    }
114    if(!MediaObjectType.KEYWORD.equals(object.getMediaObjectType())){
115      throw new IllegalArgumentException("Invalid media object type: "+object.getMediaObjectTypeValue());
116    }
117    String value = object.getValue();
118    if(StringUtils.isBlank(value) && StringUtils.isBlank((value = object.getName()))){
119      throw new IllegalArgumentException("Invalid name and/or value for tag.");
120    }
121    FacebookPhotoTag tag = new FacebookPhotoTag();
122    tag.setName(value);
123    tag.setServiceType(object.getServiceType());
124    return tag;
125  }
126
127  /**
128   * @return the serviceType
129   * @see #setServiceType(service.tut.pori.contentanalysis.CAContentCore.ServiceType)
130   */
131  public ServiceType getServiceType() {
132    return _serviceType;
133  }
134
135  /**
136   * @param serviceType the serviceType to set
137   * @see #getServiceType()
138   */
139  public void setServiceType(ServiceType serviceType) {
140    _serviceType = serviceType;
141  }
142
143  /**
144   * @param name
145   * @see com.restfb.types.NamedFacebookType#setName(java.lang.String)
146   * @see #getName()
147   */
148  public void setName(String name) {
149    _tag.setName(name);
150  }
151}