1 | import net.kromhouts.HqlBuilder |
---|
2 | import grails.orm.PagedResultList |
---|
3 | import org.compass.core.engine.SearchEngineQueryParseException |
---|
4 | |
---|
5 | /** |
---|
6 | * Service class that encapsulates the business logic for InventoryItem searches. |
---|
7 | */ |
---|
8 | class InventoryItemSearchService { |
---|
9 | |
---|
10 | boolean transactional = false |
---|
11 | |
---|
12 | def dateUtilService |
---|
13 | def messageSource |
---|
14 | |
---|
15 | def paramsMax = 100000 |
---|
16 | |
---|
17 | /** |
---|
18 | * Selects and returns the correct search results based on the supplied quickSearch. |
---|
19 | * @param params The request params, may contain params.quickSearch string to specify the search. |
---|
20 | * @param locale The locale to use when generating result.message. |
---|
21 | */ |
---|
22 | def getQuickSearch(params, locale) { |
---|
23 | def result = [:] |
---|
24 | result.quickSearch = params.quickSearch ?: "all" |
---|
25 | |
---|
26 | def getMessage = { Map m -> |
---|
27 | messageSource.getMessage(m.code, m.args == null ? null : m.args.toArray(), locale) |
---|
28 | } |
---|
29 | |
---|
30 | switch (result.quickSearch) { |
---|
31 | case "inventoryBelowReorder": |
---|
32 | result.inventoryItemList = getInventoryBelowReorder(params) |
---|
33 | if(result.inventoryItemList.totalCount > 0) |
---|
34 | result.message = getMessage(code:"inventoryItem.search.text.below.reorder.description") |
---|
35 | else |
---|
36 | result.message = getMessage(code:"inventoryItem.search.text.below.reorder.none.found") |
---|
37 | break |
---|
38 | case "inventoryBelowReorderAll": |
---|
39 | result.inventoryItemList = getInventoryBelowReorder(params, false) |
---|
40 | if(result.inventoryItemList.totalCount > 0) |
---|
41 | result.message = getMessage(code:"inventoryItem.search.text.below.reorder.all.description") |
---|
42 | else |
---|
43 | result.message = getMessage(code:"inventoryItem.search.text.below.reorder.none.found") |
---|
44 | break |
---|
45 | case "recentlyUsed": |
---|
46 | result.daysBack = params.daysBack?.isInteger() ? params.daysBack.toInteger() : 14 |
---|
47 | result.inventoryItemList = getRecentlyUsed(params, result.daysBack) |
---|
48 | if(result.inventoryItemList.totalCount > 0) |
---|
49 | result.message = getMessage(code:"inventoryItem.search.text.recently.used.description", args:[result.daysBack]) |
---|
50 | else |
---|
51 | result.message = getMessage(code:"inventoryItem.search.text.recently.used.none.found", args:[result.daysBack]) |
---|
52 | break |
---|
53 | default: |
---|
54 | result.inventoryItemList = getAll(params) |
---|
55 | if(result.inventoryItemList.totalCount > 0) |
---|
56 | result.message = getMessage(code:"inventoryItem.search.text.all.description") |
---|
57 | else |
---|
58 | result.message = getMessage(code:"inventoryItem.search.text.all.none.found") |
---|
59 | break |
---|
60 | } // switch. |
---|
61 | |
---|
62 | // Success. |
---|
63 | return result |
---|
64 | |
---|
65 | } // getQuickSearch |
---|
66 | |
---|
67 | /** |
---|
68 | * Get all inventory items. |
---|
69 | * @param params The request params. |
---|
70 | */ |
---|
71 | def getAll(params) { |
---|
72 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
73 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
74 | params.sort = params?.sort ?: "name" |
---|
75 | params.order = params?.order ?: "asc" |
---|
76 | |
---|
77 | def inventoryItemList = InventoryItem.createCriteria().list( |
---|
78 | max: params.max, |
---|
79 | offset: params.offset, |
---|
80 | sort: params.sort, |
---|
81 | order: params.order) { |
---|
82 | } // createCriteria |
---|
83 | } // getAll |
---|
84 | |
---|
85 | /** |
---|
86 | * List inventory items that are below reorder point. |
---|
87 | * @param params The request params. |
---|
88 | * @param onlyReorderEnabled Only include items with reorder enabled, defaults to true. |
---|
89 | */ |
---|
90 | def getInventoryBelowReorder(params, onlyReorderEnabled=true) { |
---|
91 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
92 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
93 | params.sort = params?.sort ?: "name" |
---|
94 | params.order = params?.order ?: "asc" |
---|
95 | |
---|
96 | def inventoryItemList = InventoryItem.createCriteria().list( |
---|
97 | max: params.max, |
---|
98 | offset: params.offset, |
---|
99 | sort: params.sort, |
---|
100 | order: params.order) { |
---|
101 | eq("isActive", true) |
---|
102 | if(onlyReorderEnabled) |
---|
103 | eq("enableReorderListing", true) |
---|
104 | leProperty("unitsInStock", "reorderPoint") |
---|
105 | } // createCriteria |
---|
106 | } // getInventoryBelowReorder |
---|
107 | |
---|
108 | /** |
---|
109 | * Search for inventory items that are below reorder point. |
---|
110 | * @param params The request params. |
---|
111 | * @param locale The locale to use when generating result.message. |
---|
112 | */ |
---|
113 | def getReorderSearch(params, locale) { |
---|
114 | def result = [:] |
---|
115 | |
---|
116 | def getMessage = { Map m -> |
---|
117 | messageSource.getMessage(m.code, m.args == null ? null : m.args.toArray(), locale) |
---|
118 | } |
---|
119 | |
---|
120 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
121 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
122 | |
---|
123 | def sort = "inventoryItem." + (params?.sort ?: "name") |
---|
124 | def order = params?.order == "desc" ? "desc" : "asc" |
---|
125 | |
---|
126 | def q = new HqlBuilder(max: params.max, offset: params.offset).query { |
---|
127 | select 'count(distinct inventoryItem) as inventoryItemCount' |
---|
128 | from 'InventoryItem as inventoryItem', |
---|
129 | 'left join inventoryItem.alternateSuppliers as alternateSupplier' |
---|
130 | where 'inventoryItem.unitsInStock <= inventoryItem.reorderPoint' |
---|
131 | and 'inventoryItem.isActive = true' |
---|
132 | and 'inventoryItem.isObsolete = false' |
---|
133 | |
---|
134 | if(!params.includeReorderListingDisabled) |
---|
135 | and "inventoryItem.enableReorderListing = true" |
---|
136 | |
---|
137 | if(params.selectedSupplier?.isLong()) { |
---|
138 | namedParams.supplier = Supplier.get(params.selectedSupplier.toLong()) |
---|
139 | if(params.includeAlternateSuppliers) |
---|
140 | and "(inventoryItem.preferredSupplier = :supplier or alternateSupplier = :supplier)" |
---|
141 | else |
---|
142 | and "inventoryItem.preferredSupplier = :supplier" |
---|
143 | } // if selectedSupplier |
---|
144 | |
---|
145 | if(params.selectedGroups) { |
---|
146 | namedParams.selectedGroupIds = params.selectedGroups |
---|
147 | and "inventoryItem.inventoryGroup.id in(:selectedGroupIds)" |
---|
148 | } |
---|
149 | |
---|
150 | if(!params.includeOnBackOrder) { |
---|
151 | // Sub query! |
---|
152 | def onBackOrder = new HqlBuilder().query { |
---|
153 | from "InventoryItemPurchase p" |
---|
154 | where "p.inventoryItem = inventoryItem" |
---|
155 | and "p.inventoryItem = inventoryItem" |
---|
156 | and "p.inventoryItemPurchaseType.id = 1" // Order Placed. |
---|
157 | and "p.receivedComplete = false" |
---|
158 | } |
---|
159 | |
---|
160 | and "not exists ($onBackOrder.query)" |
---|
161 | } |
---|
162 | |
---|
163 | } // query |
---|
164 | |
---|
165 | def totalCount = InventoryItem.executeQuery(q.query, q.namedParams)[0].toInteger() |
---|
166 | |
---|
167 | q.select = 'distinct inventoryItem' |
---|
168 | q.order = "by $sort $order, inventoryItem.id asc" |
---|
169 | def list = InventoryItem.executeQuery(q.query, q.namedParams, q.paginateParams) |
---|
170 | |
---|
171 | result.inventoryItemList = new PagedResultList(list, totalCount) |
---|
172 | |
---|
173 | // Get the result message. |
---|
174 | if(result.inventoryItemList.totalCount > 0) |
---|
175 | result.message = getMessage(code:"inventoryItem.search.text.below.reorder.description") |
---|
176 | else |
---|
177 | result.message = getMessage(code:"inventoryItem.search.text.below.reorder.none.found") |
---|
178 | |
---|
179 | // Success. |
---|
180 | return result |
---|
181 | |
---|
182 | } // getReorderSearch |
---|
183 | |
---|
184 | /** |
---|
185 | * Get a list of recently used inventory items. |
---|
186 | * @param params The request params. |
---|
187 | * @param daysBack The number of days back to get results for. |
---|
188 | */ |
---|
189 | def getRecentlyUsed(params, daysBack) { |
---|
190 | def paginateParams = [:] |
---|
191 | paginateParams.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
192 | paginateParams.offset = params?.offset?.toInteger() ?: 0 |
---|
193 | |
---|
194 | def sort = "inventoryItem." + (params?.sort ?: "name") |
---|
195 | def order = params?.order == "desc" ? "desc" : "asc" |
---|
196 | def orderBy = " order by " + sort + ' ' + order |
---|
197 | |
---|
198 | def namedParams = [:] |
---|
199 | namedParams.startOfDay = dateUtilService.today - daysBack |
---|
200 | |
---|
201 | def baseQuery = "from InventoryItem as inventoryItem \ |
---|
202 | left join inventoryItem.inventoryMovements as inventoryMovement \ |
---|
203 | where (inventoryItem.isActive = true \ |
---|
204 | and inventoryMovement.date > :startOfDay \ |
---|
205 | and inventoryMovement.inventoryMovementType = 1 \ |
---|
206 | )" |
---|
207 | |
---|
208 | def searchQuery = "select distinct inventoryItem " + baseQuery + orderBy |
---|
209 | def list = InventoryItem.executeQuery(searchQuery, namedParams, paginateParams) |
---|
210 | |
---|
211 | def countQuery = "select count(distinct inventoryItem) as inventoryItemCount " + baseQuery |
---|
212 | def totalCount = InventoryItem.executeQuery(countQuery, namedParams)[0].toInteger() |
---|
213 | |
---|
214 | def inventoryItemInstanceList = new PagedResultList(list, totalCount) |
---|
215 | return inventoryItemInstanceList |
---|
216 | } // getRecentlyUsed |
---|
217 | |
---|
218 | /** |
---|
219 | * Get a list of inventory items by search text. |
---|
220 | * @param params The request params. |
---|
221 | * @param locale The locale to use when generating result.message. |
---|
222 | */ |
---|
223 | def getTextSearch(params, locale) { |
---|
224 | def result = [:] |
---|
225 | result.searchText = params.searchText.trim() ?: "" // User supplied text. |
---|
226 | result.queryString = "" // Modified string that will be passed to searchable query. |
---|
227 | |
---|
228 | def getMessage = { Map m -> |
---|
229 | messageSource.getMessage(m.code, m.args == null ? null : m.args.toArray(), locale) |
---|
230 | } |
---|
231 | |
---|
232 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
233 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
234 | params.sort = params?.sort ?: "id" |
---|
235 | params.order = params?.order ?: "asc" |
---|
236 | |
---|
237 | // Build searchableParams. |
---|
238 | // Do not include params.sort, since not all properites are indexed. |
---|
239 | def searchableParams = [:] |
---|
240 | searchableParams.max = params.max |
---|
241 | searchableParams.offset = params.offset |
---|
242 | searchableParams.reload = true |
---|
243 | searchableParams.defaultOperator = 'or' |
---|
244 | def properitesList = [] |
---|
245 | if(params.searchName) |
---|
246 | properitesList << '$/InventoryItem/name' |
---|
247 | if(params.searchDescription) |
---|
248 | properitesList << '$/InventoryItem/description' |
---|
249 | if(params.searchComment) |
---|
250 | properitesList << '$/InventoryItem/comment' |
---|
251 | if(params.searchLocation) |
---|
252 | properitesList << '$/InventoryItem/inventoryLocation/name' |
---|
253 | if(params.searchGroup) |
---|
254 | properitesList << '$/InventoryItem/inventoryGroup/name' |
---|
255 | if(params.searchSpareFor) { |
---|
256 | properitesList << '$/InventoryItem/spareFor/name' |
---|
257 | properitesList << '$/InventoryItem/spareFor/description' |
---|
258 | properitesList << '$/InventoryItem/spareFor/comment' |
---|
259 | } |
---|
260 | if(properitesList) |
---|
261 | searchableParams.properties = properitesList |
---|
262 | |
---|
263 | // Check searchText for key words and modifiers. |
---|
264 | def hasIsActive = result.searchText.contains('isActive') |
---|
265 | def hasIsObsolete = result.searchText.contains('isObsolete') |
---|
266 | def hasBracket = result.searchText.contains('(') || result.searchText.contains(')') |
---|
267 | def containsModifier = { s -> |
---|
268 | s.contains('"') || |
---|
269 | s.contains('~') || |
---|
270 | s.contains('*') || |
---|
271 | s.contains('(') || |
---|
272 | s.contains(')') || |
---|
273 | s.contains('+') || |
---|
274 | s.contains('-') || |
---|
275 | s.contains('^') || |
---|
276 | s.contains('OR') || |
---|
277 | s.contains('AND') || |
---|
278 | s.contains('NOT') || |
---|
279 | s.contains('TO') || |
---|
280 | s.contains('isObsolete') || |
---|
281 | s.contains('isActive') |
---|
282 | } |
---|
283 | |
---|
284 | // Expand search with wildcards. |
---|
285 | def addWildcards = { text -> |
---|
286 | text = text.tokenize().collect { token -> |
---|
287 | if(!containsModifier(token)) |
---|
288 | '*'+token+'*' |
---|
289 | else |
---|
290 | token |
---|
291 | }.join(' ') |
---|
292 | return text |
---|
293 | } |
---|
294 | |
---|
295 | // Default isActive and isObsolete. |
---|
296 | def addDefaultFlags = { text -> |
---|
297 | if(!hasBracket) |
---|
298 | text = '( '+text+' )' |
---|
299 | if(!hasIsActive) |
---|
300 | text = text + ' AND isActive:"true" ' |
---|
301 | if(!hasIsObsolete) |
---|
302 | text = text + ' AND isObsolete:"false" ' |
---|
303 | return text |
---|
304 | } |
---|
305 | |
---|
306 | result.queryString = addWildcards(result.searchText) |
---|
307 | result.queryString = addDefaultFlags(result.queryString) |
---|
308 | |
---|
309 | // Perform the searchable query. |
---|
310 | try { |
---|
311 | result.inventoryItemList = InventoryItem.search(result.queryString, searchableParams) |
---|
312 | |
---|
313 | // Would be nice if this worked. |
---|
314 | // result.inventoryItemList = InventoryItem.search(result.searchText, searchableParams) { |
---|
315 | // must(term("isActive", true)) |
---|
316 | // must(term("isObsolete", false)) |
---|
317 | // } |
---|
318 | |
---|
319 | } catch (e) { |
---|
320 | log.error e |
---|
321 | result.inventoryItemList = [:] |
---|
322 | result.inventoryItemList.results = [] |
---|
323 | result.inventoryItemList.total = 0 |
---|
324 | } |
---|
325 | |
---|
326 | // Sort the returned instances. |
---|
327 | if(params.sort != 'id') { |
---|
328 | if(params.order == 'asc') { |
---|
329 | if(params.sort == 'name' || params.sort == 'description') |
---|
330 | result.inventoryItemList.results.sort { p1, p2 -> p1[params.sort].compareToIgnoreCase(p2[params.sort]) } |
---|
331 | else if(params.sort == 'inventoryGroup') { |
---|
332 | result.inventoryItemList.results.sort { p1, p2 -> |
---|
333 | p1.inventoryGroup.name.compareToIgnoreCase(p2.inventoryGroup.name) |
---|
334 | } |
---|
335 | } |
---|
336 | else if(params.sort == 'unitsInStock') |
---|
337 | result.inventoryItemList.results.sort {p1, p2 -> p1[params.sort] <=> p2[params.sort] } |
---|
338 | } // asc. |
---|
339 | else { |
---|
340 | if(params.sort == 'name' || params.sort == 'description') |
---|
341 | result.inventoryItemList.results.sort { p1, p2 -> p2[params.sort].compareToIgnoreCase(p1[params.sort]) } |
---|
342 | else if(params.sort == 'inventoryGroup') { |
---|
343 | result.inventoryItemList.results.sort { p1, p2 -> |
---|
344 | p2.inventoryGroup.name.compareToIgnoreCase(p1.inventoryGroup.name) |
---|
345 | } |
---|
346 | } |
---|
347 | else if(params.sort == 'unitsInStock') |
---|
348 | result.inventoryItemList.results.sort {p1, p2 -> p2[params.sort] <=> p1[params.sort]} |
---|
349 | } // desc. |
---|
350 | } // sort. |
---|
351 | |
---|
352 | // Create a PagedResultList. |
---|
353 | result.inventoryItemList = new PagedResultList(result.inventoryItemList.results, result.inventoryItemList.total) |
---|
354 | |
---|
355 | // Get the result message. |
---|
356 | if(result.inventoryItemList.totalCount > 0) |
---|
357 | result.message = getMessage(code:"inventoryItem.search.text.found", args: [result.queryString]) |
---|
358 | else |
---|
359 | result.message = getMessage(code:"inventoryItem.search.text.none.found", args: [result.queryString]) |
---|
360 | |
---|
361 | // Success. |
---|
362 | return result |
---|
363 | |
---|
364 | } // getTextSearch() |
---|
365 | |
---|
366 | } // end class |
---|