1
1
package com .salesmanager .shop .store .controller .search .facade ;
2
2
3
- import java .util .ArrayList ;
4
- import java .util .HashMap ;
3
+ import java .util .Collections ;
5
4
import java .util .List ;
6
5
import java .util .Map ;
6
+ import java .util .Map .Entry ;
7
+ import java .util .Optional ;
8
+ import java .util .function .Supplier ;
7
9
import java .util .stream .Collectors ;
8
10
import javax .inject .Inject ;
9
11
import org .apache .commons .collections4 .CollectionUtils ;
45
47
46
48
@ Service ("searchFacade" )
47
49
public class SearchFacadeImpl implements SearchFacade {
48
-
50
+
49
51
private static final Logger LOGGER = LoggerFactory .getLogger (SearchFacadeImpl .class );
50
-
52
+
51
53
@ Inject
52
54
private SearchService searchService ;
53
-
55
+
54
56
@ Inject
55
57
private ProductService productService ;
56
-
58
+
57
59
@ Inject
58
60
private CategoryService categoryService ;
59
-
61
+
60
62
@ Inject
61
63
private PricingService pricingService ;
62
-
64
+
63
65
@ Inject
64
66
@ Qualifier ("img" )
65
67
private ImageFilePath imageUtils ;
66
-
68
+
67
69
@ Inject
68
70
private CoreConfiguration coreConfiguration ;
69
71
70
-
72
+
71
73
private final static String CATEGORY_FACET_NAME = "categories" ;
72
74
private final static String MANUFACTURER_FACET_NAME = "manufacturer" ;
73
75
private final static int AUTOCOMPLETE_ENTRIES_COUNT = 15 ;
@@ -80,55 +82,50 @@ public class SearchFacadeImpl implements SearchFacade {
80
82
@ Override
81
83
@ Async
82
84
public void indexAllData (MerchantStore store ) throws Exception {
83
-
84
-
85
85
List <Product > products = productService .listByStore (store );
86
-
86
+
87
87
for (Product product : products ) {
88
88
searchService .index (store , product );
89
89
}
90
-
90
+
91
91
}
92
92
93
93
@ Override
94
94
public SearchProductList search (MerchantStore store , Language language , SearchProductRequest searchRequest ) {
95
95
String query = String .format (coreConfiguration .getProperty ("SEARCH_QUERY" ), searchRequest .getQuery ());
96
96
SearchResponse response = search (store , language .getCode (), query , searchRequest .getCount (), searchRequest .getStart ());
97
- return copySearchResponse (response , store , searchRequest .getStart (), searchRequest .getCount (), language );
97
+ return convertToSearchProductList (response , store , searchRequest .getStart (), searchRequest .getCount (), language );
98
98
}
99
99
100
100
private SearchResponse search (
101
101
MerchantStore store , String languageCode , String query , Integer count , Integer start ) {
102
- try {
103
- LOGGER .debug ("Search " + query );
104
- return searchService .search (store , languageCode , query , count , start );
105
- } catch (ServiceException e ){
106
- throw new ServiceRuntimeException (e );
102
+ try {
103
+ LOGGER .debug ("Search " + query );
104
+ return searchService .search (store , languageCode , query , count , start );
105
+ } catch (ServiceException e ) {
106
+ throw new ServiceRuntimeException (e );
107
107
}
108
108
}
109
109
110
110
@ Override
111
- public SearchProductList copySearchResponse (SearchResponse searchResponse , MerchantStore merchantStore , int start , int count , Language language ) {
112
-
111
+ public SearchProductList convertToSearchProductList (SearchResponse searchResponse , MerchantStore merchantStore , int start , int count , Language language ) {
112
+
113
113
SearchProductList returnList = new SearchProductList ();
114
114
List <SearchEntry > entries = searchResponse .getEntries ();
115
-
116
- if (!CollectionUtils .isEmpty (entries )) {
117
- List <Long > ids = new ArrayList <Long >();
118
- for (SearchEntry entry : entries ) {
119
- IndexProduct indexedProduct = entry .getIndexProduct ();
120
- Long id = Long .parseLong (indexedProduct .getId ());
121
-
122
- //No highlights
123
- ids .add (id );
124
- }
125
-
115
+
116
+ if (CollectionUtils .isNotEmpty (entries )) {
117
+ List <Long > ids = entries .stream ()
118
+ .map (SearchEntry ::getIndexProduct )
119
+ .map (IndexProduct ::getId )
120
+ .map (Long ::parseLong )
121
+ .collect (Collectors .toList ());
122
+
126
123
ProductCriteria searchCriteria = new ProductCriteria ();
127
124
searchCriteria .setMaxCount (count );
128
125
searchCriteria .setStartIndex (start );
129
126
searchCriteria .setProductIds (ids );
130
127
searchCriteria .setAvailable (true );
131
-
128
+
132
129
ProductList productList = productService .listByStore (merchantStore , language , searchCriteria );
133
130
134
131
List <ReadableProduct > readableProducts = productList .getProducts ()
@@ -139,52 +136,57 @@ public SearchProductList copySearchResponse(SearchResponse searchResponse, Merch
139
136
returnList .getProducts ().addAll (readableProducts );
140
137
returnList .setProductCount (productList .getProducts ().size ());
141
138
}
142
-
143
- //Facets
144
- Map <String ,List <SearchFacet >> facets = searchResponse .getFacets ();
145
- List <SearchFacet > categoriesFacets = null ;
146
- List <SearchFacet > manufacturersFacets = null ;
147
- if (facets !=null ) {
148
- for (String key : facets .keySet ()) {
149
- //supports category and manufacturer
150
- if (CATEGORY_FACET_NAME .equals (key )) {
151
- categoriesFacets = facets .get (key );
152
- }
153
-
154
- if (MANUFACTURER_FACET_NAME .equals (key )) {
155
- manufacturersFacets = facets .get (key );
156
- }
157
- }
158
-
159
-
160
- if (!CollectionUtils .isEmpty (categoriesFacets )) {
161
- List <String > categoryCodes = new ArrayList <String >();
162
- Map <String ,Long > productCategoryCount = new HashMap <String ,Long >();
163
- for (SearchFacet facet : categoriesFacets ) {
164
- categoryCodes .add (facet .getName ());
165
- productCategoryCount .put (facet .getKey (), facet .getCount ());
166
- }
167
-
168
- List <Category > categories = categoryService .listByCodes (merchantStore , categoryCodes , language );
169
- List <ReadableCategory > categoryProxies = categories
170
- .stream ()
171
- .map (category -> convertCategoryToReadableCategory (merchantStore , language ,
172
- productCategoryCount , category ))
173
- .collect (Collectors .toList ());
174
- returnList .setCategoryFacets (categoryProxies );
175
- }
176
-
177
- //todo manufacturer facets
178
- if (manufacturersFacets !=null ) {
179
-
180
- }
181
-
182
-
183
- }
184
-
185
- return returnList ;
139
+
140
+ // Facets
141
+ Map <String , List <SearchFacet >> facets =
142
+ Optional .ofNullable (searchResponse .getFacets ()).orElse (Collections .emptyMap ());
143
+
144
+ List <ReadableCategory > categoryProxies = getCategoryFacets (merchantStore , language , facets );
145
+ returnList .setCategoryFacets (categoryProxies );
146
+
147
+ List <SearchFacet > manufacturersFacets = facets .entrySet ().stream ()
148
+ .filter (e -> MANUFACTURER_FACET_NAME .equals (e .getKey ()))
149
+ .findFirst ()
150
+ .map (Entry ::getValue )
151
+ .orElse (Collections .emptyList ());
152
+
153
+ if (CollectionUtils .isNotEmpty (manufacturersFacets )) {
154
+ // TODO add manufacturer facets
155
+ }
156
+ return returnList ;
186
157
}
187
158
159
+ private List <ReadableCategory > getCategoryFacets (
160
+ MerchantStore merchantStore , Language language , Map <String , List <SearchFacet >> facets ) {
161
+ List <SearchFacet > categoriesFacets =
162
+ facets .entrySet ().stream ()
163
+ .filter (e -> CATEGORY_FACET_NAME .equals (e .getKey ()))
164
+ .findFirst ()
165
+ .map (Entry ::getValue )
166
+ .orElse (Collections .emptyList ());
167
+
168
+ if (CollectionUtils .isNotEmpty (categoriesFacets )) {
169
+
170
+ List <String > categoryCodes =
171
+ categoriesFacets .stream ().map (SearchFacet ::getName ).collect (Collectors .toList ());
172
+
173
+ Map <String , Long > productCategoryCount =
174
+ categoriesFacets .stream ()
175
+ .collect (Collectors .toMap (SearchFacet ::getKey , SearchFacet ::getCount ));
176
+
177
+ List <Category > categories =
178
+ categoryService .listByCodes (merchantStore , categoryCodes , language );
179
+ return categories .stream ()
180
+ .map (
181
+ category ->
182
+ convertCategoryToReadableCategory (
183
+ merchantStore , language , productCategoryCount , category ))
184
+ .collect (Collectors .toList ());
185
+ } else {
186
+ return Collections .emptyList ();
187
+ }
188
+ }
189
+
188
190
private ReadableCategory convertCategoryToReadableCategory (MerchantStore merchantStore ,
189
191
Language language , Map <String , Long > productCategoryCount , Category category ) {
190
192
ReadableCategoryPopulator populator = new ReadableCategoryPopulator ();
@@ -220,23 +222,20 @@ public ValueList autocompleteRequest(String query, MerchantStore store, Language
220
222
String formattedQuery = String .format (coreConfiguration .getProperty ("AUTOCOMPLETE_QUERY" ), query );
221
223
222
224
/** formatted toJSONString because of te specific field names required in the UI **/
223
-
225
+
224
226
SearchKeywords keywords = getSearchKeywords (req , formattedQuery );
225
227
ValueList returnList = new ValueList ();
226
-
227
228
returnList .setValues (keywords .getKeywords ());
228
229
return returnList ;
229
230
}
230
231
231
232
private SearchKeywords getSearchKeywords (AutoCompleteRequest req , String query ) {
232
- try {
233
- LOGGER .debug ("Search auto comlete " + query );
234
- return searchService .searchForKeywords (req .getCollectionName (), query , AUTOCOMPLETE_ENTRIES_COUNT );
233
+ try {
234
+ LOGGER .debug ("Search auto comlete " + query );
235
+ return searchService .searchForKeywords (
236
+ req .getCollectionName (), query , AUTOCOMPLETE_ENTRIES_COUNT );
235
237
} catch (ServiceException e ) {
236
- throw new ServiceRuntimeException (e );
238
+ throw new ServiceRuntimeException (e );
237
239
}
238
-
239
240
}
240
-
241
-
242
241
}
0 commit comments