[774] | 1 | /** |
---|
| 2 | * Provides a service class for the TaskProcedure domain class. |
---|
| 3 | */ |
---|
| 4 | class TaskProcedureService { |
---|
| 5 | |
---|
| 6 | boolean transactional = false |
---|
| 7 | |
---|
| 8 | def authService |
---|
| 9 | |
---|
| 10 | /** |
---|
| 11 | * Updates an existing taskProcedure. |
---|
| 12 | * @param params The params to update for taskProcedure with id of params.id. |
---|
| 13 | * @returns A map containing result.error (if any error) and result.taskProcedureInstance (if available). |
---|
| 14 | */ |
---|
| 15 | def update(params) { |
---|
| 16 | TaskProcedure.withTransaction { status -> |
---|
| 17 | def result = [:] |
---|
| 18 | |
---|
| 19 | def fail = { Map m -> |
---|
| 20 | status.setRollbackOnly() |
---|
| 21 | if(result.taskProcedureInstance && m.field) |
---|
| 22 | result.taskProcedureInstance.errors.rejectValue(m.field, m.code) |
---|
| 23 | result.error = [ code: m.code, args: ["TaskProcedure", params.id] ] |
---|
[775] | 24 | // Fetch to prevent lazy initialization error. |
---|
| 25 | result.taskProcedureInstance?.linkedTask.primaryAsset |
---|
| 26 | result.taskProcedureInstance?.createdBy |
---|
[774] | 27 | return result |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | result.taskProcedureInstance = TaskProcedure.get(params.id) |
---|
| 31 | |
---|
| 32 | if(!result.taskProcedureInstance) |
---|
| 33 | return fail(code:"default.not.found") |
---|
| 34 | |
---|
| 35 | // Optimistic locking check. |
---|
| 36 | if(params.version) { |
---|
| 37 | if(result.taskProcedureInstance.version > params.version.toLong()) |
---|
| 38 | return fail(field:"version", code:"default.optimistic.locking.failure") |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | result.taskProcedureInstance.properties = params |
---|
| 42 | result.taskProcedureInstance.lastUpdatedBy = authService.currentUser |
---|
| 43 | result.taskProcedureInstance.lastUpdated = new Date() // Required to trigger version increment. |
---|
| 44 | |
---|
| 45 | // Gaps in the html index's can be created by adding 2 items and removing the first one. |
---|
| 46 | // This creates a gap at the missing index where LazyList will return a null. |
---|
| 47 | def nullMaintenanceActions = result.taskProcedureInstance.maintenanceActions.findAll {!it} |
---|
| 48 | if (nullMaintenanceActions) { |
---|
| 49 | result.taskProcedureInstance.maintenanceActions.removeAll(nullMaintenanceActions) |
---|
| 50 | } |
---|
[793] | 51 | def nullDocumentReferences = result.taskProcedureInstance.documentReferences.findAll {!it} |
---|
| 52 | if (nullDocumentReferences) { |
---|
| 53 | result.taskProcedureInstance.documentReferences.removeAll(nullDocumentReferences) |
---|
| 54 | } |
---|
[774] | 55 | |
---|
| 56 | // Save for restoration if validation fails. |
---|
| 57 | def savedMaintenanceActions = new ArrayList(result.taskProcedureInstance.maintenanceActions) |
---|
[793] | 58 | def savedDocumentReferences = new ArrayList(result.taskProcedureInstance.documentReferences) |
---|
[774] | 59 | |
---|
| 60 | // Remove toBeDeleted before validation. |
---|
[793] | 61 | def ma_toBeDeleted = result.taskProcedureInstance.maintenanceActions.findAll {it.toBeDeleted} |
---|
| 62 | if (ma_toBeDeleted) { |
---|
| 63 | result.taskProcedureInstance.maintenanceActions.removeAll(ma_toBeDeleted) |
---|
[774] | 64 | } |
---|
[793] | 65 | def docRef_toBeDeleted = result.taskProcedureInstance.documentReferences.findAll {it.toBeDeleted} |
---|
| 66 | if (docRef_toBeDeleted) { |
---|
| 67 | result.taskProcedureInstance.documentReferences.removeAll(docRef_toBeDeleted) |
---|
| 68 | } |
---|
[774] | 69 | |
---|
| 70 | if(result.taskProcedureInstance.hasErrors() || !result.taskProcedureInstance.save()) { |
---|
| 71 | // Restore the saved items, some of which contain toBeDeleted flags but |
---|
| 72 | // have not been deleted yet since validation failed. |
---|
| 73 | // The toBeDeleted items are hidden in the view. |
---|
| 74 | result.taskProcedureInstance.maintenanceActions = savedMaintenanceActions |
---|
[793] | 75 | result.taskProcedureInstance.documentReferences = savedDocumentReferences |
---|
| 76 | // Populate collection errors for display. |
---|
[774] | 77 | result.taskProcedureInstance.maintenanceActions.each { it.validate() } |
---|
[793] | 78 | result.taskProcedureInstance.documentReferences.each { it.validate() } |
---|
[774] | 79 | return fail(code:"default.update.failure") |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | // Success. |
---|
| 83 | return result |
---|
| 84 | |
---|
| 85 | } //end withTransaction |
---|
| 86 | } // end update() |
---|
| 87 | |
---|
[775] | 88 | /** |
---|
| 89 | * Creates a new taskProcedure with the given params. |
---|
| 90 | * @param params The params to use when creating the new taskProcedure. |
---|
| 91 | * @returns A map containing result.error (if any error) and result.taskProcedure. |
---|
| 92 | */ |
---|
| 93 | def save(params) { |
---|
| 94 | def result = [:] |
---|
| 95 | TaskProcedure.withTransaction { status -> |
---|
| 96 | def fail = { Map m -> |
---|
| 97 | status.setRollbackOnly() |
---|
| 98 | if(result.taskProcedureInstance && m.field) |
---|
| 99 | result.taskProcedureInstance.errors.rejectValue(m.field, m.code) |
---|
| 100 | result.error = [ code: m.code, args: ["TaskProcedure", params.id] ] |
---|
| 101 | // Fetch to prevent lazy initialization error. |
---|
| 102 | result.taskProcedureInstance.linkedTask.primaryAsset |
---|
| 103 | return result |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | result.taskProcedureInstance = new TaskProcedure(params) |
---|
| 107 | |
---|
| 108 | // Optimistic locking check on linkedTask. |
---|
| 109 | if(result.taskProcedureInstance.linkedTask.taskProcedure) |
---|
| 110 | return fail(field:"version", code:"default.optimistic.locking.failure") |
---|
| 111 | |
---|
| 112 | result.taskProcedureInstance.createdBy = authService.currentUser |
---|
| 113 | result.taskProcedureInstance.lastUpdatedBy = authService.currentUser |
---|
| 114 | |
---|
| 115 | // Gaps in the html index's can be created by adding 2 items and removing the first one. |
---|
| 116 | // This creates a gap at the missing index where LazyList will return a null. |
---|
| 117 | def nullMaintenanceActions = result.taskProcedureInstance.maintenanceActions.findAll {!it} |
---|
| 118 | if (nullMaintenanceActions) { |
---|
| 119 | result.taskProcedureInstance.maintenanceActions.removeAll(nullMaintenanceActions) |
---|
| 120 | } |
---|
[793] | 121 | def nullDocumentReferences = result.taskProcedureInstance.documentReferences.findAll {!it} |
---|
| 122 | if (nullDocumentReferences) { |
---|
| 123 | result.taskProcedureInstance.documentReferences.removeAll(nullDocumentReferences) |
---|
| 124 | } |
---|
[775] | 125 | |
---|
| 126 | // Also sets: taskInstance.taskProcedure = taskProcedureInstance |
---|
| 127 | result.taskProcedureInstance.addToTasks(result.taskProcedureInstance.linkedTask) |
---|
| 128 | |
---|
| 129 | if(result.taskProcedureInstance.hasErrors() || !result.taskProcedureInstance.save()) { |
---|
[793] | 130 | // Populate collection errors for display. |
---|
[775] | 131 | result.taskProcedureInstance.maintenanceActions.each { it.validate() } |
---|
[793] | 132 | result.taskProcedureInstance.documentReferences.each { it.validate() } |
---|
[775] | 133 | return fail(code:"default.create.failure") |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | } //end withTransaction |
---|
| 137 | |
---|
| 138 | result.taskProcedureInstance.lastUpdated = new Date() // Required to trigger version increment to 1. |
---|
| 139 | |
---|
| 140 | // success |
---|
| 141 | return result |
---|
| 142 | } // end save() |
---|
| 143 | |
---|
[774] | 144 | } // end class |
---|