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