Skip to content

Commit

Permalink
Added a context param, and support for adding JSON content to the JMS…
Browse files Browse the repository at this point in the history
… msgs
  • Loading branch information
balunasj committed Aug 8, 2011
1 parent ccae090 commit f2b8652
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.richfaces.examples.tweetstream.domain.Tweet;
import org.richfaces.examples.tweetstream.domain.TwitterAggregate;

import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.text.MessageFormat;

Expand All @@ -42,6 +43,7 @@ public class PublishController implements Serializable


private transient TopicsContext topicsContext;
private Boolean postContent = null;

private TopicsContext getTopicsContext()
{
Expand All @@ -54,41 +56,32 @@ private TopicsContext getTopicsContext()

public void publishView(TwitterAggregate twitterAggregate)
{
//TODO Update to contain more than just tweet updates
String tweetString = "{\"tweets\":[";
if (twitterAggregate != null && twitterAggregate.getTweets() != null)
try
{
String tweetString = "";

for (int i = 0; i < twitterAggregate.getTweets().size(); i++)
{
Tweet tweet = twitterAggregate.getTweets().get(i);

tweetString += "{\"id\":" + tweet.getId()
+ ",\"text\":\"" + StringEscapeUtils.escapeHtml(tweet.getText())
+ "\",\"profileImageURL\":\"" + StringEscapeUtils.escapeHtml(tweet.getProfileImageUrl())
+ "\",\"screenName\":\"" + tweet.getScreenName()
+ "\",\"retweet\":" + tweet.isRetweet() + "}";

if (i + 1 != twitterAggregate.getTweets().size())
{
tweetString += ",";
}
//Initialize postContent
initPostContent();

}
tweetString += "]}";
if ((twitterAggregate != null) && (postContent))
tweetString = twitterAggregate.toJSON();

}

try
{
log.debug("Pushing Message : " + tweetString);
getTopicsContext().publish(new TopicKey("twitter", "content"), MessageFormat.format("{0}", ""));
getTopicsContext().publish(new TopicKey("twitter", "content"), tweetString);
}
catch (Exception e)
{
log.error(e.getMessage(), e);
}
}

private void initPostContent(){

if (postContent == null){
String postContentStr = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("tweetstream.postContent");
postContent = new Boolean(postContentStr);
}
}


}
4 changes: 4 additions & 0 deletions tweetstream/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

<context-param>
<param-name>tweetstream.postContent</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.push.jms.connectionUsername</param-name>
<param-value>guest</param-value>
Expand Down
16 changes: 14 additions & 2 deletions tweetstream/src/main/webapp/pages/tabletHome.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,27 @@

<h:outputStylesheet name="css/ipad.css"/>

<script type="text/javascript">

function updateContent(data){
//DEBUG To check if there is data in the msg uncomment this
//alert("Data is : " + data);

//trigger an update to the different parts of the page
updatePage();
}

</script>

</ui:define>

<ui:define name="body">
<h:form>
<a4j:jsFunction name="updateContent" action="#{twitterAgent.updateTweets}"
<a4j:jsFunction name="updatePage" action="#{twitterAgent.updateTweets}"
render="browser_form"/>
<a4j:push address="content@twitter"
onerror="alert('Error retrieving pushed data')"
ondataavailable="updateContent()">
ondataavailable="updateContent(event.rf.data)">
</a4j:push>
</h:form>
<h:form id="browser_form">
Expand Down

0 comments on commit f2b8652

Please sign in to comment.