[136] | 1 | import org.codehaus.groovy.runtime.TimeCategory |
---|
| 2 | |
---|
[127] | 3 | class TaskRecurringSchedule { |
---|
[121] | 4 | |
---|
[131] | 5 | Task lastGeneratedSubTask |
---|
[136] | 6 | Period recurPeriod |
---|
| 7 | Period generateAheadPeriod |
---|
| 8 | Period taskDurationPeriod |
---|
[121] | 9 | |
---|
[135] | 10 | Integer recurEvery = 1 |
---|
[136] | 11 | Integer taskDuration = 0 |
---|
| 12 | Integer generateAhead = 1 |
---|
[121] | 13 | Date startDate = new Date() |
---|
[127] | 14 | Date lastGeneratedDate |
---|
[136] | 15 | Date nextTargetStartDate = new Date() |
---|
| 16 | Date nextTargetCompletionDate = new Date() |
---|
| 17 | Date nextGenerationDate = new Date() |
---|
[131] | 18 | boolean isEnabled = true |
---|
[121] | 19 | |
---|
| 20 | // static hasMany = [] |
---|
| 21 | |
---|
[134] | 22 | static belongsTo = [task: Task] |
---|
[121] | 23 | |
---|
[122] | 24 | static constraints = { |
---|
[137] | 25 | recurEvery(min:1, max:365) |
---|
[136] | 26 | taskDuration(min:0, max:365) |
---|
| 27 | generateAhead(min:0, max:365) |
---|
[146] | 28 | lastGeneratedDate(nullable:true) |
---|
| 29 | lastGeneratedSubTask(nullable:true) |
---|
[122] | 30 | } |
---|
[121] | 31 | |
---|
[124] | 32 | String toString() { |
---|
[136] | 33 | "Recur every ${recurEvery} ${recurPeriod}" |
---|
[124] | 34 | } |
---|
[146] | 35 | |
---|
[137] | 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. |
---|
[136] | 39 | def beforeInsert = { |
---|
| 40 | nextTargetStartDate = startDate |
---|
[137] | 41 | setNextGenerationDate() |
---|
| 42 | setNextTargetCompletionDate() |
---|
| 43 | } |
---|
[146] | 44 | |
---|
[137] | 45 | public void setNextGenerationDate() { |
---|
[136] | 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 | } |
---|
[137] | 70 | def now = new Date() |
---|
[136] | 71 | if( nextGenerationDate < now) {nextGenerationDate = now} |
---|
[137] | 72 | } |
---|
[146] | 73 | |
---|
[137] | 74 | public void setNextTargetCompletionDate() { |
---|
[136] | 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 |
---|
[137] | 96 | default: |
---|
[136] | 97 | break |
---|
| 98 | } |
---|
| 99 | } |
---|
[146] | 100 | |
---|
[121] | 101 | } |
---|
| 102 | |
---|