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