Subversion Repositories SmartDukaan

Rev

Rev 28495 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLClientInfoException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.catalog.StateGstRate;
import com.spice.profitmandi.dao.entity.dtr.Document;
import com.spice.profitmandi.dao.model.ContentPojo;
import com.spice.profitmandi.dao.model.StateGstRateModel;
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
import com.spice.profitmandi.web.util.MVCResponseSender;
import com.spice.profitmandi.dao.repository.catalog.StateGstRateRepository;
@Controller
@Transactional(rollbackFor = Throwable.class)
public class TaxationController {
        
        
        @Autowired
        private DocumentRepository documentRepository;
        
        
        @Autowired
        private StateGstRateRepository stateGstRateRepository;
        
        @Autowired
        private MVCResponseSender mvcResponseSender;

        
        @Autowired
        private ResponseSender<?> responseSender;
        
        private static final Logger LOGGER = LogManager.getLogger(SellerController.class);
        
        @RequestMapping(value = "/csvFileUploader", method = RequestMethod.POST)
        public String getFileUploader(HttpServletRequest request, Model model ,HttpServletResponse response,
                        @RequestPart MultipartFile file) throws Throwable {
                
                LOGGER.info("file"+file.toString());
                String fileName= file.getName();
                
                String fileNames = file.getOriginalFilename();
                
                LOGGER.info("fileName"+ fileName);
                LOGGER.info("fileNames"+ fileNames);
                                
                List<StateGstRateModel> sgtFileName = this.readFile(file);
                
                LOGGER.info("sgtFileName"+sgtFileName);
                
                
                
                stateGstRateRepository.addStateGstRates(sgtFileName);
                
                
                
                model.addAttribute("responseSTG", mvcResponseSender.createResponseString(true));
                
                return "response";
                
        
        }
        
        private static StateGstRateModel createStateGstRate(CSVRecord record) {

                int itemId = Integer.parseInt(record.get(0));
       LOGGER.info("itemId"+itemId);
                double igstRate = Double.parseDouble(record.get(1));
                LOGGER.info("igstRate"+igstRate);
                // create and return book of this metadata
                return new StateGstRateModel(itemId, igstRate);
        }

        
        private List<StateGstRateModel> readFile(MultipartFile file) throws Exception {
                CSVParser parser = new CSVParser(new InputStreamReader(file.getInputStream()), CSVFormat.DEFAULT);
                List<CSVRecord> records = parser.getRecords();
                LOGGER.info("records"+records);
                LOGGER.info("parser"+parser);
                if (records.size() < 2) {
                        parser.close();
                        throw new ProfitMandiBusinessException("Uploaded File", "", "No records Found");
                }
                // Remove header
                records.remove(0);
                List<StateGstRateModel> returnList = new ArrayList<StateGstRateModel>();
                for (CSVRecord record : records) {
                        StateGstRateModel cp = null;
                        StateGstRateModel sgr = createStateGstRate(record);
                        returnList.add(sgr);
                        
                        
                        LOGGER.info("records"+record.get(1));
                }
                parser.close();
                return returnList;
        }
        
        
        
        
        
        @RequestMapping(value = "/createTaxation", method = RequestMethod.GET)
        public String showUploaderTaxation(HttpServletRequest request, Model model) throws Exception {

                
                return "taxation";

        }

}