Subversion Repositories SmartDukaan

Rev

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