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

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

Two new asset reports: Asset Detail and Asset Register.

File size: 4.4 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        // Inner join used to return only attribTypes that are used by AssetExtendedAttributes.
27        // So the result is only asset extendedAttributeTypes.
28        def attribTypesQ = new HqlBuilder().query {
29            select 'distinct attribT.name'
30            from 'AssetExtendedAttribute attrib',
31                    'join attrib.extendedAttributeType as attribT'
32            order 'by attribT.name asc'
33        }
34        result.attribTypes = Asset.executeQuery(attribTypesQ.query, attribTypesQ.namedParams)
35
36        // A result is returned for every asset and for any extended attributes.
37        def q = new HqlBuilder().query {
38            select 'new map(asset.name as name',
39                        'asset.description as description',
40                        'asset.comment as comment',
41                        'attribT.name as attribType',
42                        'attrib.value as attribValue)'
43            from 'Asset asset',
44                    'left join asset.assetExtendedAttributes as attrib',
45                    'left join attrib.extendedAttributeType as attribT'
46            if(params.section instanceof Section) {
47                namedParams.section = params.section
48                where 'asset.section = :section'
49            }
50            order 'by asset.name asc, attribT.name asc'
51        }
52        def assetResults = Asset.executeQuery(q.query, q.namedParams)
53
54        // Build the report table row for each asset.
55        // Rows are keyed by asset.name and the value is a Map of the attributes.
56        def rows = [:]
57        assetResults.each { assetResult ->
58            // Create row if it does not exist yet.
59            if(!rows.containsKey(assetResult.name)) {
60                rows[assetResult.name] = ['name':assetResult.name,
61                                                            'description':assetResult.description,
62                                                            'comment':assetResult.comment]
63
64                // Add all attribType columns.
65                result.attribTypes.each { column ->
66                    rows[assetResult.name][column] = null
67                }
68            }
69
70            // Assign value to column.
71            rows[assetResult.name][assetResult.attribType] = assetResult.attribValue
72        }
73
74        // The value of each row is the dataList used by the report table.
75        result.dataList = rows.collect {it.value}
76
77        // Success.
78        return result
79
80    } // getAssetRegister
81
82    /**
83    * Selects and returns an asset (or all) and its details.
84    * @param params The request params, may contain params to specify the search.
85    * @param locale The locale to use when generating result.message.
86    */
87    def getAssetDetail(params, locale) {
88        //def result = [:]
89        def result
90
91        //result.summaryOfCalculationMethod = ''
92
93        // A result is returned for every asset and for any extended attributes.
94        // The report then groups by asset.name
95        def q = new HqlBuilder().query {
96            select 'new map(asset.name as name',
97                        'asset.description as description',
98                        'asset.comment as comment',
99                        'attribT.name as attribType',
100                        'attrib.value as attribValue)'
101            from 'Asset asset',
102                    'left join asset.assetExtendedAttributes as attrib',
103                    'left join attrib.extendedAttributeType as attribT'
104            if(params.asset instanceof Asset) {
105                namedParams.asset = params.asset
106                where 'asset = :asset'
107            }
108            order 'by asset.name asc, attribT.name asc'
109        }
110
111        // result.dataList = Asset.list()
112        result = Asset.executeQuery(q.query, q.namedParams)
113
114        // Success.
115        return result
116
117    } // getAssetDetail
118
119} // end class
Note: See TracBrowser for help on using the repository browser.