1 | |
---|
2 | import net.kromhouts.HqlBuilder |
---|
3 | import org.apache.commons.lang.WordUtils |
---|
4 | |
---|
5 | class AssetSubItemService { |
---|
6 | |
---|
7 | boolean transactional = false |
---|
8 | |
---|
9 | def sessionFactory |
---|
10 | |
---|
11 | /** |
---|
12 | * Determines and returns sorted list of possible parent items. |
---|
13 | * @param assetSubItemInstance The instance to prepare the list for. |
---|
14 | * @returns A list of the possible parentItems. |
---|
15 | */ |
---|
16 | def possibleParentItems(assetSubItemInstance) { |
---|
17 | def criteria = AssetSubItem.createCriteria() |
---|
18 | def possibleParentItems = criteria.list() { |
---|
19 | and { |
---|
20 | eq('isActive', true) |
---|
21 | ne('id', assetSubItemInstance.id) |
---|
22 | assetSubItemInstance.subItems.each() { notEqual('id', it.id) } |
---|
23 | } |
---|
24 | } |
---|
25 | possibleParentItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
26 | } |
---|
27 | |
---|
28 | def delete(params) { |
---|
29 | AssetSubItem.withTransaction { status -> |
---|
30 | def result = [:] |
---|
31 | |
---|
32 | def fail = { Map m -> |
---|
33 | status.setRollbackOnly() |
---|
34 | if(result.assetSubItemInstance && m.field) |
---|
35 | result.assetSubItemInstance.errors.rejectValue(m.field, m.code) |
---|
36 | result.error = [ code: m.code, args: ["AssetSubItem", params.id] ] |
---|
37 | return result |
---|
38 | } |
---|
39 | |
---|
40 | result.assetSubItemInstance = AssetSubItem.get(params.id) |
---|
41 | |
---|
42 | if(!result.assetSubItemInstance) |
---|
43 | return fail(code:"default.not.found") |
---|
44 | |
---|
45 | for(subItem in result.assetSubItemInstance.subItems) { |
---|
46 | if(subItem.maintenanceActions) |
---|
47 | fail(code:"maintenanceActions.still.associated.subItem") |
---|
48 | } |
---|
49 | |
---|
50 | if(result.error) |
---|
51 | return result |
---|
52 | |
---|
53 | if(result.assetSubItemInstance.maintenanceActions) |
---|
54 | return fail(code:"maintenanceActions.still.associated") |
---|
55 | |
---|
56 | if(result.assetSubItemInstance.assets) |
---|
57 | return fail(code:"assetSubItem.assets.associated") |
---|
58 | |
---|
59 | // We have handled all the foreign keys so the delete should go forward. |
---|
60 | // Can't flush here due to cascading from Section and Site. |
---|
61 | // And without flush there is no point it trying to catch the dao.DataIntegrityViolationException |
---|
62 | // since that will only happen after leaving the transaction. |
---|
63 | result.assetSubItemInstance.delete() |
---|
64 | return result //Success. |
---|
65 | |
---|
66 | } // withTransaction |
---|
67 | } |
---|
68 | |
---|
69 | def create(params) { |
---|
70 | def result = [:] |
---|
71 | def fail = { Map m -> |
---|
72 | result.error = [ code: m.code, args: ["AssetSubItem", params.id] ] |
---|
73 | return result |
---|
74 | } |
---|
75 | |
---|
76 | result.assetSubItemInstance = new AssetSubItem() |
---|
77 | result.assetSubItemInstance.properties = params |
---|
78 | |
---|
79 | // pass in an asset to link to if provided. |
---|
80 | if(params.asset?.id) |
---|
81 | result.assetInstance = Asset.get(params.asset.id) |
---|
82 | |
---|
83 | // success |
---|
84 | return result |
---|
85 | } |
---|
86 | |
---|
87 | def save(params) { |
---|
88 | AssetSubItem.withTransaction { status -> |
---|
89 | def result = [:] |
---|
90 | |
---|
91 | def fail = { Map m -> |
---|
92 | status.setRollbackOnly() |
---|
93 | if(result.assetSubItemInstance && m.field) |
---|
94 | result.assetSubItemInstance.errors.rejectValue(m.field, m.code) |
---|
95 | result.error = [ code: m.code, args: ["AssetSubItem", params.id] ] |
---|
96 | return result |
---|
97 | } |
---|
98 | |
---|
99 | result.assetSubItemInstance = new AssetSubItem(params) |
---|
100 | |
---|
101 | use(WordUtils) { |
---|
102 | result.assetSubItemInstance.name = result.assetSubItemInstance.name.capitalize() |
---|
103 | result.assetSubItemInstance.description = result.assetSubItemInstance.description.capitalize() |
---|
104 | } |
---|
105 | |
---|
106 | if(params.asset?.id) { |
---|
107 | result.assetInstance = Asset.get(params.asset.id) |
---|
108 | |
---|
109 | if(!result.assetInstance) |
---|
110 | return fail(code:"assetSubItem.asset.not.found") |
---|
111 | } |
---|
112 | |
---|
113 | if(result.assetSubItemInstance.hasErrors() || !result.assetSubItemInstance.save(flush: true)) |
---|
114 | return fail(code:"default.create.failure") |
---|
115 | |
---|
116 | if(result.assetInstance) |
---|
117 | result.assetInstance.addToAssetSubItems(result.assetSubItemInstance) |
---|
118 | |
---|
119 | // Success. |
---|
120 | return result |
---|
121 | |
---|
122 | } //end withTransaction |
---|
123 | } // save() |
---|
124 | |
---|
125 | /** |
---|
126 | * Get a list of level 1 assetSubItems. |
---|
127 | * @returns By default all level 1 assetSubItems are returned. |
---|
128 | */ |
---|
129 | def getLevel1AssetSubItems(params) { |
---|
130 | |
---|
131 | // A result is returned for every level 1 assetSubItem. |
---|
132 | def q = new HqlBuilder().query { |
---|
133 | select 'assetSubItem' |
---|
134 | from 'AssetSubItem assetSubItem', |
---|
135 | 'inner join assetSubItem.assets as asset' |
---|
136 | where 'asset != null' // ensure that only level 1 assetSubItems are returned. |
---|
137 | } |
---|
138 | def assetSubItems = AssetSubItem.executeQuery(q.query, q.namedParams) |
---|
139 | |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * Create recommended extended attributes for all level 1 subItems. |
---|
144 | */ |
---|
145 | def createRecommendedExtendedAttributes() { |
---|
146 | def result = [:] |
---|
147 | |
---|
148 | def hibernateSession = sessionFactory.currentSession |
---|
149 | |
---|
150 | def assetSubItems = getLevel1AssetSubItems() |
---|
151 | |
---|
152 | def manufacturer = ExtendedAttributeType.findByName("Manufacturer") |
---|
153 | def modelNumber = ExtendedAttributeType.findByName("Model Number") |
---|
154 | def serialNumber = ExtendedAttributeType.findByName("Serial Number") |
---|
155 | def manufacturedDate = ExtendedAttributeType.findByName("Manufactured Date") |
---|
156 | |
---|
157 | for(assetSubItem in assetSubItems) { |
---|
158 | |
---|
159 | def attributeTypes = assetSubItem.assetSubItemExtendedAttributes.collect {it.extendedAttributeType} |
---|
160 | |
---|
161 | //AssetSubItemExtendedAttribute |
---|
162 | def assetSubItemExtendedAttributeInstance |
---|
163 | |
---|
164 | if(!attributeTypes.contains(manufacturer)) { |
---|
165 | //AssetSubItemExtendedAttribute #1 |
---|
166 | assetSubItemExtendedAttributeInstance = new AssetSubItemExtendedAttribute(value: "Not Specified", |
---|
167 | assetSubItem: assetSubItem, |
---|
168 | extendedAttributeType: manufacturer) |
---|
169 | assetSubItemExtendedAttributeInstance.save() |
---|
170 | } |
---|
171 | |
---|
172 | if(!attributeTypes.contains(modelNumber)) { |
---|
173 | //AssetSubItemExtendedAttribute #2 |
---|
174 | assetSubItemExtendedAttributeInstance = new AssetSubItemExtendedAttribute(value: "Not Specified", |
---|
175 | assetSubItem: assetSubItem, |
---|
176 | extendedAttributeType: modelNumber) |
---|
177 | assetSubItemExtendedAttributeInstance.save() |
---|
178 | } |
---|
179 | |
---|
180 | if(!attributeTypes.contains(serialNumber)) { |
---|
181 | //AssetSubItemExtendedAttribute #3 |
---|
182 | assetSubItemExtendedAttributeInstance = new AssetSubItemExtendedAttribute(value: "Not Specified", |
---|
183 | assetSubItem: assetSubItem, |
---|
184 | extendedAttributeType: serialNumber) |
---|
185 | assetSubItemExtendedAttributeInstance.save() |
---|
186 | } |
---|
187 | |
---|
188 | if(!attributeTypes.contains(manufacturedDate)) { |
---|
189 | //AssetSubItemExtendedAttribute #4 |
---|
190 | assetSubItemExtendedAttributeInstance = new AssetSubItemExtendedAttribute(value: "Not Specified", |
---|
191 | assetSubItem: assetSubItem, |
---|
192 | extendedAttributeType: manufacturedDate) |
---|
193 | assetSubItemExtendedAttributeInstance.save() |
---|
194 | } |
---|
195 | |
---|
196 | hibernateSession.flush() |
---|
197 | |
---|
198 | } // for |
---|
199 | |
---|
200 | // Success. |
---|
201 | return result |
---|
202 | |
---|
203 | } // createRecommendedExtendedAttributes() |
---|
204 | |
---|
205 | } // end class |
---|