// 重写方法,保存对象的同时处理价格精度并生成HTML静态文件 @Override public String save(Product product) { HtmlConfig htmlConfig = TemplateConfigUtil.getHtmlConfig(HtmlConfig.PRODUCT_CONTENT); String htmlFilePath = htmlConfig.getHtmlFilePath(); product.setHtmlFilePath(htmlFilePath); String id = productDao.save(product); productDao.flush(); productDao.evict(product); product = productDao.load(id); if (product.getIsMarketable()) { htmlService.productContentBuildHtml(product); } return id; }
// 重写方法,更新对象的同时处理价格精度并重新生成HTML静态文件 @Override public void update(Product product) { String id = product.getId(); File htmlFile = new File(ServletActionContext.getServletContext().getRealPath(product.getHtmlFilePath())); if (htmlFile.exists()) { htmlFile.delete(); } productDao.update(product); productDao.flush(); productDao.evict(product); product = productDao.load(id); if (product.getIsMarketable()) { htmlService.productContentBuildHtml(product); } }
@Transactional(readOnly = true) public int buildSitemap() { int buildCount = 0; Template sitemapIndexTemplate = templateService.get("sitemapIndex"); Template sitemapTemplate = templateService.get("sitemap"); Map<String, Object> model = new HashMap<String, Object>(); List<String> staticPaths = new ArrayList<String>(); for (int step = 0, index = 0, first = 0, count = SITEMAP_MAX_SIZE; ; ) { try { model.put("index", index); String templatePath = sitemapTemplate.getTemplatePath(); String staticPath = FreemarkerUtils.process(sitemapTemplate.getStaticPath(), model); if (step == 0) { List<Article> articles = articleDao.findList(first, count, null, null); model.put("articles", articles); if (articles.size() < count) { step++; first = 0; count -= articles.size(); } else { buildCount += build(templatePath, staticPath, model); articleDao.clear(); articleDao.flush(); staticPaths.add(staticPath); model.clear(); index++; first += articles.size(); count = SITEMAP_MAX_SIZE; } } else if (step == 1) { List<Product> products = productDao.findList(first, count, null, null); model.put("products", products); if (products.size() < count) { step++; first = 0; count -= products.size(); } else { buildCount += build(templatePath, staticPath, model); productDao.clear(); productDao.flush(); staticPaths.add(staticPath); model.clear(); index++; first += products.size(); count = SITEMAP_MAX_SIZE; } } else if (step == 2) { List<Brand> brands = brandDao.findList(first, count, null, null); model.put("brands", brands); if (brands.size() < count) { step++; first = 0; count -= brands.size(); } else { buildCount += build(templatePath, staticPath, model); brandDao.clear(); brandDao.flush(); staticPaths.add(staticPath); model.clear(); index++; first += brands.size(); count = SITEMAP_MAX_SIZE; } } else if (step == 3) { List<Promotion> promotions = promotionDao.findList(first, count, null, null); model.put("promotions", promotions); buildCount += build(templatePath, staticPath, model); promotionDao.clear(); promotionDao.flush(); staticPaths.add(staticPath); if (promotions.size() < count) { model.put("staticPaths", staticPaths); buildCount += build( sitemapIndexTemplate.getTemplatePath(), sitemapIndexTemplate.getStaticPath(), model); break; } else { model.clear(); index++; first += promotions.size(); count = SITEMAP_MAX_SIZE; } } } catch (Exception e) { e.printStackTrace(); } } return buildCount; }