Subversion Repositories SmartDukaan

Rev

Rev 7996 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.recharge.controllers;

import in.shop2020.model.v1.order.HotspotStore;
import in.shop2020.model.v1.order.Order;
import in.shop2020.model.v1.order.RechargeType;
import in.shop2020.model.v1.order.Transaction;
import in.shop2020.model.v1.order.TransactionStatus;
import in.shop2020.recharge.auxiliary.BulkRechargeInfo;
import in.shop2020.recharge.auxiliary.BulkRechargeRunnable;
import in.shop2020.thrift.clients.TransactionClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class is used to do bulk recharges by uploading the details in a sheet.
 */
public class BulkRechargeController extends BaseController{

    public class Message {
        private String text = "";
        private String type = "";
        public void setText(String text) {
            this.text = text;
        }
        public String getText() {
            return text;
        }
        public void setType(String type) {
            this.type = type;
        }
        public String getType() {
            return type;
        }
    }
    
    
    private static final List<String> STORECODES = Arrays.asList( new String[] {"9I0"});
    
    private static final int NAME_INDEX = 1;
    private static final int NUMBER_INDEX = 2;
    private static final int AMOUNT_INDEX = 4;
    private static final int OPERATOR_INDEX = 5;
    private static final int CLIENT_NAME_INDEX = 7;
    
    private File rechargefile;
    private String rechargefileContentType;
    private String rechargefileFileName;
    private enum messageType {
        ERROR, SUCCESS
    }
    private Message message = new Message();
    private static Logger logger = LoggerFactory.getLogger(BulkRechargeController.class);
    private static final long serialVersionUID = 1L;
    
    public String index() {
        return "index";
    }
    
    public String create() {
        File fileToCreate = null;
        try {
            fileToCreate = new File("/tmp/", rechargefileFileName);
            FileUtils.copyFile(this.rechargefile, fileToCreate);
        } catch (Exception e) {
           logger.error("Unable to write the rechargeFile to disk", e);
           message.setText("Error while uploading the file. Please try again.");
           message.setType(messageType.ERROR.toString());
           return index();
        }
        
        //Parse the file and submit the data for update to the transaction service
        Workbook wb = null;
        try {
            wb = new HSSFWorkbook(new FileInputStream(fileToCreate));
        } catch (Exception e) {
            logger.error("Unable to open rechargeFile", e);
            message.setText("Error while reading the file. Please try again.");
            message.setType(messageType.ERROR.toString());
            return index();
        }
        Sheet sheet = wb.getSheetAt(0);
        Row firstRow = sheet.getRow(0);
        logger.info("Last row number is:" + sheet.getLastRowNum());
        List<BulkRechargeInfo> rechargeInfoList = new ArrayList<BulkRechargeInfo>();
        String ipAddress = remoteAddr(request);
        for (Row row : sheet) {
            if(row.equals(firstRow))
                continue;

            BulkRechargeInfo rechargeInfo = new BulkRechargeInfo();
            try {
                String name = row.getCell(NAME_INDEX).getStringCellValue();
                String client = row.getCell(CLIENT_NAME_INDEX).getStringCellValue();
                rechargeInfo.setName(name + " - " + client);
            } catch(Exception e) {
                log.error("Unable to get name or client name", e);
                continue;
            }
            
            try {
                rechargeInfo.setAmount(Long.parseLong(row.getCell(AMOUNT_INDEX).getStringCellValue()));
            } catch (IllegalStateException e) { 
                rechargeInfo.setAmount((long) row.getCell(AMOUNT_INDEX).getNumericCellValue());
            } catch(NumberFormatException e) {
                log.error("Error - Amount incorrect : " + row.getCell(AMOUNT_INDEX).getStringCellValue() + ", for number : " + row.getCell(NUMBER_INDEX).getStringCellValue(), e);
                continue;
            }
            
            try {
                rechargeInfo.setNumber(new BigDecimal(row.getCell(NUMBER_INDEX).getNumericCellValue()).toString());
            } catch (IllegalStateException e) { 
                rechargeInfo.setNumber(row.getCell(NUMBER_INDEX).getStringCellValue());
            }
            rechargeInfo.setStoreId(storeId);
            rechargeInfo.setIpAddress(ipAddress);
            try {
                TransactionClient tcl = new TransactionClient();
                String circleCode = tcl.getClient().getServiceProviderForDevice(RechargeType.MOBILE, row.getCell(NUMBER_INDEX).getStringCellValue()).getCircleCode();
                rechargeInfo.setCircle(tcl.getClient().getTelecomCircle(-1, circleCode).getId());
            }catch(Exception e) {
                log.error("Unable to get circle", e);
                rechargeInfo.setCircle(0l);
            }
            
            if (operatorNametoValueMap.containsKey(row.getCell(OPERATOR_INDEX).getStringCellValue().trim())) {
                rechargeInfo.setOperator(operatorNametoValueMap.get(row.getCell(OPERATOR_INDEX).getStringCellValue().trim()));
            } else {
                continue;
            }
            rechargeInfoList.add(rechargeInfo);
        }
        BulkRechargeRunnable bulkRechargeRunnable = new BulkRechargeRunnable();
        bulkRechargeRunnable.setRechargeInfoList(rechargeInfoList);
        (new Thread(bulkRechargeRunnable)).start();
        message.setText("File Successfully uploaded. Please download the result after an hour or so.");
        message.setType(messageType.SUCCESS.toString());
        return index();
    }
    
    public List<HotspotStore> getStores() {
        List<HotspotStore> stores = new ArrayList<HotspotStore>();
        for(String storecode : STORECODES) {
            try{
                HotspotStore hotSpotStore = (new TransactionClient()).getClient().getHotspotStore(0l, storecode);
                stores.add(hotSpotStore);
            } catch (Exception e) {
                log.error("Unable to get store", e);
            }
        }
        return stores;
    }

    public void setMessage(Message message) {
        this.message = message;
    }

    public Message getMessage() {
        return message;
    }

    public File getRechargefile() {
        return rechargefile;
    }

    public void setRechargefile(File rechargefile) {
        this.rechargefile = rechargefile;
    }

    public String getRechargefileContentType() {
        return rechargefileContentType;
    }

    public void setRechargefileContentType(String rechargefileContentType) {
        this.rechargefileContentType = rechargefileContentType;
    }

    public String getRechargefileFileName() {
        return rechargefileFileName;
    }

    public void setRechargefileFileName(String rechargefileFileName) {
        this.rechargefileFileName = rechargefileFileName;
    }

    public static List<String> getStorecodes() {
        return STORECODES;
    }
}