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