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