[137] | 1 | class TaskService { |
---|
| 2 | |
---|
[180] | 3 | boolean transactional = false |
---|
[137] | 4 | |
---|
[143] | 5 | def dateUtilService |
---|
[180] | 6 | def authenticateService |
---|
[137] | 7 | |
---|
[180] | 8 | def create(params) { |
---|
| 9 | Task.withTransaction { status -> |
---|
| 10 | def result = [:] |
---|
| 11 | def taskInstance = new Task(params) |
---|
| 12 | result.taskInstance = taskInstance |
---|
| 13 | |
---|
| 14 | // Get person in a safe way to avoid a null userDomain during bootstrap. |
---|
| 15 | def userDomain = authenticateService.userDomain() |
---|
| 16 | def person = userDomain ? Person.get(userDomain.id) : Person.get(1) |
---|
| 17 | |
---|
| 18 | if(taskInstance.save()) { |
---|
| 19 | def taskModification = new TaskModification(person:person, |
---|
| 20 | taskModificationType: TaskModificationType.get(1), |
---|
| 21 | task: taskInstance) |
---|
| 22 | |
---|
| 23 | if(!taskModification.save()) { |
---|
| 24 | status.setRollbackOnly() |
---|
| 25 | taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave") |
---|
| 26 | result.error = true |
---|
| 27 | return result |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | return result |
---|
| 31 | } |
---|
| 32 | else { |
---|
| 33 | result.error = true |
---|
| 34 | return result |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | } //end withTransaction |
---|
| 38 | } // end create() |
---|
| 39 | |
---|
| 40 | def start() { |
---|
| 41 | //TaskModificationType.get(2) |
---|
| 42 | } // end start() |
---|
| 43 | |
---|
| 44 | def update(params) { |
---|
| 45 | Task.withTransaction { status -> |
---|
| 46 | def result = [:] |
---|
| 47 | result.taskInstance = Task.get(params.id) |
---|
| 48 | if(result.taskInstance) { |
---|
| 49 | |
---|
| 50 | // Optimistic locking check. |
---|
| 51 | if(params.version) { |
---|
| 52 | def version = params.version.toLong() |
---|
| 53 | if(result.taskInstance.version > version) { |
---|
| 54 | status.setRollbackOnly() |
---|
| 55 | result.taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.") |
---|
| 56 | result.error = true |
---|
| 57 | return result |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | result.taskInstance.properties = params |
---|
| 63 | |
---|
| 64 | if(result.taskInstance.save()) { |
---|
| 65 | def taskModification = new TaskModification(person:Person.get(authenticateService.userDomain().id), |
---|
| 66 | taskModificationType: TaskModificationType.get(3), |
---|
| 67 | task: result.taskInstance) |
---|
| 68 | if(taskModification.save()) { |
---|
| 69 | // All went well. |
---|
| 70 | return result |
---|
| 71 | } |
---|
| 72 | else { |
---|
| 73 | status.setRollbackOnly() |
---|
| 74 | result.taskInstance.errors.rejectValue("taskModifications", "task.modifications.failedToSave") |
---|
| 75 | result.error = true |
---|
| 76 | return result |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | // Something failed. |
---|
| 81 | status.setRollbackOnly() |
---|
| 82 | result.error = true |
---|
| 83 | return result |
---|
| 84 | |
---|
| 85 | } //end withTransaction |
---|
| 86 | } // end update() |
---|
| 87 | |
---|
| 88 | def complete() { |
---|
| 89 | //TaskModificationType.get(4) |
---|
| 90 | } // end complete() |
---|
| 91 | |
---|
| 92 | def reopen() { |
---|
| 93 | //TaskModificationType.get(5) |
---|
| 94 | } // end reopen() |
---|
| 95 | |
---|
| 96 | def trash() { |
---|
| 97 | //TaskModificationType.get(6) |
---|
| 98 | } // end trash() |
---|
| 99 | |
---|
| 100 | def restore() { |
---|
| 101 | //TaskModificationType.get(7) |
---|
| 102 | } // end restore() |
---|
| 103 | |
---|
| 104 | def approve() { |
---|
| 105 | //TaskModificationType.get(8) |
---|
| 106 | } // end approve() |
---|
| 107 | |
---|
| 108 | def renegeApproval() { |
---|
| 109 | //TaskModificationType.get(9) |
---|
| 110 | } // end renegeApproval() |
---|
| 111 | |
---|
| 112 | } // end TaskService |
---|