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;
19
import in.shop2020.model.v1.user.ShoppingCartException;
421 rajveer 20
import in.shop2020.model.v1.user.Address;
555 chandransh 21
import in.shop2020.model.v1.user.User;
419 rajveer 22
import in.shop2020.model.v1.user.UserContextException;
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.UserContextServiceClient;
449 rajveer 26
import in.shop2020.thrift.clients.WidgetServiceClient;
419 rajveer 27
 
28
import org.apache.thrift.TException;
29
 
30
public class Utils {
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){
68
		String email = "";
69
 
70
		try {
536 rajveer 71
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
72
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
73
 
555 chandransh 74
			email = userClient.getUserById(userId).getEmail();
419 rajveer 75
		} catch (UserContextException e) {
76
			e.printStackTrace();
77
		} catch (TException e) {
78
			e.printStackTrace();
536 rajveer 79
		} catch (Exception e) {
80
			e.printStackTrace();
419 rajveer 81
		}
82
		return email; 
83
	}
84
 
85
	public static int getNumberOfItemsInCart(long cartId) {
536 rajveer 86
 
87
 
419 rajveer 88
		int numberOfItems = 0;
89
		try {
555 chandransh 90
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
91
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
536 rajveer 92
 
555 chandransh 93
			numberOfItems = userClient.getCart(cartId).getLinesSize();
419 rajveer 94
		} catch (ShoppingCartException e) {
95
			e.printStackTrace();
96
		} catch (TException e) {
97
			e.printStackTrace();
536 rajveer 98
		} catch (Exception e) {
99
			e.printStackTrace();
419 rajveer 100
		}
101
		return numberOfItems;
102
	}
103
 
104
 
105
	public static long getCartId(long userId) {
106
		long cartId = 0;
107
		try {
555 chandransh 108
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
109
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
536 rajveer 110
 
555 chandransh 111
			cartId = userClient.getCurrentCart(userId).getId();
419 rajveer 112
		} catch (ShoppingCartException e) {
113
			e.printStackTrace();
114
		} catch (TException e) {
115
			e.printStackTrace();
536 rajveer 116
		} catch (Exception e) {
117
			e.printStackTrace();
419 rajveer 118
		}
119
		return cartId;
120
	}
121
 
122
 
123
	public static long addItemToCart(long catalogItemId, long userId, boolean isSessionId){
124
		long cartId = -1;
125
 
126
		try {
555 chandransh 127
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
128
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
419 rajveer 129
 
555 chandransh 130
			cartId = userClient.createCart(userId);
131
			userClient.addItemToCart(cartId, catalogItemId, 1);
419 rajveer 132
 
133
		} catch (ShoppingCartException e) {
134
			// TODO Auto-generated catch block
135
			e.printStackTrace();
136
		} catch (TException e) {
137
			// TODO Auto-generated catch block
138
			e.printStackTrace();
139
		} catch (Exception e) {
140
			// TODO Auto-generated catch block
141
			e.printStackTrace();
142
		}
143
		return cartId;
144
		//if user is logged in create  new cart
145
		//if( userClient.getState(userId, false).isIsLoggedIn()){
146
	}
421 rajveer 147
 
517 rajveer 148
	public static boolean deleteItemFromCart(long cartId, long catalogItemId, long userId, boolean isSessionId){
507 rajveer 149
		try {
555 chandransh 150
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
151
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
536 rajveer 152
 
555 chandransh 153
			userClient.deleteItemFromCart(cartId, catalogItemId);
507 rajveer 154
			return true;	
155
		} catch (ShoppingCartException e) {
156
			e.printStackTrace();
157
		} catch (TException e) {
158
			e.printStackTrace();
159
		} catch (Exception e) {
160
			e.printStackTrace();
161
		}
162
 
163
		return false;
164
	}
165
 
166
	public static boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
167
		try {
555 chandransh 168
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
169
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
536 rajveer 170
 
555 chandransh 171
			userClient.changeQuantity(cartId, itemId, quantity);
507 rajveer 172
			return true;
173
		} catch (ShoppingCartException e) {
174
			e.printStackTrace();
175
		} catch (TException e) {
176
			e.printStackTrace();
536 rajveer 177
		} catch (Exception e) {
178
			e.printStackTrace();
507 rajveer 179
		}
180
		return false;
181
	}
421 rajveer 182
 
183
 
184
 
185
	public static String getOrderDetails(long cartId){
186
		List<Line> lineItems = null;
187
		StringBuilder orderDetails = new StringBuilder();
188
		try {
555 chandransh 189
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
190
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
536 rajveer 191
 
555 chandransh 192
			lineItems = userClient.getCart(cartId).getLines();
421 rajveer 193
		} catch (ShoppingCartException e) {
194
			e.printStackTrace();
195
		} catch (TException e) {
196
			e.printStackTrace();
536 rajveer 197
		} catch (Exception e) {
198
			e.printStackTrace();
421 rajveer 199
		}
200
		for (Line line : lineItems) {
424 rajveer 201
			orderDetails.append("Item Id : " + line.getItemId() + "   ");
202
			orderDetails.append("Item Name : " + Utils.getItemName(line.getItemId()) + "   ");
203
			orderDetails.append("Item Quantity : " + line.getQuantity() + "   ");
421 rajveer 204
		}
205
		return orderDetails.toString();
206
	}
207
 
208
	public static String getContactNumber(long cartId){
209
//		in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
210
//		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
211
 
212
//		userClient.getPrimaryInfo(userId, false).getAddresses()
213
//		cartClient.getCart(cartId).getAddressId()
214
//TODO		write function to get address from addressId
424 rajveer 215
		return " test ";
421 rajveer 216
	}
217
 
218
	public static String getBillingAddress(long cartId){
536 rajveer 219
		//in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
421 rajveer 220
		//TODO		write function to get shipping and billing address
424 rajveer 221
		return " test ";
421 rajveer 222
	}
223
 
224
	public static String getItemName(long itemId){
225
		//TODO		write function to get item name given item id
424 rajveer 226
		return "test";
421 rajveer 227
	}
424 rajveer 228
 
229
 
230
	public static String getNameOfUser(long userId) {
231
		String name = "";
232
		try {
536 rajveer 233
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
234
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
235
 
555 chandransh 236
			name = userClient.getUserById(userId).getName();
424 rajveer 237
		} catch (UserContextException e) {
238
			// TODO Auto-generated catch block
239
			e.printStackTrace();
240
		} catch (TException e) {
241
			// TODO Auto-generated catch block
242
			e.printStackTrace();
536 rajveer 243
		} catch (Exception e) {
244
			// TODO Auto-generated catch block
245
			e.printStackTrace();
424 rajveer 246
		}
247
		return name; 
248
	}
421 rajveer 249
 
424 rajveer 250
	public static double getPaymentAmount(long cartId){
251
		double totalAmount = 0;
252
 
253
		Cart cart;
254
		try {
555 chandransh 255
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
256
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
536 rajveer 257
 
555 chandransh 258
			cart = userClient.getCart(cartId);
424 rajveer 259
 
260
			List<Line> lineItems = cart.getLines(); 
261
 
262
			for (Line line : lineItems) {
263
				long productId = line.getItemId();
264
				totalAmount =  totalAmount + line.getQuantity() * Utils.getItemPriceByCatalogId(productId);
265
			}
266
 
267
		} catch (ShoppingCartException e) {
268
			e.printStackTrace();
269
		} catch (TException e) {
270
			e.printStackTrace();
536 rajveer 271
		} catch (Exception e) {
272
			e.printStackTrace();
424 rajveer 273
		}
274
 
275
		return totalAmount;
276
	}
277
 
278
	public static double getItemPriceByCatalogId(long productId){
279
		CatalogServiceClient catalogServiceClient = null;
280
		Client client = null;
281
		Double itemPrice = 0.0;
282
		try {
283
			catalogServiceClient = new CatalogServiceClient();
284
			client = catalogServiceClient.getClient();
285
			Item item = client.getItemByCatalogId(productId);
517 rajveer 286
			itemPrice = item.getSellingPrice();
536 rajveer 287
 
424 rajveer 288
		}
289
		catch(Exception e){
290
			e.printStackTrace();
291
		}
292
		return itemPrice;
293
	}
294
 
449 rajveer 295
 
296
	public static void deleteFromMyResearch(long userId, long itemId) {
536 rajveer 297
 
458 rajveer 298
		try {
555 chandransh 299
			UserContextServiceClient userServiceClient = new UserContextServiceClient();
300
			in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
301
			userClient.deleteItemFromMyResearch(userId, itemId);
458 rajveer 302
		} catch (WidgetException e) {
303
			e.printStackTrace();
304
		} catch (TException e) {
305
			e.printStackTrace();
536 rajveer 306
		} catch (Exception e) {
307
			e.printStackTrace();
458 rajveer 308
		}
449 rajveer 309
 
310
	}
311
 
507 rajveer 312
 
313
	public static boolean ChangePassword(long userId, String email, String oldPassword,
314
			String newPassword) {
536 rajveer 315
 
507 rajveer 316
 
317
		try {
536 rajveer 318
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
319
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
320
 
507 rajveer 321
			userClient.updatePassword(userId, newPassword);
322
			return true;
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
 
334
	public static boolean UpdatePersonalDetails(long userId,  String name, int month,
335
			int day, int year, String sex, String communicationEmail,
336
			String subscribeNewsletter) {
337
 
338
 
339
		try {
536 rajveer 340
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
341
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
342
 
517 rajveer 343
			Calendar calendar = Calendar.getInstance();
344
			calendar.set(year + 1900, month, day);
345
			long dateOfBirth = calendar.getTimeInMillis();
507 rajveer 346
 
555 chandransh 347
			User user = userClient.getUserById(userId);		
348
			user.setDateOfBirth(dateOfBirth);
349
			user.setName(name);
350
			user.setCommunicationEmail(communicationEmail);
351
			userClient.updateUser(user);
507 rajveer 352
			return true;
353
		} catch (UserContextException e) {
354
			e.printStackTrace();
355
		} catch (TException e) {
356
			e.printStackTrace();
536 rajveer 357
		} catch (Exception e) {
358
			e.printStackTrace();
507 rajveer 359
		}
360
		return false;
361
	}
536 rajveer 362
 
363
 
364
	public static void mergeCarts(long fromCartId, long toCartId){
365
		try {
555 chandransh 366
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
367
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
368
			userClient.mergeCart(fromCartId, toCartId);
536 rajveer 369
		} catch (Exception e) {
370
			// TODO Auto-generated catch block
371
			e.printStackTrace();
372
		}
373
 
374
	}
507 rajveer 375
 
419 rajveer 376
}
421 rajveer 377