1 | class TaskRecurringScheduleService { |
---|
2 | |
---|
3 | boolean transactional = false |
---|
4 | |
---|
5 | def dateUtilService |
---|
6 | def taskService |
---|
7 | |
---|
8 | /** |
---|
9 | * Generate all enabled recurring tasks. |
---|
10 | */ |
---|
11 | def generateAll() { |
---|
12 | |
---|
13 | def taskRecurringScheduleList = TaskRecurringSchedule.findAllByEnabled(true) |
---|
14 | |
---|
15 | taskRecurringScheduleList.each() { |
---|
16 | |
---|
17 | if ( dateUtilService.getTomorrow() > it.nextGenerationDate) { |
---|
18 | def p = [:] |
---|
19 | p.targetStartDate = it.nextTargetStartDate |
---|
20 | p.targetCompletionDate = it.nextTargetCompletionDate |
---|
21 | if(it.task.taskProcedure) p.taskProcedure = it.task.taskProcedure |
---|
22 | def result = taskService.createSubTask(it.task, p) |
---|
23 | if( !result.error ) { |
---|
24 | it.lastGeneratedSubTask = result.taskInstance |
---|
25 | it.subTasksGenerated++ |
---|
26 | it.setNextTargetStartDate() |
---|
27 | it.setNextGenerationDate() |
---|
28 | it.setNextTargetCompletionDate() |
---|
29 | } |
---|
30 | else { |
---|
31 | log.error "Sub task generation for recurring schedule ${it.id} failed." |
---|
32 | log.error result.taskInstance.errors |
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | /** |
---|
40 | * Creates a new recurring schedule for a task with the given params. |
---|
41 | * @param params The params to use when creating the new recurring schedule. |
---|
42 | * @returns A map containing result.error=true (if any error) and result.taskRecurringScheduleInstance and result.taskId. |
---|
43 | */ |
---|
44 | def create(params) { |
---|
45 | TaskRecurringSchedule.withTransaction { status -> |
---|
46 | def result = [:] |
---|
47 | |
---|
48 | def fail = { Object[] args -> |
---|
49 | status.setRollbackOnly() |
---|
50 | if(args.size() == 2) result.taskRecurringScheduleInstance.errors.rejectValue(args[0], args[1]) |
---|
51 | result.error = true |
---|
52 | return result |
---|
53 | } |
---|
54 | |
---|
55 | result.taskRecurringScheduleInstance = new TaskRecurringSchedule(params) |
---|
56 | result.taskId = result.taskRecurringScheduleInstance.task.id |
---|
57 | |
---|
58 | if(!result.taskRecurringScheduleInstance.validate()) |
---|
59 | return fail() |
---|
60 | |
---|
61 | def taskInstance = Task.lock(result.taskId) |
---|
62 | |
---|
63 | if(!taskInstance) |
---|
64 | return fail('task', "task.notFound") |
---|
65 | |
---|
66 | if(taskInstance.taskRecurringSchedule) |
---|
67 | return fail('task', "tast.taskRecurringSchedule.alreadyExists") |
---|
68 | |
---|
69 | if(taskInstance.taskStatus.id == 3) |
---|
70 | return fail('task', "task.operationNotPermittedOnCompleteTask") |
---|
71 | |
---|
72 | if(taskInstance.trash) |
---|
73 | return fail('task', "task.operationNotPermittedOnTaskInTrash") |
---|
74 | |
---|
75 | if(result.taskRecurringScheduleInstance.nextTargetStartDate < dateUtilService.getToday()) |
---|
76 | return fail("nextTargetStartDate", "taskRecurring.nextTargetStartDate.NotInTheFuture") |
---|
77 | |
---|
78 | taskInstance.taskRecurringSchedule = result.taskRecurringScheduleInstance |
---|
79 | |
---|
80 | if(!result.taskRecurringScheduleInstance.save() || !taskInstance.save()) |
---|
81 | return fail() |
---|
82 | |
---|
83 | // All went well if we get to here. |
---|
84 | return result |
---|
85 | |
---|
86 | } //end withTransaction |
---|
87 | } // end create() |
---|
88 | |
---|
89 | } // end of class |
---|