| 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;
|
| 8008 |
anupam.sin |
63 |
private static final int CLIENT_NAME_INDEX = 7;
|
| 7996 |
anupam.sin |
64 |
|
|
|
65 |
private File rechargefile;
|
|
|
66 |
private String rechargefileContentType;
|
|
|
67 |
private String rechargefileFileName;
|
|
|
68 |
private enum messageType {
|
|
|
69 |
ERROR, SUCCESS
|
|
|
70 |
}
|
|
|
71 |
private Message message = new Message();
|
|
|
72 |
private static Logger logger = LoggerFactory.getLogger(BulkRechargeController.class);
|
|
|
73 |
private static final long serialVersionUID = 1L;
|
|
|
74 |
|
|
|
75 |
public String index() {
|
|
|
76 |
return "index";
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
public String create() {
|
|
|
80 |
File fileToCreate = null;
|
|
|
81 |
try {
|
|
|
82 |
fileToCreate = new File("/tmp/", rechargefileFileName);
|
|
|
83 |
FileUtils.copyFile(this.rechargefile, fileToCreate);
|
|
|
84 |
} catch (Exception e) {
|
|
|
85 |
logger.error("Unable to write the rechargeFile to disk", e);
|
|
|
86 |
message.setText("Error while uploading the file. Please try again.");
|
|
|
87 |
message.setType(messageType.ERROR.toString());
|
|
|
88 |
return index();
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
//Parse the file and submit the data for update to the transaction service
|
|
|
92 |
Workbook wb = null;
|
|
|
93 |
try {
|
|
|
94 |
wb = new HSSFWorkbook(new FileInputStream(fileToCreate));
|
|
|
95 |
} catch (Exception e) {
|
|
|
96 |
logger.error("Unable to open rechargeFile", e);
|
|
|
97 |
message.setText("Error while reading the file. Please try again.");
|
|
|
98 |
message.setType(messageType.ERROR.toString());
|
|
|
99 |
return index();
|
|
|
100 |
}
|
|
|
101 |
Sheet sheet = wb.getSheetAt(0);
|
|
|
102 |
Row firstRow = sheet.getRow(0);
|
|
|
103 |
logger.info("Last row number is:" + sheet.getLastRowNum());
|
|
|
104 |
List<BulkRechargeInfo> rechargeInfoList = new ArrayList<BulkRechargeInfo>();
|
|
|
105 |
String ipAddress = remoteAddr(request);
|
|
|
106 |
for (Row row : sheet) {
|
|
|
107 |
if(row.equals(firstRow))
|
|
|
108 |
continue;
|
|
|
109 |
|
|
|
110 |
BulkRechargeInfo rechargeInfo = new BulkRechargeInfo();
|
|
|
111 |
try {
|
| 8008 |
anupam.sin |
112 |
String name = row.getCell(NAME_INDEX).getStringCellValue();
|
|
|
113 |
String client = row.getCell(CLIENT_NAME_INDEX).getStringCellValue();
|
|
|
114 |
rechargeInfo.setName(name + " - " + client);
|
|
|
115 |
} catch(Exception e) {
|
|
|
116 |
log.error("Unable to get name or client name", e);
|
|
|
117 |
continue;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
try {
|
| 7996 |
anupam.sin |
121 |
rechargeInfo.setAmount(Long.parseLong(row.getCell(AMOUNT_INDEX).getStringCellValue()));
|
|
|
122 |
} catch (IllegalStateException e) {
|
|
|
123 |
rechargeInfo.setAmount((long) row.getCell(AMOUNT_INDEX).getNumericCellValue());
|
|
|
124 |
} catch(NumberFormatException e) {
|
|
|
125 |
log.error("Error - Amount incorrect : " + row.getCell(AMOUNT_INDEX).getStringCellValue() + ", for number : " + row.getCell(NUMBER_INDEX).getStringCellValue(), e);
|
|
|
126 |
continue;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
try {
|
|
|
130 |
rechargeInfo.setNumber(new BigDecimal(row.getCell(NUMBER_INDEX).getNumericCellValue()).toString());
|
|
|
131 |
} catch (IllegalStateException e) {
|
|
|
132 |
rechargeInfo.setNumber(row.getCell(NUMBER_INDEX).getStringCellValue());
|
|
|
133 |
}
|
|
|
134 |
rechargeInfo.setStoreId(storeId);
|
|
|
135 |
rechargeInfo.setIpAddress(ipAddress);
|
|
|
136 |
try {
|
|
|
137 |
TransactionClient tcl = new TransactionClient();
|
|
|
138 |
String circleCode = tcl.getClient().getServiceProviderForDevice(RechargeType.MOBILE, row.getCell(NUMBER_INDEX).getStringCellValue()).getCircleCode();
|
|
|
139 |
rechargeInfo.setCircle(tcl.getClient().getTelecomCircle(-1, circleCode).getId());
|
|
|
140 |
}catch(Exception e) {
|
|
|
141 |
log.error("Unable to get circle", e);
|
|
|
142 |
rechargeInfo.setCircle(0l);
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
if (operatorNametoValueMap.containsKey(row.getCell(OPERATOR_INDEX).getStringCellValue().trim())) {
|
|
|
146 |
rechargeInfo.setOperator(operatorNametoValueMap.get(row.getCell(OPERATOR_INDEX).getStringCellValue().trim()));
|
|
|
147 |
} else {
|
|
|
148 |
continue;
|
|
|
149 |
}
|
|
|
150 |
rechargeInfoList.add(rechargeInfo);
|
|
|
151 |
}
|
|
|
152 |
BulkRechargeRunnable bulkRechargeRunnable = new BulkRechargeRunnable();
|
|
|
153 |
bulkRechargeRunnable.setRechargeInfoList(rechargeInfoList);
|
|
|
154 |
(new Thread(bulkRechargeRunnable)).start();
|
|
|
155 |
message.setText("File Successfully uploaded. Please download the result after an hour or so.");
|
|
|
156 |
message.setType(messageType.SUCCESS.toString());
|
|
|
157 |
return index();
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
public List<HotspotStore> getStores() {
|
|
|
161 |
List<HotspotStore> stores = new ArrayList<HotspotStore>();
|
|
|
162 |
for(String storecode : STORECODES) {
|
|
|
163 |
try{
|
|
|
164 |
HotspotStore hotSpotStore = (new TransactionClient()).getClient().getHotspotStore(0l, storecode);
|
|
|
165 |
stores.add(hotSpotStore);
|
|
|
166 |
} catch (Exception e) {
|
|
|
167 |
log.error("Unable to get store", e);
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
return stores;
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
public void setMessage(Message message) {
|
|
|
174 |
this.message = message;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
public Message getMessage() {
|
|
|
178 |
return message;
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
public File getRechargefile() {
|
|
|
182 |
return rechargefile;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
public void setRechargefile(File rechargefile) {
|
|
|
186 |
this.rechargefile = rechargefile;
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
public String getRechargefileContentType() {
|
|
|
190 |
return rechargefileContentType;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
public void setRechargefileContentType(String rechargefileContentType) {
|
|
|
194 |
this.rechargefileContentType = rechargefileContentType;
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
public String getRechargefileFileName() {
|
|
|
198 |
return rechargefileFileName;
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
public void setRechargefileFileName(String rechargefileFileName) {
|
|
|
202 |
this.rechargefileFileName = rechargefileFileName;
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
public static List<String> getStorecodes() {
|
|
|
206 |
return STORECODES;
|
|
|
207 |
}
|
|
|
208 |
}
|