Subversion Repositories SmartDukaan

Rev

Rev 22860 | Rev 23020 | 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.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.CreateSchemeRequest;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.dao.entity.catalog.Scheme;
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
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;

        @RequestMapping(value = "/createScheme", method = RequestMethod.GET)
        public String createScheme(HttpServletRequest request, Model model){
                return "create-scheme";
        }
        
        @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");
                
                List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
                long count = schemeRepository.selectCount(offset, limit);
                model.addAttribute("schemes", schemes);
                model.addAttribute("start", offset + 1);
                model.addAttribute("size", count);
                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){
                List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
                long count = schemeRepository.selectCount(offset, limit);
                model.addAttribute("schemes", schemes);
                model.addAttribute("start", offset + 1);
                model.addAttribute("size", count);
                if (schemes.size() < limit){
                        model.addAttribute("end", offset + schemes.size());
                }
                else{
                        model.addAttribute("end", offset + limit);
                }
                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){
                List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
                model.addAttribute("schemes", schemes);
                return "schemes-paginated";
        }
        
        @RequestMapping(value = "/getSchemeById", method = RequestMethod.GET)
        public String getSchemeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, Model model)  throws ProfitMandiBusinessException{
                Scheme scheme = schemeService.getSchemeById(schemeId);
                model.addAttribute("scheme", scheme);
                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";
        }
}