Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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