Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7996 anupam.sin 1
package in.shop2020.recharge.controllers;
2
 
3
import in.shop2020.model.v1.order.HotspotStore;
4
import in.shop2020.model.v1.order.Order;
5
import in.shop2020.model.v1.order.RechargeType;
6
import in.shop2020.model.v1.order.Transaction;
7
import in.shop2020.model.v1.order.TransactionStatus;
8
import in.shop2020.recharge.auxiliary.BulkRechargeInfo;
9
import in.shop2020.recharge.auxiliary.BulkRechargeRunnable;
10
import in.shop2020.thrift.clients.TransactionClient;
11
 
12
import java.io.File;
13
import java.io.FileInputStream;
14
import java.io.FileNotFoundException;
15
import java.io.IOException;
16
import java.math.BigDecimal;
17
import java.util.ArrayList;
18
import java.util.Arrays;
19
import java.util.Date;
20
import java.util.HashMap;
21
import java.util.List;
22
import java.util.Map;
23
import java.util.Map.Entry;
24
 
25
import org.apache.commons.io.FileUtils;
26
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
27
import org.apache.poi.ss.usermodel.Cell;
28
import org.apache.poi.ss.usermodel.Row;
29
import org.apache.poi.ss.usermodel.Sheet;
30
import org.apache.poi.ss.usermodel.Workbook;
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33
 
34
/**
35
 * This class is used to do bulk recharges by uploading the details in a sheet.
36
 */
37
public class BulkRechargeController extends BaseController{
38
 
39
    public class Message {
40
        private String text = "";
41
        private String type = "";
42
        public void setText(String text) {
43
            this.text = text;
44
        }
45
        public String getText() {
46
            return text;
47
        }
48
        public void setType(String type) {
49
            this.type = type;
50
        }
51
        public String getType() {
52
            return type;
53
        }
54
    }
55
 
56
 
57
    private static final List<String> STORECODES = Arrays.asList( new String[] {"9I0"});
58
 
59
    private static final int NAME_INDEX = 1;
60
    private static final int NUMBER_INDEX = 2;
61
    private static final int AMOUNT_INDEX = 4;
62
    private static final int OPERATOR_INDEX = 5;
63
 
64
    private File rechargefile;
65
    private String rechargefileContentType;
66
    private String rechargefileFileName;
67
    private enum messageType {
68
        ERROR, SUCCESS
69
    }
70
    private Message message = new Message();
71
    private static Logger logger = LoggerFactory.getLogger(BulkRechargeController.class);
72
    private static final long serialVersionUID = 1L;
73
 
74
    public String index() {
75
        return "index";
76
    }
77
 
78
    public String create() {
79
        File fileToCreate = null;
80
        try {
81
            fileToCreate = new File("/tmp/", rechargefileFileName);
82
            FileUtils.copyFile(this.rechargefile, fileToCreate);
83
        } catch (Exception e) {
84
           logger.error("Unable to write the rechargeFile to disk", e);
85
           message.setText("Error while uploading the file. Please try again.");
86
           message.setType(messageType.ERROR.toString());
87
           return index();
88
        }
89
 
90
        //Parse the file and submit the data for update to the transaction service
91
        Workbook wb = null;
92
        try {
93
            wb = new HSSFWorkbook(new FileInputStream(fileToCreate));
94
        } catch (Exception e) {
95
            logger.error("Unable to open rechargeFile", e);
96
            message.setText("Error while reading the file. Please try again.");
97
            message.setType(messageType.ERROR.toString());
98
            return index();
99
        }
100
        Sheet sheet = wb.getSheetAt(0);
101
        Row firstRow = sheet.getRow(0);
102
        logger.info("Last row number is:" + sheet.getLastRowNum());
103
        List<BulkRechargeInfo> rechargeInfoList = new ArrayList<BulkRechargeInfo>();
104
        String ipAddress = remoteAddr(request);
105
        for (Row row : sheet) {
106
            if(row.equals(firstRow))
107
                continue;
108
 
109
            BulkRechargeInfo rechargeInfo = new BulkRechargeInfo();
110
            rechargeInfo.setName(row.getCell(NAME_INDEX).getStringCellValue());
111
            try {
112
                rechargeInfo.setAmount(Long.parseLong(row.getCell(AMOUNT_INDEX).getStringCellValue()));
113
            } catch (IllegalStateException e) { 
114
                rechargeInfo.setAmount((long) row.getCell(AMOUNT_INDEX).getNumericCellValue());
115
            } catch(NumberFormatException e) {
116
                log.error("Error - Amount incorrect : " + row.getCell(AMOUNT_INDEX).getStringCellValue() + ", for number : " + row.getCell(NUMBER_INDEX).getStringCellValue(), e);
117
                continue;
118
            }
119
 
120
            try {
121
                rechargeInfo.setNumber(new BigDecimal(row.getCell(NUMBER_INDEX).getNumericCellValue()).toString());
122
            } catch (IllegalStateException e) { 
123
                rechargeInfo.setNumber(row.getCell(NUMBER_INDEX).getStringCellValue());
124
            }
125
            rechargeInfo.setStoreId(storeId);
126
            rechargeInfo.setIpAddress(ipAddress);
127
            try {
128
                TransactionClient tcl = new TransactionClient();
129
                String circleCode = tcl.getClient().getServiceProviderForDevice(RechargeType.MOBILE, row.getCell(NUMBER_INDEX).getStringCellValue()).getCircleCode();
130
                rechargeInfo.setCircle(tcl.getClient().getTelecomCircle(-1, circleCode).getId());
131
            }catch(Exception e) {
132
                log.error("Unable to get circle", e);
133
                rechargeInfo.setCircle(0l);
134
            }
135
 
136
            if (operatorNametoValueMap.containsKey(row.getCell(OPERATOR_INDEX).getStringCellValue().trim())) {
137
                rechargeInfo.setOperator(operatorNametoValueMap.get(row.getCell(OPERATOR_INDEX).getStringCellValue().trim()));
138
            } else {
139
                continue;
140
            }
141
            rechargeInfoList.add(rechargeInfo);
142
        }
143
        BulkRechargeRunnable bulkRechargeRunnable = new BulkRechargeRunnable();
144
        bulkRechargeRunnable.setRechargeInfoList(rechargeInfoList);
145
        (new Thread(bulkRechargeRunnable)).start();
146
        message.setText("File Successfully uploaded. Please download the result after an hour or so.");
147
        message.setType(messageType.SUCCESS.toString());
148
        return index();
149
    }
150
 
151
    public List<HotspotStore> getStores() {
152
        List<HotspotStore> stores = new ArrayList<HotspotStore>();
153
        for(String storecode : STORECODES) {
154
            try{
155
                HotspotStore hotSpotStore = (new TransactionClient()).getClient().getHotspotStore(0l, storecode);
156
                stores.add(hotSpotStore);
157
            } catch (Exception e) {
158
                log.error("Unable to get store", e);
159
            }
160
        }
161
        return stores;
162
    }
163
 
164
    public void setMessage(Message message) {
165
        this.message = message;
166
    }
167
 
168
    public Message getMessage() {
169
        return message;
170
    }
171
 
172
    public File getRechargefile() {
173
        return rechargefile;
174
    }
175
 
176
    public void setRechargefile(File rechargefile) {
177
        this.rechargefile = rechargefile;
178
    }
179
 
180
    public String getRechargefileContentType() {
181
        return rechargefileContentType;
182
    }
183
 
184
    public void setRechargefileContentType(String rechargefileContentType) {
185
        this.rechargefileContentType = rechargefileContentType;
186
    }
187
 
188
    public String getRechargefileFileName() {
189
        return rechargefileFileName;
190
    }
191
 
192
    public void setRechargefileFileName(String rechargefileFileName) {
193
        this.rechargefileFileName = rechargefileFileName;
194
    }
195
 
196
    public static List<String> getStorecodes() {
197
        return STORECODES;
198
    }
199
}