Subversion Repositories SmartDukaan

Rev

Rev 23419 | Rev 23556 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
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.enumuration.DateTimePattern;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.CreateSchemeRequest;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.model.SchemeModel;
import com.spice.profitmandi.common.util.ExcelUtils;
import com.spice.profitmandi.common.util.StringUtils;
import com.spice.profitmandi.dao.entity.catalog.Scheme;
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
import com.spice.profitmandi.service.inventory.InventoryService;
import com.spice.profitmandi.service.scheme.SchemeService;
import com.spice.profitmandi.web.model.LoginDetails;
import com.spice.profitmandi.web.util.CookiesProcessor;

@Controller
@Transactional(rollbackFor=Throwable.class)
public class SchemeController {

        private static final Logger LOGGER = LoggerFactory.getLogger(SchemeController.class);

        @Autowired
        private SchemeService schemeService;
        
        @Autowired
        private SchemeRepository schemeRepository;

        @Autowired
        private CookiesProcessor cookiesProcessor;
        
        @Autowired
        private InventoryService inventoryService;
        
        @Autowired
        PurchaseRepository purchaseRepository;

        @RequestMapping(value = "/createScheme", method = RequestMethod.GET)
        public String createScheme(HttpServletRequest request, Model model){
                //Map<Integer, String> itemIdItemDescriptionMap = inventoryService.getAllItemIdItemDescriptionMap();
                //model.addAttribute("itemIdItemDescriptionMap", itemIdItemDescriptionMap);
                model.addAttribute("brands", inventoryService.getAllTagListingBrands());
                return "create-scheme";
        }
        
        @RequestMapping(value = "/getTagListingItemsByBrand", method = RequestMethod.GET)
        public String getTagListingItemsByBrand(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.BRAND) String brand, Model model){
                Map<Integer, String> itemIdItemDescriptionMap = inventoryService.getAllTagListingItemIdItemDescriptionMap(brand);
                model.addAttribute("itemIdItemDescriptionMap", itemIdItemDescriptionMap);
                //model.addAttribute("brands", inventoryService.getAllBrands());
                return "tag-listing-items-description";
        }
        
        
        @RequestMapping(value = "/createScheme", method = RequestMethod.POST)
        public String createScheme(HttpServletRequest request, @RequestBody CreateSchemeRequest createSchemeRequest, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)  throws ProfitMandiBusinessException{
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                LOGGER.info("CreateSchemeRequest {}", createSchemeRequest);
                schemeService.saveScheme(loginDetails.getFofoId(), createSchemeRequest);
                LOGGER.info("Scheme saved successfully");
                long size = schemeRepository.selectAllCount();
                List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
                model.addAttribute("schemes", schemes);
                model.addAttribute("start", offset + 1);
                model.addAttribute("size", size);
                if (schemes.size() < limit){
                        model.addAttribute("end", offset + schemes.size());
                }
                else{
                        model.addAttribute("end", offset + limit);
                }
                return "schemes";
                
        }
        
        @RequestMapping(value = "/getSchemes", method = RequestMethod.GET)
        public String getSchemes(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                List<Scheme> schemes = null;
                long size = 0;
                if(loginDetails.getRoleTypes().contains(RoleType.FOFO_ADMIN)){
                        schemes = schemeRepository.selectAll(offset, limit);
                        size = schemeRepository.selectAllCount();
                }else{
                        schemes = schemeRepository.selectActiveAll(offset, limit);
                        size = schemeRepository.selectAllActiveCount();
                }
                model.addAttribute("schemes", schemes);
                model.addAttribute("start", offset + 1);
                model.addAttribute("size", size);
                if (schemes.size() < limit){
                        model.addAttribute("end", offset + schemes.size());
                }
                else{
                        model.addAttribute("end", offset + limit);
                }
                //model.addAttribute("roleTypes", loginDetails.getRoleTypes());
                return "schemes";
        }
        
        @RequestMapping(value = "/getPaginatedSchemes", method = RequestMethod.GET)
        public String getPaginatedSchemes(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
                List<Scheme> schemes = null;
                if(loginDetails.getRoleTypes().contains(RoleType.FOFO_ADMIN)){
                        schemes = schemeRepository.selectAll(offset, limit);
                }else{
                        schemes = schemeRepository.selectActiveAll(offset, limit);
                }
                model.addAttribute("schemes", schemes);
                model.addAttribute("roleTypes", loginDetails.getRoleTypes());
                return "schemes-paginated";
        }
        
        @RequestMapping(value = "/schemes/downloadPage", method = RequestMethod.GET)
        public String downloadPage(HttpServletRequest request, Model model){
                return "schemes-download";
        }
        
        @RequestMapping(value = "/schemes/download", method = RequestMethod.GET)
        public ResponseEntity<?> downloadInventoryItemAgingByInterval(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.START_DATE_TIME) String startDateTimeString, @RequestParam(name = ProfitMandiConstants.END_DATE_TIME) String endDateTimeString, Model model) throws ProfitMandiBusinessException{
                LocalDateTime startDateTime = StringUtils.toDateTime(startDateTimeString, DateTimePattern.DD_MM_YYYY_T_HH_MM_SS);
                LocalDateTime endDateTime = StringUtils.toDateTime(endDateTimeString, DateTimePattern.DD_MM_YYYY_T_HH_MM_SS);
                
                List<SchemeModel> schemeModels = schemeService.getAllSchemeModels(startDateTime, endDateTime);
                
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
                
                final HttpHeaders headers=new HttpHeaders();
        headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                headers.set("Content-disposition", "inline; filename=SchemesReport.xlsx");
        headers.setContentLength(byteArrayOutputStream.toByteArray().length);
        final InputStream inputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        final InputStreamResource inputStreamResource=new InputStreamResource(inputStream);
        return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
                
                //return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
        }
        
        @RequestMapping(value = "/getSchemeById", method = RequestMethod.GET)
        public String getSchemeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, Model model)  throws ProfitMandiBusinessException{
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Scheme scheme = schemeService.getSchemeById(schemeId);
                model.addAttribute("scheme", scheme);
                model.addAttribute("roleTypes", loginDetails.getRoleTypes());
                return "scheme-details";
        }
        
        @RequestMapping(value = "/activeSchemeById", method = RequestMethod.PUT)
        public String activeSchemeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)  throws ProfitMandiBusinessException{
                schemeService.activeSchemeById(schemeId);
                List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
                model.addAttribute("schemes", schemes);
                return "schemes-paginated";
        }
        
        
        @RequestMapping(value = "/expireSchemeById", method = RequestMethod.PUT)
        public String expireSchemeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)  throws ProfitMandiBusinessException{
                schemeService.expireSchemeById(schemeId);
                List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
                model.addAttribute("schemes", schemes);
                return "schemes-paginated";
        }

}