source: trunk/grails-app/services/AssetReportService.groovy @ 681

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

Improvements to assetRegister report, adding site and section to page header.

File size: 9.7 KB
Line 
1import net.kromhouts.HqlBuilder
2
3/**
4* Service class that encapsulates the business logic for Asset Reports.
5*/
6class AssetReportService {
7
8    boolean transactional = false
9
10    def authService
11    def dateUtilService
12//     def messageSource
13
14    def g = new org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib()
15
16    def paramsMax = 100000
17
18    /**
19    * Selects and returns the assets and their details.
20    * @param params The request params, may contain params to specify the search.
21    * @param locale The locale to use when generating result.message.
22    */
23    def getAssetRegister(params, locale) {
24        def result = [:]
25
26        result.section = Section.get(params.section.id.toLong())
27        result.site = result.section.site
28
29        // Inner join used to return only attribTypes that are used by AssetExtendedAttributes.
30        // So the result is only asset extendedAttributeTypes.
31        def attribTypesQ = new HqlBuilder().query {
32            select 'distinct attribT.name'
33            from 'AssetExtendedAttribute attrib',
34                    'join attrib.extendedAttributeType as attribT'
35            order 'by attribT.name asc'
36        }
37        result.attribTypes = Asset.executeQuery(attribTypesQ.query, attribTypesQ.namedParams)
38
39        // A result is returned for every asset and for any extended attributes.
40        def q = new HqlBuilder().query {
41            select 'new map(asset.name as name',
42                        'asset.description as description',
43                        'asset.comment as comment',
44                        'attribT.name as attribType',
45                        'attrib.value as attribValue)'
46            from 'Asset asset',
47                    'left join asset.assetExtendedAttributes as attrib',
48                    'left join attrib.extendedAttributeType as attribT'
49            where 'asset.section = :section'
50                    namedParams.section = result.section
51            order 'by asset.name asc, attribT.name asc'
52        }
53        def assetResults = Asset.executeQuery(q.query, q.namedParams)
54
55        // Build the report table row for each asset.
56        // Rows are keyed by asset.name and the value is a Map of the attributes.
57        def rows = [:]
58        assetResults.each { assetResult ->
59            // Create row if it does not exist yet.
60            if(!rows.containsKey(assetResult.name)) {
61                rows[assetResult.name] = ['name':assetResult.name,
62                                                            'description':assetResult.description,
63                                                            'comment':assetResult.comment]
64
65                // Add all attribType columns.
66                result.attribTypes.each { column ->
67                    rows[assetResult.name][column] = '-'
68                }
69            }
70
71            // Assign value to column.
72            rows[assetResult.name][assetResult.attribType] = assetResult.attribValue
73        }
74
75        // The value of each row is the dataList used by the report table.
76        result.dataList = rows.collect {it.value}
77
78        // Success.
79        return result
80
81    } // getAssetRegister
82
83    /**
84    * Selects and returns an asset (or all) and its details.
85    * @param params The request params, may contain params to specify the search.
86    * @param locale The locale to use when generating result.message.
87    */
88    def getAssetDetail(params, locale) {
89        //def result = [:]
90        def result
91
92        //result.summaryOfCalculationMethod = ''
93
94        // A result is returned for every asset and for any extended attributes.
95        // The report then groups by asset.name
96        def q = new HqlBuilder().query {
97            select 'new map(asset.name as name',
98                        'asset.description as description',
99                        'asset.comment as comment',
100                        'attribT.name as attribType',
101                        'attrib.value as attribValue)'
102            from 'Asset asset',
103                    'left join asset.assetExtendedAttributes as attrib',
104                    'left join attrib.extendedAttributeType as attribT'
105            if(params.asset instanceof Asset) {
106                namedParams.asset = params.asset
107                where 'asset = :asset'
108            }
109            order 'by asset.name asc, attribT.name asc'
110        }
111
112        // result.dataList = Asset.list()
113        result = Asset.executeQuery(q.query, q.namedParams)
114
115        // Success.
116        return result
117
118    } // getAssetDetail
119
120    /**
121    * Selects and returns level 1 sub items (aka machines or equipment) and their details.
122    * @param params The request params, may contain params to specify the search.
123    * @param locale The locale to use when generating result.message.
124    */
125    def getEquipmentRegister(params, locale) {
126        def result = [:]
127
128        result.section = Section.get(params.section.id.toLong())
129        result.site = result.section.site
130
131        // Inner join used to return only attribTypes that are used by AssetSubItemExtendedAttributes.
132        // So the result is only assetSubItem extendedAttributeTypes.
133        def attribTypesQ = new HqlBuilder().query {
134            select 'distinct attribT.name'
135            from 'AssetSubItemExtendedAttribute attrib',
136                    'join attrib.extendedAttributeType as attribT'
137            order 'by attribT.name asc'
138        }
139        result.attribTypes = Asset.executeQuery(attribTypesQ.query, attribTypesQ.namedParams)
140
141        // A useful list of assets without subItems to be given to the user.
142        def assetsWithoutEquipmentQ = new HqlBuilder().query {
143            select 'distinct asset'
144            from 'Asset asset',
145                    'left join asset.assetSubItems as assetSubItem'
146            where 'assetSubItem = null'
147                namedParams.section = result.section
148                and 'asset.section = :section'
149        }
150        result.assetsWithoutEquipment = AssetSubItem.executeQuery(assetsWithoutEquipmentQ.query, assetsWithoutEquipmentQ.namedParams)
151
152        // A result is returned for every level 1 assetSubItem and for any extended attributes.
153        def q = new HqlBuilder().query {
154            select 'new map(asset.name as assetName',
155                        'assetSubItem.name as name',
156                        'assetSubItem.description as description',
157                        'assetSubItem.comment as comment',
158                        'attribT.name as attribType',
159                        'attrib.value as attribValue)'
160            from 'AssetSubItem assetSubItem',
161                    'inner join assetSubItem.assets as asset',
162                    'left join assetSubItem.assetSubItemExtendedAttributes as attrib',
163                    'left join attrib.extendedAttributeType as attribT'
164            where 'asset != null' // ensure that only level 1 assetSubItems are returned.
165                namedParams.section = result.section
166                and 'asset.section = :section'
167            order 'by asset.name asc, assetSubItem.name asc, attribT.name asc'
168        }
169        def equipmentResults = AssetSubItem.executeQuery(q.query, q.namedParams)
170
171        // A result is returned for every asset and for any extended attributes.
172        def assetResultsQ = new HqlBuilder().query {
173            select 'new map(asset.name as assetName',
174                        "'   Asset Details' as name", // Place holder 'equipment' name, 3 leading spaces for sorting.
175                        'asset.description as description',
176                        'asset.comment as comment',
177                        'attribT.name as attribType',
178                        'attrib.value as attribValue)'
179            from 'Asset asset',
180                    'left join asset.assetExtendedAttributes as attrib',
181                    'left join attrib.extendedAttributeType as attribT'
182            where 'asset.section = :section'
183                    namedParams.section = result.section
184            order 'by asset.name asc, attribT.name asc'
185        }
186        def assetResults = Asset.executeQuery(assetResultsQ.query, assetResultsQ.namedParams)
187
188        // Add asset details to equipmentResults.
189        equipmentResults.addAll(assetResults)
190        equipmentResults.sort { p1, p2 -> p1.assetName.compareToIgnoreCase(p2.assetName) ?: p1.name.compareToIgnoreCase(p2.name) }
191
192        // Build the report table rows.
193        // Rows are keyed by equipmentResult.assetName+equipmentResult.name` while the value is a Map of the attributes.
194        // The report table then groups by assetName.
195        def rows = [:]
196        equipmentResults.each { equipmentResult ->
197            // Create row if it does not exist yet.
198            def rowKey = equipmentResult.assetName+equipmentResult.name
199            if(!rows.containsKey(rowKey)) {
200                rows[rowKey] = ['assetName': equipmentResult.assetName,
201                                                            'name':equipmentResult.name,
202                                                            'description':equipmentResult.description,
203                                                            'comment':equipmentResult.comment]
204
205                // Add all attribType columns.
206                result.attribTypes.each { column ->
207                    rows[rowKey][column] = '-'
208                }
209            }
210
211            // Assign value to column.
212            rows[rowKey][equipmentResult.attribType] = equipmentResult.attribValue
213        }
214
215        // The value of each row is the dataList used by the report table.
216        result.dataList = rows.collect {it.value}
217        // Print formatting, since we are done with these as objects.
218        result.attribTypes = result.attribTypes.join(', ')
219        result.assetsWithoutEquipment = result.assetsWithoutEquipment.collect {it.name}.join(', ')
220
221        // Success.
222        return result
223
224    } // getEquipmentRegister
225
226} // end class
Note: See TracBrowser for help on using the repository browser.