FileDocCategorySizeDatePackage
TemplatizedAction.javaAPI DocGoogle Facebook API v1.515327Sun Nov 11 15:18:58 GMT 2007com.facebook.api

TemplatizedAction

public class TemplatizedAction extends Object
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.

Fields Summary
private String
titleTemplate
private String
bodyTemplate
private String
bodyGeneral
private String
targetIds
private JSONObject
titleParams
private JSONObject
bodyParams
private List
pictures
Constructors Summary
private TemplatizedAction()

        //empty constructor not allowed, at a minimum the titleTemplate parameter is needed
    
public TemplatizedAction(String titleTemplate)
Constructor

param
titleTemplate the title-template to set.

        this.setTitleTemplate(titleTemplate);
        this.titleParams = new JSONObject();
        this.bodyParams = new JSONObject();
        this.bodyTemplate = null;
        this.bodyGeneral = null;
        this.targetIds = null;
        this.pictures = new ArrayList<IPair<Object, URL>>();
    
public TemplatizedAction(String titleTemplate, String bodyTemplate)
Constructor

param
titleTemplate the title template to use
param
bodyTemplate the body template to use

        this(titleTemplate);
        this.setBodyTemplate(bodyTemplate);
        this.bodyGeneral = null;
        this.targetIds = null;
    
public TemplatizedAction(String titleTemplate, String bodyTemplate, String bodyGeneral)
Constructor

param
titleTemplate the title template to use
param
bodyTemplate the body template to use
param
bodyGeneral the non-templatized body content to use

        this(titleTemplate, bodyTemplate);
        this.setBodyGeneral(bodyGeneral);
        this.targetIds = null;
    
Methods Summary
public voidaddBodyParam(java.lang.String key, java.lang.String value)
Add a parameter value for the body template. It will be used to replace the corresponding token when the feed entry is rendered.

param
key the name of the parameter/token.
param
value the value to set it to.

        addParam(bodyParams, key, value);
    
private voidaddParam(org.json.JSONObject map, java.lang.String key, java.lang.String value)

        if (("actor".equals(key)) || ("target".equals(key))) {
            throw new RuntimeException(key + " is a reserved token name, you cannot set it yourself!");
        }
        try {
            map.put(key, value);
        }
        catch (JSONException e) {
            System.out.println("JSONException for key=" + key + ", value=" + value + "!");
            e.printStackTrace();
        }
    
public voidaddPicture(java.lang.String imageHref, java.lang.String 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.

        if (linkHref == null) {
            this.addPicture(imageHref);
        }
        try {
            addPicture(new URL(imageHref), new URL(linkHref));
        }
        catch (Exception e) {
            System.out.println("Could not add entry for picture!");
            e.printStackTrace();
        }
    
public voidaddPicture(java.lang.String imageHref)
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.

        try {
            addPicture(new URL(imageHref), null);
        }
        catch (Exception e) {
            System.out.println("Could not add entry for picture!");
            e.printStackTrace();
        }
    
private voidaddPicture(java.net.URL imageUrl, java.net.URL linkUrl)

        if (this.pictures == null) {
            this.pictures = new ArrayList<IPair<Object, URL>>();
        }
        if (this.pictures.size() < 4) {
            this.pictures.add(new Pair<Object, URL>(imageUrl, linkUrl));
        }
    
public voidaddTargetIds(java.lang.String newIds)
Append to the list of friends who are associated with this action.

This method *will not* clear out any previously added target ids.

param
newIds a comma-seperated list of Facebook UID's representing any friends of the current user who are associated with the action being published (so if the action is "Bob waves to Sally and Susie", Sally and Susie are the targets).

        if (this.targetIds == null) {
            this.targetIds = "";
        }
        if (! "".equals(this.targetIds)) {
            this.targetIds += ",";
        }
        this.targetIds += newIds;
    
public voidaddTargetIds(java.util.Collection facebookIds)
Append to the set of friends who are associated with this action. This must be specified if you have used the "{target}" token in any of your markup.

This method *will not* clear out any previously added target ids.

param
facebookIds a list of all the Facebook UID to specify as targets. The elements in the collection may only be of type Long or type String.

        if (this.targetIds == null) {
            this.targetIds = "";
        }
        for (Object current : facebookIds) {
            if (! "".equals(this.targetIds)) {
                this.targetIds += ",";
            }
            this.targetIds += current;
        }
    
public voidaddTitleParam(java.lang.String key, java.lang.String value)
Add a parameter value for the title template. It will be used to replace the corresponding token when the feed entry is rendered.

param
key the name of the parameter/token.
param
value the value to set it to.

        addParam(titleParams, key, value);
    
public java.lang.StringgetBodyGeneral()

return
the bodyGeneral

        return bodyGeneral;
    
public java.lang.StringgetBodyParams()
Get the body params as a JSON-formatted string.

return
the parameters for the templatized body tokens.

        return getJsonParams(bodyParams);
    
public java.lang.StringgetBodyTemplate()

return
the bodyTemplate

        return bodyTemplate;
    
private java.lang.StringgetJsonParams(org.json.JSONObject params)

        if (params.length() == 0) {
            return null;
        }
        return params.toString();
    
public java.util.CollectiongetPictures()
Get the list of pictures.

return
the list of pictures.

        if ((this.pictures == null) || (this.pictures.isEmpty())) {
            return null;
        }
        return this.pictures;
    
public java.lang.StringgetTargetIds()

return
the targetIds

        if ("".equals(targetIds)) {
            targetIds = null;
        }
        return targetIds;
    
public java.lang.StringgetTitleParams()
Get the title params as a JSON-formatted string.

return
the parameters for the templatized title tokens.

        return getJsonParams(titleParams);
    
public java.lang.StringgetTitleTemplate()

return
the titleTemplate

        return titleTemplate;
    
public voidremovePicture(int index)
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.

        if ((this.pictures == null) || (index > this.pictures.size())) {
            return;
        }
        this.pictures.remove(index);
    
public voidsetBodyGeneral(java.lang.String bodyGeneral)
Set the general body content for this feed entry. This is optional, non-templatized markup, and is distinct from and unrelated to bodyTemplate. A feed entry can have both at once, either or, or neither.

param
bodyGeneral non-templatized markup that will be displayed in this feed entry. When multiple entries are aggregated, only the bodyGeneral from (an arbitrarily chosen) one of them will appear in the aggregate entry, meaning that any markup specified here must be generic enough that it can make sense in any context.

        if ("".equals(bodyGeneral)) {
            bodyGeneral = null;
        }
        this.bodyGeneral = bodyGeneral;
    
public voidsetBodyTemplate(java.lang.String bodyTemplate)
Set the body template for this feed entry. The body template is optinal.

param
bodyTemplate templatized markup that will be used to compute the body section of this feed entry. Unlike titleTemplate, this markup *is not* required to utilize the "{actor}" token, although you may choose to use any desired tokens in this section if you wish.

        if ("".equals(bodyTemplate)) {
            bodyTemplate = null;
        }
        this.bodyTemplate = bodyTemplate;
    
public voidsetPictures(java.util.List pics)
Set the pictures to display all at once, if you feel like building the Collection> on your own. Otherwise use the more convenient addPic() method instead.

param
pics the pictures to set.

        if ((pics == null) || (pics.isEmpty())) {
            this.pictures = null;
            return;
        }
        if (pics.size() <= 4) {
            this.pictures = (List<IPair<Object, URL>>)pics;
        }
        if (pics.size() > 4) {
            int count = 0;
            for (IPair<Object, URL> pic : pics) {
                this.pictures.add(pic);
                count++;
                if (count == 4) {
                    break;
                }
            }
        }
    
public voidsetTargetIds(java.lang.String targetIds)
Set the target ids of friends who are associated with this action. This must be specified if you have used the "{target}" token in any of your markup.

This method will clear out any previously added target ids. To append additional target ids to a previous list, use addTargetIds instead.

param
targetIds a comma-seperated list of Facebook UID's representing any friends of the current user who are associated with the action being published (so if the action is "Bob waves to Sally and Susie", Sally and Susie are the targets).

        if ("".equals(targetIds)) {
            targetIds = null;
        }
        this.targetIds = targetIds;
    
public voidsetTargetIds(java.util.Collection facebookIds)
Set the target ids of friends who are associated with this action. This must be specified if you have used the "{target}" token in any of your markup.

This method will clear out any previously added target ids. To append additional target ids to a previous list, use addTargetIds instead.

param
facebookIds a list of all the Facebook UID to specify as targets. The elements in the collection may only be of type Integer or type String.

        if (facebookIds.isEmpty()) {
            this.targetIds = null;
            return;
        }
        this.targetIds = "";
        for (Object current : facebookIds) {
            if (! "".equals(this.targetIds)) {
                this.targetIds += ",";
            }
            this.targetIds += current;
        }
    
public voidsetTitleTemplate(java.lang.String titleTemplate)
Set the title template for this feed entry. This is a required field, and the template must always contain the "{actor}" token.

param
titleTemplate templatized markup to use as the title of this feed entry. It must contain the "{actor}" token.

        if (titleTemplate == null) {
            throw new RuntimeException("The title-template cannot be null!");
        }
        if (! titleTemplate.contains("{actor}")) {
            throw new RuntimeException(titleTemplate + " is an invalid template!  The title-template must contain the \"{actor}\" token.");
        }
        this.titleTemplate = titleTemplate;