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