Skip to content

Instantly share code, notes, and snippets.

@joshbirk
Last active December 10, 2015 21:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshbirk/4498619 to your computer and use it in GitHub Desktop.
Save joshbirk/4498619 to your computer and use it in GitHub Desktop.
PushTopic Management with Apex + Visualfroce
public with sharing class PushTopicController {
public List<PushTopic> pushtopics {get; set;}
public PushTopic currentPushTopic {get; set;}
public String editTopicId {get; set;}
public PushTopicController() {
pushtopics = new List<PushTopic>();
pushtopics = [SELECT Id, Name, Query, Description, ApiVersion, NotifyForOperations, NotifyForFields from PushTopic];
currentPushTopic = null;
editTopicId = '';
}
public PageReference setCurrentTopic() {
if(editTopicId == 'New') { currentPushTopic = new PushTopic(ApiVersion=26.0,NotifyForOperations='All',NotifyForFields='Referenced'); }
else {
for(PushTopic t : pushtopics) {
if(t.Id == Id.valueOf(editTopicId)) { currentPushTopic = t; }
}
}
return null;
}
public PageReference saveCurrentTopic() {
try {
upsert currentPushTopic;
pushtopics = [SELECT Id, Name, Query, Description, ApiVersion, NotifyForOperations, NotifyForFields from PushTopic];
currentPushTopic = null;
return null;
} catch (Exception e) {
return null;
}
}
public PageReference deleteCurrentTopic() {
delete currentPushTopic;
pushtopics = [SELECT Id, Name, Query, Description, ApiVersion, NotifyForOperations, NotifyForFields from PushTopic];
currentPushTopic = null;
return null;
}
public PageReference cancelCurrentTopic() {
currentPushTopic = null;
return null;
}
@isTest
static public void testTopicController() {
PushTopic pushTopic = new PushTopic();
pushTopic.Name = 'Accounts';
pushTopic.Description = 'All records for the Account object';
pushtopic.Query = 'SELECT Id, Name from Account';
pushTopic.ApiVersion = 26.0;
pushTopic.NotifyForOperations = 'All';
pushTopic.NotifyForFields = 'Referenced';
insert pushTopic;
PushTopicController ptc = new PushTopicController();
ptc.editTopicId = 'New';
ptc.setCurrentTopic();
System.assertNotEquals(ptc.currentPushTopic,null);
ptc.cancelCurrentTopic();
System.assertEquals(ptc.currentPushTopic,null);
ptc.editTopicId = pushTopic.Id;
ptc.setCurrentTopic();
System.assertNotEquals(ptc.currentPushTopic,null);
ptc.saveCurrentTopic();
System.assertEquals(ptc.currentPushTopic,null);
ptc.editTopicId = pushTopic.Id;
ptc.setCurrentTopic();
ptc.deleteCurrentTopic();
System.assertEquals(ptc.pushTopics.size(),0);
}
}
<apex:page controller="PushTopicController">
<style>
#topicsList input, .long {
width: 200px;
}
#topicsList {
list-style: none;
padding: 0;
}
#topicsList li {
margin-bottom: 5px;
color: #999;
}
</style>
<div id="page" style="width: 700px;">
<apex:form>
<apex:outputPanel id="list" >
<div style="float: left; width: 250px;">
<UL id="topicsList">
<apex:repeat value="{!pushtopics}" var="topic">
<LI><apex:commandButton action="{!setCurrentTopic}" rerender="object, list" value="{!topic.Name}" disabled="{!currentPushTopic != null}">
<apex:param name="topicId{!topic.Id}" value="{!topic.Id}" assignTo="{!editTopicId}" />
</apex:commandButton><BR />
<apex:outputText value="{!topic.Description}" /><BR />
</LI>
</apex:repeat>
<LI><apex:commandButton action="{!setCurrentTopic}" rerender="object, list" value="New Topic" disabled="{!currentPushTopic != null}">
<apex:param value="New" assignTo="{!editTopicId}" />
</apex:commandButton></LI>
</UL>
</div>
</apex:outputPanel>
<apex:outputPanel id="object" >
<div style="float: right; width: 350px;">
<apex:outputPanel id="form" rendered="{!currentPushTopic != null}" >
<apex:pageBlock>
<apex:PageBlockButtons>
<apex:commandButton action="{!saveCurrentTopic}" rerender="object, list" value="Upsert" />
<apex:actionRegion>
<apex:commandButton onmousedown="if(confirm('Really?')){deleteCurrentTopic();}" value="Delete" disabled="{!currentPushTopic.Id == null}" />
<apex:actionFunction action="{!deleteCurrentTopic}" name="deleteCurrentTopic" rerender="object, list" />
</apex:actionRegion>
<apex:actionRegion>
<apex:commandButton action="{!cancelCurrentTopic}" rerender="object, list" value="Cancel" />
</apex:actionRegion>
</apex:PageBlockButtons>
<apex:pageBlockSection>
<apex:inputField value="{!currentPushTopic.Name}" /><BR />
<apex:inputField value="{!currentPushTopic.Description}" /><BR />
<apex:inputField value="{!currentPushTopic.Query}" /><BR />
<apex:inputField value="{!currentPushTopic.ApiVersion}" /><BR />
<apex:inputField value="{!currentPushTopic.NotifyForOperations}" />
<em>a = 'all', c = 'create', u = 'update'</em><BR />
<apex:inputField value="{!currentPushTopic.NotifyForFields}" />
<em>s = 'select', w = 'where', r = 'referenced', a = 'all'</em><BR />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
</div>
</apex:outputPanel>
</apex:form>
</div>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment