use of com.salesmanager.shop.model.catalog.category.ReadableCategory in project shopizer by shopizer-ecommerce.
the class ShoppingCategoryRESTController method getCategory.
@RequestMapping(value = "/public/{store}/category/{id}", method = RequestMethod.GET)
@ResponseBody
public ReadableCategory getCategory(@PathVariable final String store, @PathVariable Long id, HttpServletRequest request, HttpServletResponse response) {
try {
/**
* default routine *
*/
MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
if (merchantStore != null) {
if (!merchantStore.getCode().equals(store)) {
merchantStore = null;
}
}
if (merchantStore == null) {
merchantStore = merchantStoreService.getByCode(store);
}
if (merchantStore == null) {
LOGGER.error("Merchant store is null for code " + store);
response.sendError(503, "Merchant store is null for code " + store);
return null;
}
Language language = languageUtils.getRequestLanguage(request, response);
/**
* Language language = merchantStore.getDefaultLanguage();
*
* Map<String,Language> langs = languageService.getLanguagesMap();
*
* if(!StringUtils.isBlank(request.getParameter(Constants.LANG))) {
* String lang = request.getParameter(Constants.LANG);
* if(lang!=null) {
* language = langs.get(language);
* }
* }
*
* if(language==null) {
* language = merchantStore.getDefaultLanguage();
* }
*/
/**
* end default routine *
*/
ReadableCategory category = categoryFacade.getById(merchantStore, id, language);
if (category == null) {
response.sendError(503, "Invalid category id");
return null;
}
return category;
} catch (Exception e) {
LOGGER.error("Error while saving category", e);
try {
response.sendError(503, "Error while saving category " + e.getMessage());
} catch (Exception ignore) {
}
return null;
}
}
use of com.salesmanager.shop.model.catalog.category.ReadableCategory in project shopizer by shopizer-ecommerce.
the class BreadcrumbsUtils method buildCategoryBreadcrumb.
public Breadcrumb buildCategoryBreadcrumb(ReadableCategory categoryClicked, MerchantStore store, Language language, String contextPath) throws Exception {
/**
* Rebuild breadcrumb *
*/
BreadcrumbItem home = new BreadcrumbItem();
home.setItemType(BreadcrumbItemType.HOME);
home.setLabel(messages.getMessage(Constants.HOME_MENU_KEY, LocaleUtils.getLocale(language)));
home.setUrl(filePathUtils.buildStoreUri(store, contextPath) + Constants.SHOP_URI);
Breadcrumb breadCrumb = new Breadcrumb();
breadCrumb.setLanguage(language);
List<BreadcrumbItem> items = new ArrayList<BreadcrumbItem>();
items.add(home);
List<String> categoryIds = parseCategoryLineage(categoryClicked.getLineage());
List<Long> ids = new ArrayList<Long>();
for (String c : categoryIds) {
ids.add(Long.parseLong(c));
}
ids.add(categoryClicked.getId());
List<Category> categories = categoryService.listByIds(store, ids, language);
// category path - use lineage
for (Category c : categories) {
BreadcrumbItem categoryBreadcrump = new BreadcrumbItem();
categoryBreadcrump.setItemType(BreadcrumbItemType.CATEGORY);
categoryBreadcrump.setLabel(c.getDescription().getName());
categoryBreadcrump.setUrl(filePathUtils.buildCategoryUrl(store, contextPath, c.getDescription().getSeUrl()));
items.add(categoryBreadcrump);
}
breadCrumb.setUrlRefContent(buildBreadCrumb(ids));
breadCrumb.setBreadCrumbs(items);
breadCrumb.setItemType(BreadcrumbItemType.CATEGORY);
return breadCrumb;
}
use of com.salesmanager.shop.model.catalog.category.ReadableCategory in project shopizer by shopizer-ecommerce.
the class StoreFilter method getCategories.
/**
* @param store
* @param language
* @return
* @throws Exception
*/
// private Map<String, List<Category>> getCategories(MerchantStore store,
// Language language)
// throws Exception {
private Map<String, List<ReadableCategory>> getCategories(MerchantStore store, Language language) throws Exception {
// Map<String, List<Category>> objects = new ConcurrentHashMap<String,
// List<Category>>();
Map<String, List<ReadableCategory>> objects = new ConcurrentHashMap<String, List<ReadableCategory>>();
/**
* returns categories with required depth, 0 = root category, 1 = root +
* 1 layer child ...)
*/
List<Category> categories = categoryService.getListByDepth(store, 0, language);
ReadableCategoryPopulator readableCategoryPopulator = new ReadableCategoryPopulator();
Map<String, ReadableCategory> subs = new ConcurrentHashMap<String, ReadableCategory>();
if (categories != null && categories.size() > 0) {
// create a Map<String,List<Content>
for (Category category : categories) {
if (category.isVisible()) {
// if(category.getDepth().intValue()==0) {
// ReadableCategory readableCategory = new
// ReadableCategory();
// readableCategoryPopulator.populate(category,
// readableCategory, store, language);
Set<CategoryDescription> descriptions = category.getDescriptions();
for (CategoryDescription description : descriptions) {
Language lang = description.getLanguage();
ReadableCategory readableCategory = new ReadableCategory();
readableCategoryPopulator.populate(category, readableCategory, store, language);
String key = new StringBuilder().append(store.getId()).append("_").append(Constants.CATEGORIES_CACHE_KEY).append("-").append(lang.getCode()).toString();
if (category.getDepth().intValue() == 0) {
// List<Category> cacheCategories = null;
List<ReadableCategory> cacheCategories = null;
if (objects == null || objects.size() == 0) {
// objects = new HashMap<String,
// List<Category>>();
objects = new HashMap<String, List<ReadableCategory>>();
}
if (!objects.containsKey(key)) {
// cacheCategories = new ArrayList<Category>();
cacheCategories = new ArrayList<ReadableCategory>();
objects.put(key, cacheCategories);
} else {
cacheCategories = objects.get(key.toString());
if (cacheCategories == null) {
LOGGER.error("Cannot find categories key in cache " + key);
continue;
}
}
// cacheCategories.add(category);
cacheCategories.add(readableCategory);
} else {
subs.put(lang.getCode(), readableCategory);
}
}
}
}
}
return objects;
}
use of com.salesmanager.shop.model.catalog.category.ReadableCategory in project shopizer by shopizer-ecommerce.
the class ShoppingCategoryController method displayCategory.
@SuppressWarnings("unchecked")
private String displayCategory(final String friendlyUrl, final String ref, Model model, HttpServletRequest request, HttpServletResponse response, Locale locale) throws Exception {
MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
// set ref as request attribute
String encoded = SanitizeUtils.getSafeRequestParamString(ref);
if (!encoded.equals(ref)) {
// possible xss
throw new Exception("Wrong input parameter [" + ref + "]");
}
request.setAttribute("ref", encoded);
// get category
Category category = categoryService.getBySeUrl(store, friendlyUrl);
Language language = (Language) request.getAttribute("LANGUAGE");
if (category == null) {
LOGGER.error("No category found for friendlyUrl " + friendlyUrl);
// redirect on page not found
return PageBuilderUtils.build404(store);
}
if (!category.isVisible()) {
return PageBuilderUtils.buildHomePage(store);
}
ReadableCategoryPopulator populator = new ReadableCategoryPopulator();
ReadableCategory categoryProxy = populator.populate(category, new ReadableCategory(), store, language);
Breadcrumb breadCrumb = breadcrumbsUtils.buildCategoryBreadcrumb(categoryProxy, store, language, request.getContextPath());
request.getSession().setAttribute(Constants.BREADCRUMB, breadCrumb);
request.setAttribute(Constants.BREADCRUMB, breadCrumb);
// meta information
PageInformation pageInformation = new PageInformation();
pageInformation.setPageDescription(categoryProxy.getDescription().getMetaDescription());
pageInformation.setPageKeywords(categoryProxy.getDescription().getKeyWords());
pageInformation.setPageTitle(categoryProxy.getDescription().getTitle());
pageInformation.setPageUrl(categoryProxy.getDescription().getFriendlyUrl());
// ** retrieves category id drill down**//
String lineage = new StringBuilder().append(category.getLineage()).append(Constants.CATEGORY_LINEAGE_DELIMITER).toString();
request.setAttribute(Constants.REQUEST_PAGE_INFORMATION, pageInformation);
List<Category> categs = categoryService.getListByLineage(store, lineage);
categs.add(category);
StringBuilder subCategoriesCacheKey = new StringBuilder();
subCategoriesCacheKey.append(store.getId()).append("_").append(category.getId()).append("_").append(Constants.SUBCATEGORIES_CACHE_KEY).append("-").append(language.getCode());
StringBuilder subCategoriesMissed = new StringBuilder();
subCategoriesMissed.append(subCategoriesCacheKey.toString()).append(Constants.MISSED_CACHE_KEY);
List<BigDecimal> prices = new ArrayList<BigDecimal>();
List<ReadableCategory> subCategories = null;
Map<Long, Long> countProductsByCategories = null;
if (store.isUseCache()) {
// get from the cache
subCategories = (List<ReadableCategory>) cache.getFromCache(subCategoriesCacheKey.toString());
if (subCategories == null) {
// get from missed cache
// Boolean missedContent = (Boolean)cache.getFromCache(subCategoriesMissed.toString());
// if(missedContent==null) {
countProductsByCategories = getProductsByCategory(store, categs);
subCategories = getSubCategories(store, category, countProductsByCategories, language, locale);
if (subCategories != null) {
cache.putInCache(subCategories, subCategoriesCacheKey.toString());
} else {
// cache.putInCache(new Boolean(true), subCategoriesCacheKey.toString());
}
// }
}
} else {
countProductsByCategories = getProductsByCategory(store, categs);
subCategories = getSubCategories(store, category, countProductsByCategories, language, locale);
}
// Parent category
ReadableCategory parentProxy = null;
if (category.getParent() != null) {
Category parent = categoryService.getById(category.getParent().getId(), store.getId());
parentProxy = populator.populate(parent, new ReadableCategory(), store, language);
}
// ** List of manufacturers **//
List<ReadableManufacturer> manufacturerList = getManufacturersByProductAndCategory(store, category, categs, language);
model.addAttribute("manufacturers", manufacturerList);
model.addAttribute("parent", parentProxy);
model.addAttribute("category", categoryProxy);
model.addAttribute("subCategories", subCategories);
if (parentProxy != null) {
request.setAttribute(Constants.LINK_CODE, parentProxy.getDescription().getFriendlyUrl());
}
/**
* template *
*/
StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Category.category).append(".").append(store.getStoreTemplate());
return template.toString();
}
use of com.salesmanager.shop.model.catalog.category.ReadableCategory in project shopizer by shopizer-ecommerce.
the class ShoppingCategoryController method getCategories.
/**
* Returns all categories for a given MerchantStore
*/
@RequestMapping("/services/public/category/{store}/{language}")
@ResponseBody
public List<ReadableCategory> getCategories(@PathVariable final String language, @PathVariable final String store, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Language> langs = languageService.getLanguagesMap();
Language l = langs.get(language);
if (l == null) {
l = languageService.getByCode(Constants.DEFAULT_LANGUAGE);
}
MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
if (merchantStore != null) {
if (!merchantStore.getCode().equals(store)) {
// reset for the current request
merchantStore = null;
}
}
if (merchantStore == null) {
merchantStore = merchantStoreService.getByCode(store);
}
if (merchantStore == null) {
LOGGER.error("Merchant store is null for code " + store);
// TODO localized message
response.sendError(503, "Merchant store is null for code " + store);
return null;
}
List<Category> categories = categoryService.listByStore(merchantStore, l);
ReadableCategoryPopulator populator = new ReadableCategoryPopulator();
List<ReadableCategory> returnCategories = new ArrayList<ReadableCategory>();
for (Category category : categories) {
ReadableCategory categoryProxy = populator.populate(category, new ReadableCategory(), merchantStore, l);
returnCategories.add(categoryProxy);
}
return returnCategories;
}
Aggregations