Hi all,
I’m trying to copy an attachment from one page to another with the onBlueprintCreateEvent however I keep seeing the error “Caused by: java.lang.NullPointerException” in the console.
I’m fairly certain it’s related to the PageManager but I’m not sure how to fix it. I’m setting the PageManager as described in Accessing Confluence Components.
Anyone have suggestions? Here’s my code:
import com.atlassian.confluence.plugins.createcontent.api.events.BlueprintPageCreateEvent;
import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.confluence.pages.Page;
import com.atlassian.confluence.pages.PageManager;
import com.atlassian.confluence.pages.AbstractPage;
import com.atlassian.confluence.pages.Attachment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PiBlueprintListener {
private static final Logger log = LoggerFactory.getLogger(PiBlueprintListener.class);
public PiBlueprintListener(EventPublisher eventPublisher) {
eventPublisher.register(this); //demonstration only
}
public PageManager pageManager;
public void setPageManager(PageManager pageManager)
{
this.pageManager = pageManager;
}
@EventListener
public void onBlueprintCreateEvent(BlueprintPageCreateEvent event){
Page targetPage = event.getPage();
String tempPageId = (String) event.getContext().get("tempPageId");
System.out.println("tempPageId: " + tempPageId);
AbstractPage attachmentPage = this.pageManager.getAbstractPage(Long.parseLong(tempPageId));
Attachment attachment = attachmentPage.getAttachments().iterator().next();
targetPage.addAttachment(attachment);
}
}
FYI, the tempPageId is the id of the page where the attachment is stored and the targetPage is where I want the attachment to go.