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 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.XmlEnum;
026import javax.xml.bind.annotation.XmlRootElement;
027import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
028
029import org.apache.commons.lang3.StringUtils;
030
031import com.restfb.types.Group;
032import com.restfb.types.NamedFacebookType;
033
034import core.tut.pori.utils.ISODateAdapter;
035
036
037/**
038 * A group retrieved from Facebook.
039 * 
040 * <h2>Optional Elements</h2>
041 * <ul>
042 *  <li>{@value service.tut.pori.facebookjazz.Definitions#ELEMENT_DESCRIPTION}</li>
043 *  <li>{@value service.tut.pori.facebookjazz.Definitions#ELEMENT_PRIVACY}</li>
044 *  <li>{@value service.tut.pori.facebookjazz.Definitions#ELEMENT_LOCATION}</li>
045 * </ul>
046 * 
047 * <h3>XML Example</h3>
048 * 
049 * {@doc.restlet service="[service.tut.pori.facebookjazz.reference.Definitions#SERVICE_FBJ_REFERENCE_EXAMPLE]" method="[service.tut.pori.facebookjazz.Definitions#ELEMENT_GROUP]" type="GET" query="" body_uri=""}
050 *
051 * @see com.restfb.types.Group
052 */
053@XmlRootElement(name=Definitions.ELEMENT_GROUP)
054@XmlAccessorType(XmlAccessType.NONE)
055public class FacebookGroup{
056  private Integer _descriptionWeight = null;
057  private Group _group = null;
058  private Integer _nameWeight = null;
059
060  /**
061   * Facebook Privacy setting.
062   */
063  @XmlEnum
064  public enum Privacy{
065    /** group is open */
066    OPEN,
067    /** group is closed */
068    CLOSED,
069    /** group is not publicly visible */
070    SECRET;
071
072    /**
073     * 
074     * @return the privacy as string
075     */
076    public String toPrivacyString(){
077      return name();
078    }
079
080    /**
081     * 
082     * @param privacy
083     * @return the Privacy or null if invalid string
084     */
085    public static Privacy fromPrivacyString(String privacy){
086      if(privacy != null){
087        for(Privacy p : Privacy.values()){
088          if(p.toPrivacyString().equalsIgnoreCase(privacy)){
089            return p;
090          }
091        }
092      }
093      return null;
094    }
095  }  // enum Privacy
096  
097  /**
098   * 
099   */
100  public FacebookGroup(){
101    _group = new Group();
102  }
103  
104  /**
105   * 
106   * @param group
107   * @throws IllegalArgumentException
108   */
109  public FacebookGroup(Group group) throws IllegalArgumentException{
110    if(group == null){
111      throw new IllegalArgumentException("Invalid group.");
112    }
113    _group = group;
114  }
115
116  /**
117   * @see com.restfb.types.Group#getDescription()
118   * @see #setDescription(String)
119   * 
120   * @return description
121   */
122  @XmlElement(name = Definitions.ELEMENT_DESCRIPTION)
123  public WeightedStringElement getWDescription() {
124    String description = _group.getDescription();
125    if(StringUtils.isBlank(description)){
126      return null;
127    }else{
128      return new WeightedStringElement(description, _descriptionWeight);
129    }
130  }
131  
132  /**
133   * for serialization
134   * @param description
135   * @see #getDescription()
136   */
137  @SuppressWarnings("unused")
138  private void setWDescription(WeightedStringElement description) {
139    if(description == null){
140      setDescription(null);
141      setDescriptionWeight(null);
142    }else{
143      setDescription(description.getValue());
144      setDescriptionWeight(description.getWeight());
145    }
146  }
147
148  /**
149   * @see com.restfb.types.Group#getOwner()
150   * @see com.restfb.types.NamedFacebookType#getName()
151   * @see #setGroupOwnerName(String)
152   * 
153   * @return owner name
154   */
155  @XmlElement(name = Definitions.ELEMENT_OWNER)
156  public String getGroupOwnerName() {
157    NamedFacebookType owner = _group.getOwner();
158    return (owner == null ? null : owner.getName());
159  }
160  
161  /**
162   * @param name
163   * @see com.restfb.types.Group#setOwner(com.restfb.types.NamedFacebookType)
164   * @see #getGroupOwnerName()
165   */
166  public void setGroupOwnerName(String name) {
167    if(StringUtils.isBlank(name)){
168      _group.setOwner(null);
169    }else{
170      NamedFacebookType owner = _group.getOwner();
171      if(owner == null){
172        owner = new NamedFacebookType();
173        _group.setOwner(owner);
174      }
175      owner.setName(name);
176    }
177  }
178
179  /**
180   * @see com.restfb.types.Group#getPrivacy()
181   * @see #setPrivacy(Privacy)
182   * 
183   * @return privacy
184   */
185  @XmlElement(name = Definitions.ELEMENT_PRIVACY)
186  public Privacy getPrivacy(){
187    return Privacy.fromPrivacyString(_group.getPrivacy());  
188  }
189  
190  /**
191   * @param privacy
192   * @see com.restfb.types.Group#setPrivacy(java.lang.String)
193   * @see #getPrivacy()
194   */
195  public void setPrivacy(Privacy privacy) {
196    _group.setPrivacy((privacy == null ? null : privacy.toPrivacyString()));
197  }
198
199  /**
200   * @see com.restfb.types.Group#getUpdatedTime()
201   * @see #setUpdatedTime(Date)
202   * 
203   * @return updated timestamp
204   */
205  @XmlElement(name = Definitions.ELEMENT_UPDATED_TIMESTAMP)
206  @XmlJavaTypeAdapter(ISODateAdapter.class)
207  public Date getUpdatedTime() {
208    return _group.getUpdatedTime();
209  }
210
211  /**
212   * @see com.restfb.types.Group#getName()
213   * @see #setName(String)
214   * 
215   * @return name
216   */
217  @XmlElement(name = Definitions.ELEMENT_NAME)
218  public WeightedStringElement getWName() {
219    String name = _group.getName();
220    if(StringUtils.isBlank(name)){
221      return null;
222    }else{
223      return new WeightedStringElement(name, _nameWeight);
224    }
225  }
226  
227  /**
228   * for serialization
229   * @param name 
230   * @see #getName()
231   */
232  @SuppressWarnings("unused")
233  private void setWName(WeightedStringElement name) {
234    if(name == null){
235      setName(null);
236      setNameWeight(null);
237    }else{
238      setName(name.getValue());
239      setNameWeight(name.getWeight());
240    }
241  }
242
243  /**
244   * @return name
245   * @see com.restfb.types.NamedFacebookType#getName()
246   * @see #setName(String)
247   */
248  public String getName() {
249    return _group.getName();
250  }
251
252  /**
253   * @see com.restfb.types.Group#getVenue()
254   * @see #setFacebookLocation(FacebookLocation)
255   * 
256   * @return location
257   */
258  @XmlElement(name = Definitions.ELEMENT_LOCATION)
259  public FacebookLocation getFacebookLocation() {
260    return FacebookLocation.getFacebookLocation(_group.getVenue());
261  }
262  
263  
264  /**
265   * @param location
266   * @see com.restfb.types.Group#setVenue(com.restfb.types.Venue)
267   * @see #getFacebookLocation()
268   */
269  public void setFacebookLocation(FacebookLocation location) {
270    _group.setVenue((location == null ? null : location.toVenue()));
271  }
272
273  /**
274   * 
275   * @return description weight
276   * @see #setDescriptionWeight(Integer)
277   */
278  public Integer getDescriptionWeight() {
279    return _descriptionWeight;
280  }
281
282  /**
283   * 
284   * @param descriptionWeight
285   * @see #getDescriptionWeight()
286   */
287  public void setDescriptionWeight(Integer descriptionWeight) {
288    _descriptionWeight = descriptionWeight;
289  }
290
291  /**
292   * 
293   * @return name weight
294   * @see #setNameWeight(Integer)
295   */
296  public Integer getNameWeight() {
297    return _nameWeight;
298  }
299
300  /**
301   * 
302   * @param nameWeight
303   * @see #getNameWeight()
304   */
305  public void setNameWeight(Integer nameWeight) {
306    _nameWeight = nameWeight;
307  }
308
309  /**
310   * @param description
311   * @see com.restfb.types.Group#setDescription(java.lang.String)
312   * @see #getDescription()
313   */
314  public void setDescription(String description) {
315    _group.setDescription(description);
316  }
317
318  /**
319   * @param name
320   * @see com.restfb.types.NamedFacebookType#setName(java.lang.String)
321   * @see #getName()
322   */
323  public void setName(String name) {
324    _group.setName(name);
325  }
326
327  /**
328   * @param date
329   * @see com.restfb.types.Group#setUpdatedTime(java.util.Date)
330   * @see #getUpdatedTime()
331   */
332  public void setUpdatedTime(Date date) {
333    _group.setUpdatedTime(date);
334  }
335  
336  /**
337   * 
338   * @param groups
339   * @return the groups wrapped to facebook groups or null if null or empty list was passed
340   */
341  public static List<FacebookGroup> getFacebookGroups(List<Group> groups){
342    List<FacebookGroup> fg = new ArrayList<>(groups.size());
343    for(Group g : groups){
344      fg.add(new FacebookGroup(g));
345    }
346    return fg;
347  }
348
349  /**
350   * @return description
351   * @see com.restfb.types.Group#getDescription()
352   * @see #setDescription(String)
353   */
354  public String getDescription() {
355    return _group.getDescription();
356  }
357}