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

Last change on this file since 477 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
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    Integer maxSubTasks = 0
15    Date nextGenerationDate = new Date()
16    Date nextTargetStartDate = new Date()+1
17    Date nextTargetCompletionDate = new Date()
18    boolean enabled = true
19    boolean useTargetCompletionDate = false
20
21//     static hasMany = []
22
23    static belongsTo = [task: Task]
24
25    static constraints = {
26        recurEvery(min:1, max:365)
27        taskDuration(min:0, max:365)
28        generateAhead(min:0, max:62)
29        lastGeneratedSubTask(nullable:true)
30    }
31
32    String toString() {
33        "Recur every ${recurEvery} ${recurPeriod}"
34    }
35
36    // As of Grails 1.1.1 this does not fire/pass before validation.
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.
39    def beforeInsert = {
40        setNextGenerationDate()
41        setNextTargetCompletionDate()
42    }
43
44    public void setNextTargetStartDate() {
45        switch (recurPeriod.period) {
46            case "Day(s)":
47                use(TimeCategory) {
48                    nextTargetStartDate = nextTargetStartDate + recurEvery.days
49                }
50                break
51            case "Week(s)":
52                use(TimeCategory) {
53                    nextTargetStartDate = nextTargetStartDate + recurEvery.weeks
54                }
55                break
56            case "Month(s)":
57                use(TimeCategory) {
58                    nextTargetStartDate = nextTargetStartDate + recurEvery.months
59                }
60                break
61            case "Year(s)":
62                use(TimeCategory) {
63                    nextTargetStartDate = nextTargetStartDate + recurEvery.years
64                }
65                break
66        default:
67                log.error "No case for recurPeriod.period: ${recurPeriod.period}"
68                break
69        }
70    }
71
72    public void setNextGenerationDate() {
73        use(TimeCategory) {
74            nextGenerationDate = nextTargetStartDate - generateAhead.days
75        }
76        def now = new Date()
77        if( nextGenerationDate < now) {nextGenerationDate = now}
78    }
79
80    public void setNextTargetCompletionDate() {
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
102            default:
103                log.error "No case for taskDurationPeriod.period: ${taskDurationPeriod.period}"
104                break
105        }
106    }
107
108}
109
Note: See TracBrowser for help on using the repository browser.