com.facebook.api.TemplatizedAction Maven / Gradle / Ivy
/*
* Copyright 2007, BigTribe Corporation. All rights reserved.
*
* This software is an unpublished work subject to a confidentiality agreement
* and protected by copyright and trade secret law. Unauthorized copying,
* redistribution or other use of this work is prohibited. All copies must
* retain this copyright notice. Any use or exploitation of this work without
* authorization could subject the perpetrator to criminal and civil liability.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The information in this software is subject to change without notice
* and should not be construed as a commitment by BigTribe Corporation.
*
* The above copyright notice does not indicate actual or intended publication
* of this source code.
*
* $Id: bigtribetemplates.xml 5524 2006-04-06 09:40:52 -0700 (Thu, 06 Apr 2006) greening $
*/
package com.facebook.api;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Utility class to assist in creating a templatized action for publishing to the
* minifeed/newsfeed, because the API call Facebook decided to add in order to do
* it is ridiculously complex.
*/
public class TemplatizedAction {
private String titleTemplate;
private String bodyTemplate;
private String bodyGeneral;
private String targetIds;
private JSONObject titleParams;
private JSONObject bodyParams;
private List> pictures;
private Long pageActorId;
public static final String UID_TOKEN = "http://UID/";
private TemplatizedAction() {
//empty constructor not allowed, at a minimum the titleTemplate parameter is needed
}
/**
* Constructor
*
* @param titleTemplate the title-template to set.
*/
public TemplatizedAction(String titleTemplate) {
this.setTitleTemplate(titleTemplate);
this.titleParams = new JSONObject();
this.bodyParams = new JSONObject();
this.bodyTemplate = null;
this.bodyGeneral = null;
this.targetIds = null;
this.pictures = new ArrayList>();
this.pageActorId = null;
}
/**
* Constructor
*
* @param titleTemplate the title template to use
* @param bodyTemplate the body template to use
*/
public TemplatizedAction(String titleTemplate, String bodyTemplate) {
this(titleTemplate);
this.setBodyTemplate(bodyTemplate);
this.bodyGeneral = null;
this.targetIds = null;
}
/**
* Constructor
*
* @param titleTemplate the title template to use
* @param bodyTemplate the body template to use
* @param bodyGeneral the non-templatized body content to use
*/
public TemplatizedAction (String titleTemplate, String bodyTemplate, String bodyGeneral) {
this(titleTemplate, bodyTemplate);
this.setBodyGeneral(bodyGeneral);
this.targetIds = null;
}
/**
* Remove a picture from the list, this can be used to revise the list/free up space for alternate pictures.
*
* @param index the index to remove from.
*/
public void removePicture(int index) {
if ((this.pictures == null) || (index > this.pictures.size())) {
return;
}
this.pictures.remove(index);
}
/**
* Add a picture to be associated with this feed entry, along with a link to be associated with the picture (clicking
* on the picture in the feed will go to the specified link).
*
* Note that only 4 pictures may be present at any given time. Any pictures beyond this are ignored. Use removePicture
* if you need to change something after 4 pictures have been added.
*
* @param imageUid the id of the image to display. This can be a picture id or a Facebook user-id.
* @param linkHref the URL of the link to go to when the image is clicked.
*/
public void addPicture(Long imageUid, String linkHref) {
addPicture(Long.toString(imageUid), linkHref);
}
/**
* Add a picture to be associated with this feed entry, along with a link to be associated with the picture (clicking
* on the picture in the feed will go to the specified link).
*
* Note that only 4 pictures may be present at any given time. Any pictures beyond this are ignored. Use removePicture
* if you need to change something after 4 pictures have been added.
*
* @param imageHref the URL of the image to display in the feed.
* @param linkHref the URL of the link to go to when the image is clicked.
*/
public void addPicture(String imageHref, String linkHref) {
if (linkHref == null) {
this.addPicture(imageHref);
}
try {
if (! imageHref.startsWith("http")) {
imageHref = UID_TOKEN + imageHref;
}
addPicture(new URL(imageHref), new URL(linkHref));
}
catch (Exception e) {
if (FacebookRestClient.DEBUG || ExtensibleClient.DEBUG) {
System.out.println("Could not add entry for picture!");
e.printStackTrace();
}
}
}
/**
* Add a picture to be associated with this feed entry, the picture will not have an associated link.
*
* Note that only 4 pictures may be present at any given time. Any pictures beyond this are ignored. Use removePicture
* if you need to change something after 4 pictures have been added.
*
* @param imageHref the URL of the image to display in the feed.
*/
public void addPicture(String imageHref) {
try {
if (! imageHref.startsWith("http")) {
imageHref = UID_TOKEN + imageHref;
}
addPicture(new URL(imageHref), null);
}
catch (Exception e) {
if (FacebookRestClient.DEBUG || ExtensibleClient.DEBUG) {
System.out.println("Could not add entry for picture!");
e.printStackTrace();
}
}
}
private void addPicture(URL imageUrl, URL linkUrl) {
if (this.pictures == null) {
this.pictures = new ArrayList>();
}
if (this.pictures.size() < 4) {
this.pictures.add(new Pair
© 2015 - 2025 Weber Informatics LLC | Privacy Policy