Groovy
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.SAXReader;
import org.dom4j.dom.DOMDocument;
import org.dom4j.dom.DOMElement;
import groovy.xml.MarkupBuilder
import com.xpn.xwiki.doc.XWikiAttachment;
public class MindMapGroovy {
def xwiki;
def context;
def deletedDocuments = new java.util.ArrayList();
def createdDocuments = new java.util.ArrayList();
def mmDirty = false;
public setXWiki(xwiki, context) {
this.xwiki = xwiki;
this.context = context;
}
public getDeletedDocuments() {
return deletedDocuments;
}
public getCreatedDocuments() {
return createdDocuments;
}
public handleMindMapAction(action, page) {
def msg;
if (action=="view") {
msg = viewMindMapWiki(page);
} else if (action=="update") {
msg = updateMindMapWiki(page);
} else if (action=="delete") {
msg = deleteMindMapWiki(page);
} else if (action=="updateMap") {
msg = updateMindMap(page, context.request.startPage, false);
} else if (action=="updateMapRendered") {
msg = updateMindMap(page, context.request.startPage, true);
} else {
msg = "mindmap.noaction";
}
return msg;
}
public findMindMapAttachment(page) {
def pagedoc = xwiki.getDocument(page);
for (attach in pagedoc.attachmentList) {
if (attach.getFilename().endsWith(".mm"))
return attach;
}
return null;
}
public findMindMapWikiHomeURL(String xml) {
def reader = new SAXReader();
def domdoc = null;
try {
domdoc = reader.read(new StringReader(xml));
} catch (DocumentException e) {
throw e;
}
if (domdoc==null)
return "mindmap.errorreadingmmxml";
def docroot = domdoc.getRootElement();
def link = findMindMapHomeFromNode(docroot);
return link;
}
public findMindMapWikiHome(node) {
def link = findMindMapWikiHomeURL(node);
if (link==null)
return null;
else {
return link.substring(link.lastIndexOf("/") + 1);
}
}
public viewMindMapWiki(page) {
def attach = findMindMapAttachment(page);
if (attach==null)
return "mindmap.notfound";
else {
def link = findMindMapWikiHomeURL(new String(attach.getContent()));
if (link==null)
return "mindmap.wikihomenotfound";
else {
context.response.sendRedirect(link);
return "mindmap.redirectdone";
}
}
}
public findMindMapWikiHomeFromPage(page) {
def attach = findMindMapAttachment(page);
if (attach==null)
return "mindmap.notfound";
else {
def wikiPage = findMindMapWikiHome(new String(attach.getContent()));
if (wikiPage==null)
return "mindmap.wikihomenotfound";
else {
return wikiPage;
}
}
}
public createMindMapWiki(page) {
def attach = findMindMapAttachment(page);
if (attach==null)
return "mindmap.notfound";
createMindMapWikiFromAttachment(xwiki.getDocument(page), attach);
return "mindmap.createdone";
}
public updateMindMapWiki(page) {
def attach = findMindMapAttachment(page);
if (attach==null)
return "mindmap.notfound";
else {
deleteMindMapWiki(page);
createMindMapWiki(page);
}
return "mindmap.updatedone";
}
public deleteMindMapWiki(page) {
def doc = xwiki.getDocument(page);
def sql = ", BaseObject as obj, StringProperty as prop where doc.web='MindMap' and obj.name=doc.fullName and obj.className='MindMap.MindMapClass' and obj.id=prop.id.id and prop.id.name='MapName' and prop.value='$page'";
for(pagename in xwiki.searchDocuments(sql)) {
def pagedoc = xwiki.getDocument(pagename);
if (!pagedoc.isNew()) {
pagedoc.delete();
deletedDocuments.add(pagename);
}
}
def attach = findMindMapAttachment(page);
def mmcontent = new String(attach.getContent());
def newmmcontent = mmcontent.replaceAll("LINK=\"/xwiki/(.?)\"(.?>)","$2");
newmmcontent = newmmcontent.replaceAll("link=\"/xwiki/(.?)\"(.?>)","$2");
attach.getAttachment().setContent(newmmcontent.getBytes());
if (!mmcontent.equals(newmmcontent)) {
doc.getDocument().saveAttachmentContent(attach.getAttachment(), context.getContext());
}
return "mindmap.deletedone";
}
public createMindMapWikiFromAttachment(doc, attachment) {
def reader = new SAXReader();
def domdoc = null;
try {
domdoc = reader.read(new StringReader(new String(attachment.getContent())));
} catch (DocumentException e) {
throw e;
}
if (domdoc==null)
return "mindmap.errorreadingmmxml";
def docroot = domdoc.getRootElement();
createMindMapWikiFromNode(doc, doc.getFullName(), docroot, 0);
attachment.getAttachment().setContent(docroot.asXML().getBytes());
if (mmDirty)
{
// we need a priviledged API to modify the attachment content
doc.getDocument().saveAttachmentContent(attachment.getAttachment(), context.getContext());
return "mindmap.mmupdated";
}
}
public findMindMapHomeFromNode(node) {
def el = node.elements("node");
def j = 0;
while(j < el.size())
{
def link = getAttr(el.get(j),"link");
if (link != null && link.matches("^/xwiki/.*")) {
return link;
}
j++;
}
return null;
}
public findMindMapWikiHomeFromNode(node) {
def link = findMindMapHomeFromNode(node);
if (link==null)
return null;
else {
return link.substring(link.lastIndexOf("/") + 1);
}
}
public createMindMapWikiFromNode(mmDoc, parent, node, deph) {
def el = node.elements("node");
def j = 0;
def pages = new java.util.ArrayList();
while(j < el.size())
{
def title = getAttr(el.get(j),"text");
title = (title!=null) ? title : el.get(j).attributeValue("text")
title = (title==null) ? "notitle" : title.replaceAll(">>", "");
def text = title;
def link = getAttr(el.get(j), "link");
link = (link!=null) ? link : el.get(j).attributeValue("link")
// cleaning up to generate a clean page name
text = xwiki.clearName(text);
text = text.replaceAll(">", "");
text = text.replaceAll("<", "");
if (text.size() > 80) {
def i = text.substring(0,80).lastIndexOf(" ", 60);
if (i==-1)
text = text.substring(0, 80);
else
text = text.substring(0, i);
}
// def tmpEl = el.get(j).elements("icon");
// if(tmpEl && tmpEl.size() > 0)
// text = "aaa_" + text;
def newDoc = null;
def pagename = xwiki.getUniquePageName(mmDoc.web, text);
newDoc = xwiki.getDocument(mmDoc.web, pagename);
def desc = "";
def childWikiPages = new java.util.ArrayList();
/* if (nodeHasChild(el.get(j))) {
if (link == null || link.length()==0) {
el.get(j).addAttribute("link", xwiki.getURL(newDoc.getFullName(), "view"));
mmDirty = true;
}
if ((!nodeHasChildWithLink(el.get(j)))&&nodeChildIsLastChild(el.get(j))) {
desc = el.get(j).elements("node").get(0).attributeValue("text");
desc = (desc==null) ? "" : desc.replaceAll(">>", "");
}
}
*/
if (link == null || link.length()==0) {
el.get(j).addAttribute("link", xwiki.getURL(newDoc.getFullName(), "view"));
mmDirty = true;
}
def desc2 = getTextChild(el.get(j));
if (desc2!=null)
desc += "rnrn" + desc2;
def icon = getIconChild(el.get(j));
//if (!nodeChildIsLastChild(el.get(j)))
childWikiPages = createMindMapWikiFromNode(mmDoc, newDoc.getFullName(), el.get(j), deph + 1);
def content = renderNodeContent(el.get(j), 0, -1, childWikiPages, desc);
newDoc.setContent(content);
newDoc.setParent(parent);
newDoc.setTitle(title);
newDoc.newObject("MindMap.MindMapClass");
newDoc.use("MindMap.MindMapClass");
newDoc.set("mapname", mmDoc.getFullName());
newDoc.set("title", title);
newDoc.set("text", desc);
if (icon!=null)
newDoc.set("icon", icon);
newDoc.saveWithProgrammingRights();
createdDocuments.add(newDoc.getFullName());
pages.add(newDoc.getFullName());
j++;
}
return pages;
}
protected boolean nodeHasChild(node)
{
def el = node.elements("node");
def j = 0;
if (!el || el.size() == 0)
return false;
return true;
}
protected boolean nodeChildIsLastChild(node)
{
def el = node.elements("node");
def j = 0;
if (!el || el.size() == 0 || el.size() > 1) {
return false;
}
return !nodeHasChild(el.get(0));
}
protected String getTextChild(node)
{
def el = node.elements("hook");
if (el && el.size() > 0) {
def el2 = el.get(0).elements("text");
if (el2 && el2.size() > 0) {
return new String(el2.get(0).getText());
}
}
return null;
}
protected String getIconChild(node)
{
def el = node.elements("icon");
if (el && el.size() > 0) {
return getAttr(el.get(0), "builtin");
}
return null;
}
protected boolean nodeHasChildWithLink(node) {
def el = node.elements("node");
def j = 0;
while(j < el.size())
{
def link = getAttr(el.get(j), "link");
if (link != null && link.length() > 0 && !link.matches("^/xwiki/.*"))
{
return true;
}
j++
}
return false;
}
protected getAttr(el, name) {
def val = el.attributeValue(name.toLowerCase());
if (val!=null)
return val;
else
return el.attributeValue(name.toUpperCase());
}
protected String renderNodeContent(node, deph, maxDeph, childPages, desc) {
if (deph == maxDeph)
return "";
def title = getAttr(node, "text");
title = (title==null) ? "" : title.replaceAll(">","");
def link = getAttr(node, "link");
if (link == null || link.length() == 0 || link.matches("^/xwiki/.*"))
link = "";
// adding variables to the velocity context
def vcontext = context.getContext().get("vcontext");
vcontext.put("mmlink", link);
vcontext.put("mmtitle", title);
vcontext.put("mmnode", node);
vcontext.put("mmdesc", desc);
vcontext.put("mmchilds", childPages);
def content = xwiki.getDocument("MindMap", "WikiPageTemplate").getRenderedContent()
def navigate = xwiki.parseMessage("mindmap.navigatetoparent")
def returntomindmap = xwiki.parseMessage("mindmap.returntomindmap")
content += "rnrn${navigate}? ${returntomindmap}?";
return content;
}
protected boolean removeXWikiLinksFromNode(node) {
def el = node.elements("node");
def j = 0;
boolean result = false;
while(j < el.size())
{
def childNode = el.get(j);
def link = getAttr(childNode, "link");
if (link != null && link.matches("^/xwiki/.*"))
{
childNode.remove(childNode.attribute("LINK"));
result = true;
}
link = childNode.attributeValue("link");
if (link != null && link.matches("^/xwiki/.*"))
{
childNode.remove(childNode.attribute("link"));
result = true;
}
result |= removeXWikiLinksFromNode(childNode);
j++;
}
return result;
}
protected generateXmlForChildNode(nodeName, xmlBuilder, rendered) {
def nodeDoc = xwiki.getDocument(nodeName)
nodeDoc.use("MindMap.MindMapClass")
def title = nodeDoc.getValue("title");
def text = nodeDoc.getValue("text");
def icon = nodeDoc.getValue("icon");
if ((title==null)||(title==""))
title = nodeDoc.displayTitle;
if (rendered)
text = xwiki.getXMLEncoded(nodeDoc.getRenderedContent(text));
else
text = xwiki.getXMLEncoded(text);
xmlBuilder.node(link:xwiki.getURL(nodeName, "view"), text:title, id:nodeName) {
if ((text!=null)&&(text!="")) {
xmlBuilder.hook(name:"accessories/plugins/NodeNote.properties") {
xmlBuilder.text(text)
}
}
if ((icon!=null)&&(icon!="")) {
xmlBuilder.icon(BUILTIN:icon) {
}
}
def sql = "select distinct doc.web, doc.name, doc.parent from XWikiDocument as doc where doc.parent='" + xwiki.sqlfilter(nodeName) + "'"
def doclist = xwiki.search(sql)
for(item in doclist) {
generateXmlForChildNode(item0? + "." + item1?, xmlBuilder, rendered)
}
}
}
protected updateMindMap(mmPage, startPage, rendered) {
def startDoc = xwiki.getDocument(startPage);
if (startDoc.isNew())
return "mindmap.nostartpage"
def mmDoc = xwiki.getDocument(mmPage);
def attach = findMindMapAttachment(mmPage);
if (attach==null) {
attach = new XWikiAttachment(mmDoc.getDocument(), "mindmap.mm")
mmDoc.getDocument().getAttachmentList().add(attach)
}
def writer = new StringWriter()
def xmlBuilder = new MarkupBuilder(writer)
xmlBuilder.map() {
generateXmlForChildNode(startPage, xmlBuilder, rendered)
}
def mmcontent = attach.content
def newmmcontent = writer.toString()
attach.getAttachment().setContent(newmmcontent.getBytes());
if (!mmcontent.equals(newmmcontent)) {
mmDoc.getDocument().saveAttachmentContent(attach.getAttachment(), context.getContext());
}
return "mindmap.updatemapdone"
}
}
Cedric Thomas
Patrick Starck
Mark Weitzel
Alexandre Zapolsky
Round Table1
Gianfranco Boccalon
Thomas Mortagne
Serge Lacourte
Florent Garin
Frederic Aatz
Alfonso Castro
Jamie Marshall
Bernard Boltz
Alexandre Lefebvre
Thomas Debru
Denis Caromel
Daniel Stern
Bruno Dillenseger
Jean-Christophe Vuillot
Jeanne Le Garrec
Sylvain Baubeau
Jean-Marc Menaud
Fabien Hermenier
Christophe Hamerling
Patrick Petit
Marius Preda
Frederic Dang Tran
Pawel Rubach
Eric Debeau
Alban Richard
Andrea Manieri
Ali Ataya
Round Table2
Rani Halimi
Nicolas Aube
Marc Triboulet
Jim Walker
Monica Franceschini
Cedric Carbone
John Mertic
Marc Dutoo
Clement Oudot
Christophe Hamerling
Alexis Portmann
Goulven Le Jeune
Gerard Dupont
Alexandre Lefebvre
Guillaume Sauthier
Clement Escoffier
Christophe Hamerling
Guillaume Sauthier
Williams Lamar
Philippe Bolac
Meng Li
Gabriele Giammatteo
Daniele Gagliardi
Marc Lacoste
Pierre Chatel
Gaurav Parakh
Michel Catan
Cedric Thomas
Follow us on Twitter
Network @ LinkedIn

































