Skip to content

Commit fb1b525

Browse files
author
Dima
committed
refactoring searchService and FilePathUtils
1 parent 1b06c93 commit fb1b525

File tree

6 files changed

+240
-295
lines changed

6 files changed

+240
-295
lines changed

sm-shop/src/main/java/com/salesmanager/shop/store/api/v1/search/SearchApi.java

+11-20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
package com.salesmanager.shop.store.api.v1.search;
22

3-
4-
import javax.inject.Inject;
5-
import javax.servlet.http.HttpServletRequest;
6-
import org.slf4j.Logger;
7-
import org.slf4j.LoggerFactory;
8-
import org.springframework.web.bind.annotation.PostMapping;
9-
import org.springframework.web.bind.annotation.RequestBody;
10-
import org.springframework.web.bind.annotation.RequestMapping;
11-
import org.springframework.web.bind.annotation.ResponseBody;
12-
import org.springframework.web.bind.annotation.RestController;
133
import com.salesmanager.core.model.merchant.MerchantStore;
144
import com.salesmanager.core.model.reference.language.Language;
155
import com.salesmanager.shop.model.catalog.SearchProductList;
@@ -20,6 +10,13 @@
2010
import com.salesmanager.shop.utils.LanguageUtils;
2111
import io.swagger.annotations.ApiImplicitParam;
2212
import io.swagger.annotations.ApiImplicitParams;
13+
import javax.inject.Inject;
14+
import javax.servlet.http.HttpServletRequest;
15+
import org.springframework.web.bind.annotation.PostMapping;
16+
import org.springframework.web.bind.annotation.RequestBody;
17+
import org.springframework.web.bind.annotation.RequestMapping;
18+
import org.springframework.web.bind.annotation.ResponseBody;
19+
import org.springframework.web.bind.annotation.RestController;
2320
import springfox.documentation.annotations.ApiIgnore;
2421

2522
/**
@@ -32,8 +29,6 @@
3229
@RequestMapping("/api/v1")
3330
public class SearchApi {
3431

35-
private static final Logger LOGGER = LoggerFactory.getLogger(SearchApi.class);
36-
3732
@Inject private SearchFacade searchFacade;
3833

3934
@Inject private StoreFacade storeFacade;
@@ -42,15 +37,11 @@ public class SearchApi {
4237

4338
/**
4439
* Search products from underlying elastic search
45-
*
46-
* @param searchRequest
47-
* @param request
48-
* @return
4940
*/
5041
@PostMapping("/search")
5142
@ApiImplicitParams({
52-
@ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"),
53-
@ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en")
43+
@ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"),
44+
@ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en")
5445
})
5546
public @ResponseBody SearchProductList search(
5647
@RequestBody SearchProductRequest searchRequest,
@@ -62,8 +53,8 @@ public class SearchApi {
6253

6354
@PostMapping("/search/autocomplete")
6455
@ApiImplicitParams({
65-
@ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"),
66-
@ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en")
56+
@ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"),
57+
@ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en")
6758
})
6859
public @ResponseBody ValueList autocomplete(
6960
@RequestBody SearchProductRequest searchRequest,

sm-shop/src/main/java/com/salesmanager/shop/store/controller/search/SearchController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public SearchProductList search(
166166
}
167167
168168
SearchResponse resp = searchService.search(merchantStore, language, json, max, start);
169-
return searchFacade.copySearchResponse(resp, merchantStore, start, max, l);
169+
return searchFacade.convertToSearchProductList(resp, merchantStore, start, max, l);
170170
171171
} catch (Exception e) {
172172
LOGGER.error("Exception occured while querying " + json,e);

sm-shop/src/main/java/com/salesmanager/shop/store/controller/search/facade/SearchFacade.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public interface SearchFacade {
3535
* @param searchResponse
3636
* @return
3737
*/
38-
public SearchProductList copySearchResponse(SearchResponse searchResponse, MerchantStore store, int start, int count, Language language) throws Exception;
38+
public SearchProductList convertToSearchProductList(SearchResponse searchResponse, MerchantStore store, int start, int count, Language language) throws Exception;
3939

4040
/**
4141
* List of keywords / autocompletes for a given word being typed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.salesmanager.shop.store.controller.search.facade;
22

3-
import java.util.ArrayList;
4-
import java.util.HashMap;
3+
import java.util.Collections;
54
import java.util.List;
65
import java.util.Map;
6+
import java.util.Map.Entry;
7+
import java.util.Optional;
8+
import java.util.function.Supplier;
79
import java.util.stream.Collectors;
810
import javax.inject.Inject;
911
import org.apache.commons.collections4.CollectionUtils;
@@ -45,29 +47,29 @@
4547

4648
@Service("searchFacade")
4749
public class SearchFacadeImpl implements SearchFacade {
48-
50+
4951
private static final Logger LOGGER = LoggerFactory.getLogger(SearchFacadeImpl.class);
50-
52+
5153
@Inject
5254
private SearchService searchService;
53-
55+
5456
@Inject
5557
private ProductService productService;
56-
58+
5759
@Inject
5860
private CategoryService categoryService;
59-
61+
6062
@Inject
6163
private PricingService pricingService;
62-
64+
6365
@Inject
6466
@Qualifier("img")
6567
private ImageFilePath imageUtils;
66-
68+
6769
@Inject
6870
private CoreConfiguration coreConfiguration;
6971

70-
72+
7173
private final static String CATEGORY_FACET_NAME = "categories";
7274
private final static String MANUFACTURER_FACET_NAME = "manufacturer";
7375
private final static int AUTOCOMPLETE_ENTRIES_COUNT = 15;
@@ -80,55 +82,50 @@ public class SearchFacadeImpl implements SearchFacade {
8082
@Override
8183
@Async
8284
public void indexAllData(MerchantStore store) throws Exception {
83-
84-
8585
List<Product> products = productService.listByStore(store);
86-
86+
8787
for(Product product : products) {
8888
searchService.index(store, product);
8989
}
90-
90+
9191
}
9292

9393
@Override
9494
public SearchProductList search(MerchantStore store, Language language, SearchProductRequest searchRequest) {
9595
String query = String.format(coreConfiguration.getProperty("SEARCH_QUERY"), searchRequest.getQuery());
9696
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);
9898
}
9999

100100
private SearchResponse search(
101101
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);
107107
}
108108
}
109109

110110
@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+
113113
SearchProductList returnList = new SearchProductList();
114114
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+
126123
ProductCriteria searchCriteria = new ProductCriteria();
127124
searchCriteria.setMaxCount(count);
128125
searchCriteria.setStartIndex(start);
129126
searchCriteria.setProductIds(ids);
130127
searchCriteria.setAvailable(true);
131-
128+
132129
ProductList productList = productService.listByStore(merchantStore, language, searchCriteria);
133130

134131
List<ReadableProduct> readableProducts = productList.getProducts()
@@ -139,52 +136,57 @@ public SearchProductList copySearchResponse(SearchResponse searchResponse, Merch
139136
returnList.getProducts().addAll(readableProducts);
140137
returnList.setProductCount(productList.getProducts().size());
141138
}
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;
186157
}
187158

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+
188190
private ReadableCategory convertCategoryToReadableCategory(MerchantStore merchantStore,
189191
Language language, Map<String, Long> productCategoryCount, Category category) {
190192
ReadableCategoryPopulator populator = new ReadableCategoryPopulator();
@@ -220,23 +222,20 @@ public ValueList autocompleteRequest(String query, MerchantStore store, Language
220222
String formattedQuery = String.format(coreConfiguration.getProperty("AUTOCOMPLETE_QUERY"), query);
221223

222224
/** formatted toJSONString because of te specific field names required in the UI **/
223-
225+
224226
SearchKeywords keywords = getSearchKeywords(req, formattedQuery);
225227
ValueList returnList = new ValueList();
226-
227228
returnList.setValues(keywords.getKeywords());
228229
return returnList;
229230
}
230231

231232
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);
235237
} catch (ServiceException e) {
236-
throw new ServiceRuntimeException(e);
238+
throw new ServiceRuntimeException(e);
237239
}
238-
239240
}
240-
241-
242241
}

sm-shop/src/main/java/com/salesmanager/shop/store/facade/content/ContentFacadeImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private ReadableContentPage convertContentToReadableContentPage(MerchantStore st
145145
page.setMetaDetails(contentDescription.get().getMetatagDescription());
146146
page.setContentType(ContentType.PAGE.name());
147147
page.setCode(content.getCode());
148-
page.setPath(fileUtils.buildStaticFilePath(store, contentDescription.get().getSeUrl()));
148+
page.setPath(fileUtils.buildStaticFilePath(store.getCode(), contentDescription.get().getSeUrl()));
149149
return page;
150150
}
151151

0 commit comments

Comments
 (0)