Subversion Repositories SmartDukaan

Rev

Rev 22927 | 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.common.web.util.ResponseSender;
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;
import com.spice.profitmandi.web.util.MVCResponseSender;

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

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

        @Autowired
        SchemeService schemeService;
        
        @Autowired
        SchemeRepository schemeRepository;

        @Autowired
        MVCResponseSender mvcResponseSender;

        @Autowired
        CookiesProcessor cookiesProcessor;
        
        @Autowired
        ResponseSender<?> responseSender;

        @RequestMapping(value = "/createScheme", method = RequestMethod.GET)
        public String createScheme(HttpServletRequest request, Model model)  throws Throwable{
                try {
                        cookiesProcessor.getCookiesObject(request);
                } catch (ProfitMandiBusinessException e) {
                        model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
                        return "response";
                }
                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 Throwable{
                LoginDetails loginDetails = null;
                try {
                        loginDetails = cookiesProcessor.getCookiesObject(request);
                } catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
                        return "response";
                }
                LOGGER.info("CreateSchemeRequest {}", createSchemeRequest);
                try{
                        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";
                }catch(ProfitMandiBusinessException profitMandiBusinessException){
                        LOGGER.error("Unable to save Scheme : ", profitMandiBusinessException);
                        return "error";
                }
        }
        
        @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 Throwable{
                try {
                        cookiesProcessor.getCookiesObject(request);
                } catch (ProfitMandiBusinessException e) {
                        model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
                        return "response";
                }
                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)  throws Throwable{
                try {
                        cookiesProcessor.getCookiesObject(request);
                } catch (ProfitMandiBusinessException e) {
                        model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
                        return "response";
                }
                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 Throwable{
                try {
                        cookiesProcessor.getCookiesObject(request);
                } catch (ProfitMandiBusinessException e) {
                        model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
                        return "response";
                }
                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 Throwable{
                try {
                        cookiesProcessor.getCookiesObject(request);
                } catch (ProfitMandiBusinessException e) {
                        model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
                        return "response";
                }
                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 Throwable{
                try {
                        cookiesProcessor.getCookiesObject(request);
                } catch (ProfitMandiBusinessException e) {
                        model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
                        return "response";
                }
                schemeService.expireSchemeById(schemeId);
                List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
                model.addAttribute("schemes", schemes);
                return "schemes-paginated";
        }
}