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
 
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;
555 chandransh 15
import in.shop2020.model.v1.user.Cart;
16
 
17
import in.shop2020.model.v1.user.Line;
18
import in.shop2020.model.v1.user.LineStatus;
595 rajveer 19
import in.shop2020.model.v1.user.Sex;
555 chandransh 20
import in.shop2020.model.v1.user.ShoppingCartException;
421 rajveer 21
import in.shop2020.model.v1.user.Address;
555 chandransh 22
import in.shop2020.model.v1.user.User;
419 rajveer 23
import in.shop2020.model.v1.user.UserContextException;
458 rajveer 24
import in.shop2020.model.v1.widgets.WidgetException;
421 rajveer 25
import in.shop2020.thrift.clients.CatalogServiceClient;
419 rajveer 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){ 
555 chandransh 52
		boolean isNotLoggedIn = true;
419 rajveer 53
		try {
536 rajveer 54
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
55
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
555 chandransh 56
			isNotLoggedIn = userClient.getUserById(userId).isIsAnonymous();
419 rajveer 57
		} catch (UserContextException e) {
58
			e.printStackTrace();
59
		} catch (TException e) {
60
			e.printStackTrace();
536 rajveer 61
		} catch (Exception e) {
62
			e.printStackTrace();
419 rajveer 63
		}
555 chandransh 64
		return !isNotLoggedIn;
419 rajveer 65
	}
66
 
67
 
68
	public static String getEmailId(long userId){
69
		String email = "";
70
 
71
		try {
536 rajveer 72
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
73
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
74
 
555 chandransh 75
			email = userClient.getUserById(userId).getEmail();
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
 
186
	public static String getOrderDetails(long cartId){
187
		List<Line> lineItems = null;
188
		StringBuilder orderDetails = new StringBuilder();
189
		try {
555 chandransh 190
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
191
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
536 rajveer 192
 
555 chandransh 193
			lineItems = userClient.getCart(cartId).getLines();
421 rajveer 194
		} catch (ShoppingCartException e) {
195
			e.printStackTrace();
196
		} catch (TException e) {
197
			e.printStackTrace();
536 rajveer 198
		} catch (Exception e) {
199
			e.printStackTrace();
421 rajveer 200
		}
201
		for (Line line : lineItems) {
424 rajveer 202
			orderDetails.append("Item Id : " + line.getItemId() + "   ");
203
			orderDetails.append("Item Name : " + Utils.getItemName(line.getItemId()) + "   ");
204
			orderDetails.append("Item Quantity : " + line.getQuantity() + "   ");
421 rajveer 205
		}
206
		return orderDetails.toString();
207
	}
208
 
209
	public static String getContactNumber(long cartId){
210
//		in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
211
//		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
212
 
213
//		userClient.getPrimaryInfo(userId, false).getAddresses()
214
//		cartClient.getCart(cartId).getAddressId()
215
//TODO		write function to get address from addressId
424 rajveer 216
		return " test ";
421 rajveer 217
	}
218
 
219
	public static String getBillingAddress(long cartId){
536 rajveer 220
		//in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
421 rajveer 221
		//TODO		write function to get shipping and billing address
424 rajveer 222
		return " test ";
421 rajveer 223
	}
224
 
225
	public static String getItemName(long itemId){
226
		//TODO		write function to get item name given item id
424 rajveer 227
		return "test";
421 rajveer 228
	}
424 rajveer 229
 
230
 
231
	public static String getNameOfUser(long userId) {
232
		String name = "";
233
		try {
536 rajveer 234
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
235
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
236
 
555 chandransh 237
			name = userClient.getUserById(userId).getName();
424 rajveer 238
		} catch (UserContextException e) {
239
			// TODO Auto-generated catch block
240
			e.printStackTrace();
241
		} catch (TException e) {
242
			// TODO Auto-generated catch block
243
			e.printStackTrace();
536 rajveer 244
		} catch (Exception e) {
245
			// TODO Auto-generated catch block
246
			e.printStackTrace();
424 rajveer 247
		}
248
		return name; 
249
	}
421 rajveer 250
 
424 rajveer 251
	public static double getPaymentAmount(long cartId){
252
		double totalAmount = 0;
253
 
254
		Cart cart;
255
		try {
555 chandransh 256
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
257
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
536 rajveer 258
 
555 chandransh 259
			cart = userClient.getCart(cartId);
424 rajveer 260
 
261
			List<Line> lineItems = cart.getLines(); 
262
 
263
			for (Line line : lineItems) {
264
				long productId = line.getItemId();
265
				totalAmount =  totalAmount + line.getQuantity() * Utils.getItemPriceByCatalogId(productId);
266
			}
267
 
268
		} catch (ShoppingCartException e) {
269
			e.printStackTrace();
270
		} catch (TException e) {
271
			e.printStackTrace();
536 rajveer 272
		} catch (Exception e) {
273
			e.printStackTrace();
424 rajveer 274
		}
275
 
276
		return totalAmount;
277
	}
278
 
279
	public static double getItemPriceByCatalogId(long productId){
280
		CatalogServiceClient catalogServiceClient = null;
281
		Client client = null;
282
		Double itemPrice = 0.0;
283
		try {
284
			catalogServiceClient = new CatalogServiceClient();
285
			client = catalogServiceClient.getClient();
286
			Item item = client.getItemByCatalogId(productId);
517 rajveer 287
			itemPrice = item.getSellingPrice();
536 rajveer 288
 
424 rajveer 289
		}
290
		catch(Exception e){
291
			e.printStackTrace();
292
		}
293
		return itemPrice;
294
	}
295
 
449 rajveer 296
 
297
	public static void deleteFromMyResearch(long userId, long itemId) {
536 rajveer 298
 
458 rajveer 299
		try {
555 chandransh 300
			UserContextServiceClient userServiceClient = new UserContextServiceClient();
301
			in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
302
			userClient.deleteItemFromMyResearch(userId, itemId);
458 rajveer 303
		} catch (WidgetException e) {
304
			e.printStackTrace();
305
		} catch (TException e) {
306
			e.printStackTrace();
536 rajveer 307
		} catch (Exception e) {
308
			e.printStackTrace();
458 rajveer 309
		}
449 rajveer 310
 
311
	}
312
 
507 rajveer 313
 
314
	public static boolean ChangePassword(long userId, String email, String oldPassword,
315
			String newPassword) {
536 rajveer 316
 
507 rajveer 317
 
318
		try {
536 rajveer 319
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
320
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
321
 
595 rajveer 322
			return userClient.updatePassword(userId,oldPassword, newPassword);
507 rajveer 323
		} catch (UserContextException e) {
324
			e.printStackTrace();
325
		} catch (TException e) {
326
			e.printStackTrace();
536 rajveer 327
		} catch (Exception e) {
328
			e.printStackTrace();
507 rajveer 329
		}
330
		return false;
331
	}
332
 
333
 
595 rajveer 334
	public static boolean UpdatePersonalDetails(long userId,  String name,  String phone, String dateOfBirth, String sex, String communicationEmail,
507 rajveer 335
			String subscribeNewsletter) {
336
 
337
		try {
536 rajveer 338
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
339
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
340
 
507 rajveer 341
 
555 chandransh 342
			User user = userClient.getUserById(userId);		
343
			user.setDateOfBirth(dateOfBirth);
344
			user.setName(name);
345
			user.setCommunicationEmail(communicationEmail);
595 rajveer 346
			user.setSex(Sex.findByValue(Integer.parseInt(sex)));
347
			user.setMobileNumber(phone);
555 chandransh 348
			userClient.updateUser(user);
569 rajveer 349
 
507 rajveer 350
			return true;
351
		} catch (UserContextException e) {
352
			e.printStackTrace();
353
		} catch (TException e) {
354
			e.printStackTrace();
536 rajveer 355
		} catch (Exception e) {
356
			e.printStackTrace();
507 rajveer 357
		}
358
		return false;
359
	}
536 rajveer 360
 
361
 
362
	public static void mergeCarts(long fromCartId, long toCartId){
363
		try {
555 chandransh 364
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
365
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
366
			userClient.mergeCart(fromCartId, toCartId);
536 rajveer 367
		} catch (Exception e) {
368
			// TODO Auto-generated catch block
369
			e.printStackTrace();
370
		}
371
 
372
	}
507 rajveer 373
 
419 rajveer 374
}
421 rajveer 375