Java tutorial
package com.yaodu.framework.controller.product; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; import javax.annotation.Resource; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.support.RequestContextUtils; import com.yaodu.framework.datamodel.Pager; import com.yaodu.framework.datamodel.customerinfo.CustomerInfo; import com.yaodu.framework.datamodel.product.HotTopic; import com.yaodu.framework.datamodel.product.MarketInfo; import com.yaodu.framework.datamodel.product.Menu; import com.yaodu.framework.datamodel.product.NIds; import com.yaodu.framework.datamodel.product.ProductBascInfo; import com.yaodu.framework.datamodel.product.SearchCondition; import com.yaodu.framework.datamodel.product.SearchKeyWord; import com.yaodu.framework.dbservice.myfavorite.IMyFavoriteControllerService; import com.yaodu.framework.dbservice.product.IMenuService; import com.yaodu.framework.dbservice.product.IProductBasicInfoService; import com.yaodu.framework.dbservice.search.IProductSearchService; import com.yaodu.framework.dbservice.search.IProductSolrjSearchService; import com.yaodu.framework.dbservice.search.ISearchKeyWordService; import com.yaodu.framework.utils.service.PageListUtils; import com.yaodu.framework.utils.service.ProductUtils; import com.yaodu.framework.utils.service.search.SearchUtil; /** * * @description ? * @fileName ProductSearchController.java * @author Ali Cao * @date 201532 * * All CopyRights reserved by YaoDu */ @Controller @RequestMapping("/productSearch") public class ProductSearchController { public static final Logger log = Logger.getLogger(ProductSearchController.class); @Resource(name = "productSearchService") private IProductSearchService productSearchService; @Resource(name = "ProductSolrjSearchService") private IProductSolrjSearchService productSolrjSearchService; @Resource(name = "menuService") private IMenuService menuService; @Resource(name = "productBasicInfoService") private IProductBasicInfoService productBasicInfoService; @Resource(name = "keyWordService") private ISearchKeyWordService keyWordService; @Resource(name = "favoriteService") private IMyFavoriteControllerService myFavoriteControllerService; private StringBuffer targetStr = null; /** * * @description ?? * @author Ali Cao * @date 201537 * @param request * @param searchTxt * @param model * @return */ @RequestMapping(value = "/searchProductBaseInfo") public ModelAndView searchProductBaseInfo(@RequestParam(required = false, defaultValue = "1") int pageNo, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false, defaultValue = "2") int showModel, HttpServletRequest request, String searchTxt, ModelAndView model) { long l1 = new Date().getTime(); if (log.isDebugEnabled()) { log.debug("entering searchProductBaseInfo(String,Model) method"); } CustomerInfo temCustomerInfo = (CustomerInfo) request.getSession().getAttribute("customerInfo"); Integer memberId = null; if (temCustomerInfo != null) { memberId = temCustomerInfo.getId(); } Map<String, Set<String>> drugTypeMap = null; List<Map.Entry<String, String>> therapyDomainMap = null; // ? List<ProductBascInfo> newProList = null; HttpSession session = request.getSession(); Pager pager = (Pager) session.getAttribute("page"); //add 2015-05-04 yannis ? bug 898 ????2040????????20 if (pageSize == null) { if (pager == null) { pageSize = 20; } else { pageSize = pager.getPageSize(); } } @SuppressWarnings("unchecked") List<ProductBascInfo> productsList = (List<ProductBascInfo>) session.getAttribute("productsList"); Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request); // ?json?????? if (null != searchTxt && !"".equals(searchTxt)) { SearchCondition searchCondition = SearchUtil.getSearchCondition(searchTxt); if (searchCondition != null) { session.setAttribute("sortByTime", searchCondition.getSortByTime()); session.setAttribute("sortByCharactor", searchCondition.getSortByCharactor()); if ((searchCondition.getSortByTime() == null || "".equals(searchCondition.getSortByTime())) && (null == searchCondition.getSortByCharactor() || "".equals(searchCondition.getSortByCharactor()))) { searchCondition.setSortByTime("d"); session.setAttribute("sortByTime", searchCondition.getSortByTime()); } List<NIds> list = null; // ?? ? if (searchCondition.getSearchTxt() != null && !"".equals(searchCondition.getSearchTxt())) { list = getIdsByKeyWord(searchCondition.getSearchTxt()); // ?? ?HotTpic } else if (searchCondition.getHotTopicId() != null && !"".equals(searchCondition.getHotTopicId())) { String name = SearchUtil.getHotTopicNameById(productSearchService, Integer.valueOf(searchCondition.getHotTopicId()).intValue()); Map<String, String> hotTopic = new HashMap<String, String>(); hotTopic.put(searchCondition.getHotTopicId(), name); model.addObject("hotTopic", hotTopic); model.addObject("hotTopicName", name); list = getIdsByHotpic(searchCondition.getHotTopicId()); // ?? } else if ((searchCondition.getDrugClassificationOne() != null && !"".equals(searchCondition.getDrugClassificationOne())) || (((searchCondition.getDrugClassificationOne() != null && !"".equals(searchCondition.getDrugClassificationOne())) && (searchCondition.getDrugClassificationTwo() != null && !"".equals(searchCondition.getDrugClassificationTwo()))))) { String classificationOneId = searchCondition.getDrugClassificationOne(); if (!classificationOneId.isEmpty()) { Map<String, String> drugClassificationOne = new HashMap<String, String>(); String name = ""; Menu menu = SearchUtil.getClassificationNameById(productSearchService, classificationOneId); if (null != menu) { if (!locale.getCountry().equals("CN")) { name = menu.getLabelEn(); } else { name = menu.getName(); } } drugClassificationOne.put(classificationOneId, name); model.addObject("drugClassificationOne", drugClassificationOne); model.addObject("drugClassificationOneName", name); } String classificationTwoId = searchCondition.getDrugClassificationTwo(); if (!classificationTwoId.isEmpty()) { Map<String, String> drugClassificationTwo = new HashMap<String, String>(); String name = ""; Menu menu = SearchUtil.getClassificationNameById(productSearchService, classificationTwoId); if (null != menu) { if (!locale.getCountry().equals("CN")) { name = menu.getLabelEn(); } else { name = menu.getName(); } } drugClassificationTwo.put(classificationTwoId, name); model.addObject("drugClassificationTwo", drugClassificationTwo); model.addObject("drugClassificationTwoName", name); } list = getIdsByClassification(searchCondition.getDrugClassificationOne(), searchCondition.getDrugClassificationTwo()); // ?? ??? } else if (searchCondition.getTargets() != null && searchCondition.getTargets().size() > 0) { list = productSolrjSearchService.getIdsByTarget(searchTxt); } List<String> nIds = changeList(list); searchCondition.setnIds(nIds); // customer first research if (null == productsList || pageNo == 1) { productsList = null; pager = null; pager = new Pager(); pager.setPageSize(pageSize); if (searchCondition.getSearchTxt() != null && !"".equals(searchCondition.getSearchTxt())) { if (nIds != null && nIds.size() > 0) { productsList = productSolrjSearchService.searchProductBaseInfo(searchCondition); } } else if (searchCondition.getHotTopicId() != null && !"".equals(searchCondition.getHotTopicId())) { if (nIds != null && nIds.size() > 0) { productsList = productSolrjSearchService.searchProductBaseInfo(searchCondition); } } else { productsList = productSolrjSearchService.searchProductBaseInfo(searchCondition); } session.setAttribute("productsList", productsList); session.setAttribute("page", pager); pager.setRecordCount(productsList == null ? 0 : productsList.size()); pager.setPageCount(pager.getPageCount()); } PageListUtils.fillPageInfo(pageNo, pager); newProList = PageListUtils.getNewProList(productsList, pager); newProList = PageListUtils.fillNewList(newProList, myFavoriteControllerService, memberId); drugTypeMap = getDrugType(productsList); therapyDomainMap = getTherapyDomain(targetStr, locale); } // ????? if (null != searchCondition.getTherapyDomain() && searchCondition.getTherapyDomain().size() > 0) { List<String> lst = searchCondition.getTherapyDomain(); List<Map<String, String>> lstTherapyDomain = null; if (lst != null && lst.size() > 0) { lstTherapyDomain = new ArrayList<Map<String, String>>(); for (int i = 0; i < lst.size(); i++) { Map<String, String> therapyDomain = new HashMap<String, String>(); String cat_id = lst.get(i); String name = ""; Menu menu = SearchUtil.getClassificationNameById(productSearchService, cat_id); if (null != menu) { if (!locale.getCountry().equals("CN")) { name = menu.getLabelEn(); } else { name = menu.getName(); } } therapyDomain.put(cat_id, name); lstTherapyDomain.add(therapyDomain); } } model.addObject("lstTherapyDomain", lstTherapyDomain); } model.addObject("searchTxt", searchCondition.getSearchTxt()); model.addObject("searchCondition", searchCondition); } else { // find all Object sortByTime = session.getAttribute("sortByTime"); Object sortByChar = session.getAttribute("sortByCharactor"); SearchCondition searchCondition = new SearchCondition(); if (null != sortByTime && !"".equals(sortByTime)) { searchCondition.setSortByTime(sortByTime.toString()); } else if (null != sortByChar && !"".equals(sortByChar)) { searchCondition.setSortByCharactor(sortByChar.toString()); } else { searchCondition.setSortByTime("d"); session.setAttribute("sortByTime", "d"); } if (null == productsList || pageNo == 1) { productsList = null; pager = null; pager = new Pager(); pager.setPageSize(pageSize); productsList = productSolrjSearchService.findAllProducts(searchCondition); session.setAttribute("productsList", productsList); session.setAttribute("page", pager); pager.setRecordCount(productsList.size()); pager.setPageCount(pager.getPageCount()); } PageListUtils.fillPageInfo(pageNo, pager); newProList = PageListUtils.getNewProList(productsList, pager); newProList = PageListUtils.fillNewList(newProList, myFavoriteControllerService, null); drugTypeMap = getDrugType(productsList); therapyDomainMap = getTherapyDomain(targetStr, locale); } List<ProductBascInfo> recentlyViewedList = new ProductUtils().getRecentlyViewedList(request, productSolrjSearchService); model.addObject("drugTypeMap", drugTypeMap); model.addObject("therapyDomain", therapyDomainMap); model.addObject("newProList", newProList); model.addObject("recentlyViewedList", recentlyViewedList); model.addObject("pager", pager); // hotTopic? add GaoFei 2015-03-09 List<HotTopic> hotTopicList = productBasicInfoService.getHotTopicList(); model.addObject("hotTopicList", hotTopicList); //?? model.addObject("showModel", showModel); long l2 = new Date().getTime(); log.info("search pros used times ----------->" + (l2 - l1) + " ms"); model.setViewName("product/searchResults"); return model; } /** * * @description ?? * @author Ali Cao * @date 201534 * @param searchTxt * @return */ private List<NIds> getIdsByKeyWord(String searchTxt) { List<NIds> lst = productSolrjSearchService.getIdsByKeyWord(searchTxt); return lst; } /** * * @description ????? * @author Ali Cao * @date 201537 * @param classificationOne * @param classificationTwo * @return */ private List<NIds> getIdsByClassification(String classificationOne, String classificationTwo) { List<NIds> lst = productSolrjSearchService.getIdsByClassification(classificationOne, classificationTwo); return lst; } /** * * @description ?? * @author Ali Cao * @date 201536 * @param hotpic * @return */ private List<NIds> getIdsByHotpic(String hotpic) { List<NIds> lst = productSearchService.getIdsByHotpic(hotpic); return lst; } /** * * @description List??ListString? * @author Ali Cao * @date 201534 * @param lst * @return */ public List<String> changeList(List<NIds> lst) { if (lst != null) { List<String> list = new ArrayList<String>(); for (int i = 0; i < lst.size(); i++) { NIds nIds = (NIds) lst.get(i); if (nIds != null) { list.add(nIds.getnId()); } } return list; } else { return null; } } @RequestMapping(value = "/findSearchKeyWords", method = RequestMethod.POST) @ResponseBody public List<SearchKeyWord> findSearchKeyWords() { int num = 5; List<SearchKeyWord> keyWordList = keyWordService.findSearchKeyWords(num); return keyWordList; } @RequestMapping(value = "/findAllProducts") public ModelAndView findAllProducts(@RequestParam(required = false, defaultValue = "1") int pageNo, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false, defaultValue = "2") int showModel, HttpServletRequest request, ModelAndView model) { log.debug("find all products"); long l1 = new Date().getTime(); List<ProductBascInfo> newProList = null; HttpSession session = request.getSession(); Object sortByTime = session.getAttribute("sortByTime"); Object sortByChar = session.getAttribute("sortByCharactor"); SearchCondition searchCondition = new SearchCondition(); if (null != sortByTime && !"".equals(sortByTime)) { searchCondition.setSortByTime(sortByTime.toString()); } else if (null != sortByChar && !"".equals(sortByChar)) { searchCondition.setSortByCharactor(sortByChar.toString()); } else { searchCondition.setSortByTime("d"); session.setAttribute("sortByTime", "d"); } Pager pager = (Pager) session.getAttribute("page"); //add 2015-05-04 yannis ? bug 898 ????2040????????20 if (pageSize == null) { if (pager == null) { pageSize = 20; } else { pageSize = pager.getPageSize(); } } @SuppressWarnings("unchecked") List<ProductBascInfo> productsList = (List<ProductBascInfo>) session.getAttribute("productsList"); CustomerInfo temCustomerInfo = (CustomerInfo) request.getSession().getAttribute("customerInfo"); Integer memberId = null; if (temCustomerInfo != null) { memberId = temCustomerInfo.getId(); } // customer first research if (null == productsList || pageNo == 1) { productsList = null; pager = null; pager = new Pager(); pager.setPageSize(pageSize); productsList = productSolrjSearchService.findAllProducts(searchCondition); session.setAttribute("productsList", productsList); session.setAttribute("page", pager); pager.setRecordCount(productsList.size()); pager.setPageCount(pager.getPageCount()); } PageListUtils.fillPageInfo(pageNo, pager); newProList = PageListUtils.getNewProList(productsList, pager); newProList = PageListUtils.fillNewList(newProList, myFavoriteControllerService, memberId); Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request); Map<String, Set<String>> drugTypeMap = getDrugType(productsList); List<Map.Entry<String, String>> therapyDomainMap = getTherapyDomain(targetStr, locale); model.addObject("therapyDomain", therapyDomainMap); model.addObject("drugTypeMap", drugTypeMap); model.addObject("pager", pager); model.addObject("newProList", newProList); List<ProductBascInfo> recentlyViewedList = getRecentlyViewedList(request); model.addObject("recentlyViewedList", recentlyViewedList); // hotTopic? add GaoFei 2015-03-09 List<HotTopic> hotTopicList = productBasicInfoService.getHotTopicList(); model.addObject("hotTopicList", hotTopicList); //?? 2015-04-13 model.addObject("showModel", showModel); model.setViewName("product/searchResults"); long l2 = new Date().getTime(); log.info("find all pros used times ----------->" + (l2 - l1) + " ms"); return model; } // Get Recently Viewed private List<ProductBascInfo> getRecentlyViewedList(HttpServletRequest request) { List<ProductBascInfo> recentlyViewedList = null; if (null != request) { Cookie[] cookie = request.getCookies(); if (null == cookie) { return recentlyViewedList; } for (int i = 0; i < cookie.length; i++) { Cookie cook = cookie[i]; if (cook.getName().equalsIgnoreCase("relatedProducts")) { // ? if (!"".equals(cook.getValue().toString())) { final String[] arrs = cook.getValue().split("\\|"); recentlyViewedList = productSolrjSearchService.findProductsByIds(arrs); Collections.sort(recentlyViewedList, new Comparator<ProductBascInfo>() { public int compare(ProductBascInfo p1, ProductBascInfo p2) { return Integer.valueOf(ArrayUtils.indexOf(arrs, p1.getnId())) .compareTo(ArrayUtils.indexOf(arrs, p2.getnId())); } }); } break; } } } return recentlyViewedList; } /** * * @description ??????? * @author Ali Cao * @date 201535 * @param list * @param locale * @return */ public Map<String, Set<String>> getDrugType(List<ProductBascInfo> list) { Map<String, Set<String>> typeMap = new HashMap<String, Set<String>>(); targetStr = new StringBuffer(); if (list != null && list.size() > 0) { Set<String> category = new HashSet<String>(); Set<String> marketTimeSet = new TreeSet<String>(new Comparator<String>() { public int compare(String str1, String str2) { return str2.compareTo(str1); } }); Set<String> marketAreaSet = new TreeSet<String>(); Set<String> searchCompany = new TreeSet<String>(); Set<String> research = new TreeSet<String>(); for (int i = 0, j = list.size(); i < j; i++) { ProductBascInfo pbi = (ProductBascInfo) list.get(i); // ?profile if (StringUtils.isNotEmpty(pbi.getProfile())) { pbi.setProfile(new ProductDetailsController().delHTMLTag(pbi.getProfile())); } // ?? if (StringUtils.isNotEmpty(pbi.getDrugType())) { category.add(pbi.getDrugType()); } // ??? if (StringUtils.isNotEmpty(pbi.getRAndDCompany())) { searchCompany.add(pbi.getRAndDCompany()); } // ??? if (StringUtils.isNotEmpty(pbi.getLicensee())) { String[] str = pbi.getLicensee().split(","); for (int m = 0, n = str.length; m < n; m++) { searchCompany.add(str[m]); } } // ? if (StringUtils.isNotEmpty(pbi.getDrugClassificationOne())) { targetStr.append(pbi.getDrugClassificationOne() + ","); } // ?? if (StringUtils.isNotEmpty(pbi.getRAndDStage())) { research.add(pbi.getRAndDStage()); } List<MarketInfo> lstMkt = pbi.getMarketInfoList(); // ?? if (lstMkt != null && lstMkt.size() > 0) { for (int m = 0; m < lstMkt.size(); m++) { MarketInfo marketInfo = (MarketInfo) lstMkt.get(m); String approvalData = marketInfo.getApprovalDate(); String approvalCountry = marketInfo.getApprovedCountry(); if (StringUtils.isNotEmpty(approvalData)) { marketTimeSet.add(approvalData.substring(0, 4)); } if (StringUtils.isNotEmpty(approvalCountry)) { marketAreaSet.add(approvalCountry); } } } } typeMap.put("category", category); typeMap.put("marketTimeSet", marketTimeSet); typeMap.put("marketAreaSet", marketAreaSet); typeMap.put("searchCompany", searchCompany); typeMap.put("research", research); } return typeMap; } public List<Map.Entry<String, String>> getTherapyDomain(StringBuffer targetSB, Locale locale) { List<Map.Entry<String, String>> therapyDomainMap = new ArrayList<Map.Entry<String, String>>(); if (null != targetSB && !"".equals(targetSB)) { therapyDomainMap = fillTargetSet(targetStr, therapyDomainMap, locale); } return therapyDomainMap; } private List<Map.Entry<String, String>> fillTargetSet(StringBuffer targetStr, List<Map.Entry<String, String>> therapyDomainMap, Locale locale) { if (!"".equals(targetStr)) { Set<String> typeIdSet = new HashSet<String>(); String[] arrStrs = targetStr.toString().split(","); for (int k = 0, h = arrStrs.length; k < h; k++) { if (StringUtils.isNotEmpty(arrStrs[k])) { typeIdSet.add(arrStrs[k]); } } if (typeIdSet.isEmpty()) { return therapyDomainMap; } List<Menu> menuList = menuService.findMenuListByIds(typeIdSet.toArray()); if (null != menuList && !menuList.isEmpty()) { Map<String, String> therapyDomainMapTemp = new TreeMap<String, String>(); Menu tempMenu = null; for (int i = 0, j = menuList.size(); i < j; i++) { //2015-05-01 working day //yannis ?others? if (null != menuList.get(i).getLabelEn() && "Others".equalsIgnoreCase(menuList.get(i).getLabelEn())) { tempMenu = menuList.get(i); continue; } if ("zh_CN".equals(locale.toString())) { therapyDomainMapTemp.put(String.valueOf(menuList.get(i).getId()), menuList.get(i).getName()); } else if ("en_US".equals(locale.toString())) { therapyDomainMapTemp.put(String.valueOf(menuList.get(i).getId()), menuList.get(i).getLabelEn()); } } therapyDomainMap = sortMap(therapyDomainMap, therapyDomainMapTemp); if (null != tempMenu) { Map<String, String> tempMap = new HashMap<String, String>(); if ("zh_CN".equals(locale.toString())) { tempMap.put(String.valueOf(tempMenu.getId()), tempMenu.getName()); } else if ("en_US".equals(locale.toString())) { tempMap.put(String.valueOf(tempMenu.getId()), tempMenu.getLabelEn()); } Iterator itor = tempMap.entrySet().iterator(); while (itor.hasNext()) { Entry entry = (Entry) itor.next(); therapyDomainMap.add(entry); } } } } return therapyDomainMap; } /** * @description * @author Ali Cao * @date 2015314 * @param therapyDomainMap * @param therapyDomainMapTemp */ private List<Map.Entry<String, String>> sortMap(List<Map.Entry<String, String>> therapyDomainMap, Map<String, String> therapyDomainMapTemp) { // map.entrySet()??list List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>( therapyDomainMapTemp.entrySet()); // ??? Collections.sort(list, new Comparator<Map.Entry<String, String>>() { // ??? public int compare(Entry<String, String> o1, Entry<String, String> o2) { return o1.getValue().compareTo(o2.getValue()); } }); return list; } /** * @see : ?hotTopicID? * @param hotTopicID * @return */ @RequestMapping(value = "/getProductInfoByHotTopicID", method = RequestMethod.GET) public ModelAndView getProductInfoByHotTopicID(HttpServletRequest request, ModelAndView model, int hotTopicID) { List<ProductBascInfo> productBasicInfo = new ArrayList<ProductBascInfo>(); // TODO ?hotTopicID?? model.addObject("productList", productBasicInfo); // hotTopic? List<HotTopic> hotTopicList = productBasicInfoService.getHotTopicList(); model.addObject("hotTopicList", hotTopicList); model.setViewName("product/searchResults"); return model; } }