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

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

Update event comment on TaskRecurringSchedule domain class, no domain change made.

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
[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
[603]36    // Events do not fire/pass before validation, since they are called during flushing.
37    // Defaults set above to pass validation.
[136]38    def beforeInsert = {
[137]39        setNextGenerationDate()
40        setNextTargetCompletionDate()
41    }
[146]42
[199]43    public void setNextTargetStartDate() {
44        switch (recurPeriod.period) {
[136]45            case "Day(s)":
46                use(TimeCategory) {
[199]47                    nextTargetStartDate = nextTargetStartDate + recurEvery.days
[136]48                }
49                break
50            case "Week(s)":
51                use(TimeCategory) {
[199]52                    nextTargetStartDate = nextTargetStartDate + recurEvery.weeks
[136]53                }
54                break
55            case "Month(s)":
56                use(TimeCategory) {
[199]57                    nextTargetStartDate = nextTargetStartDate + recurEvery.months
[136]58                }
59                break
60            case "Year(s)":
61                use(TimeCategory) {
[199]62                    nextTargetStartDate = nextTargetStartDate + recurEvery.years
[136]63                }
64                break
65        default:
[199]66                log.error "No case for recurPeriod.period: ${recurPeriod.period}"
[136]67                break
68        }
[199]69    }
70
71    public void setNextGenerationDate() {
72        use(TimeCategory) {
73            nextGenerationDate = nextTargetStartDate - generateAhead.days
74        }
[137]75        def now = new Date()
[136]76        if( nextGenerationDate < now) {nextGenerationDate = now}
[137]77    }
[146]78
[137]79    public void setNextTargetCompletionDate() {
[136]80        switch (taskDurationPeriod.period) {
81            case "Day(s)":
82                use(TimeCategory) {
83                    nextTargetCompletionDate = nextTargetStartDate + taskDuration.days
84                }
85                break
86            case "Week(s)":
87                use(TimeCategory) {
88                    nextTargetCompletionDate = nextTargetStartDate + taskDuration.weeks
89                }
90                break
91            case "Month(s)":
92                use(TimeCategory) {
93                    nextTargetCompletionDate = nextTargetStartDate + taskDuration.months
94                }
95                break
96            case "Year(s)":
97                use(TimeCategory) {
98                    nextTargetCompletionDate = nextTargetStartDate + taskDuration.years
99                }
100                break
[137]101            default:
[199]102                log.error "No case for taskDurationPeriod.period: ${taskDurationPeriod.period}"
[136]103                break
104        }
105    }
[146]106
[121]107}
108
Note: See TracBrowser for help on using the repository browser.