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