Subversion Repositories SmartDukaan

Rev

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