source: trunk/grails-app/domain/TaskRecurringSchedule.groovy @ 210

Last change on this file since 210 was 210, checked in by gav, 14 years ago

Make date calculations and formatting more groovy.

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