Rev 35434 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.controller;import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Set;import javax.servlet.http.HttpServletRequest;import org.apache.logging.log4j.Logger;import org.apache.logging.log4j.LogManager;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.transaction.annotation.Transactional;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.common.web.util.ResponseSender;import com.spice.profitmandi.dao.entity.dtr.Brand;import com.spice.profitmandi.dao.repository.dtr.BrandRepository;import com.spice.profitmandi.web.req.Category;@Controller@Transactional(rollbackFor = Throwable.class)public class BrandController {@Autowiredprivate ResponseSender<?> responseSender;private static final Logger LOGGER=LogManager.getLogger(BrandController.class);@Autowiredprivate BrandRepository brandRepository;@RequestMapping(value = ProfitMandiConstants.URL_BRAND_ALL, method=RequestMethod.GET)public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize){LOGGER.info("requested url : "+request.getRequestURL().toString());List<Brand> brands = brandRepository.selectAll(pageNumber, pageSize);List<Category> categories = new ArrayList<>();for(Brand brand : brands){Category category = new Category();category.setId(brand.getCategoryId());if(!categories.contains(category)){switch(brand.getCategoryId()){case 3:{category.setName("Mobiles");break;}case 5:{category.setName("Tablets");break;}case 6:{category.setName("Accessories");break;}}com.spice.profitmandi.web.req.Brand brandModel = new com.spice.profitmandi.web.req.Brand();brandModel.setId(brand.getId());brandModel.setName(brand.getName());Set<com.spice.profitmandi.web.req.Brand> brandsModels = new HashSet<>();brandsModels.add(brandModel);category.setBrands(brandsModels);categories.add(category);}else{Category foundCategory = categories.get(categories.indexOf(category));com.spice.profitmandi.web.req.Brand brandModel = new com.spice.profitmandi.web.req.Brand();brandModel.setId(brand.getId());brandModel.setName(brand.getName());foundCategory.getBrands().add(brandModel);}}return responseSender.ok(categories);}@RequestMapping(value = ProfitMandiConstants.URL_BRAND_ID, method=RequestMethod.GET)public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){LOGGER.info("requested url : "+request.getRequestURL().toString());try {return responseSender.ok(brandRepository.selectById(id));}catch (ProfitMandiBusinessException profitMandiBusinessException) {LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);return responseSender.badRequest(profitMandiBusinessException);}}}