source: branches/features/taskProcedureRework/grails-app/services/TaskProcedureService.groovy @ 816

Last change on this file since 816 was 793, checked in by gav, 14 years ago

Domain change, add DocumentReference.

File size: 6.8 KB
Line 
1/**
2* Provides a service class for the TaskProcedure domain class.
3*/
4class 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            def nullDocumentReferences = result.taskProcedureInstance.documentReferences.findAll {!it}
52            if (nullDocumentReferences) {
53                result.taskProcedureInstance.documentReferences.removeAll(nullDocumentReferences)
54            }
55
56            // Save for restoration if validation fails.
57            def savedMaintenanceActions = new ArrayList(result.taskProcedureInstance.maintenanceActions)
58            def savedDocumentReferences = new ArrayList(result.taskProcedureInstance.documentReferences)
59
60            // Remove toBeDeleted before validation.
61            def ma_toBeDeleted = result.taskProcedureInstance.maintenanceActions.findAll {it.toBeDeleted}
62            if (ma_toBeDeleted) {
63                result.taskProcedureInstance.maintenanceActions.removeAll(ma_toBeDeleted)
64            }
65            def docRef_toBeDeleted = result.taskProcedureInstance.documentReferences.findAll {it.toBeDeleted}
66            if (docRef_toBeDeleted) {
67                result.taskProcedureInstance.documentReferences.removeAll(docRef_toBeDeleted)
68            }
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
75                result.taskProcedureInstance.documentReferences = savedDocumentReferences
76                // Populate collection errors for display.
77                result.taskProcedureInstance.maintenanceActions.each { it.validate() }
78                result.taskProcedureInstance.documentReferences.each { it.validate() }
79                return fail(code:"default.update.failure")
80            }
81
82            // Success.
83            return result
84
85        } //end withTransaction
86    }  // end update()
87
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            }
121            def nullDocumentReferences = result.taskProcedureInstance.documentReferences.findAll {!it}
122            if (nullDocumentReferences) {
123                result.taskProcedureInstance.documentReferences.removeAll(nullDocumentReferences)
124            }
125
126            // Also sets: taskInstance.taskProcedure = taskProcedureInstance
127            result.taskProcedureInstance.addToTasks(result.taskProcedureInstance.linkedTask)
128
129            if(result.taskProcedureInstance.hasErrors() || !result.taskProcedureInstance.save()) {
130                // Populate collection errors for display.
131                result.taskProcedureInstance.maintenanceActions.each { it.validate() }
132                result.taskProcedureInstance.documentReferences.each { it.validate() }
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
144} // end class
Note: See TracBrowser for help on using the repository browser.