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

cliff kirkman July 11, 2017

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