Subversion Repositories SmartDukaan

Rev

Rev 23528 | 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.HashMap;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.OperatorType;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.model.RechargeRequest;
import com.spice.profitmandi.dao.entity.dtr.RechargeOperator;
import com.spice.profitmandi.dao.entity.dtr.RechargeTransaction;
import com.spice.profitmandi.dao.enumuration.dtr.RechargeType;
import com.spice.profitmandi.dao.repository.dtr.RechargeOperatorRepository;
import com.spice.profitmandi.dao.repository.dtr.RechargeTransactionRepository;
import com.spice.profitmandi.service.recharge.RechargeService;
import com.spice.profitmandi.web.model.LoginDetails;
import com.spice.profitmandi.web.util.CookiesProcessor;

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

        private static final Logger LOGGER = LoggerFactory.getLogger(RechargeController.class);
        
        @Value("${recharge.api.host}")
        private String rechargeApiHost;
                
        @Value("${recharge.api.port}")
        private int rechargeApiPort;
        
        @Value("${recharge.auth.key}")
        private String rechargeAuthKey;
        
        @Value("${recharge.validation.api.host}")
        private String rechargeValidationApiHost;
                
        @Value("${recharge.validation.api.port}")
        private int rechargeValidationApiPort;
        
        @Value("${recharge.validation.auth.key}")
        private String rechargeValidationAuthKey;
        
        @Autowired
        private RechargeService rechargeService;
        
        @Autowired
        private RechargeOperatorRepository rechargeOperatorRepository;
        
        @Autowired
        private RechargeTransactionRepository rechargeTransactionRepository;
        
        @Autowired
        private CookiesProcessor cookiesProcessor;
        
        @RequestMapping(value = "/createRecharge", method = RequestMethod.GET)
        public String createRecharge(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.RECHARGE_TYPE) String rechargeTypeString, Model model){
                //model.addAttribute("rechargeOperators", rechargeOperatorRepository.selectAllByRechargeType(RechargeType.MOBILE));
                //model.addAttribute("rechargeTypes", RechargeType.values());
                RechargeType rechargeType = RechargeType.valueOf(rechargeTypeString);
                List<RechargeOperator> rechargeOperators = null;
                if(rechargeType == RechargeType.MOBILE) {
                        model.addAttribute("operatorTypes", OperatorType.values());
                        rechargeOperators = rechargeOperatorRepository.selectAllByOperatorType(OperatorType.PREPAID);
                }else {
                        rechargeOperators = rechargeOperatorRepository.selectAllByRechargeType(rechargeType);
                }
                model.addAttribute("rechargeOperators", rechargeOperators);
                return "create-recharge";
        }
        
        private Map<Integer, String> rechargeOperatorIdRechargeOperatorNameMap(List<RechargeTransaction> rechargeTransactions){
                Map<Integer, String> rechargeOperatorIdrechargeOperatorNameMap = new HashMap<>();
                if(rechargeTransactions.isEmpty()) {
                        return rechargeOperatorIdrechargeOperatorNameMap;
                }
                Set<Integer> operatorIds = new HashSet<>();
                for(RechargeTransaction rechargeTransaction : rechargeTransactions) {
                        operatorIds.add(rechargeTransaction.getOperatorId());
                }
                
                List<RechargeOperator> rechargeOperators = rechargeOperatorRepository.selectAllByIds(operatorIds);
                for(RechargeOperator rechargeOperator : rechargeOperators) {
                        rechargeOperatorIdrechargeOperatorNameMap.put(rechargeOperator.getId(), rechargeOperator.getName());
                }
                return rechargeOperatorIdrechargeOperatorNameMap;
        }
        
        @RequestMapping(value = "/createRecharge", method = RequestMethod.POST)
        public String createRecharge(HttpServletRequest request, @RequestBody RechargeRequest rechargeRequest, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                rechargeService.doRecharge(rechargeApiHost, rechargeApiPort, rechargeAuthKey, rechargeValidationApiHost, rechargeValidationApiPort, rechargeValidationAuthKey, loginDetails.getFofoId(), rechargeRequest);
                RechargeType rechargeType = RechargeType.valueOf(rechargeRequest.getRechargeType());
                List<RechargeTransaction> rechargeTransactions = rechargeTransactionRepository.selectAllByRetailerIdAndType(loginDetails.getFofoId(), rechargeType, offset, limit);
                long size = rechargeTransactionRepository.selectCountByRetailerIdAndType(loginDetails.getFofoId(), rechargeType);
                Map<Integer, String> rechargeOperatorIdRechargeOperatorNameMap = this.rechargeOperatorIdRechargeOperatorNameMap(rechargeTransactions);
                model.addAttribute("rechargeTransactions", rechargeTransactions);
                model.addAttribute("rechargeOperatorIdRechargeOperatorNameMap", rechargeOperatorIdRechargeOperatorNameMap);
                model.addAttribute("start", offset + 1);
                model.addAttribute("size", size);
                if (rechargeTransactions.size() < limit){
                        model.addAttribute("end", offset + rechargeTransactions.size());
                }else{
                        model.addAttribute("end", offset + limit);
                }
                if(RechargeType.valueOf(rechargeRequest.getRechargeType()) == RechargeType.MOBILE) {
                        return "mobile-recharges";
                }else {
                        return "dth-recharges";
                }
        }
        
        @RequestMapping(value = "/getRechargeById", method = RequestMethod.GET)
        public String getRechargeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.RECHARGE_TRANSACTION_ID) int rechargeTransactionId, Model model)  throws ProfitMandiBusinessException{
                RechargeTransaction rechargeTransaction = rechargeTransactionRepository.selectById(rechargeTransactionId);
                model.addAttribute("rechargeTransaction", rechargeTransaction);
                return "recharge-details";
        }
        
        @RequestMapping(value = "/checkStatus", method = RequestMethod.GET)
        public String checkStatus(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.REQUEST_ID) String requestId, @RequestParam(name = ProfitMandiConstants.RECHARGE_TYPE) String rechargeTypeString, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
                LOGGER.info("RequestId [{}], rechargeType [{}]", requestId, rechargeTypeString);
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                rechargeService.checkStatus(rechargeApiHost, rechargeApiPort, rechargeAuthKey, loginDetails.getFofoId(), requestId);
                RechargeType rechargeType = RechargeType.valueOf(rechargeTypeString);
                List<RechargeTransaction> rechargeTransactions = rechargeTransactionRepository.selectAllByRetailerIdAndType(loginDetails.getFofoId(), rechargeType, offset, limit);
                //long size = rechargeTransactionRepository.selectCountByRetailerId(loginDetails.getFofoId());
                Map<Integer, String> rechargeOperatorIdRechargeOperatorNameMap = this.rechargeOperatorIdRechargeOperatorNameMap(rechargeTransactions);
                model.addAttribute("rechargeTransactions", rechargeTransactions);
                model.addAttribute("rechargeOperatorIdRechargeOperatorNameMap", rechargeOperatorIdRechargeOperatorNameMap);
                
                if(RechargeType.valueOf(rechargeTypeString) == RechargeType.MOBILE) {
                        return "mobile-recharges-paginated";
                }else {
                        return "dth-recharges-paginated";
                }
        }
        
        @RequestMapping(value = "/getRecharges", method = RequestMethod.GET)
        public String getRecharges(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.RECHARGE_TYPE) String rechargeTypeString, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                RechargeType rechargeType = RechargeType.valueOf(rechargeTypeString);
                List<RechargeTransaction> rechargeTransactions = rechargeTransactionRepository.selectAllByRetailerIdAndType(loginDetails.getFofoId(), rechargeType, offset, limit);
                long size = rechargeTransactionRepository.selectCountByRetailerIdAndType(loginDetails.getFofoId(), rechargeType);
                Map<Integer, String> rechargeOperatorIdRechargeOperatorNameMap = this.rechargeOperatorIdRechargeOperatorNameMap(rechargeTransactions);
                model.addAttribute("rechargeTransactions", rechargeTransactions);
                model.addAttribute("rechargeOperatorIdRechargeOperatorNameMap", rechargeOperatorIdRechargeOperatorNameMap);
                model.addAttribute("start", offset + 1);
                model.addAttribute("size", size);
                if (rechargeTransactions.size() < limit){
                        model.addAttribute("end", offset + rechargeTransactions.size());
                }else{
                        model.addAttribute("end", offset + limit);
                }
                if(RechargeType.valueOf(rechargeTypeString) == RechargeType.MOBILE) {
                        return "mobile-recharges";
                }else {
                        return "dth-recharges";
                }
        }
        
        @RequestMapping(value = "/getPaginatedRecharges", method = RequestMethod.GET)
        public String getPaginatedRecharges(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.RECHARGE_TYPE) String rechargeTypeString, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                RechargeType rechargeType = RechargeType.valueOf(rechargeTypeString);
                List<RechargeTransaction> rechargeTransactions = rechargeTransactionRepository.selectAllByRetailerIdAndType(loginDetails.getFofoId(), rechargeType, offset, limit);
                Map<Integer, String> rechargeOperatorIdRechargeOperatorNameMap = this.rechargeOperatorIdRechargeOperatorNameMap(rechargeTransactions);
                model.addAttribute("rechargeTransactions", rechargeTransactions);
                model.addAttribute("rechargeOperatorIdRechargeOperatorNameMap", rechargeOperatorIdRechargeOperatorNameMap);
                if(RechargeType.valueOf(rechargeTypeString) == RechargeType.MOBILE) {
                        return "mobile-recharges-paginated";
                }else {
                        return "dth-recharges-paginated";
                }
        }
        
        @RequestMapping(value = "/getOperators", method = RequestMethod.GET)
        public String getOperators(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.OPERATOR_TYPE) String operatorTypeString, Model model){
                //model.addAttribute("rechargeOperators", rechargeOperatorRepository.selectAllByRechargeType(RechargeType.MOBILE));
                //model.addAttribute("rechargeTypes", RechargeType.values());
                OperatorType operatorType = OperatorType.valueOf(operatorTypeString);
                List<RechargeOperator> rechargeOperators = rechargeOperatorRepository.selectAllByOperatorType(operatorType);
                model.addAttribute("rechargeOperators", rechargeOperators);
                return "recharge-operators";
        }
        
        
}