Adding a label from a listener

Hello,

I am working on a custom blueprint that should force the users to add labels to their page, before saving the page. The approach for this that I took is that when the user tries to save the blank page thats created by the blueprint, the code below listening for the PageCreateEvent and its during this time that I am currently trying to add an hardcoded label. But as of now I am not sure why the label is not getting added to the page. I do see those sysouts on the console but am clueless why the label is not getting added to the page. Please help.

public class PageCreatedListener implements DisposableBean {
	
	 protected EventPublisher eventPublisher;
	 private final LabelManager labelManager;
	 private final PageManager pageManager;
	 private final Logger log;

	 
	 public PageCreatedListener(EventPublisher eventPublisher, LabelManager labelManager, PageManager pageManager) {
	        this.eventPublisher = eventPublisher;
	        eventPublisher.register(this);
	        this.labelManager = labelManager;
	        this.pageManager = pageManager;
	        log = LoggerFactory.getLogger(PageCreatedListener.class);
	 }
	 
	 @EventListener
	 public void pageCreateFromTemplateEvent(PageCreateEvent event) {
		 Page page= pageManager.getPage(event.getPage().getId());
		 
		
		System.out.println("PAGE ID " + page.getId());
		
		labelManager.addLabel((Labelable) page, new Label("timesheet"));
		System.out.println("LABEL 1 ADDED");
        labelManager.addLabel((Labelable) page, new Label("timesheettwo"));
        System.out.println("LABEL 2 ADDED");

		 
	 }

	@Override
	public void destroy() throws Exception {
		eventPublisher.unregister(this);
		
	}
}

1 answer

1 accepted

0 votes
Answer accepted

To add a label to a page, try to use the LabelUtil.addLabel("label", labelManager, page)- method.
This works fine for me:
for(String i : labels)LabelUtil.addLabel(i, labelManager, event.getPage());

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Mar 12, 2019 in Confluence

Confluence Admin Certification now $150 for Community Members

More and more people are building their careers with Atlassian, and we want you to be at the front of this wave! Important Dates Start the Certification Prep Course by 2 April 2019 Take your e...

1,268 views 2 13
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you