1 | import org.codehaus.groovy.runtime.TimeCategory |
---|
2 | // the above will be deprecated and replaced by: groovy.time.TimeCategory |
---|
3 | |
---|
4 | class TaskRecurringSchedule { |
---|
5 | |
---|
6 | Task lastGeneratedSubTask |
---|
7 | Period recurPeriod |
---|
8 | Period taskDurationPeriod |
---|
9 | |
---|
10 | Integer recurEvery = 1 |
---|
11 | Integer taskDuration = 0 |
---|
12 | Integer generateAhead = 1 |
---|
13 | Integer subTasksGenerated = 0 |
---|
14 | Integer maxSubTasks = 0 |
---|
15 | Date nextGenerationDate = new Date() |
---|
16 | Date nextTargetStartDate = new Date()+1 |
---|
17 | Date nextTargetCompletionDate = new Date() |
---|
18 | boolean enabled = true |
---|
19 | boolean useTargetCompletionDate = false |
---|
20 | |
---|
21 | // static hasMany = [] |
---|
22 | |
---|
23 | static belongsTo = [task: Task] |
---|
24 | |
---|
25 | static constraints = { |
---|
26 | recurEvery(min:1, max:365) |
---|
27 | taskDuration(min:0, max:365) |
---|
28 | generateAhead(min:0, max:62) |
---|
29 | lastGeneratedSubTask(nullable:true) |
---|
30 | } |
---|
31 | |
---|
32 | String toString() { |
---|
33 | "Recur every ${recurEvery} ${recurPeriod}" |
---|
34 | } |
---|
35 | |
---|
36 | // Events do not fire/pass before validation, since they are called during flushing. |
---|
37 | // Defaults set above to pass validation. |
---|
38 | def beforeInsert = { |
---|
39 | setNextGenerationDate() |
---|
40 | setNextTargetCompletionDate() |
---|
41 | } |
---|
42 | |
---|
43 | public void setNextTargetStartDate() { |
---|
44 | switch (recurPeriod.period) { |
---|
45 | case "Day(s)": |
---|
46 | use(TimeCategory) { |
---|
47 | nextTargetStartDate = nextTargetStartDate + recurEvery.days |
---|
48 | } |
---|
49 | break |
---|
50 | case "Week(s)": |
---|
51 | use(TimeCategory) { |
---|
52 | nextTargetStartDate = nextTargetStartDate + recurEvery.weeks |
---|
53 | } |
---|
54 | break |
---|
55 | case "Month(s)": |
---|
56 | use(TimeCategory) { |
---|
57 | nextTargetStartDate = nextTargetStartDate + recurEvery.months |
---|
58 | } |
---|
59 | break |
---|
60 | case "Year(s)": |
---|
61 | use(TimeCategory) { |
---|
62 | nextTargetStartDate = nextTargetStartDate + recurEvery.years |
---|
63 | } |
---|
64 | break |
---|
65 | default: |
---|
66 | log.error "No case for recurPeriod.period: ${recurPeriod.period}" |
---|
67 | break |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | public void setNextGenerationDate() { |
---|
72 | use(TimeCategory) { |
---|
73 | nextGenerationDate = nextTargetStartDate - generateAhead.days |
---|
74 | } |
---|
75 | def now = new Date() |
---|
76 | if( nextGenerationDate < now) {nextGenerationDate = now} |
---|
77 | } |
---|
78 | |
---|
79 | public void setNextTargetCompletionDate() { |
---|
80 | switch (taskDurationPeriod.period) { |
---|
81 | case "Day(s)": |
---|
82 | use(TimeCategory) { |
---|
83 | nextTargetCompletionDate = nextTargetStartDate + taskDuration.days |
---|
84 | } |
---|
85 | break |
---|
86 | case "Week(s)": |
---|
87 | use(TimeCategory) { |
---|
88 | nextTargetCompletionDate = nextTargetStartDate + taskDuration.weeks |
---|
89 | } |
---|
90 | break |
---|
91 | case "Month(s)": |
---|
92 | use(TimeCategory) { |
---|
93 | nextTargetCompletionDate = nextTargetStartDate + taskDuration.months |
---|
94 | } |
---|
95 | break |
---|
96 | case "Year(s)": |
---|
97 | use(TimeCategory) { |
---|
98 | nextTargetCompletionDate = nextTargetStartDate + taskDuration.years |
---|
99 | } |
---|
100 | break |
---|
101 | default: |
---|
102 | log.error "No case for taskDurationPeriod.period: ${taskDurationPeriod.period}" |
---|
103 | break |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | } |
---|
108 | |
---|