0 Votes

Wiki source code of Kanban Macro Groovy Code

Last modified by Ryan C on 2025/03/07 00:41

Hide last authors
Ryan C 1.1 1 import org.xwiki.rendering.block.*;
2 import org.xwiki.rendering.renderer.*;
3 import org.xwiki.rendering.syntax.*;
4 import org.xwiki.rendering.parser.*;
5 import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
6 import org.xwiki.rendering.renderer.printer.WikiPrinter;
7 import com.xpn.xwiki.web.Utils;
8 import com.xpn.xwiki.api.*;
9 import java.util.*;
10 import java.lang.reflect.*;
11
12 public class KanbanGroovy {
13 def xwiki;
14 def xcontext;
15
16 public setXWiki(xcontext, xwiki) {
17 this.xcontext = xcontext;
18 this.xwiki = xwiki;
19 }
20
21 public String getContentFromXDOM(xdom) {
22 WikiPrinter printer = new DefaultWikiPrinter();
23 BlockRenderer renderer = (BlockRenderer) Utils.getComponent(BlockRenderer.class, Syntax.XWIKI_2_0.toIdString());
24 renderer.render(xdom, printer);
25 return printer.toString();
26 }
27
28 public int updateDocument(doc, macroName, nb, newContent) {
29 try {
30 XDOM xdom = (doc == null) ? null : doc.getDocument().getXDOM();
31 xdom = (xdom == null) ? null : xdom.clone();
32
33 def result = updateDocumentXDOM(xdom, macroName, nb, newContent);
34
35 // we updated the xdom, apply it
36 if (result!=0) {
37 doc.setContent(getContentFromXDOM(xdom));
38 doc.save("Kanban content updated")
39 return result;
40 }
41
42 return 0;
43 } catch (e) {
44 xcontext.getContext().put("exception", e);
45 return 0;
46 }
47 }
48
49 public int updateDocumentXDOM(xdom, macroName, nb, newContent) {
50 def xdomchanged = false;
51 // Finding Macro Block
52 int currentNb = 0;
53 List<MacroBlock> macroBlocks = xdom.getChildrenByType(MacroBlock.class, true);
54 for (MacroBlock macroBlock : macroBlocks) {
55 def id = macroBlock.getId();
56 System.out.println("Found macro " + id);
57 if (id==macroName) {
58 if (nb!=currentNb) {
59 currentNb++;
60 } else {
61 System.out.println("Found macro number " + currentNb);
62 def currentContent = macroBlock.getContent();
63 if (currentContent!=newContent) {
64 currentContent = newContent;
65 System.out.println("Macro block " + macroBlock);
66 // We need to use reflection until this exists: macroBlock.setContent(newContent);
67 Field field;
68 try {
69 field = macroBlock.getClass().getSuperclass().getDeclaredField("content");
70 } catch (e) {
71 field = macroBlock.getClass().getDeclaredField("content");
72 }
73 field.setAccessible(true);
74 field.set(macroBlock, newContent);
75 return 1;
76 }
77 }
78 }
79 }
80 return 0;
81 }
82 }