/
ReportModel.groovy
273 lines (253 loc) · 10 KB
/
ReportModel.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package freeplaneGTD
import org.freeplane.core.util.TextUtils
import org.freeplane.plugin.script.proxy.ConvertibleDate
import org.freeplane.plugin.script.proxy.Proxy
/**
* Model for the freeplaneGTD.report pane.
*
* Created by gpapp on 2015.03.05..
*/
class ReportModel {
Proxy.Node rootNode
List actionList
GTDMapReader mapReader
String todayText = TextUtils.getText("freeplaneGTD.view.when.today")
String thisWeekText = TextUtils.getText("freeplaneGTD.view.when.this_week")
enum VIEW {
PROJECT, WHO, CONTEXT, WHEN, ABOUT
}
/**
* Calculate the next time the action should be taken care of
* (either waitUntil date, or the due date, whichever comes first)
*/
def taskDate = { task ->
def when = task['when']
def waitUntil = task['waitUntil']
Date today = new Date().clearTime()
Date whenDate = null
Date waitUntilDate = null
if (when == todayText) whenDate = today
else if (when instanceof Date) whenDate = when
else if (when instanceof ConvertibleDate) whenDate = when.calendar.time
if (waitUntil instanceof Date) waitUntilDate = waitUntil
else if (waitUntil instanceof ConvertibleDate) waitUntilDate = waitUntil.calendar.time
if (!whenDate && !waitUntilDate) {
return waitUntil?: (when?: thisWeekText)
}
Date retval
if (whenDate && !waitUntilDate) retval = whenDate
else if (!whenDate && waitUntilDate) retval = waitUntilDate
else retval = whenDate < waitUntilDate ? whenDate : waitUntilDate
return retval.equals(today) ? todayText : retval
}
/**
* Compare tasks based on the task date calculated above
*/
def taskDateComparator = { a, b ->
def aw = a['when']
def bw = b['when']
if ((!aw && !bw) || aw.equals(bw)) {
return 0
}
Date ad = null
Date bd = null
Date today = new Date().clearTime()
if (aw instanceof Date) ad = aw
else if (aw == todayText) ad = today
else if (aw == thisWeekText) ad = today + 7
if (bw instanceof Date) bd = bw
else if (bw == todayText) bd = today
else if (bw == thisWeekText) bd = today + 7
if (ad==null && bd==null) {
return aw <=> bw
}
if (ad && !bd) return -1
if (!ad && bd) return 1
return ad <=> bd
}
/**
* Sort tasks according to priority and next action time
*/
def taskSortComparator = { a, b ->
def ap = a['priority'] ?: '5'
def bp = b['priority'] ?: '5'
if ((!ap && !bp) || ap.equals(bp)) {
return taskDateComparator(a, b)
}
return ap <=> bp
}
ReportModel(Proxy.Node rootNode) {
this.rootNode = rootNode
this.mapReader = GTDMapReader.instance
}
//--------------------------------------------------------------
// parse the GTD mind map
void parseMap(boolean filterDone) {
// Expand any nodes with next action shorthand
mapReader.convertShorthand(rootNode);
// Get next action lists
actionList = mapReader.getActionList(rootNode, filterDone)
// Fill actionList with next actionable time
actionList.each { it['time'] = taskDate(it) }
}
def projectList() {
Map<String, Object> retval = [type: 'project']
List<Map> groups = []
Map<Object, List> naByGroup = actionList.groupBy { it['project'] }
naByGroup = naByGroup.sort { it.toString().toLowerCase() }
naByGroup.each {
key, value ->
List<Map> items = []
def curGroup = naByGroup[key].sort { a, b -> taskSortComparator(a, b) }
curGroup.each {
items << [done : it['done'],
time : it['time'],
priority : it['priority'],
action : it['action'],
nodeID : it['nodeID'],
who : it['who'],
when : it['when'],
context : it['context'],
waitFor : it['waitFor'],
waitUntil: it['waitUntil'],
details : it['details'],
notes : it['notes']
]
}
groups << [title: key, items: items]
}
retval['groups'] = groups
return retval
}
def delegateList() {
Map retval = [type: 'who']
List groups = []
Map naByWhoFull = actionList.groupBy({ it['who'] })
Map naByWaitForFull = actionList.groupBy({ it['waitFor'] })
Map naByDelegate = [:]
naByWhoFull.each {
key, value ->
if (key) {
def keyList = key.split(',')*.trim()
keyList.each {
naByDelegate.put(it, naByDelegate[it] ? (naByDelegate[it] + value).unique() : value)
}
}
}
naByWaitForFull.each {
key, value ->
if (key) {
def keyList = key.split(',')*.trim()
keyList.each {
naByDelegate.put(it, naByDelegate[it] ? (naByDelegate[it] + value).unique() : value)
}
}
}
naByDelegate = naByDelegate.sort { it.toString().toLowerCase() }
naByDelegate.each {
key, value ->
List<Map> items = []
def curGroup = naByDelegate[key].sort { a, b -> taskSortComparator(a, b) }
curGroup.each {
def newItem = [done : it['done'],
time : it['time'],
priority : it['priority'],
action : it['action'],
nodeID : it['nodeID'],
when : it['when'],
context : it['context'],
project : it['project'],
waitUntil: it['waitUntil'],
details : it['details'],
notes : it['notes']
]
if (it['who'] && it['who'].trim() != key) {
newItem['who'] = it['who']
}
if (it['waitFor'] && it['waitFor'].trim() != key) {
newItem['waitFor'] = it['waitFor']
}
items << newItem
}
groups << [title: key, items: items]
}
retval['groups'] = groups
return retval
}
def contextList() {
Map retval = [type: 'context']
List groups = []
Map naByGroupFull = actionList.groupBy { it['context'] }
Map naByGroup = [:]
naByGroupFull.each {
key, value ->
if (!key) {
naByGroup.put(key, value)
} else {
def keyList = key?.split(',')
keyList.each {
naByGroup.put(it, naByGroup[it] ? naByGroup[it] + value : value)
}
}
}
naByGroup = naByGroup.sort { it.toString().toLowerCase() }
naByGroup.each {
key, value ->
List<Map> items = []
def curGroup = naByGroup[key].sort { a, b -> taskSortComparator(a, b) }
curGroup.each {
items << [done : it['done'],
time : it['time'],
priority : it['priority'],
action : it['action'],
nodeID : it['nodeID'],
when : it['when'],
who : it['who'],
project : it['project'],
waitFor : it['waitFor'],
waitUntil: it['waitUntil'],
details : it['details'],
notes : it['notes']
]
}
groups << [title: key ?: TextUtils.getText("freeplaneGTD.view.context.unassigned"), items: items]
}
retval['groups'] = groups
return retval
}
def timelineList() {
Map retval = [type: 'when']
List groups = []
def sortedList = actionList.sort { a, b -> taskDateComparator(a, b) }
def naByGroup = sortedList.groupBy { it['time'] }
naByGroup.each {
key, value ->
List<Map> items = []
def curGroup = naByGroup[key].sort { a, b -> taskSortComparator(a, b) }
curGroup.each {
def newItem = [done : it['done'],
time : it['time'],
priority: it['priority'],
action : it['action'],
nodeID : it['nodeID'],
who : it['who'],
project : it['project'],
context : it['context'],
waitFor : it['waitFor'],
details : it['details'],
notes : it['notes']
]
if (it['when'] && it['when'] != key) {
newItem['when'] = it['when']
}
if (it['waitUntil'] && it['waitUntil'] != key) {
newItem['waitUntil'] = it['waitUntil']
}
items << newItem
}
groups << [title: key, items: items]
}
retval['groups'] = groups
return retval
}
}