Subversion Repositories SmartDukaan

Rev

Rev 8241 | Rev 8303 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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");
84
        }
85
 
86
 
87
        if(checkForErrors())
88
            return "authsuccess";
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 the File for Order Creation for Ebay ", e);
96
            addActionError("Unable to open the File for Order creation");
97
        }
98
        if(checkForErrors())
99
            return "authsuccess";
100
 
101
        TransactionService.Client tsc = null;
102
        try {
103
            tsc = new TransactionClient().getClient();
104
        } catch (Exception e) {
105
            logger.error("Unable to establish connection to the transaction service", e);
106
            addActionError("Unable to establish connection to the transaction service");
107
		}
108
 
109
        if(checkForErrors())
110
            return "authsuccess";
111
 
112
        if(checkForErrors())
113
            return "authsuccess";
114
 
115
        Sheet sheet = wb.getSheetAt(0);
116
        Row firstRow = sheet.getRow(0);
117
        logger.info("Last row number is:" + sheet.getLastRowNum());
118
        for (Row row : sheet) {
119
        	User user = null;
120
        	long orderCountForRow = 0;
121
            if(row.equals(firstRow))
122
                continue;
123
            rowId++;
124
 
125
            Order order = null;
126
            Transaction transaction = null;
127
            try {
128
            	row.getCell(EBAY_LISTINGID_INDEX).setCellType(Cell.CELL_TYPE_STRING);
129
	            //EbayOrder ebayOrder = tsc.getEbayOrderBySalesRecNumber(new Double(row.getCell(SALES_RECORD_NUM_INDEX).getNumericCellValue()).longValue());
130
	            EbayOrder ebayOrder = tsc.getEbayOrder(new Double(row.getCell(SALES_RECORD_NUM_INDEX).getNumericCellValue()).longValue(), 
8241 amar.kumar 131
	            		row.getCell(EBAY_LISTINGID_INDEX).getStringCellValue(), null).get(0);
8182 amar.kumar 132
	            row.getCell(TRANSACTION_ID_INDEX).setCellType(Cell.CELL_TYPE_STRING);
133
	            ebayOrder.setTransactionId(row.getCell(TRANSACTION_ID_INDEX).getStringCellValue());
134
	            tsc.updateEbayOrder(ebayOrder);
135
	            order = tsc.getOrder(ebayOrder.getOrderId());
136
	            transaction = tsc.getTransaction(order.getTransactionId());
137
            } catch (Exception e) {
138
            	logger.error("Unable to get EbayOrder for row number " + (rowId + 1), e);
139
    	        addActionError("Unable to get EbayOrder for row number " +  (rowId + 1));
140
    	        return "authsuccess";
141
            }
142
 
143
            try {   
144
    	        in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient().getClient();
145
    	        if (userClient.userExists(row.getCell(EMAIL_INDEX).getStringCellValue())) {
146
    	        	user = userClient.getUserByEmail(row.getCell(EMAIL_INDEX).getStringCellValue());
147
    	        } else {
148
    	        	user = new User();
149
    	        	user.setName(row.getCell(BUYER_NAME_INDEX).getStringCellValue());
150
    	        	user.setEmail(row.getCell(EMAIL_INDEX).getStringCellValue());
151
    	        	//TODO set password for user;
152
    	        	user.setIsAnonymous(false);
153
    	        	user = userClient.createUser(user);
154
    	        }
155
    	    } catch (Exception e) {
156
    	        logger.error("Unable to establish connection to the User service for row number " + (rowId + 1), e);
157
    	        addActionError("Unable to establish connection to the User servicefor row number " + (rowId + 1));
158
    	        return "authsuccess";
159
    		}
160
 
161
    	    //TODO Shopping cart is not being set right now Will 
162
            transaction.setShoppingCartid(user.getActiveCartId());
163
            transaction.setCustomer_id(user.getUserId());
164
            transaction.setCreatedOn(new Date().getTime());
165
 
166
            order.setCustomer_id(user.getUserId());
167
            order.setCustomer_email(user.getEmail());
168
        	order.setCustomer_name(user.getName());
169
        	if (order.getCustomer_mobilenumber()== null || order.getCustomer_mobilenumber() == "") {
170
        		order.setCustomer_mobilenumber(user.getMobileNumber());
171
        	}
172
        	order.setCustomer_address1(row.getCell(ADDR1_INDEX).getStringCellValue());
8259 amar.kumar 173
        	Cell address2Cell = row.getCell(ADDR2_INDEX);
174
        	if(address2Cell != null && address2Cell.getCellType() != Cell.CELL_TYPE_BLANK) {
175
        		order.setCustomer_address2(row.getCell(ADDR2_INDEX).getStringCellValue());
176
        	}
8182 amar.kumar 177
        	order.setCustomer_city(row.getCell(CITY_INDEX).getStringCellValue());
178
        	order.setCustomer_state(row.getCell(STATE_INDEX).getStringCellValue());
179
        	row.getCell(PINCODE_INDEX).setCellType(Cell.CELL_TYPE_STRING);
180
        	order.setCustomer_pincode(row.getCell(PINCODE_INDEX).getStringCellValue());
181
 
182
            order.setStatus(OrderStatus.SUBMITTED_FOR_PROCESSING);
183
            order.setStatusDescription("IN Process");
184
            order.setCreated_timestamp(new Date().getTime());
185
 
186
            orderCountForRow++;
187
            try {
188
            	updatePayment(transaction.getId());
189
            } catch (Exception e) {
190
            	logger.error("Unable to update payment for row number " + (rowId + 1), e);
191
                addActionError("Unable to update payment for row number " + (rowId + 1));
192
                return "authsuccess";
193
            }
194
            try {
195
            	tsc.updateOrderForEbay(order);
196
                //tsc.changeTransactionStatus(Long.valueOf(transaction.getId()), TransactionStatus.AUTHORIZED, "", new Long(PickUpType.SELF.getValue()), OrderType.B2C, OrderSource.EBAY);
197
            } catch (Exception e) {
198
                logger.error("Unable to update transaction status for row number " + (rowId +1), e);
199
                addActionError("Unable to update transaction status for row number " + (rowId +1));
200
                return "authsuccess";
201
    		}
202
        }
203
 
204
        checkForErrors();
205
 
206
        return "authsuccess";
207
    }
208
 
209
    private void updatePayment(long merchantTxnId) throws NumberFormatException, PaymentException, TException {
210
        in.shop2020.payments.PaymentService.Client client = new PaymentClient().getClient();
211
        Payment payment = client.getPaymentForTxnId(merchantTxnId).get(0);
212
        client.updatePaymentDetails(payment.getPaymentId(), null, null, null, null, null, null, payment.getReferenceCode(), null, PaymentStatus.AUTHORIZED, null, null);
213
    }   
214
 
215
	public String index() {
216
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), "/ebay-dashboard"))
217
            return "authfail";
218
        checkForErrors();
219
        return "authsuccess";
220
    }
221
 
222
	private boolean checkForErrors(){
223
        Collection<String> actionErrors = getActionErrors();
224
        if(actionErrors != null && !actionErrors.isEmpty()){
225
            for (String str : actionErrors) {
226
                errorMsg += "<BR/>" + str;
227
            }
228
            if(rowId>1) {
229
            	errorMsg += "<BR/>" + "Error while processing rowNumber : " + rowId;
230
            }
231
            return true;
232
        }
233
        return false;
234
    }
235
 
236
	@Override
237
	public void setServletRequest(HttpServletRequest request) {
238
		this.request = request;
239
        this.session = request.getSession();
240
	}
241
 
242
	public String getErrorMsg() {
243
		return errorMsg;
244
	}
245
 
246
	public void setErrorMsg(String errorMsg) {
247
		this.errorMsg = errorMsg;
248
	}
249
 
250
	public Long getRowId() {
251
		return rowId;
252
	}
253
 
254
	public void setRowId(Long rowId) {
255
		this.rowId = rowId;
256
	}
257
 
258
	public File getOrderDataFile() {
259
		return orderDataFile;
260
	}
261
 
262
	public void setOrderDataFile(File orderDataFile) {
263
		this.orderDataFile = orderDataFile;
264
	}
265
 
266
}