Changeset 562 for trunk/grails-app/services
- Timestamp:
- Jun 2, 2010, 4:31:17 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified trunk/grails-app/services/InventoryItemSearchService.groovy ¶
r489 r562 1 1 import grails.orm.PagedResultList 2 import org.compass.core.engine.SearchEngineQueryParseException 2 3 3 4 /** … … 138 139 } // getRecentlyUsed 139 140 141 /** 142 * Get a list of inventory items by search text. 143 * @param params The request params. 144 * @param locale The locale to use when generating result.message. 145 */ 146 def getTextSearch(params, locale) { 147 def result = [:] 148 result.searchText = params.searchText.trim() ?: "" 149 150 def getMessage = { Map m -> 151 messageSource.getMessage(m.code, m.args == null ? null : m.args.toArray(), locale) 152 } 153 154 params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) 155 params.offset = params?.offset?.toInteger() ?: 0 156 params.sort = params?.sort ?: "id" 157 params.order = params?.order ?: "asc" 158 159 // Build searchableParams. 160 // Do not include params.sort, since not all properites are indexed. 161 def searchableParams = [max: params.max, offset: params.offset, 162 reload: true, defaultOperator: 'or'] 163 164 // Perform the searchable query. 165 try { 166 result.inventoryItemList = InventoryItem.search(result.searchText, searchableParams) 167 } catch (e) { 168 log.error e 169 result.inventoryItemList = [:] 170 result.inventoryItemList.results = [] 171 result.inventoryItemList.total = 0 172 } 173 174 // Sort the returned instances. 175 if(params.sort != 'id') { 176 if(params.order == 'asc') { 177 if(params.sort == 'name' || params.sort == 'description') 178 result.inventoryItemList.results.sort { p1, p2 -> p1[params.sort].compareToIgnoreCase(p2[params.sort]) } 179 else if(params.sort == 'inventoryGroup') { 180 result.inventoryItemList.results.sort { p1, p2 -> 181 p1.inventoryGroup.name.compareToIgnoreCase(p2.inventoryGroup.name) 182 } 183 } 184 else if(params.sort == 'unitsInStock') 185 result.inventoryItemList.results.sort {p1, p2 -> p1[params.sort] <=> p2[params.sort] } 186 } // asc. 187 else { 188 if(params.sort == 'name' || params.sort == 'description') 189 result.inventoryItemList.results.sort { p1, p2 -> p2[params.sort].compareToIgnoreCase(p1[params.sort]) } 190 else if(params.sort == 'inventoryGroup') { 191 result.inventoryItemList.results.sort { p1, p2 -> 192 p2.inventoryGroup.name.compareToIgnoreCase(p1.inventoryGroup.name) 193 } 194 } 195 else if(params.sort == 'unitsInStock') 196 result.inventoryItemList.results.sort {p1, p2 -> p2[params.sort] <=> p1[params.sort]} 197 } // desc. 198 } // sort. 199 200 // Create a PagedResultList. 201 result.inventoryItemList = new PagedResultList(result.inventoryItemList.results, result.inventoryItemList.total) 202 203 // Get the result message. 204 if(result.inventoryItemList.totalCount > 0) 205 result.message = getMessage(code:"inventoryItem.search.text.found", args: [result.searchText]) 206 else 207 result.message = getMessage(code:"inventoryItem.search.text.none.found", args: [result.searchText]) 208 209 // Success. 210 return result 211 212 } // getTextSearch() 213 140 214 } // end class
Note: See TracChangeset
for help on using the changeset viewer.