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
			}
262
			if(order.getCustomer_address()!=null){
263
				orderDetails.append(order.getCustomer_address());
264
			}
265
			if(order.getCustomer_city()!=null){
266
				orderDetails.append(order.getCustomer_city());
267
			}
268
			if(order.getCustomer_state()!=null){
269
				orderDetails.append(order.getCustomer_state());
270
			}
271
			if(order.getCustomer_pincode()!=null){
272
				orderDetails.append(order.getCustomer_pincode());
273
			}
274
		}
275
 
276
	} catch (ShoppingCartException e) {
277
		e.printStackTrace();
278
	} catch (TException e) {
279
		e.printStackTrace();
280
	} catch (Exception e) {
281
		e.printStackTrace();
421 rajveer 282
	}
283
 
741 rajveer 284
	return orderDetails.toString() + " ";
285
 
421 rajveer 286
	}
741 rajveer 287
 
288
 
424 rajveer 289
	public static String getNameOfUser(long userId) {
741 rajveer 290
		String name = " ";
424 rajveer 291
		try {
536 rajveer 292
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
293
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
294
 
555 chandransh 295
			name = userClient.getUserById(userId).getName();
424 rajveer 296
		} catch (UserContextException e) {
297
			// TODO Auto-generated catch block
298
			e.printStackTrace();
299
		} catch (TException e) {
300
			// TODO Auto-generated catch block
301
			e.printStackTrace();
536 rajveer 302
		} catch (Exception e) {
303
			// TODO Auto-generated catch block
304
			e.printStackTrace();
424 rajveer 305
		}
306
		return name; 
307
	}
421 rajveer 308
 
424 rajveer 309
	public static double getPaymentAmount(long cartId){
310
		double totalAmount = 0;
311
 
312
		Cart cart;
313
		try {
555 chandransh 314
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
315
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
536 rajveer 316
 
555 chandransh 317
			cart = userClient.getCart(cartId);
424 rajveer 318
 
319
			List<Line> lineItems = cart.getLines(); 
320
 
321
			for (Line line : lineItems) {
322
				long productId = line.getItemId();
637 rajveer 323
				totalAmount =  totalAmount + line.getQuantity() * Utils.getItemPrice(productId);
424 rajveer 324
			}
325
 
326
		} catch (ShoppingCartException e) {
327
			e.printStackTrace();
328
		} catch (TException e) {
329
			e.printStackTrace();
536 rajveer 330
		} catch (Exception e) {
331
			e.printStackTrace();
424 rajveer 332
		}
333
 
334
		return totalAmount;
335
	}
336
 
637 rajveer 337
	public static double getItemPrice(long itemId){
424 rajveer 338
		CatalogServiceClient catalogServiceClient = null;
339
		Client client = null;
340
		Double itemPrice = 0.0;
341
		try {
342
			catalogServiceClient = new CatalogServiceClient();
343
			client = catalogServiceClient.getClient();
637 rajveer 344
			Item item = client.getItem(itemId);
517 rajveer 345
			itemPrice = item.getSellingPrice();
536 rajveer 346
 
424 rajveer 347
		}
348
		catch(Exception e){
349
			e.printStackTrace();
350
		}
351
		return itemPrice;
352
	}
353
 
449 rajveer 354
 
355
	public static void deleteFromMyResearch(long userId, long itemId) {
536 rajveer 356
 
458 rajveer 357
		try {
555 chandransh 358
			UserContextServiceClient userServiceClient = new UserContextServiceClient();
359
			in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
360
			userClient.deleteItemFromMyResearch(userId, itemId);
458 rajveer 361
		} catch (WidgetException e) {
362
			e.printStackTrace();
363
		} catch (TException e) {
364
			e.printStackTrace();
536 rajveer 365
		} catch (Exception e) {
366
			e.printStackTrace();
458 rajveer 367
		}
449 rajveer 368
 
369
	}
370
 
507 rajveer 371
 
372
	public static boolean ChangePassword(long userId, String email, String oldPassword,
373
			String newPassword) {
536 rajveer 374
 
507 rajveer 375
 
376
		try {
536 rajveer 377
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
378
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
379
 
595 rajveer 380
			return userClient.updatePassword(userId,oldPassword, newPassword);
507 rajveer 381
		} catch (UserContextException e) {
382
			e.printStackTrace();
383
		} catch (TException e) {
384
			e.printStackTrace();
536 rajveer 385
		} catch (Exception e) {
386
			e.printStackTrace();
507 rajveer 387
		}
388
		return false;
389
	}
390
 
391
 
595 rajveer 392
	public static boolean UpdatePersonalDetails(long userId,  String name,  String phone, String dateOfBirth, String sex, String communicationEmail,
507 rajveer 393
			String subscribeNewsletter) {
394
 
395
		try {
536 rajveer 396
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
397
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
398
 
507 rajveer 399
 
555 chandransh 400
			User user = userClient.getUserById(userId);		
401
			user.setDateOfBirth(dateOfBirth);
402
			user.setName(name);
403
			user.setCommunicationEmail(communicationEmail);
595 rajveer 404
			user.setSex(Sex.findByValue(Integer.parseInt(sex)));
405
			user.setMobileNumber(phone);
555 chandransh 406
			userClient.updateUser(user);
569 rajveer 407
 
507 rajveer 408
			return true;
409
		} catch (UserContextException e) {
410
			e.printStackTrace();
411
		} catch (TException e) {
412
			e.printStackTrace();
536 rajveer 413
		} catch (Exception e) {
414
			e.printStackTrace();
507 rajveer 415
		}
416
		return false;
417
	}
536 rajveer 418
 
419
 
420
	public static void mergeCarts(long fromCartId, long toCartId){
421
		try {
555 chandransh 422
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
423
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
424
			userClient.mergeCart(fromCartId, toCartId);
536 rajveer 425
		} catch (Exception e) {
426
			// TODO Auto-generated catch block
427
			e.printStackTrace();
428
		}
429
 
430
	}
507 rajveer 431
 
637 rajveer 432
	public static void storeCategories(Object obj) throws Exception {
433
 
434
		ByteArrayOutputStream bStream = new ByteArrayOutputStream();
435
		ObjectOutputStream oStream = new ObjectOutputStream( bStream );
436
		oStream.writeObject ( obj );
437
 
438
		byte[] byteVal = bStream. toByteArray();
439
 
440
		CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
441
		Client client = catalogServiceClient.getClient();
442
 
443
		client.putCategoryObject(byteVal);
444
	}
445
 
446
	public static Object getCategories() throws Exception {
447
		CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
448
		Client client = catalogServiceClient.getClient();
449
 
450
		byte[] byteVal  = client.getCategoryObject();
451
 
452
		Object obj = null;
453
		ObjectInputStream in = null;
454
		try {
455
			ByteArrayInputStream bStream = new ByteArrayInputStream(byteVal);
456
			in = new ObjectInputStream(bStream);
457
			obj = in.readObject();
458
		}
459
		finally {
460
			if(in != null) {
461
				in.close();
462
			}
463
		}
464
		return obj;
465
	}
649 chandransh 466
 
467
//	public static void main(String args[]) throws Exception{
468
//		// to store categories
469
//		Map<Long, Category> categories = new HashMap<Long, Category>();
470
//		Map<Long, in.shop2020.metamodel.definitions.Category> cmsCategories = Catalog.getInstance().getDefinitionsContainer().getCategories();
471
//		for(in.shop2020.metamodel.definitions.Category cmsCategory: cmsCategories.values()){
472
//			Category category = new Category(cmsCategory.getID());
473
//			category.setLabel(cmsCategory.getLabel());
474
//			if(cmsCategory.getParentCategory()!=null){
475
//				category.setParentCategoryId(cmsCategory.getParentCategory().getID());
476
//			}
477
//			if(cmsCategory.getChildrenCategory()!=null){
478
//				for(in.shop2020.metamodel.definitions.Category childCategory: cmsCategory.getChildrenCategory()){
479
//					category.addChild(childCategory.getID());
480
//				}
481
//			}
482
//			categories.put(cmsCategory.getID(), category);
483
//		}
484
//		Utils.storeCategories(categories);
485
//		// to get categories
486
//		//Map<Long, Category> 
487
//		categories = (Map<Long, Category>)Utils.getCategories();
488
//		System.out.println("hello");
489
//	}
490
 
419 rajveer 491
}
421 rajveer 492