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