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] = '-' |
---|
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 | result.section = Section.get(params.section.id.toLong()) |
---|
128 | result.site = result.section.site |
---|
129 | |
---|
130 | // Inner join used to return only attribTypes that are used by AssetSubItemExtendedAttributes. |
---|
131 | // So the result is only assetSubItem extendedAttributeTypes. |
---|
132 | def attribTypesQ = new HqlBuilder().query { |
---|
133 | select 'distinct attribT.name' |
---|
134 | from 'AssetSubItemExtendedAttribute attrib', |
---|
135 | 'join attrib.extendedAttributeType as attribT' |
---|
136 | order 'by attribT.name asc' |
---|
137 | } |
---|
138 | result.attribTypes = Asset.executeQuery(attribTypesQ.query, attribTypesQ.namedParams) |
---|
139 | |
---|
140 | // A useful list of assets without subItems to be given to the user. |
---|
141 | def assetsWithoutEquipmentQ = new HqlBuilder().query { |
---|
142 | select 'distinct asset' |
---|
143 | from 'Asset asset', |
---|
144 | 'left join asset.assetSubItems as assetSubItem' |
---|
145 | where 'assetSubItem = null' |
---|
146 | namedParams.section = result.section |
---|
147 | and 'asset.section = :section' |
---|
148 | } |
---|
149 | result.assetsWithoutEquipment = AssetSubItem.executeQuery(assetsWithoutEquipmentQ.query, assetsWithoutEquipmentQ.namedParams) |
---|
150 | |
---|
151 | // A result is returned for every level 1 assetSubItem and for any extended attributes. |
---|
152 | def q = new HqlBuilder().query { |
---|
153 | select 'new map(asset.name as assetName', |
---|
154 | 'assetSubItem.name as name', |
---|
155 | 'assetSubItem.description as description', |
---|
156 | 'assetSubItem.comment as comment', |
---|
157 | 'attribT.name as attribType', |
---|
158 | 'attrib.value as attribValue)' |
---|
159 | from 'AssetSubItem assetSubItem', |
---|
160 | 'inner join assetSubItem.assets as asset', |
---|
161 | 'left join assetSubItem.assetSubItemExtendedAttributes as attrib', |
---|
162 | 'left join attrib.extendedAttributeType as attribT' |
---|
163 | where 'asset != null' // ensure that only level 1 assetSubItems are returned. |
---|
164 | namedParams.section = result.section |
---|
165 | and 'asset.section = :section' |
---|
166 | order 'by asset.name asc, assetSubItem.name asc, attribT.name asc' |
---|
167 | } |
---|
168 | def equipmentResults = AssetSubItem.executeQuery(q.query, q.namedParams) |
---|
169 | |
---|
170 | // A result is returned for every asset and for any extended attributes. |
---|
171 | def assetResultsQ = new HqlBuilder().query { |
---|
172 | select 'new map(asset.name as assetName', |
---|
173 | "' Asset Details' as name", // Place holder 'equipment' name, 3 leading spaces for sorting. |
---|
174 | 'asset.description as description', |
---|
175 | 'asset.comment as comment', |
---|
176 | 'attribT.name as attribType', |
---|
177 | 'attrib.value as attribValue)' |
---|
178 | from 'Asset asset', |
---|
179 | 'left join asset.assetExtendedAttributes as attrib', |
---|
180 | 'left join attrib.extendedAttributeType as attribT' |
---|
181 | where 'asset.section = :section' |
---|
182 | namedParams.section = result.section |
---|
183 | order 'by asset.name asc, attribT.name asc' |
---|
184 | } |
---|
185 | def assetResults = Asset.executeQuery(assetResultsQ.query, assetResultsQ.namedParams) |
---|
186 | |
---|
187 | // Add asset details to equipmentResults. |
---|
188 | equipmentResults.addAll(assetResults) |
---|
189 | equipmentResults.sort { p1, p2 -> p1.assetName.compareToIgnoreCase(p2.assetName) ?: p1.name.compareToIgnoreCase(p2.name) } |
---|
190 | |
---|
191 | // Build the report table rows. |
---|
192 | // Rows are keyed by equipmentResult.assetName+equipmentResult.name` while the value is a Map of the attributes. |
---|
193 | // The report table then groups by assetName. |
---|
194 | def rows = [:] |
---|
195 | equipmentResults.each { equipmentResult -> |
---|
196 | // Create row if it does not exist yet. |
---|
197 | def rowKey = equipmentResult.assetName+equipmentResult.name |
---|
198 | if(!rows.containsKey(rowKey)) { |
---|
199 | rows[rowKey] = ['assetName': equipmentResult.assetName, |
---|
200 | 'name':equipmentResult.name, |
---|
201 | 'description':equipmentResult.description, |
---|
202 | 'comment':equipmentResult.comment] |
---|
203 | |
---|
204 | // Add all attribType columns. |
---|
205 | result.attribTypes.each { column -> |
---|
206 | rows[rowKey][column] = '-' |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | // Assign value to column. |
---|
211 | rows[rowKey][equipmentResult.attribType] = equipmentResult.attribValue |
---|
212 | } |
---|
213 | |
---|
214 | // The value of each row is the dataList used by the report table. |
---|
215 | result.dataList = rows.collect {it.value} |
---|
216 | // Print formatting, since we are done with these as objects. |
---|
217 | result.attribTypes = result.attribTypes.join(', ') |
---|
218 | result.assetsWithoutEquipment = result.assetsWithoutEquipment.collect {it.name}.join(', ') |
---|
219 | |
---|
220 | // Success. |
---|
221 | return result |
---|
222 | |
---|
223 | } // getEquipmentRegister |
---|
224 | |
---|
225 | } // end class |
---|