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 ). 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!
Community moderators have prevented the ability to post new answers.
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); } }
Thanks for the suggestions, I will take a look and let you know about the results.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Please accept it as answer then :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It looks like the version I get when doing atlas-run is Atlassian Confluence 5.8.2.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Which confluence version are you using?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.