Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
419 rajveer 1
package in.shop2020.serving.utils;
2
 
637 rajveer 3
import java.io.ByteArrayInputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.ObjectInputStream;
6
import java.io.ObjectOutputStream;
421 rajveer 7
import java.util.List;
8
 
424 rajveer 9
import in.shop2020.model.v1.catalog.Item;
10
import in.shop2020.model.v1.catalog.InventoryService.Client;
741 rajveer 11
import in.shop2020.model.v1.order.LineItem;
12
import in.shop2020.model.v1.order.Order;
13
import in.shop2020.model.v1.order.Transaction;
555 chandransh 14
import in.shop2020.model.v1.user.Cart;
15
 
16
import in.shop2020.model.v1.user.Line;
595 rajveer 17
import in.shop2020.model.v1.user.Sex;
555 chandransh 18
import in.shop2020.model.v1.user.ShoppingCartException;
19
import in.shop2020.model.v1.user.User;
419 rajveer 20
import in.shop2020.model.v1.user.UserContextException;
681 rajveer 21
import in.shop2020.model.v1.user.WidgetException;
421 rajveer 22
import in.shop2020.thrift.clients.CatalogServiceClient;
741 rajveer 23
import in.shop2020.thrift.clients.TransactionServiceClient;
419 rajveer 24
import in.shop2020.thrift.clients.UserContextServiceClient;
25
 
26
import org.apache.thrift.TException;
27
 
28
public class Utils {
649 chandransh 29
	//FIXME: Read this path from the config server
637 rajveer 30
	public static final String EXPORT_ENTITIES_PATH =	"/var/lib/tomcat6/webapps/export/html/entities/";
536 rajveer 31
	/*
419 rajveer 32
	private static UserContextServiceClient userContextServiceClient;
33
	private static ShoppingCartClient shoppingCartClient;
421 rajveer 34
	private static CatalogServiceClient catalogServiceClient;
449 rajveer 35
	private static WidgetServiceClient widgetServiceClient;
419 rajveer 36
 
449 rajveer 37
 
419 rajveer 38
	static {
39
		try {
40
			userContextServiceClient = new UserContextServiceClient();
41
			shoppingCartClient = new ShoppingCartClient();
421 rajveer 42
			catalogServiceClient = new CatalogServiceClient();
449 rajveer 43
			widgetServiceClient = new WidgetServiceClient();
419 rajveer 44
		} catch (Exception e) {
45
			// TODO Auto-generated catch block
46
			e.printStackTrace();
47
		}
48
	}
536 rajveer 49
	*/
50
	public static boolean isUserLoggedIn(long userId){ 
555 chandransh 51
		boolean isNotLoggedIn = true;
419 rajveer 52
		try {
536 rajveer 53
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
54
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
555 chandransh 55
			isNotLoggedIn = userClient.getUserById(userId).isIsAnonymous();
419 rajveer 56
		} catch (UserContextException e) {
57
			e.printStackTrace();
58
		} catch (TException e) {
59
			e.printStackTrace();
536 rajveer 60
		} catch (Exception e) {
61
			e.printStackTrace();
419 rajveer 62
		}
555 chandransh 63
		return !isNotLoggedIn;
419 rajveer 64
	}
65
 
66
 
67
	public static String getEmailId(long userId){
741 rajveer 68
		String email = " ";
419 rajveer 69
 
70
		try {
536 rajveer 71
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
72
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
741 rajveer 73
			if(userClient.getUserById(userId).getEmail() != null){
74
				email = userClient.getUserById(userId).getEmail();
75
			}
419 rajveer 76
		} catch (UserContextException e) {
77
			e.printStackTrace();
78
		} catch (TException e) {
79
			e.printStackTrace();
536 rajveer 80
		} catch (Exception e) {
81
			e.printStackTrace();
419 rajveer 82
		}
83
		return email; 
84
	}
85
 
86
	public static int getNumberOfItemsInCart(long cartId) {
536 rajveer 87
 
88
 
419 rajveer 89
		int numberOfItems = 0;
90
		try {
555 chandransh 91
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
92
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
536 rajveer 93
 
555 chandransh 94
			numberOfItems = userClient.getCart(cartId).getLinesSize();
419 rajveer 95
		} catch (ShoppingCartException e) {
96
			e.printStackTrace();
97
		} catch (TException e) {
98
			e.printStackTrace();
536 rajveer 99
		} catch (Exception e) {
100
			e.printStackTrace();
419 rajveer 101
		}
102
		return numberOfItems;
103
	}
104
 
105
 
106
	public static long getCartId(long userId) {
107
		long cartId = 0;
108
		try {
555 chandransh 109
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
110
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
536 rajveer 111
 
555 chandransh 112
			cartId = userClient.getCurrentCart(userId).getId();
419 rajveer 113
		} catch (ShoppingCartException e) {
114
			e.printStackTrace();
115
		} catch (TException e) {
116
			e.printStackTrace();
536 rajveer 117
		} catch (Exception e) {
118
			e.printStackTrace();
419 rajveer 119
		}
120
		return cartId;
121
	}
122
 
123
 
124
	public static long addItemToCart(long catalogItemId, long userId, boolean isSessionId){
125
		long cartId = -1;
126
 
127
		try {
555 chandransh 128
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
129
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
419 rajveer 130
 
555 chandransh 131
			cartId = userClient.createCart(userId);
132
			userClient.addItemToCart(cartId, catalogItemId, 1);
419 rajveer 133
 
134
		} catch (ShoppingCartException e) {
135
			// TODO Auto-generated catch block
136
			e.printStackTrace();
137
		} catch (TException e) {
138
			// TODO Auto-generated catch block
139
			e.printStackTrace();
140
		} catch (Exception e) {
141
			// TODO Auto-generated catch block
142
			e.printStackTrace();
143
		}
144
		return cartId;
145
		//if user is logged in create  new cart
146
		//if( userClient.getState(userId, false).isIsLoggedIn()){
147
	}
421 rajveer 148
 
517 rajveer 149
	public static boolean deleteItemFromCart(long cartId, long catalogItemId, long userId, boolean isSessionId){
507 rajveer 150
		try {
555 chandransh 151
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
152
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
536 rajveer 153
 
555 chandransh 154
			userClient.deleteItemFromCart(cartId, catalogItemId);
507 rajveer 155
			return true;	
156
		} catch (ShoppingCartException e) {
157
			e.printStackTrace();
158
		} catch (TException e) {
159
			e.printStackTrace();
160
		} catch (Exception e) {
161
			e.printStackTrace();
162
		}
163
 
164
		return false;
165
	}
166
 
167
	public static boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
168
		try {
555 chandransh 169
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
170
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
536 rajveer 171
 
555 chandransh 172
			userClient.changeQuantity(cartId, itemId, quantity);
507 rajveer 173
			return true;
174
		} catch (ShoppingCartException e) {
175
			e.printStackTrace();
176
		} catch (TException e) {
177
			e.printStackTrace();
536 rajveer 178
		} catch (Exception e) {
179
			e.printStackTrace();
507 rajveer 180
		}
181
		return false;
182
	}
421 rajveer 183
 
184
 
185
 
741 rajveer 186
	public static String getOrderDetails(long txnId){
187
		Transaction transaction;
421 rajveer 188
		StringBuilder orderDetails = new StringBuilder();
189
		try {
741 rajveer 190
			TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
191
			in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
536 rajveer 192
 
741 rajveer 193
			transaction = txnClient.getTransaction(txnId);
194
 
195
			for (Order order : transaction.getOrders()) {
196
				for(LineItem line: order.getLineitems()){
197
					if(line.getBrand() != null){
198
						orderDetails.append(line.getBrand());
199
					}
200
					if(line.getModel_name() != null){
201
						orderDetails.append(line.getModel_name()); 
202
					}
203
					if(line.getModel_number() != null){
204
						orderDetails.append(line.getModel_number());
205
					}
206
					if(line.getColor() != null){
207
						orderDetails.append(line.getColor());
208
					}
209
				}
210
 
211
			}
212
 
421 rajveer 213
		} catch (ShoppingCartException e) {
214
			e.printStackTrace();
215
		} catch (TException e) {
216
			e.printStackTrace();
536 rajveer 217
		} catch (Exception e) {
218
			e.printStackTrace();
421 rajveer 219
		}
741 rajveer 220
		return orderDetails.toString() + " ";
421 rajveer 221
	}
222
 
741 rajveer 223
	public static String getContactNumber(long txnId){
224
		Transaction transaction;
225
		StringBuilder orderDetails = new StringBuilder();
226
		try {
227
			TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
228
			in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
229
 
230
			transaction = txnClient.getTransaction(txnId);
231
			for (Order order : transaction.getOrders()) {
232
				if(order.getCustomer_mobilenumber() != null){
233
					orderDetails.append(order.getCustomer_mobilenumber()) ;	
234
				}
235
 
236
			}
237
 
238
		} catch (ShoppingCartException e) {
239
			e.printStackTrace();
240
		} catch (TException e) {
241
			e.printStackTrace();
242
		} catch (Exception e) {
243
			e.printStackTrace();
244
		}
421 rajveer 245
 
741 rajveer 246
		return orderDetails.toString() + " ";
421 rajveer 247
	}
248
 
741 rajveer 249
	public static String getBillingAddress(long txnId){		
250
	Transaction transaction;
251
	StringBuilder orderDetails = new StringBuilder();
252
	try {
253
		TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
254
		in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();
255
 
256
		transaction = txnClient.getTransaction(txnId);
257
 
258
		for (Order order : transaction.getOrders()) {
259
			if(order.getCustomer_name()!=null){
260
				orderDetails.append(order.getCustomer_name());
261
			}
745 chandransh 262
			if(order.getCustomer_address1()!=null){
263
				orderDetails.append(order.getCustomer_address1());
741 rajveer 264
			}
745 chandransh 265
			if(order.getCustomer_address2()!=null){
266
				orderDetails.append(order.getCustomer_address2());
267
			}
741 rajveer 268
			if(order.getCustomer_city()!=null){
269
				orderDetails.append(order.getCustomer_city());
270
			}
271
			if(order.getCustomer_state()!=null){
272
				orderDetails.append(order.getCustomer_state());
273
			}
274
			if(order.getCustomer_pincode()!=null){
275
				orderDetails.append(order.getCustomer_pincode());
276
			}
277
		}
278
 
279
	} catch (ShoppingCartException e) {
280
		e.printStackTrace();
281
	} catch (TException e) {
282
		e.printStackTrace();
283
	} catch (Exception e) {
284
		e.printStackTrace();
421 rajveer 285
	}
286
 
741 rajveer 287
	return orderDetails.toString() + " ";
288
 
421 rajveer 289
	}
741 rajveer 290
 
291
 
424 rajveer 292
	public static String getNameOfUser(long userId) {
741 rajveer 293
		String name = " ";
424 rajveer 294
		try {
536 rajveer 295
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
296
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
297
 
555 chandransh 298
			name = userClient.getUserById(userId).getName();
424 rajveer 299
		} catch (UserContextException e) {
300
			// TODO Auto-generated catch block
301
			e.printStackTrace();
302
		} catch (TException e) {
303
			// TODO Auto-generated catch block
304
			e.printStackTrace();
536 rajveer 305
		} catch (Exception e) {
306
			// TODO Auto-generated catch block
307
			e.printStackTrace();
424 rajveer 308
		}
309
		return name; 
310
	}
421 rajveer 311
 
424 rajveer 312
	public static double getPaymentAmount(long cartId){
313
		double totalAmount = 0;
314
 
315
		Cart cart;
316
		try {
555 chandransh 317
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
318
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
536 rajveer 319
 
555 chandransh 320
			cart = userClient.getCart(cartId);
424 rajveer 321
 
322
			List<Line> lineItems = cart.getLines(); 
323
 
324
			for (Line line : lineItems) {
325
				long productId = line.getItemId();
637 rajveer 326
				totalAmount =  totalAmount + line.getQuantity() * Utils.getItemPrice(productId);
424 rajveer 327
			}
328
 
329
		} catch (ShoppingCartException e) {
330
			e.printStackTrace();
331
		} catch (TException e) {
332
			e.printStackTrace();
536 rajveer 333
		} catch (Exception e) {
334
			e.printStackTrace();
424 rajveer 335
		}
336
 
337
		return totalAmount;
338
	}
339
 
637 rajveer 340
	public static double getItemPrice(long itemId){
424 rajveer 341
		CatalogServiceClient catalogServiceClient = null;
342
		Client client = null;
343
		Double itemPrice = 0.0;
344
		try {
345
			catalogServiceClient = new CatalogServiceClient();
346
			client = catalogServiceClient.getClient();
637 rajveer 347
			Item item = client.getItem(itemId);
517 rajveer 348
			itemPrice = item.getSellingPrice();
536 rajveer 349
 
424 rajveer 350
		}
351
		catch(Exception e){
352
			e.printStackTrace();
353
		}
354
		return itemPrice;
355
	}
356
 
449 rajveer 357
 
358
	public static void deleteFromMyResearch(long userId, long itemId) {
536 rajveer 359
 
458 rajveer 360
		try {
555 chandransh 361
			UserContextServiceClient userServiceClient = new UserContextServiceClient();
362
			in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
363
			userClient.deleteItemFromMyResearch(userId, itemId);
458 rajveer 364
		} catch (WidgetException e) {
365
			e.printStackTrace();
366
		} catch (TException e) {
367
			e.printStackTrace();
536 rajveer 368
		} catch (Exception e) {
369
			e.printStackTrace();
458 rajveer 370
		}
449 rajveer 371
 
372
	}
373
 
507 rajveer 374
 
375
	public static boolean ChangePassword(long userId, String email, String oldPassword,
376
			String newPassword) {
536 rajveer 377
 
507 rajveer 378
 
379
		try {
536 rajveer 380
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
381
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
382
 
595 rajveer 383
			return userClient.updatePassword(userId,oldPassword, newPassword);
507 rajveer 384
		} catch (UserContextException e) {
385
			e.printStackTrace();
386
		} catch (TException e) {
387
			e.printStackTrace();
536 rajveer 388
		} catch (Exception e) {
389
			e.printStackTrace();
507 rajveer 390
		}
391
		return false;
392
	}
393
 
394
 
595 rajveer 395
	public static boolean UpdatePersonalDetails(long userId,  String name,  String phone, String dateOfBirth, String sex, String communicationEmail,
507 rajveer 396
			String subscribeNewsletter) {
397
 
398
		try {
536 rajveer 399
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
400
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
401
 
507 rajveer 402
 
555 chandransh 403
			User user = userClient.getUserById(userId);		
404
			user.setDateOfBirth(dateOfBirth);
405
			user.setName(name);
406
			user.setCommunicationEmail(communicationEmail);
595 rajveer 407
			user.setSex(Sex.findByValue(Integer.parseInt(sex)));
408
			user.setMobileNumber(phone);
555 chandransh 409
			userClient.updateUser(user);
569 rajveer 410
 
507 rajveer 411
			return true;
412
		} catch (UserContextException e) {
413
			e.printStackTrace();
414
		} catch (TException e) {
415
			e.printStackTrace();
536 rajveer 416
		} catch (Exception e) {
417
			e.printStackTrace();
507 rajveer 418
		}
419
		return false;
420
	}
536 rajveer 421
 
422
 
423
	public static void mergeCarts(long fromCartId, long toCartId){
424
		try {
555 chandransh 425
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
426
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
427
			userClient.mergeCart(fromCartId, toCartId);
536 rajveer 428
		} catch (Exception e) {
429
			// TODO Auto-generated catch block
430
			e.printStackTrace();
431
		}
432
 
433
	}
507 rajveer 434
 
637 rajveer 435
	public static void storeCategories(Object obj) throws Exception {
436
 
437
		ByteArrayOutputStream bStream = new ByteArrayOutputStream();
438
		ObjectOutputStream oStream = new ObjectOutputStream( bStream );
439
		oStream.writeObject ( obj );
440
 
441
		byte[] byteVal = bStream. toByteArray();
442
 
443
		CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
444
		Client client = catalogServiceClient.getClient();
445
 
446
		client.putCategoryObject(byteVal);
447
	}
448
 
449
	public static Object getCategories() throws Exception {
450
		CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
451
		Client client = catalogServiceClient.getClient();
452
 
453
		byte[] byteVal  = client.getCategoryObject();
454
 
455
		Object obj = null;
456
		ObjectInputStream in = null;
457
		try {
458
			ByteArrayInputStream bStream = new ByteArrayInputStream(byteVal);
459
			in = new ObjectInputStream(bStream);
460
			obj = in.readObject();
461
		}
462
		finally {
463
			if(in != null) {
464
				in.close();
465
			}
466
		}
467
		return obj;
468
	}
649 chandransh 469
 
470
//	public static void main(String args[]) throws Exception{
471
//		// to store categories
472
//		Map<Long, Category> categories = new HashMap<Long, Category>();
473
//		Map<Long, in.shop2020.metamodel.definitions.Category> cmsCategories = Catalog.getInstance().getDefinitionsContainer().getCategories();
474
//		for(in.shop2020.metamodel.definitions.Category cmsCategory: cmsCategories.values()){
475
//			Category category = new Category(cmsCategory.getID());
476
//			category.setLabel(cmsCategory.getLabel());
477
//			if(cmsCategory.getParentCategory()!=null){
478
//				category.setParentCategoryId(cmsCategory.getParentCategory().getID());
479
//			}
480
//			if(cmsCategory.getChildrenCategory()!=null){
481
//				for(in.shop2020.metamodel.definitions.Category childCategory: cmsCategory.getChildrenCategory()){
482
//					category.addChild(childCategory.getID());
483
//				}
484
//			}
485
//			categories.put(cmsCategory.getID(), category);
486
//		}
487
//		Utils.storeCategories(categories);
488
//		// to get categories
489
//		//Map<Long, Category> 
490
//		categories = (Map<Long, Category>)Utils.getCategories();
491
//		System.out.println("hello");
492
//	}
493
 
419 rajveer 494
}
745 chandransh 495