Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Getting Page Body Content On Event.

Bart Kerfeld July 1, 2015

Hello, I am struggling to build a plugin capable of adding links to pages on the creation and edit of a page. Here is what I have that works.

package learning.confluence;
#all imports are here ill save you the space
public class Listener implements EventListener {
    
    private final PageManager pageManager;
    private final PlatformTransactionManager transactionManager;
    
    public Listener(PageManager pageManager) {
        this.pageManager = pageManager;
    }
    @Override
    public void handleEvent(Event event) {
        // TODO Auto-generated method stub
        if (event instanceof PageCreatedEvent) {
            PageCreatedEvent pageCreatedEvent = (PageCreatedEvent) event;
            final long pageId = pageCreatedEvent.getPageId();
			Page page = pageManager.getPage(pageId);
            System.out.println("########## GOT IN ###############");
			System.out.println(page.getTitle() + " : " + pageId);
            System.out.println("########## GOT OUT ###############");
        }
        else if (event instanceof PageEditedEvent) {
            PageEditedEvent pageEditedEvent = (PageEditedEvent) event;
            final long pageId = pageEditedEvent.getPageId();
			Page page = pageManager.getPage(pageId);
            System.out.println("########## GOT IN ###############");
			System.out.println(page.getTitle() + " : " + pageId);
            System.out.println("########## GOT OUT ###############");
        }
    }
    private static final Class[] HANDLED_EVENTS = new Class[] {
        PageCreatedEvent.class, PageEditedEvent.class
    };
    
    @Override
    public Class[] getHandledEventClasses() {
        return HANDLED_EVENTS;
    }
}

This code will print the text to the console whenever I create a new or edit a page. Now the fun part... I cannot seem to get the body contents at all which I plan on manipulated somehow later (but that will be a different question smile). I ran into problems, however, when trying to get the body of the page. The original problem I ran into occurred when trying to insert this code into what I had above (either line, I didn't run both at the same time)

String bodyContent = page.getBodyContent().getBody();
String otherBodyContent = page.getBodyAsString();

Failed to lazily initialize a collection - no session or session was closed was the problem I ran into so I did a little research and changed my code to the following.

package learning.confluence;
#again ill spare you the imports
public class Listener implements EventListener {
    
    private final PageManager pageManager;
    private final PlatformTransactionManager transactionManager;
    
    public Listener(PageManager pageManager, PlatformTransactionManager transactionManager) {
        this.pageManager = pageManager;
        this.transactionManager = transactionManager;
    }
    @Override
    public void handleEvent(Event event) {
        // TODO Auto-generated method stub
        if (event instanceof PageCreatedEvent) {
            PageCreatedEvent pageCreatedEvent = (PageCreatedEvent) event;
            final long pageId = pageCreatedEvent.getPageId();
            Page page = pageManager.getPage(pageId);
            System.out.println("########## GOT IN ###############");
            TransactionDefinition transactionDefinition = new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED);
            new TransactionTemplate(transactionManager, transactionDefinition).execute(new TransactionCallback() {
                @Override
                public Object doInTransaction(TransactionStatus status) {
                    try
                    {
                        Page p = pageManager.getPage(pageId);
                        String body = p.getBodyContent().getBody();
                        System.out.println("Worked!\n" + body);
                    }
                    catch (Exception e)
                    {
                        System.out.println("exception create");
                    }
                    return null;
                }
            });    
            System.out.println("########## GOT OUT ###############");
        }
        else if (event instanceof PageEditedEvent) {
            #this is the same idea is pageCreate so ill spare you the text
        }
    }
    private static final Class[] HANDLED_EVENTS = new Class[] {
        PageCreatedEvent.class, PageEditedEvent.class
    };
    
    @Override
    public Class[] getHandledEventClasses() {
        return HANDLED_EVENTS;
    }
}

Now when I run the plugin, I do not get any text. Not even the ###### GOT IN HERE ####### which happens before any of the Transaction Stuff. Am I missing something somewhere? Is there something which needs to be added to one of the xml files? Or, should I be doing something entirely different to try to accomplish this?

Thanks for the help everyone!

 

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
xpauls
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 2, 2015

Maybe try using AnnotatedListener.

public class AnnotatedListener implements DisposableBean {
	protected EventPublisher eventPublisher;

	public AnnotatedListener(EventPublisher eventPublisher)
	{
		this.eventPublisher = eventPublisher;
		eventPublisher.register(this);
	}
	
	@EventListener
    public void pageCreateEvent(PageCreateEvent event) {
            Page page = event.getPage();
            page.setBodyAsString(page.getBodyAsString() + "<br/> THIS IS APPENDED TEXT FROM CREATE");     
    }

	@EventListener
    public void pageUpdateEvent(PageUpdateEvent event) {
            Page page = event.getPage();
            page.setBodyAsString(page.getBodyAsString() + "<br/> THIS IS APPENDED TEXT FROM EDIT");     
    }

	public void destroy() throws Exception {
        eventPublisher.unregister(this);
    }
 }
Bart Kerfeld July 2, 2015

Thanks for the suggestions, I will take a look and let you know about the results.

Bart Kerfeld July 2, 2015

The code works as expected. Thanks so much for the help!

xpauls
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 2, 2015

Please accept it as answer then :)

0 votes
Bart Kerfeld July 2, 2015

It looks like the version I get when doing atlas-run is Atlassian Confluence 5.8.2.

0 votes
xpauls
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 2, 2015

Which confluence version are you using?

TAGS
AUG Leaders

Atlassian Community Events