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 | // startDate(validator: {return (it > new Date())}) |
---|
26 | recurEvery(min:0, max:365) |
---|
27 | taskDuration(min:0, max:365) |
---|
28 | generateAhead(min:0, max:365) |
---|
29 | lastGeneratedDate(blank:true, nullable:true) |
---|
30 | lastGeneratedSubTask(blank:true, nullable:true) |
---|
31 | } |
---|
32 | |
---|
33 | String toString() { |
---|
34 | "Recur every ${recurEvery} ${recurPeriod}" |
---|
35 | } |
---|
36 | |
---|
37 | //As of Grails 1.1 this does not fire/pass before validation. |
---|
38 | //But setting defaults above and placing this code here in the hope that this will be fixed in future versions. |
---|
39 | def beforeInsert = { |
---|
40 | def now = new Date() |
---|
41 | |
---|
42 | nextTargetStartDate = startDate |
---|
43 | |
---|
44 | //nextGenerationDate |
---|
45 | switch (generateAheadPeriod.period) { |
---|
46 | case "Day(s)": |
---|
47 | use(TimeCategory) { |
---|
48 | nextGenerationDate = nextTargetStartDate - generateAhead.days |
---|
49 | } |
---|
50 | break |
---|
51 | case "Week(s)": |
---|
52 | use(TimeCategory) { |
---|
53 | nextGenerationDate = nextTargetStartDate - generateAhead.weeks |
---|
54 | } |
---|
55 | break |
---|
56 | case "Month(s)": |
---|
57 | use(TimeCategory) { |
---|
58 | nextGenerationDate = nextTargetStartDate - generateAhead.months |
---|
59 | } |
---|
60 | break |
---|
61 | case "Year(s)": |
---|
62 | use(TimeCategory) { |
---|
63 | nextGenerationDate = nextTargetStartDate - generateAhead.years |
---|
64 | } |
---|
65 | break |
---|
66 | default: |
---|
67 | break |
---|
68 | } |
---|
69 | |
---|
70 | if( nextGenerationDate < now) {nextGenerationDate = now} |
---|
71 | |
---|
72 | //nextTargetCompletionDate |
---|
73 | switch (taskDurationPeriod.period) { |
---|
74 | case "Day(s)": |
---|
75 | use(TimeCategory) { |
---|
76 | nextTargetCompletionDate = nextTargetStartDate + taskDuration.days |
---|
77 | } |
---|
78 | break |
---|
79 | case "Week(s)": |
---|
80 | use(TimeCategory) { |
---|
81 | nextTargetCompletionDate = nextTargetStartDate + taskDuration.weeks |
---|
82 | } |
---|
83 | break |
---|
84 | case "Month(s)": |
---|
85 | use(TimeCategory) { |
---|
86 | nextTargetCompletionDate = nextTargetStartDate + taskDuration.months |
---|
87 | } |
---|
88 | break |
---|
89 | case "Year(s)": |
---|
90 | use(TimeCategory) { |
---|
91 | nextTargetCompletionDate = nextTargetStartDate + taskDuration.years |
---|
92 | } |
---|
93 | break |
---|
94 | default: |
---|
95 | break |
---|
96 | } |
---|
97 | |
---|
98 | } |
---|
99 | |
---|
100 | } |
---|
101 | |
---|