Skip to content

Instantly share code, notes, and snippets.

@rah003
Last active May 21, 2019 09:39
Show Gist options
  • Save rah003/033b2d5dd6f4c5075994d49dcc6d8aaf to your computer and use it in GitHub Desktop.
Save rah003/033b2d5dd6f4c5075994d49dcc6d8aaf to your computer and use it in GitHub Desktop.
Restructure workspace
s = ctx.getJCRSession("foflinks")
topPage = "/"
level = 30
root = s.getNode(topPage)
println("start with " + topPage)
listChildren(root, "")
println("done")
def listChildren(node, padding) {
if (node.getDepth() > 0) {
parent = node.getParent()
} else {
parent = null
}
if (parent != null && parent.isNodeType("link") && node.isNodeType("link") && node.getDepth() > 1) {
structureName1 = node.getName().substring(0,1)
if (parent.hasNode(structureName1) || node.getName().length() ==1) {
structureNode1 = parent.getNode(structureName1)
// if node exists but is of wrong type, create folder and move it within folder
// regarding node name len = 1, trick is that parent check above will return that node to us so we can use same procedure to move it within
if (!structureNode1.isNodeType("mgnl:folder")) {
// JR doesn't allow move into item with index!!! so we have to work around
tempNode = structureNode1
// move coliding node to root first
println("1 moving " + tempNode.getPath() + " to " + "/" + tempNode.getName())
s.move(tempNode.getPath(),"/" + tempNode.getName())
// create folder with same name
structureNode1 = parent.addNode(structureName1, "mgnl:folder")
// move node from root to the folder
println("2 moving " + tempNode.getPath() + " to " + structureNode1.getPath() + "/" + tempNode.getName())
s.move("/" + tempNode.getName(),structureNode1.path + "/" + tempNode.getName())
}
} else {
structureNode1 = parent.addNode(structureName1, "mgnl:folder")
}
if (node.getName().length() > 1) {
structureName2 = node.getName().substring(0,2)
if (structureNode1.hasNode(structureName2)) {
structureNode2 = structureNode1.getNode(structureName2)
// if node exists but is of wrong type, create folder and move it within folder
if (!structureNode2.isNodeType("mgnl:folder")) {
// JR doesn't allow move into item with index!!! so we have to work around
tempNode = structureNode2
// move coliding node to root first
s.move(tempNode.getPath(),"/" + tempNode.getName())
// create folder with same name
structureNode2 = structureNode1.addNode(structureName2, "mgnl:folder")
// move node from root to the folder
s.move("/"+ tempNode.getName(),structureNode2.getPath() + "/" + tempNode.getName())
}
} else {
structureNode2 = structureNode1.addNode(structureName2, "mgnl:folder")
}
}
println("3 moving " + node.getPath() + " to " + structureNode2.getPath() + "/" + node.getName())
s.move(node.getPath(), structureNode2.getPath() + "/" + node.getName())
s.save()
}
if (!node.hasNodes()) {
return
}
int count = 0
def curLevel = 1 + padding.length()/4
def iter = node.nodes
while (iter.hasNext()) {
child = iter.nextNode()
name = child.name
if (!child.isNodeType("link")) {
continue;
}
count++
println(padding + count + " name:" + name)
if (level > curLevel) {
listChildren(child, padding + " ")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment