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] ] |
---|
24 | // Fetch to prevent lazy initialization error. |
---|
25 | result.taskProcedureInstance?.linkedTask.primaryAsset |
---|
26 | result.taskProcedureInstance?.createdBy |
---|
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 | } |
---|
51 | |
---|
52 | // Save for restoration if validation fails. |
---|
53 | def savedMaintenanceActions = new ArrayList(result.taskProcedureInstance.maintenanceActions) |
---|
54 | |
---|
55 | // Remove toBeDeleted before validation. |
---|
56 | def toBeDeleted = result.taskProcedureInstance.maintenanceActions.findAll {it.toBeDeleted} |
---|
57 | if (toBeDeleted) { |
---|
58 | result.taskProcedureInstance.maintenanceActions.removeAll(toBeDeleted) |
---|
59 | } |
---|
60 | |
---|
61 | if(result.taskProcedureInstance.hasErrors() || !result.taskProcedureInstance.save()) { |
---|
62 | // Restore the saved items, some of which contain toBeDeleted flags but |
---|
63 | // have not been deleted yet since validation failed. |
---|
64 | // The toBeDeleted items are hidden in the view. |
---|
65 | result.taskProcedureInstance.maintenanceActions = savedMaintenanceActions |
---|
66 | // Populate maintenanceAction errors for display. |
---|
67 | result.taskProcedureInstance.maintenanceActions.each { it.validate() } |
---|
68 | return fail(code:"default.update.failure") |
---|
69 | } |
---|
70 | |
---|
71 | // Success. |
---|
72 | return result |
---|
73 | |
---|
74 | } //end withTransaction |
---|
75 | } // end update() |
---|
76 | |
---|
77 | /** |
---|
78 | * Creates a new taskProcedure with the given params. |
---|
79 | * @param params The params to use when creating the new taskProcedure. |
---|
80 | * @returns A map containing result.error (if any error) and result.taskProcedure. |
---|
81 | */ |
---|
82 | def save(params) { |
---|
83 | def result = [:] |
---|
84 | TaskProcedure.withTransaction { status -> |
---|
85 | def fail = { Map m -> |
---|
86 | status.setRollbackOnly() |
---|
87 | if(result.taskProcedureInstance && m.field) |
---|
88 | result.taskProcedureInstance.errors.rejectValue(m.field, m.code) |
---|
89 | result.error = [ code: m.code, args: ["TaskProcedure", params.id] ] |
---|
90 | // Fetch to prevent lazy initialization error. |
---|
91 | result.taskProcedureInstance.linkedTask.primaryAsset |
---|
92 | return result |
---|
93 | } |
---|
94 | |
---|
95 | result.taskProcedureInstance = new TaskProcedure(params) |
---|
96 | |
---|
97 | // Optimistic locking check on linkedTask. |
---|
98 | if(result.taskProcedureInstance.linkedTask.taskProcedure) |
---|
99 | return fail(field:"version", code:"default.optimistic.locking.failure") |
---|
100 | |
---|
101 | result.taskProcedureInstance.createdBy = authService.currentUser |
---|
102 | result.taskProcedureInstance.lastUpdatedBy = authService.currentUser |
---|
103 | |
---|
104 | // Gaps in the html index's can be created by adding 2 items and removing the first one. |
---|
105 | // This creates a gap at the missing index where LazyList will return a null. |
---|
106 | def nullMaintenanceActions = result.taskProcedureInstance.maintenanceActions.findAll {!it} |
---|
107 | if (nullMaintenanceActions) { |
---|
108 | result.taskProcedureInstance.maintenanceActions.removeAll(nullMaintenanceActions) |
---|
109 | } |
---|
110 | |
---|
111 | // Also sets: taskInstance.taskProcedure = taskProcedureInstance |
---|
112 | result.taskProcedureInstance.addToTasks(result.taskProcedureInstance.linkedTask) |
---|
113 | |
---|
114 | if(result.taskProcedureInstance.hasErrors() || !result.taskProcedureInstance.save()) { |
---|
115 | // Populate maintenanceAction errors for display. |
---|
116 | result.taskProcedureInstance.maintenanceActions.each { it.validate() } |
---|
117 | return fail(code:"default.create.failure") |
---|
118 | } |
---|
119 | |
---|
120 | } //end withTransaction |
---|
121 | |
---|
122 | result.taskProcedureInstance.lastUpdated = new Date() // Required to trigger version increment to 1. |
---|
123 | |
---|
124 | // success |
---|
125 | return result |
---|
126 | } // end save() |
---|
127 | |
---|
128 | } // end class |
---|