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