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

Last change on this file since 202 was 199, checked in by gav, 15 years ago

Upgrade quartz plugin to 0.4.1 propper.
Refactor and enable quartz scheduling for recurring tasks.
Adjust svn ignores to ignore all log files.
Create a pseudo system person for automated insertions.

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