1 | import net.kromhouts.HqlBuilder |
---|
2 | |
---|
3 | /** |
---|
4 | * Service class that encapsulates the business logic for Asset Reports. |
---|
5 | */ |
---|
6 | class 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 | /** |
---|
120 | * Selects and returns level 1 sub items (aka machines or equipment) and their details. |
---|
121 | * @param params The request params, may contain params to specify the search. |
---|
122 | * @param locale The locale to use when generating result.message. |
---|
123 | */ |
---|
124 | def getEquipmentRegister(params, locale) { |
---|
125 | def result = [:] |
---|
126 | |
---|
127 | // Inner join used to return only attribTypes that are used by AssetSubItemExtendedAttributes. |
---|
128 | // So the result is only assetSubItem extendedAttributeTypes. |
---|
129 | def attribTypesQ = new HqlBuilder().query { |
---|
130 | select 'distinct attribT.name' |
---|
131 | from 'AssetSubItemExtendedAttribute attrib', |
---|
132 | 'join attrib.extendedAttributeType as attribT' |
---|
133 | order 'by attribT.name asc' |
---|
134 | } |
---|
135 | result.attribTypes = Asset.executeQuery(attribTypesQ.query, attribTypesQ.namedParams) |
---|
136 | |
---|
137 | // Since there will be nothing to show in the report table for assets without level 1 assetSubItems, |
---|
138 | // a list needs to be given to the user. |
---|
139 | def assetsWithoutEquipmentQ = new HqlBuilder().query { |
---|
140 | select 'distinct asset' |
---|
141 | from 'Asset asset', |
---|
142 | 'left join asset.assetSubItems as assetSubItem' |
---|
143 | where 'assetSubItem = null' |
---|
144 | } |
---|
145 | result.assetsWithoutEquipment = AssetSubItem.executeQuery(assetsWithoutEquipmentQ.query, assetsWithoutEquipmentQ.namedParams) |
---|
146 | |
---|
147 | // A result is returned for every level 1 assetSubItem and for any extended attributes. |
---|
148 | def q = new HqlBuilder().query { |
---|
149 | select 'new map(asset.name as assetName', |
---|
150 | 'assetSubItem.name as name', |
---|
151 | 'assetSubItem.description as description', |
---|
152 | 'assetSubItem.comment as comment', |
---|
153 | 'attribT.name as attribType', |
---|
154 | 'attrib.value as attribValue)' |
---|
155 | from 'AssetSubItem assetSubItem', |
---|
156 | 'inner join assetSubItem.assets as asset', |
---|
157 | 'left join assetSubItem.assetSubItemExtendedAttributes as attrib', |
---|
158 | 'left join attrib.extendedAttributeType as attribT' |
---|
159 | where 'asset != null' // ensure that only level 1 assetSubItems are returned. |
---|
160 | if(params.section instanceof Section) { |
---|
161 | namedParams.section = params.section |
---|
162 | and 'asset.section = :section' |
---|
163 | } |
---|
164 | order 'by asset.name asc, assetSubItem.name asc, attribT.name asc' |
---|
165 | } |
---|
166 | def equipmentResults = AssetSubItem.executeQuery(q.query, q.namedParams) |
---|
167 | |
---|
168 | // Build the report table rows. |
---|
169 | // Rows are keyed by equipmentResult.assetName+equipmentResult.name` while the value is a Map of the attributes. |
---|
170 | // The report table then groups by assetName. |
---|
171 | def rows = [:] |
---|
172 | equipmentResults.each { equipmentResult -> |
---|
173 | // Create row if it does not exist yet. |
---|
174 | def rowKey = equipmentResult.assetName+equipmentResult.name |
---|
175 | if(!rows.containsKey(rowKey)) { |
---|
176 | rows[rowKey] = ['assetName': equipmentResult.assetName, |
---|
177 | 'name':equipmentResult.name, |
---|
178 | 'description':equipmentResult.description, |
---|
179 | 'comment':equipmentResult.comment] |
---|
180 | |
---|
181 | // Add all attribType columns. |
---|
182 | result.attribTypes.each { column -> |
---|
183 | rows[rowKey][column] = null |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | // Assign value to column. |
---|
188 | rows[rowKey][equipmentResult.attribType] = equipmentResult.attribValue |
---|
189 | } |
---|
190 | |
---|
191 | // The value of each row is the dataList used by the report table. |
---|
192 | result.dataList = rows.collect {it.value} |
---|
193 | // Print formatting, since we are done with these as objects. |
---|
194 | result.attribTypes = result.attribTypes.join(', ') |
---|
195 | result.assetsWithoutEquipment = result.assetsWithoutEquipment.collect {it.name}.join(', ') |
---|
196 | |
---|
197 | // Success. |
---|
198 | return result |
---|
199 | |
---|
200 | } // getEquipmentRegister |
---|
201 | |
---|
202 | } // end class |
---|