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
RevLine 
[136]1import org.codehaus.groovy.runtime.TimeCategory
[210]2// the above will be deprecated and replaced by: groovy.time.TimeCategory
[136]3
[127]4class TaskRecurringSchedule {
[121]5
[131]6    Task lastGeneratedSubTask
[136]7    Period recurPeriod
8    Period taskDurationPeriod
[121]9
[135]10    Integer recurEvery = 1
[136]11    Integer taskDuration = 0
12    Integer generateAhead = 1
[199]13    Integer subTasksGenerated = 0
14    Date nextGenerationDate = new Date()
[136]15    Date nextTargetStartDate = new Date()
16    Date nextTargetCompletionDate = new Date()
[199]17    boolean enabled = true
[121]18
19//     static hasMany = []
20
[134]21    static belongsTo = [task: Task]
[121]22
[122]23    static constraints = {
[137]24        recurEvery(min:1, max:365)
[136]25        taskDuration(min:0, max:365)
[199]26        generateAhead(min:0, max:62)
[146]27        lastGeneratedSubTask(nullable:true)
[122]28    }
[121]29
[124]30    String toString() {
[136]31        "Recur every ${recurEvery} ${recurPeriod}"
[124]32    }
[146]33
[137]34    // As of Grails 1.1.1 this does not fire/pass before validation.
[195]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.
[136]37    def beforeInsert = {
[137]38        setNextGenerationDate()
39        setNextTargetCompletionDate()
40    }
[146]41
[199]42    public void setNextTargetStartDate() {
43        switch (recurPeriod.period) {
[136]44            case "Day(s)":
45                use(TimeCategory) {
[199]46                    nextTargetStartDate = nextTargetStartDate + recurEvery.days
[136]47                }
48                break
49            case "Week(s)":
50                use(TimeCategory) {
[199]51                    nextTargetStartDate = nextTargetStartDate + recurEvery.weeks
[136]52                }
53                break
54            case "Month(s)":
55                use(TimeCategory) {
[199]56                    nextTargetStartDate = nextTargetStartDate + recurEvery.months
[136]57                }
58                break
59            case "Year(s)":
60                use(TimeCategory) {
[199]61                    nextTargetStartDate = nextTargetStartDate + recurEvery.years
[136]62                }
63                break
64        default:
[199]65                log.error "No case for recurPeriod.period: ${recurPeriod.period}"
[136]66                break
67        }
[199]68    }
69
70    public void setNextGenerationDate() {
71        use(TimeCategory) {
72            nextGenerationDate = nextTargetStartDate - generateAhead.days
73        }
[137]74        def now = new Date()
[136]75        if( nextGenerationDate < now) {nextGenerationDate = now}
[137]76    }
[146]77
[137]78    public void setNextTargetCompletionDate() {
[136]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
[137]100            default:
[199]101                log.error "No case for taskDurationPeriod.period: ${taskDurationPeriod.period}"
[136]102                break
103        }
104    }
[146]105
[121]106}
107
Note: See TracBrowser for help on using the repository browser.