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