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

Last change on this file since 450 was 445, checked in by gav, 15 years ago

Add maxSubTasks and useTargetCompletionDate to TaskRecurringSchedule as features to end subTask generation.

File size: 3.4 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
[445]14    Integer maxSubTasks = 0
[199]15    Date nextGenerationDate = new Date()
[445]16    Date nextTargetStartDate = new Date()+1
[136]17    Date nextTargetCompletionDate = new Date()
[199]18    boolean enabled = true
[445]19    boolean useTargetCompletionDate = false
[121]20
21//     static hasMany = []
22
[134]23    static belongsTo = [task: Task]
[121]24
[122]25    static constraints = {
[137]26        recurEvery(min:1, max:365)
[136]27        taskDuration(min:0, max:365)
[199]28        generateAhead(min:0, max:62)
[146]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.
[195]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 = {
[137]40        setNextGenerationDate()
41        setNextTargetCompletionDate()
42    }
[146]43
[199]44    public void setNextTargetStartDate() {
45        switch (recurPeriod.period) {
[136]46            case "Day(s)":
47                use(TimeCategory) {
[199]48                    nextTargetStartDate = nextTargetStartDate + recurEvery.days
[136]49                }
50                break
51            case "Week(s)":
52                use(TimeCategory) {
[199]53                    nextTargetStartDate = nextTargetStartDate + recurEvery.weeks
[136]54                }
55                break
56            case "Month(s)":
57                use(TimeCategory) {
[199]58                    nextTargetStartDate = nextTargetStartDate + recurEvery.months
[136]59                }
60                break
61            case "Year(s)":
62                use(TimeCategory) {
[199]63                    nextTargetStartDate = nextTargetStartDate + recurEvery.years
[136]64                }
65                break
66        default:
[199]67                log.error "No case for recurPeriod.period: ${recurPeriod.period}"
[136]68                break
69        }
[199]70    }
71
72    public void setNextGenerationDate() {
73        use(TimeCategory) {
74            nextGenerationDate = nextTargetStartDate - generateAhead.days
75        }
[137]76        def now = new Date()
[136]77        if( nextGenerationDate < now) {nextGenerationDate = now}
[137]78    }
[146]79
[137]80    public void setNextTargetCompletionDate() {
[136]81        switch (taskDurationPeriod.period) {
82            case "Day(s)":
83                use(TimeCategory) {
84                    nextTargetCompletionDate = nextTargetStartDate + taskDuration.days
85                }
86                break
87            case "Week(s)":
88                use(TimeCategory) {
89                    nextTargetCompletionDate = nextTargetStartDate + taskDuration.weeks
90                }
91                break
92            case "Month(s)":
93                use(TimeCategory) {
94                    nextTargetCompletionDate = nextTargetStartDate + taskDuration.months
95                }
96                break
97            case "Year(s)":
98                use(TimeCategory) {
99                    nextTargetCompletionDate = nextTargetStartDate + taskDuration.years
100                }
101                break
[137]102            default:
[199]103                log.error "No case for taskDurationPeriod.period: ${taskDurationPeriod.period}"
[136]104                break
105        }
106    }
[146]107
[121]108}
109
Note: See TracBrowser for help on using the repository browser.