Wiki source code of TodoListsGroovy
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | /* | ||
2 | #* | ||
3 | TodoMacroGroovy | ||
4 | |||
5 | *# | ||
6 | */ | ||
7 | |||
8 | import org.xwiki.rendering.block.*; | ||
9 | import org.xwiki.rendering.renderer.*; | ||
10 | import org.xwiki.rendering.syntax.*; | ||
11 | import org.xwiki.rendering.parser.*; | ||
12 | import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; | ||
13 | import org.xwiki.rendering.renderer.printer.WikiPrinter; | ||
14 | import com.xpn.xwiki.web.Utils; | ||
15 | import com.xpn.xwiki.api.*; | ||
16 | import java.util.*; | ||
17 | import java.lang.reflect.*; | ||
18 | import groovy.json.JsonSlurper; | ||
19 | |||
20 | |||
21 | public class TodoListGroovy { | ||
22 | def xwiki; | ||
23 | def context; | ||
24 | |||
25 | public setContext(context, xwiki) { | ||
26 | this.xwiki = xwiki; | ||
27 | this.context = context; | ||
28 | } | ||
29 | |||
30 | public int updateDocumentXDOM(xdom, newcontent, append) { | ||
31 | def xdomchanged = false; | ||
32 | // Finding Macro Block | ||
33 | List<MacroBlock> macroBlocks = xdom.getChildrenByType(MacroBlock.class, true); | ||
34 | for (MacroBlock macroBlock : macroBlocks) { | ||
35 | def id = macroBlock.getId(); | ||
36 | if (id.equals("todolist")) { | ||
37 | |||
38 | def currentContent = macroBlock.getContent(); | ||
39 | currentContent = (currentContent==null) ? "" : currentContent; | ||
40 | |||
41 | // System.out.println("found macro"); | ||
42 | boolean done = true; | ||
43 | |||
44 | if ((currentContent!=newcontent) || append) { | ||
45 | // System.out.println("priority different changing it"); | ||
46 | newcontent = (append) ? currentContent + newcontent : newcontent; | ||
47 | |||
48 | def replacementBlock = new MacroBlock(id, macroBlock.getParameters(), newcontent, macroBlock.isInline()) | ||
49 | macroBlock.getParent().replaceChild(replacementBlock, macroBlock) | ||
50 | return 1; | ||
51 | done = true; | ||
52 | } | ||
53 | } | ||
54 | } | ||
55 | return 0; | ||
56 | } | ||
57 | |||
58 | public String getContentFromXDOM(xdom) { | ||
59 | WikiPrinter printer = new DefaultWikiPrinter(); | ||
60 | BlockRenderer renderer = (BlockRenderer) Utils.getComponent(BlockRenderer.class, Syntax.XWIKI_2_0.toIdString()); | ||
61 | renderer.render(xdom, printer); | ||
62 | return printer.toString(); | ||
63 | } | ||
64 | |||
65 | public XDOM getXDOMFromContent(content) { | ||
66 | Parser parser = (Parser) Utils.getComponent(Parser.class, Syntax.XWIKI_2_0.toIdString()); | ||
67 | return parser.parse(new StringReader(content)); | ||
68 | } | ||
69 | |||
70 | public int updateDocument(doc, newcontent) { | ||
71 | def slurper = new JsonSlurper() | ||
72 | def jsonobj = slurper.parseText(newcontent) | ||
73 | def finalcontent = "" | ||
74 | for (item in jsonobj) { | ||
75 | finalcontent += item.title + "|" + (item.isCompleted ? "1" : "0") + "\r\n"; | ||
76 | } | ||
77 | |||
78 | XDOM xdom = (doc == null) ? null : doc.getDocument().getXDOM(); | ||
79 | xdom = (xdom == null) ? null : xdom.clone(); | ||
80 | |||
81 | def result = updateDocumentXDOM(xdom, finalcontent, false); | ||
82 | |||
83 | // we updated the xdom, apply it | ||
84 | if (result!=0) { | ||
85 | doc.setContent(getContentFromXDOM(xdom)); | ||
86 | return result; | ||
87 | } | ||
88 | |||
89 | return 0; | ||
90 | } | ||
91 | |||
92 | public int addToDocument(doc, newcontent) { | ||
93 | def slurper = new JsonSlurper() | ||
94 | def item = slurper.parseText(newcontent) | ||
95 | def finalcontent = "" | ||
96 | finalcontent += item.title + "|" + (item.isCompleted ? "1" : "0") + "\r\n"; | ||
97 | |||
98 | XDOM xdom = (doc == null) ? null : doc.getDocument().getXDOM(); | ||
99 | xdom = (xdom == null) ? null : xdom.clone(); | ||
100 | |||
101 | def result = updateDocumentXDOM(xdom, finalcontent, true); | ||
102 | |||
103 | // we updated the xdom, apply it | ||
104 | if (result!=0) { | ||
105 | doc.setContent(getContentFromXDOM(xdom)); | ||
106 | return result; | ||
107 | } | ||
108 | |||
109 | return 0; | ||
110 | } | ||
111 | public List getTodos(Document doc) { | ||
112 | def counter = 0; | ||
113 | XDOM xdom = (doc == null) ? null : doc.getDocument().getXDOM(); | ||
114 | xdom = (xdom == null) ? null : xdom.clone(); | ||
115 | def res = getTodosFromXDOM(xdom); | ||
116 | def list = res.split("\r\n") | ||
117 | def list2 = new ArrayList(); | ||
118 | for (item in list) { | ||
119 | counter++; | ||
120 | def map = new HashMap(); | ||
121 | map.put("id", counter); | ||
122 | map.put("title", "") | ||
123 | map.put("isCompleted", "false") | ||
124 | def item2 = item.split("\\|") | ||
125 | if (item2.length>1) | ||
126 | map.put("isCompleted", ((item2[1]=="1") ? true : false)) | ||
127 | if (item2.length>0) | ||
128 | map.put("title", item2[0]) | ||
129 | list2.add(map); | ||
130 | } | ||
131 | return list2; | ||
132 | } | ||
133 | |||
134 | public String getTodosFromXDOM(xdom) { | ||
135 | // Finding Macro Block | ||
136 | List<MacroBlock> macroBlocks = xdom?.getChildrenByType(MacroBlock.class, true); | ||
137 | for (MacroBlock macroBlock : macroBlocks) { | ||
138 | def id = macroBlock.getId(); | ||
139 | if (id.equals("todolist")) { | ||
140 | return macroBlock.getContent(); | ||
141 | } | ||
142 | } | ||
143 | return ""; | ||
144 | } | ||
145 | } |