How to get my created subpage to display? (Created using a Java api Macro)

I have a macro which automatically runs when a project page is opened, it checks to see if all the subpages are there. If any are missing I call a create page function to generate it.

The page generated has the correct parent page according to the confluence homepage but when I go to the project page I cannot see it listed underneath.

This is causing an error to occur and confluence to crash because it still thinks it needs to generate this page, when it trys again it errors saying a page cannot be created with the same title.

Code snippet:

public String execute(Map params, String body, RenderContext renderContext) throws MacroException {


System.out.println("First print out......................");

if (!(renderContext instanceof PageContext)) {
throw new MacroException("This macro can only be used in a page");
}

Map<String, Object> context = MacroUtils.defaultVelocityContext();
PageContext pageContext = (PageContext) renderContext;
ContentEntityObject contentEntityObject = pageContext.getEntity();
long currentPageId = contentEntityObject.getId();
Page page = pageManager.getPage(currentPageId);

List<Page> pagesList = page.getChildren();
String direct = System.getProperty("user.dir") + "/pagelist.txt";
boolean flag = false;
Space ourSpace = page.getSpace();

try (BufferedReader br = new BufferedReader(new FileReader(direct))) {
String line = null;
while ((line = br.readLine()) != null) {
for (int i = 0; i < pagesList.size(); i++) {
String temp = pagesList.get(i).toString().replace("page:", "").split("v.")[0];
if (temp.contains(line)) {
System.out.println("Page: " + line + " found in subpages!");
flag = true;
break;
}
}
if (flag == false) {
System.out.println("Page: " + line + " needs creating!");
String newString = line.trim();
Page missingPage = createPage(newString, ourSpace, page, 0);
break;
}
flag = false;
}
} catch (Exception e) {
System.out.println("Threw an exception!!!!!!!!!!!!!!!!!!!!");
System.out.println(e);
}

System.out.println("Macro ran");
// render the Velocity template with the assembled context*/
return VelocityUtils.getRenderedTemplate(MACRO_BODY_TEMPLATE, context);
}

 

 

private Page createPage(String title, Space space, Page parentPage, int position) {
Page subPage = new Page();

System.out.println(title);
System.out.println(space.toString());
System.out.println(parentPage.toString());
System.out.println(position);

subPage.setTitle(title);
subPage.setSpace(space);
subPage.setParentPage(parentPage);
subPage.setPosition(position);
pageManager.saveContentEntity(subPage, null);

try {
Files.write(Paths.get("pagelist.txt"), (" " + title + "\n").getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {
System.out.println(e);
}

return subPage;
}
}

 

 

0 answers

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

172 views 1 3
Join discussion

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