| 8182 |
amar.kumar |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.model.v1.order.EbayOrder;
|
|
|
4 |
import in.shop2020.model.v1.order.Order;
|
|
|
5 |
import in.shop2020.model.v1.order.OrderStatus;
|
|
|
6 |
import in.shop2020.model.v1.order.Transaction;
|
|
|
7 |
import in.shop2020.model.v1.order.TransactionService;
|
|
|
8 |
import in.shop2020.model.v1.user.User;
|
|
|
9 |
import in.shop2020.payments.Payment;
|
|
|
10 |
import in.shop2020.payments.PaymentException;
|
|
|
11 |
import in.shop2020.payments.PaymentStatus;
|
| 8259 |
amar.kumar |
12 |
import in.shop2020.support.utils.ReportsUtils;
|
| 8182 |
amar.kumar |
13 |
import in.shop2020.thrift.clients.PaymentClient;
|
|
|
14 |
import in.shop2020.thrift.clients.TransactionClient;
|
|
|
15 |
import in.shop2020.thrift.clients.UserClient;
|
|
|
16 |
|
| 8259 |
amar.kumar |
17 |
import java.io.File;
|
|
|
18 |
import java.io.FileInputStream;
|
|
|
19 |
import java.util.Collection;
|
|
|
20 |
import java.util.Date;
|
|
|
21 |
|
| 8182 |
amar.kumar |
22 |
import javax.servlet.http.HttpServletRequest;
|
|
|
23 |
import javax.servlet.http.HttpSession;
|
|
|
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.apache.struts2.convention.annotation.InterceptorRef;
|
|
|
32 |
import org.apache.struts2.convention.annotation.InterceptorRefs;
|
|
|
33 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
34 |
import org.apache.struts2.convention.annotation.Results;
|
|
|
35 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
36 |
import org.apache.thrift.TException;
|
|
|
37 |
import org.slf4j.Logger;
|
|
|
38 |
import org.slf4j.LoggerFactory;
|
|
|
39 |
|
|
|
40 |
import com.opensymphony.xwork2.ActionSupport;
|
|
|
41 |
|
|
|
42 |
@SuppressWarnings("serial")
|
|
|
43 |
@InterceptorRefs({
|
|
|
44 |
@InterceptorRef("defaultStack"),
|
|
|
45 |
@InterceptorRef("login")
|
|
|
46 |
})
|
|
|
47 |
@Results({
|
|
|
48 |
@Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
|
|
|
49 |
})
|
|
|
50 |
public class EbayPsOrderUpdateController extends ActionSupport implements ServletRequestAware {
|
|
|
51 |
|
|
|
52 |
private static Logger logger = LoggerFactory.getLogger(EbayPsOrderUpdateController.class);
|
|
|
53 |
|
|
|
54 |
private HttpServletRequest request;
|
|
|
55 |
private HttpSession session;
|
|
|
56 |
|
|
|
57 |
private static final int EBAY_SOURCE_ID = 6;
|
|
|
58 |
|
|
|
59 |
private static final int SALES_RECORD_NUM_INDEX = 0;
|
|
|
60 |
private static final int BUYER_NAME_INDEX = 2;
|
|
|
61 |
private static final int EMAIL_INDEX = 4;
|
|
|
62 |
private static final int ADDR1_INDEX = 5;
|
|
|
63 |
private static final int ADDR2_INDEX = 6;
|
|
|
64 |
private static final int CITY_INDEX = 7;
|
|
|
65 |
private static final int STATE_INDEX = 8;
|
|
|
66 |
private static final int PINCODE_INDEX = 9;
|
|
|
67 |
private static final int EBAY_LISTINGID_INDEX = 11;
|
|
|
68 |
private static final int TRANSACTION_ID_INDEX = 31;
|
|
|
69 |
|
|
|
70 |
private File orderDataFile;
|
|
|
71 |
private String orderDataFileName;
|
|
|
72 |
private String errorMsg = "";
|
|
|
73 |
private Long rowId = 0L;
|
|
|
74 |
|
|
|
75 |
public String create() throws TException {
|
|
|
76 |
File fileToCreate = null;
|
|
|
77 |
orderDataFileName = "OrderSheet_"+EBAY_SOURCE_ID+"_"+(new Date().toString());
|
|
|
78 |
try {
|
|
|
79 |
fileToCreate = new File("/tmp/", this.orderDataFileName);
|
|
|
80 |
FileUtils.copyFile(this.orderDataFile, fileToCreate);
|
|
|
81 |
} catch (Exception e) {
|
|
|
82 |
logger.error("Error while writing order data file to the local file system for Ebay", e);
|
|
|
83 |
addActionError("Error while writing order data file to the local file system");
|
| 8303 |
amar.kumar |
84 |
setErrorMsg(getErrorMsg() + "Error in opening File for Order creation");
|
| 8182 |
amar.kumar |
85 |
}
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
if(checkForErrors())
|
|
|
89 |
return "authsuccess";
|
|
|
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 the File for Order Creation for Ebay ", e);
|
|
|
97 |
addActionError("Unable to open the File for Order creation");
|
| 8303 |
amar.kumar |
98 |
setErrorMsg(getErrorMsg() + "Error in opening File for Order creation");
|
| 8182 |
amar.kumar |
99 |
}
|
|
|
100 |
if(checkForErrors())
|
|
|
101 |
return "authsuccess";
|
|
|
102 |
|
|
|
103 |
TransactionService.Client tsc = null;
|
|
|
104 |
try {
|
|
|
105 |
tsc = new TransactionClient().getClient();
|
|
|
106 |
} catch (Exception e) {
|
|
|
107 |
logger.error("Unable to establish connection to the transaction service", e);
|
|
|
108 |
addActionError("Unable to establish connection to the transaction service");
|
| 8303 |
amar.kumar |
109 |
setErrorMsg(getErrorMsg() + "Error in connecting to Order Service");
|
| 8182 |
amar.kumar |
110 |
}
|
|
|
111 |
|
|
|
112 |
if(checkForErrors())
|
|
|
113 |
return "authsuccess";
|
|
|
114 |
|
|
|
115 |
if(checkForErrors())
|
|
|
116 |
return "authsuccess";
|
|
|
117 |
|
|
|
118 |
Sheet sheet = wb.getSheetAt(0);
|
|
|
119 |
Row firstRow = sheet.getRow(0);
|
|
|
120 |
for (Row row : sheet) {
|
|
|
121 |
User user = null;
|
| 8303 |
amar.kumar |
122 |
rowId++;
|
| 8182 |
amar.kumar |
123 |
long orderCountForRow = 0;
|
|
|
124 |
if(row.equals(firstRow))
|
|
|
125 |
continue;
|
| 8303 |
amar.kumar |
126 |
try {
|
|
|
127 |
Order order = null;
|
|
|
128 |
Transaction transaction = null;
|
|
|
129 |
try {
|
| 8323 |
amar.kumar |
130 |
tsc = new TransactionClient().getClient();
|
| 8303 |
amar.kumar |
131 |
row.getCell(EBAY_LISTINGID_INDEX).setCellType(Cell.CELL_TYPE_STRING);
|
|
|
132 |
EbayOrder ebayOrder = tsc.getEbayOrder(new Double(row.getCell(SALES_RECORD_NUM_INDEX).getNumericCellValue()).longValue(),
|
|
|
133 |
row.getCell(EBAY_LISTINGID_INDEX).getStringCellValue(), null).get(0);
|
|
|
134 |
row.getCell(TRANSACTION_ID_INDEX).setCellType(Cell.CELL_TYPE_STRING);
|
|
|
135 |
ebayOrder.setTransactionId(row.getCell(TRANSACTION_ID_INDEX).getStringCellValue());
|
|
|
136 |
tsc.updateEbayOrder(ebayOrder);
|
|
|
137 |
order = tsc.getOrder(ebayOrder.getOrderId());
|
| 8432 |
amar.kumar |
138 |
if(order.getStatus() != OrderStatus.PAYMENT_PENDING) {
|
|
|
139 |
setErrorMsg(getErrorMsg() + "<br>Order for row number " + rowId + " has been already processed");
|
|
|
140 |
continue;
|
|
|
141 |
}
|
| 8303 |
amar.kumar |
142 |
transaction = tsc.getTransaction(order.getTransactionId());
|
|
|
143 |
} catch (Exception e) {
|
|
|
144 |
logger.error("Unable to get EbayOrder for row number " + rowId, e);
|
|
|
145 |
addActionError("Unable to get EbayOrder for row number " + rowId);
|
|
|
146 |
setErrorMsg(getErrorMsg() + "<br>Unable to get EbayOrder for row number " + rowId);
|
|
|
147 |
continue;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
try {
|
|
|
151 |
in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient().getClient();
|
|
|
152 |
if (userClient.userExists(row.getCell(EMAIL_INDEX).getStringCellValue())) {
|
|
|
153 |
user = userClient.getUserByEmail(row.getCell(EMAIL_INDEX).getStringCellValue());
|
|
|
154 |
} else {
|
|
|
155 |
user = new User();
|
|
|
156 |
user.setName(row.getCell(BUYER_NAME_INDEX).getStringCellValue());
|
|
|
157 |
user.setEmail(row.getCell(EMAIL_INDEX).getStringCellValue());
|
| 8723 |
amar.kumar |
158 |
user.setSourceId(EBAY_SOURCE_ID);
|
| 8303 |
amar.kumar |
159 |
//TODO set password for user;
|
|
|
160 |
user.setIsAnonymous(false);
|
|
|
161 |
user = userClient.createUser(user);
|
|
|
162 |
}
|
|
|
163 |
} catch (Exception e) {
|
|
|
164 |
logger.error("Unable to get or create user for row number " + rowId, e);
|
|
|
165 |
addActionError("Unable to get or create user for row number " + rowId);
|
|
|
166 |
setErrorMsg(getErrorMsg() + "<br>Unable to get or create user for row number " + rowId);
|
|
|
167 |
continue;
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
//TODO Shopping cart is not being set right now Will
|
|
|
171 |
transaction.setShoppingCartid(user.getActiveCartId());
|
|
|
172 |
transaction.setCustomer_id(user.getUserId());
|
|
|
173 |
transaction.setCreatedOn(new Date().getTime());
|
|
|
174 |
|
|
|
175 |
order.setCustomer_id(user.getUserId());
|
|
|
176 |
order.setCustomer_email(user.getEmail());
|
| 8624 |
amar.kumar |
177 |
order.setCustomer_name(row.getCell(BUYER_NAME_INDEX).getStringCellValue());
|
| 8303 |
amar.kumar |
178 |
if (order.getCustomer_mobilenumber()== null || order.getCustomer_mobilenumber() == "") {
|
|
|
179 |
order.setCustomer_mobilenumber(user.getMobileNumber());
|
|
|
180 |
}
|
|
|
181 |
order.setCustomer_address1(row.getCell(ADDR1_INDEX).getStringCellValue());
|
|
|
182 |
Cell address2Cell = row.getCell(ADDR2_INDEX);
|
|
|
183 |
if(address2Cell != null && address2Cell.getCellType() != Cell.CELL_TYPE_BLANK) {
|
|
|
184 |
order.setCustomer_address2(row.getCell(ADDR2_INDEX).getStringCellValue());
|
|
|
185 |
}
|
|
|
186 |
order.setCustomer_city(row.getCell(CITY_INDEX).getStringCellValue());
|
|
|
187 |
order.setCustomer_state(row.getCell(STATE_INDEX).getStringCellValue());
|
|
|
188 |
row.getCell(PINCODE_INDEX).setCellType(Cell.CELL_TYPE_STRING);
|
|
|
189 |
order.setCustomer_pincode(row.getCell(PINCODE_INDEX).getStringCellValue());
|
|
|
190 |
|
|
|
191 |
order.setStatus(OrderStatus.SUBMITTED_FOR_PROCESSING);
|
|
|
192 |
order.setStatusDescription("IN Process");
|
|
|
193 |
order.setCreated_timestamp(new Date().getTime());
|
|
|
194 |
|
|
|
195 |
orderCountForRow++;
|
|
|
196 |
try {
|
|
|
197 |
updatePayment(transaction.getId());
|
|
|
198 |
} catch (Exception e) {
|
|
|
199 |
logger.error("Unable to update payment for row number " + rowId, e);
|
|
|
200 |
addActionError("Unable to update payment for row number " + rowId);
|
|
|
201 |
setErrorMsg(getErrorMsg() + "<br>Unable to update payment for row number " + rowId);
|
|
|
202 |
continue;
|
|
|
203 |
}
|
|
|
204 |
try {
|
|
|
205 |
tsc.updateOrderForEbay(order);
|
|
|
206 |
} catch (Exception e) {
|
|
|
207 |
logger.error("Unable to update Ebay Order for row number " + rowId, e);
|
|
|
208 |
addActionError("Unable to update Ebay Order for row number " + rowId);
|
|
|
209 |
setErrorMsg(getErrorMsg() + "<br>Unable to update Ebay Order for row number " + rowId);
|
|
|
210 |
continue;
|
|
|
211 |
}
|
| 8182 |
amar.kumar |
212 |
} catch (Exception e) {
|
| 8303 |
amar.kumar |
213 |
logger.error("Unable to update Order for Ebay Order " + rowId, e);
|
|
|
214 |
addActionError("Unable to update Order for Ebay Order " + rowId);
|
|
|
215 |
setErrorMsg(getErrorMsg() + "<br>Unable to update Order for Ebay Order " + rowId);
|
|
|
216 |
continue;
|
| 8182 |
amar.kumar |
217 |
}
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
checkForErrors();
|
|
|
221 |
|
|
|
222 |
return "authsuccess";
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
private void updatePayment(long merchantTxnId) throws NumberFormatException, PaymentException, TException {
|
|
|
226 |
in.shop2020.payments.PaymentService.Client client = new PaymentClient().getClient();
|
|
|
227 |
Payment payment = client.getPaymentForTxnId(merchantTxnId).get(0);
|
|
|
228 |
client.updatePaymentDetails(payment.getPaymentId(), null, null, null, null, null, null, payment.getReferenceCode(), null, PaymentStatus.AUTHORIZED, null, null);
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
public String index() {
|
|
|
232 |
if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), "/ebay-dashboard"))
|
|
|
233 |
return "authfail";
|
|
|
234 |
checkForErrors();
|
|
|
235 |
return "authsuccess";
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
private boolean checkForErrors(){
|
|
|
239 |
Collection<String> actionErrors = getActionErrors();
|
|
|
240 |
if(actionErrors != null && !actionErrors.isEmpty()){
|
|
|
241 |
for (String str : actionErrors) {
|
|
|
242 |
errorMsg += "<BR/>" + str;
|
|
|
243 |
}
|
|
|
244 |
return true;
|
|
|
245 |
}
|
|
|
246 |
return false;
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
@Override
|
|
|
250 |
public void setServletRequest(HttpServletRequest request) {
|
|
|
251 |
this.request = request;
|
|
|
252 |
this.session = request.getSession();
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
public String getErrorMsg() {
|
|
|
256 |
return errorMsg;
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
public void setErrorMsg(String errorMsg) {
|
|
|
260 |
this.errorMsg = errorMsg;
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
public Long getRowId() {
|
|
|
264 |
return rowId;
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
public void setRowId(Long rowId) {
|
|
|
268 |
this.rowId = rowId;
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
public File getOrderDataFile() {
|
|
|
272 |
return orderDataFile;
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
public void setOrderDataFile(File orderDataFile) {
|
|
|
276 |
this.orderDataFile = orderDataFile;
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
}
|