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;
4
import java.util.HashMap;
421 rajveer 5
import java.util.Iterator;
6
import java.util.List;
7
import java.util.Map;
424 rajveer 8
import java.util.Map.Entry;
421 rajveer 9
 
10
import in.shop2020.model.v1.catalog.InventoryServiceException;
424 rajveer 11
import in.shop2020.model.v1.catalog.Item;
12
import in.shop2020.model.v1.catalog.InventoryService.Client;
13
import in.shop2020.model.v1.shoppingcart.Cart;
421 rajveer 14
import in.shop2020.model.v1.shoppingcart.Line;
419 rajveer 15
import in.shop2020.model.v1.shoppingcart.ShoppingCartException;
421 rajveer 16
import in.shop2020.model.v1.user.Address;
419 rajveer 17
import in.shop2020.model.v1.user.UserContextException;
458 rajveer 18
import in.shop2020.model.v1.widgets.WidgetException;
421 rajveer 19
import in.shop2020.thrift.clients.CatalogServiceClient;
419 rajveer 20
import in.shop2020.thrift.clients.ShoppingCartClient;
21
import in.shop2020.thrift.clients.UserContextServiceClient;
449 rajveer 22
import in.shop2020.thrift.clients.WidgetServiceClient;
419 rajveer 23
 
24
import org.apache.thrift.TException;
25
 
26
public class Utils {
27
	private static UserContextServiceClient userContextServiceClient;
28
	private static ShoppingCartClient shoppingCartClient;
421 rajveer 29
	private static CatalogServiceClient catalogServiceClient;
449 rajveer 30
	private static WidgetServiceClient widgetServiceClient;
419 rajveer 31
 
449 rajveer 32
 
419 rajveer 33
	static {
34
		try {
35
			userContextServiceClient = new UserContextServiceClient();
36
			shoppingCartClient = new ShoppingCartClient();
421 rajveer 37
			catalogServiceClient = new CatalogServiceClient();
449 rajveer 38
			widgetServiceClient = new WidgetServiceClient();
419 rajveer 39
		} catch (Exception e) {
40
			// TODO Auto-generated catch block
41
			e.printStackTrace();
42
		}
43
	}
44
 
45
	public static boolean isUserLoggedIn(long userId){
421 rajveer 46
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
419 rajveer 47
		boolean isLoggedin = false;
48
		try {
49
			isLoggedin = userClient.getState(userId, false).isIsLoggedIn();
50
		} catch (UserContextException e) {
51
			// TODO Auto-generated catch block
52
			e.printStackTrace();
53
		} catch (TException e) {
54
			// TODO Auto-generated catch block
55
			e.printStackTrace();
56
		}
57
		return isLoggedin;
58
	}
59
 
60
 
61
	public static String getEmailId(long userId){
421 rajveer 62
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
419 rajveer 63
		String email = "";
64
 
65
		try {
66
			email = userClient.getPrimaryInfo(userId, false).getEmail();
67
		} catch (UserContextException e) {
68
			// TODO Auto-generated catch block
69
			e.printStackTrace();
70
		} catch (TException e) {
71
			// TODO Auto-generated catch block
72
			e.printStackTrace();
73
		}
74
		return email; 
75
	}
76
 
77
	public static int getNumberOfItemsInCart(long cartId) {
421 rajveer 78
		in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
419 rajveer 79
		int numberOfItems = 0;
80
		try {
81
			numberOfItems = cartClient.getShadowCart(cartId).getLinesSize();
82
		} catch (ShoppingCartException e) {
83
			// TODO Auto-generated catch block
84
			e.printStackTrace();
85
		} catch (TException e) {
86
			// TODO Auto-generated catch block
87
			e.printStackTrace();
88
		}
89
		return numberOfItems;
90
	}
91
 
92
 
93
	public static long getCartId(long userId) {
421 rajveer 94
		in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
419 rajveer 95
		long cartId = 0;
96
		try {
97
			cartId = cartClient.getCurrentCart(userId, false).getId();
98
		} catch (ShoppingCartException e) {
99
			// TODO Auto-generated catch block
100
			e.printStackTrace();
101
		} catch (TException e) {
102
			// TODO Auto-generated catch block
103
			e.printStackTrace();
104
		}
105
		return cartId;
106
	}
107
 
108
 
109
	public static long addItemToCart(long catalogItemId, long userId, boolean isSessionId){
421 rajveer 110
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
111
		in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
419 rajveer 112
		long cartId = -1;
113
 
114
		try {
421 rajveer 115
//			userContextServiceClient = new UserContextServiceClient();
116
//			userClient = userContextServiceClient.getClient();
117
//
118
//			shoppingCartClient = new ShoppingCartClient();
119
//			cartClient = shoppingCartClient.getClient();
419 rajveer 120
 
121
			cartId = cartClient.createCart(userId, isSessionId);
122
			cartClient.addItemToCart(cartId, catalogItemId, 1);
123
 
124
		} catch (ShoppingCartException e) {
125
			// TODO Auto-generated catch block
126
			e.printStackTrace();
127
		} catch (TException e) {
128
			// TODO Auto-generated catch block
129
			e.printStackTrace();
130
		} catch (Exception e) {
131
			// TODO Auto-generated catch block
132
			e.printStackTrace();
133
		}
134
		return cartId;
135
		//if user is logged in create  new cart
136
		//if( userClient.getState(userId, false).isIsLoggedIn()){
137
	}
421 rajveer 138
 
139
 
140
	public static String getItemPrice(long itemId) throws InventoryServiceException, TException{
141
		in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = catalogServiceClient.getClient();
142
		Map priceMap = catalogClient.getItem(itemId).getPrice();
143
		return (String)priceMap.get("");
144
		// TODO to implement this function correctly
145
	}
146
 
147
 
148
	public static String getOrderDetails(long cartId){
149
		in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
150
		List<Line> lineItems = null;
151
		StringBuilder orderDetails = new StringBuilder();
152
		try {
153
			lineItems = cartClient.getCart(cartId).getLines();
154
		} catch (ShoppingCartException e) {
155
			// TODO Auto-generated catch block
156
			e.printStackTrace();
157
		} catch (TException e) {
158
			// TODO Auto-generated catch block
159
			e.printStackTrace();
160
		}
161
		for (Line line : lineItems) {
424 rajveer 162
			orderDetails.append("Item Id : " + line.getItemId() + "   ");
163
			orderDetails.append("Item Name : " + Utils.getItemName(line.getItemId()) + "   ");
164
			orderDetails.append("Item Quantity : " + line.getQuantity() + "   ");
421 rajveer 165
		}
166
		return orderDetails.toString();
167
	}
168
 
169
	public static String getContactNumber(long cartId){
170
//		in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
171
//		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
172
 
173
//		userClient.getPrimaryInfo(userId, false).getAddresses()
174
//		cartClient.getCart(cartId).getAddressId()
175
//TODO		write function to get address from addressId
424 rajveer 176
		return " test ";
421 rajveer 177
	}
178
 
179
	public static String getBillingAddress(long cartId){
180
		in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
181
		//TODO		write function to get shipping and billing address
424 rajveer 182
		return " test ";
421 rajveer 183
	}
184
 
185
	public static String getItemName(long itemId){
186
		//TODO		write function to get item name given item id
424 rajveer 187
		return "test";
421 rajveer 188
	}
424 rajveer 189
 
190
 
191
	public static String getNameOfUser(long userId) {
192
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
193
		String name = "";
194
 
195
		try {
196
			name = userClient.getPrimaryInfo(userId, false).getFirstName();
197
		} catch (UserContextException e) {
198
			// TODO Auto-generated catch block
199
			e.printStackTrace();
200
		} catch (TException e) {
201
			// TODO Auto-generated catch block
202
			e.printStackTrace();
203
		}
204
		return name; 
205
	}
421 rajveer 206
 
424 rajveer 207
	public static double getPaymentAmount(long cartId){
208
		in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
209
		double totalAmount = 0;
210
 
211
		Cart cart;
212
		try {
213
			cart = cartClient.getCart(cartId);
214
 
215
			List<Line> lineItems = cart.getLines(); 
216
 
217
			for (Line line : lineItems) {
218
				long productId = line.getItemId();
219
				totalAmount =  totalAmount + line.getQuantity() * Utils.getItemPriceByCatalogId(productId);
220
			}
221
 
222
		} catch (ShoppingCartException e) {
223
			// TODO Auto-generated catch block
224
			e.printStackTrace();
225
		} catch (TException e) {
226
			// TODO Auto-generated catch block
227
			e.printStackTrace();
228
		}
229
 
230
		return totalAmount;
231
	}
232
 
233
	public static double getItemPriceByCatalogId(long productId){
234
		CatalogServiceClient catalogServiceClient = null;
235
		Client client = null;
236
		Double itemPrice = 0.0;
237
		try {
238
			catalogServiceClient = new CatalogServiceClient();
239
			client = catalogServiceClient.getClient();
240
			Item item = client.getItemByCatalogId(productId);
241
			Map<Long,Double> priceMap = item.getPrice();
242
			if(priceMap == null || priceMap.isEmpty()){
243
				System.out.println("Price Not Found");
244
			}else{
245
				for(Entry<Long, Double> e: priceMap.entrySet()){
246
					itemPrice = e.getValue();
247
				}
248
			}
249
		}
250
		catch(Exception e){
251
			e.printStackTrace();
252
		}
253
		return itemPrice;
254
	}
255
 
449 rajveer 256
 
257
	public static void deleteFromMyResearch(long userId, long itemId) {
258
		in.shop2020.model.v1.widgets.WidgetService.Client widgetClient = widgetServiceClient.getClient();
458 rajveer 259
		try {
260
			widgetClient.deleteItemFromMyResearch(userId, itemId);
261
		} catch (WidgetException e) {
262
			// TODO Auto-generated catch block
263
			e.printStackTrace();
264
		} catch (TException e) {
265
			// TODO Auto-generated catch block
266
			e.printStackTrace();
267
		}
449 rajveer 268
 
269
	}
270
 
419 rajveer 271
}
421 rajveer 272