Subversion Repositories SmartDukaan

Rev

Rev 555 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 555 Rev 572
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
3
import in.shop2020.logistics.LogisticsInfo;
-
 
4
import in.shop2020.logistics.LogisticsServiceException;
-
 
5
import in.shop2020.model.v1.catalog.InventoryServiceException;
-
 
6
import in.shop2020.model.v1.catalog.Item;
-
 
7
import in.shop2020.model.v1.order.LineItem;
-
 
8
import in.shop2020.model.v1.order.Order;
3
import in.shop2020.model.v1.order.Order;
9
import in.shop2020.model.v1.order.OrderStatus;
-
 
10
import in.shop2020.model.v1.order.Payment;
-
 
11
import in.shop2020.model.v1.order.PaymentInfo;
-
 
12
import in.shop2020.model.v1.order.PaymentStatus;
-
 
13
import in.shop2020.model.v1.order.Transaction;
-
 
14
import in.shop2020.model.v1.order.TransactionStatus;
-
 
15
import in.shop2020.model.v1.user.Cart;
-
 
16
import in.shop2020.model.v1.user.LineStatus;
-
 
17
import in.shop2020.model.v1.user.ShoppingCartException;
-
 
18
import in.shop2020.model.v1.user.Address;
-
 
19
import in.shop2020.model.v1.user.UserContextException;
-
 
20
import in.shop2020.payments.PaymentException;
-
 
21
import in.shop2020.serving.controllers.BaseController;
4
import in.shop2020.serving.controllers.BaseController;
22
import in.shop2020.serving.pages.PageContentKeys;
5
import in.shop2020.serving.pages.PageContentKeys;
23
import in.shop2020.serving.pages.PageEnum;
6
import in.shop2020.serving.pages.PageEnum;
24
import in.shop2020.serving.pages.PageManager;
7
import in.shop2020.serving.pages.PageManager;
25
import in.shop2020.serving.services.PageLoaderHandler;
8
import in.shop2020.serving.services.PageLoaderHandler;
26
import in.shop2020.thrift.clients.CatalogServiceClient;
-
 
27
import in.shop2020.thrift.clients.LogisticsServiceClient;
-
 
28
import in.shop2020.thrift.clients.TransactionServiceClient;
9
import in.shop2020.thrift.clients.TransactionServiceClient;
29
import in.shop2020.thrift.clients.UserContextServiceClient;
10
import in.shop2020.thrift.clients.UserContextServiceClient;
30
 
11
 
31
import java.util.*;
12
import java.util.*;
32
 
13
 
33
import org.apache.juli.logging.Log;
14
import org.apache.juli.logging.Log;
34
import org.apache.juli.logging.LogFactory;
15
import org.apache.juli.logging.LogFactory;
35
import org.apache.struts2.convention.annotation.Result;
16
import org.apache.struts2.convention.annotation.Result;
36
import org.apache.struts2.convention.annotation.Results;
17
import org.apache.struts2.convention.annotation.Results;
37
import org.apache.struts2.interceptor.ParameterAware;
18
import org.apache.struts2.interceptor.ParameterAware;
38
import org.apache.struts2.rest.DefaultHttpHeaders;
-
 
39
import org.apache.struts2.rest.HttpHeaders;
-
 
40
import org.apache.thrift.TException;
19
import org.apache.thrift.TException;
41
 
20
 
42
@Results({
21
@Results({
43
	@Result(name="redirect", location="${url}", type="redirect")
22
	@Result(name="redirect", location="${url}", type="redirect")
44
})
23
})
Line 78... Line 57...
78
    	
57
    	
79
    	htmlSnippets = pageManager.getPageContents(PageEnum.ORDER_DETAILS_PAGE, params);
58
    	htmlSnippets = pageManager.getPageContents(PageEnum.ORDER_DETAILS_PAGE, params);
80
    	return "show";
59
    	return "show";
81
    }
60
    }
82
 
61
 
-
 
62
    // POST /order/
-
 
63
    public String create(){
-
 
64
    	long addressId = Long.parseLong(this.request.getParameter("addressid"));
-
 
65
    	long currentCartId = userinfo.getCartId();
-
 
66
    	List<Order> orders = null;
-
 
67
		try {
-
 
68
			UserContextServiceClient userServiceClient = new UserContextServiceClient();
-
 
69
			in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
-
 
70
			
-
 
71
			userClient.addAddressToCart(currentCartId, addressId);
-
 
72
			Long txn_id = userClient.commitCart(currentCartId);
-
 
73
			
-
 
74
			TransactionServiceClient tsc = new TransactionServiceClient();
-
 
75
			orders = tsc.getClient().getOrdersForTransaction(txn_id);
-
 
76
			
-
 
77
			long newCartId = userClient.getUserById(userinfo.getUserId()).getActiveCartId();
-
 
78
			userinfo.setCartId(newCartId);
-
 
79
			userinfo.setTotalItems(0);
-
 
80
		} catch (TException e) {
-
 
81
			e.printStackTrace();
-
 
82
		} catch (Exception e) {
-
 
83
			e.printStackTrace();
-
 
84
		}
-
 
85
    	
-
 
86
		this.message = "Your order numbers are as below:";
-
 
87
		
-
 
88
		for(Order order: orders){
-
 
89
			this.message = this.message  + "<br> <a href=\"./order/" + order.getId() + "\">" + order.getId() + "</a>";
-
 
90
		}
-
 
91
		
-
 
92
		PageLoaderHandler pageLoader = new PageLoaderHandler();
-
 
93
		htmlSnippets = new HashMap<String, String>();
-
 
94
		htmlSnippets.put("HEADER", pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getNameOfUser()));
-
 
95
		htmlSnippets.put("MAIN_MENU", pageLoader.getMainMenuHtml());
-
 
96
		htmlSnippets.put("SEARCH_BAR", pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000));
-
 
97
		htmlSnippets.put("CUSTOMER_SERVICE", pageLoader.getCustomerServiceHtml());
-
 
98
		htmlSnippets.put("FOOTER",pageLoader.getFooterHtml());
-
 
99
		
-
 
100
		return "success";
-
 
101
    }
-
 
102
    
83
	public String getId(){
103
	public String getId(){
84
		return id;
104
		return id;
85
	}
105
	}
86
	
106
	
87
	public void setId(String id){
107
	public void setId(String id){
Line 133... Line 153...
133
	}
153
	}
134
	
154
	
135
	public String getCssFileSnippet(){
155
	public String getCssFileSnippet(){
136
		return htmlSnippets.get("CSS_FILES");
156
		return htmlSnippets.get("CSS_FILES");
137
	}
157
	}
138
 
-
 
139
    // POST /order/
-
 
140
    public String create() throws Exception {
-
 
141
    	long addressId = Long.parseLong(this.request.getParameter("addressid"));
-
 
142
    	UserContextServiceClient cl = new UserContextServiceClient();
-
 
143
    	cl.getClient().addAddressToCart(userinfo.getCartId(), addressId);
-
 
144
    	
-
 
145
		Transaction t = getTransaction();
-
 
146
		TransactionServiceClient tsc = new TransactionServiceClient();
-
 
147
		long transaction_id = tsc.getClient().createTransaction(t);
-
 
148
		
-
 
149
		List<Order> orders = tsc.getClient().getOrdersForTransaction(transaction_id);
-
 
150
		this.message = "Your order numbers are as below:";
-
 
151
		
-
 
152
		for(Order order: orders){
-
 
153
			this.message = this.message  + "<br> <a href=\"./order/" + order.getId() + "\">" + order.getId() + "</a>";
-
 
154
		}
-
 
155
		
-
 
156
		
-
 
157
		cl.getClient().commitCart(userinfo.getCartId());
-
 
158
		
-
 
159
		userinfo.setCartId(-1);
-
 
160
		userinfo.setTotalItems(0);
-
 
161
		this.session.setAttribute("userinfo", userinfo);
-
 
162
		
-
 
163
		PageLoaderHandler pageLoader = new PageLoaderHandler();
-
 
164
		htmlSnippets = new HashMap<String, String>();
-
 
165
		htmlSnippets.put("HEADER", pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getNameOfUser()));
-
 
166
		htmlSnippets.put("MAIN_MENU", pageLoader.getMainMenuHtml());
-
 
167
		htmlSnippets.put("SEARCH_BAR", pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000));
-
 
168
		htmlSnippets.put("CUSTOMER_SERVICE", pageLoader.getCustomerServiceHtml());
-
 
169
		htmlSnippets.put("FOOTER",pageLoader.getFooterHtml());
-
 
170
		
-
 
171
		return "success";
-
 
172
    }
-
 
173
	
158
	
174
	public String getMessage(){
159
	public String getMessage(){
175
		return this.message;
160
		return this.message;
176
	}
161
	}
177
	
-
 
178
	private Transaction getTransaction(){
-
 
179
		Transaction t = new Transaction();
-
 
180
		PaymentInfo paymentInfo = new PaymentInfo();
-
 
181
		t.setPaymentInfo(paymentInfo);
-
 
182
		t.setShoppingCartid(userinfo.getCartId());
-
 
183
		t.setCustomer_id(userinfo.getUserId());
-
 
184
		t.setCreatedOn((new Date()).getTime());
-
 
185
		t.setTransactionStatus(TransactionStatus.INIT);
-
 
186
		t.setStatusDescription("New order");
-
 
187
		
-
 
188
		t.setOrders(getOrders());
-
 
189
		
-
 
190
		return t;
-
 
191
	}
-
 
192
 
-
 
193
	private List<Order> getOrders(){
-
 
194
		List<Order> orders = new ArrayList<Order>();
-
 
195
		UserContextServiceClient usc = null;
-
 
196
		Cart c = null;
-
 
197
		try {
-
 
198
			usc = new UserContextServiceClient();
-
 
199
			c = usc.getClient().getCart(userinfo.getCartId());
-
 
200
		} catch (ShoppingCartException e) {
-
 
201
			e.printStackTrace();
-
 
202
		} catch (TException e) {
-
 
203
			e.printStackTrace();
-
 
204
		} catch (Exception e) {
-
 
205
			e.printStackTrace();
-
 
206
		}
-
 
207
		List<in.shop2020.model.v1.user.Line> cartLines = c.getLines();
-
 
208
		
-
 
209
		for(in.shop2020.model.v1.user.Line cartLine : cartLines){
-
 
210
			if(cartLine.getLineStatus() == LineStatus.LINE_ACTIVE){
-
 
211
				//line is active
-
 
212
				LineItem lineItem = getLineItem(cartLine.getItemId());
-
 
213
				// create orders equivalent to quantity. Create one order per item.
-
 
214
				for(int i= 0; i<cartLine.getQuantity(); i++){
-
 
215
					//  set order
-
 
216
					Order order = getOrder(c.getAddressId(), lineItem);
-
 
217
					order.addToLineitems(lineItem);
-
 
218
					orders.add(order);
-
 
219
				}
-
 
220
			}
-
 
221
		}
-
 
222
		return orders;
-
 
223
	}
-
 
224
	
-
 
225
	
-
 
226
	private Order getOrder(long addressId, LineItem lineItem){
-
 
227
		String sku_id = lineItem.getSku_id();
-
 
228
		double total_amount = lineItem.getTotal_price();
-
 
229
		double total_weight = lineItem.getTotal_weight();
-
 
230
		
-
 
231
		long customer_id = userinfo.getUserId();
-
 
232
		Address address = getAddress(addressId);
-
 
233
		String customer_email = userinfo.getEmail();
-
 
234
		String customer_name = address.getName();
-
 
235
		String customer_address = getAddressString(address);
-
 
236
		String customer_mobilenumber = address.getPhone();
-
 
237
		String customer_pincode = address.getPin();
-
 
238
		
-
 
239
		
-
 
240
		
-
 
241
		// get logistics information
-
 
242
		LogisticsInfo logistics_info = null;
-
 
243
		try {
-
 
244
			LogisticsServiceClient lsc = new LogisticsServiceClient();
-
 
245
			logistics_info = lsc.getClient().getLogisticsInfo(customer_pincode, sku_id);
-
 
246
		} catch (TException e) {
-
 
247
			// TODO Auto-generated catch block
-
 
248
			e.printStackTrace();
-
 
249
		} catch (LogisticsServiceException e) {
-
 
250
			// TODO Auto-generated catch block
-
 
251
			e.printStackTrace();
-
 
252
		} catch (Exception e) {
-
 
253
			// TODO Auto-generated catch block
-
 
254
			e.printStackTrace();
-
 
255
		}
-
 
256
	
-
 
257
		long warehouse_id = logistics_info.getWarehouse_id();
-
 
258
		long logistics_provider_id= logistics_info.getProvider_id();
-
 
259
		String airwaybill_no = String.valueOf(logistics_info.getAirway_billno()); // should it be really long ?
-
 
260
		String tracking_id = airwaybill_no; //right now both are same
-
 
261
		long expected_delivery_time = (new Date()).getTime() + 60*60*1000*logistics_info.getDelivery_estimate(); 
-
 
262
		
-
 
263
		long created_timestamp = (new Date()).getTime();
-
 
264
		OrderStatus status = OrderStatus.SUBMITTED_FOR_PROCESSING; // to get from orderstatus file
-
 
265
		String statusDescription = "Submitted to warehouse";
-
 
266
		
-
 
267
		Order order = new Order();
-
 
268
		order.setLogistics_provider_id(logistics_provider_id);
-
 
269
		order.setAirwaybill_no(airwaybill_no);
-
 
270
		order.setExpected_delivery_time(expected_delivery_time);
-
 
271
		order.setTracking_id(tracking_id);
-
 
272
		
-
 
273
		order.setWarehouse_id(warehouse_id);
-
 
274
		order.setCreated_timestamp(created_timestamp);
-
 
275
		order.setStatus(status);
-
 
276
		order.setStatusDescription(statusDescription);
-
 
277
		
-
 
278
		order.setCustomer_id(customer_id);
-
 
279
		order.setCustomer_name(customer_name);
-
 
280
		order.setCustomer_address(customer_address);
-
 
281
		order.setCustomer_email(customer_email);
-
 
282
		order.setCustomer_mobilenumber(customer_mobilenumber);
-
 
283
		order.setCustomer_pincode(customer_pincode);
-
 
284
		
-
 
285
		order.setTotal_amount(total_amount);
-
 
286
		order.setTotal_weight(total_weight);
-
 
287
		
-
 
288
		return order;
-
 
289
	}
-
 
290
	
-
 
291
 
-
 
292
	private Address getAddress(long addressId){
-
 
293
		List<Address> addresses = null;
-
 
294
		
-
 
295
		try {
-
 
296
			UserContextServiceClient usc = new UserContextServiceClient();
-
 
297
			addresses = usc.getClient().getUserById(userinfo.getUserId()).getAddresses();
-
 
298
		} catch (UserContextException e) {
-
 
299
			e.printStackTrace();
-
 
300
		} catch (TException e) {
-
 
301
			e.printStackTrace();
-
 
302
		} catch (Exception e) {
-
 
303
			e.printStackTrace();
-
 
304
		}
-
 
305
		Address address = null;
-
 
306
		for(Address a : addresses){
-
 
307
			if(a.getId() == addressId){
-
 
308
				address = a;
-
 
309
			}
-
 
310
		}
-
 
311
		return address;
-
 
312
	}
-
 
313
 
-
 
314
	private LineItem getLineItem(long catelogItemId){
-
 
315
		
-
 
316
		LineItem lineItem = new LineItem();
-
 
317
		Item item = null;
-
 
318
		try {
-
 
319
			CatalogServiceClient csc = new CatalogServiceClient();
-
 
320
			item = csc.getClient().getItemByCatalogId(catelogItemId);
-
 
321
		} catch (InventoryServiceException e) {
-
 
322
			e.printStackTrace();
-
 
323
		} catch (TException e) {
-
 
324
			e.printStackTrace();
-
 
325
		} catch (Exception e) {
-
 
326
			e.printStackTrace();
-
 
327
		}
-
 
328
	
-
 
329
		String model_name = item.getModelName();
-
 
330
		String model_number = item.getModelNumber();
-
 
331
		String brand = item.getManufacturerName();
-
 
332
		String sku_id = item.getVendorItemId();
-
 
333
		double unit_price = item.getSellingPrice();
-
 
334
		double unit_weight = item.getWeight();
-
 
335
		double total_price = item.getSellingPrice();
-
 
336
		double total_weight = item.getWeight();
-
 
337
		String  extra_info = item.getFeatureDescription();
-
 
338
		double quantity = 1; // because now we will create one order per item
-
 
339
		
-
 
340
		lineItem.setSku_id(sku_id);
-
 
341
		lineItem.setBrand(brand);
-
 
342
		lineItem.setExtra_info(extra_info);
-
 
343
		lineItem.setModel_name(model_name);
-
 
344
		lineItem.setModel_number(model_number);
-
 
345
		lineItem.setQuantity(quantity);
-
 
346
		lineItem.setSku_id(sku_id);
-
 
347
		lineItem.setUnit_price(unit_price);
-
 
348
		lineItem.setUnit_weight(unit_weight);
-
 
349
		lineItem.setTotal_price(total_price);
-
 
350
		lineItem.setTotal_weight(total_weight);
-
 
351
		return lineItem;
-
 
352
	}
-
 
353
 
-
 
354
	private String getAddressString(Address a){
-
 
355
		String add = a.getLine1()+",\n"+a.getLine2()+",\n Landmark"+a.getLandmark()+",/n"+a.getCity()+",\n"+a.getState()+",\n"+a.getCountry();
-
 
356
		return add;
-
 
357
	}
-
 
358
	
-
 
359
	private String getAddress(Set<Address> address, long id){
-
 
360
		for(Address a : address){
-
 
361
			if(a.getId() == id){
-
 
362
				//Prepare String
-
 
363
				String add = a.getName()+",\n"+a.getLine1()+",\n"+a.getLine2()+",\n Landmark"+a.getLandmark()+",/n"+a.getCity()+",\n"+a.getState()+",\n"+a.getCountry()+",\n"+a.getPin()+",\n Phone :- "+a.getPhone();
-
 
364
				return add;
-
 
365
			}
-
 
366
		}
-
 
367
		return "";
-
 
368
	}
-
 
369
 
162
 
370
}
163
}