Subversion Repositories SmartDukaan

Rev

Rev 419 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 419 Rev 421
Line 1... Line 1...
1
package in.shop2020.serving.utils;
1
package in.shop2020.serving.utils;
2
 
2
 
-
 
3
import java.util.Iterator;
-
 
4
import java.util.List;
-
 
5
import java.util.Map;
-
 
6
 
-
 
7
import in.shop2020.model.v1.catalog.InventoryServiceException;
-
 
8
import in.shop2020.model.v1.shoppingcart.Line;
3
import in.shop2020.model.v1.shoppingcart.ShoppingCartException;
9
import in.shop2020.model.v1.shoppingcart.ShoppingCartException;
-
 
10
import in.shop2020.model.v1.user.Address;
4
import in.shop2020.model.v1.user.UserContextException;
11
import in.shop2020.model.v1.user.UserContextException;
-
 
12
import in.shop2020.thrift.clients.CatalogServiceClient;
5
import in.shop2020.thrift.clients.ShoppingCartClient;
13
import in.shop2020.thrift.clients.ShoppingCartClient;
6
import in.shop2020.thrift.clients.UserContextServiceClient;
14
import in.shop2020.thrift.clients.UserContextServiceClient;
7
 
15
 
8
import org.apache.thrift.TException;
16
import org.apache.thrift.TException;
9
 
17
 
10
public class Utils {
18
public class Utils {
11
	private static UserContextServiceClient userContextServiceClient;
19
	private static UserContextServiceClient userContextServiceClient;
12
	private static in.shop2020.model.v1.user.UserContextService.Client userClient;
-
 
13
 
-
 
14
	private static ShoppingCartClient shoppingCartClient;
20
	private static ShoppingCartClient shoppingCartClient;
15
	private static in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient;
21
	private static CatalogServiceClient catalogServiceClient;
16
 
22
 
17
	
23
	
18
	static {
24
	static {
19
		try {
25
		try {
20
			userContextServiceClient = new UserContextServiceClient();
26
			userContextServiceClient = new UserContextServiceClient();
21
			userClient = userContextServiceClient.getClient();
-
 
22
			
-
 
23
			shoppingCartClient = new ShoppingCartClient();
27
			shoppingCartClient = new ShoppingCartClient();
24
			cartClient = shoppingCartClient.getClient();
28
			catalogServiceClient = new CatalogServiceClient();
25
			
-
 
26
		} catch (Exception e) {
29
		} catch (Exception e) {
27
			// TODO Auto-generated catch block
30
			// TODO Auto-generated catch block
28
			e.printStackTrace();
31
			e.printStackTrace();
29
		}
32
		}
30
	}
33
	}
31
	
34
	
32
	public static boolean isUserLoggedIn(long userId){
35
	public static boolean isUserLoggedIn(long userId){
-
 
36
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
33
		boolean isLoggedin = false;
37
		boolean isLoggedin = false;
34
		try {
38
		try {
35
			isLoggedin = userClient.getState(userId, false).isIsLoggedIn();
39
			isLoggedin = userClient.getState(userId, false).isIsLoggedIn();
36
		} catch (UserContextException e) {
40
		} catch (UserContextException e) {
37
			// TODO Auto-generated catch block
41
			// TODO Auto-generated catch block
Line 43... Line 47...
43
		return isLoggedin;
47
		return isLoggedin;
44
	}
48
	}
45
 
49
 
46
	
50
	
47
	public static String getEmailId(long userId){
51
	public static String getEmailId(long userId){
-
 
52
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
48
		String email = "";
53
		String email = "";
49
		
54
		
50
		try {
55
		try {
51
			email = userClient.getPrimaryInfo(userId, false).getEmail();
56
			email = userClient.getPrimaryInfo(userId, false).getEmail();
52
		} catch (UserContextException e) {
57
		} catch (UserContextException e) {
Line 58... Line 63...
58
		}
63
		}
59
		return email; 
64
		return email; 
60
	}
65
	}
61
 
66
 
62
	public static int getNumberOfItemsInCart(long cartId) {
67
	public static int getNumberOfItemsInCart(long cartId) {
-
 
68
		in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
63
		int numberOfItems = 0;
69
		int numberOfItems = 0;
64
		try {
70
		try {
65
			numberOfItems = cartClient.getShadowCart(cartId).getLinesSize();
71
			numberOfItems = cartClient.getShadowCart(cartId).getLinesSize();
66
		} catch (ShoppingCartException e) {
72
		} catch (ShoppingCartException e) {
67
			// TODO Auto-generated catch block
73
			// TODO Auto-generated catch block
Line 73... Line 79...
73
		return numberOfItems;
79
		return numberOfItems;
74
	}
80
	}
75
	
81
	
76
	
82
	
77
	public static long getCartId(long userId) {
83
	public static long getCartId(long userId) {
-
 
84
		in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
78
		long cartId = 0;
85
		long cartId = 0;
79
		try {
86
		try {
80
			cartId = cartClient.getCurrentCart(userId, false).getId();
87
			cartId = cartClient.getCurrentCart(userId, false).getId();
81
		} catch (ShoppingCartException e) {
88
		} catch (ShoppingCartException e) {
82
			// TODO Auto-generated catch block
89
			// TODO Auto-generated catch block
Line 88... Line 95...
88
		return cartId;
95
		return cartId;
89
	}
96
	}
90
 
97
 
91
	
98
	
92
	public static long addItemToCart(long catalogItemId, long userId, boolean isSessionId){
99
	public static long addItemToCart(long catalogItemId, long userId, boolean isSessionId){
-
 
100
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
-
 
101
		in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
93
		long cartId = -1;
102
		long cartId = -1;
94
		
103
		
95
		try {
104
		try {
96
			userContextServiceClient = new UserContextServiceClient();
105
//			userContextServiceClient = new UserContextServiceClient();
97
			userClient = userContextServiceClient.getClient();
106
//			userClient = userContextServiceClient.getClient();
98
 
107
//
99
			shoppingCartClient = new ShoppingCartClient();
108
//			shoppingCartClient = new ShoppingCartClient();
100
			cartClient = shoppingCartClient.getClient();
109
//			cartClient = shoppingCartClient.getClient();
101
			
110
			
102
			cartId = cartClient.createCart(userId, isSessionId);
111
			cartId = cartClient.createCart(userId, isSessionId);
103
			cartClient.addItemToCart(cartId, catalogItemId, 1);
112
			cartClient.addItemToCart(cartId, catalogItemId, 1);
104
	
113
	
105
		} catch (ShoppingCartException e) {
114
		} catch (ShoppingCartException e) {
Line 114... Line 123...
114
		}
123
		}
115
		return cartId;
124
		return cartId;
116
		//if user is logged in create  new cart
125
		//if user is logged in create  new cart
117
		//if( userClient.getState(userId, false).isIsLoggedIn()){
126
		//if( userClient.getState(userId, false).isIsLoggedIn()){
118
	}
127
	}
-
 
128
 
-
 
129
	
-
 
130
	public static String getItemPrice(long itemId) throws InventoryServiceException, TException{
-
 
131
		in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = catalogServiceClient.getClient();
-
 
132
		Map priceMap = catalogClient.getItem(itemId).getPrice();
-
 
133
		return (String)priceMap.get("");
-
 
134
		// TODO to implement this function correctly
-
 
135
	}
-
 
136
	
-
 
137
	
-
 
138
	public static String getOrderDetails(long cartId){
-
 
139
		in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
-
 
140
		List<Line> lineItems = null;
-
 
141
		StringBuilder orderDetails = new StringBuilder();
-
 
142
		try {
-
 
143
			lineItems = cartClient.getCart(cartId).getLines();
-
 
144
		} catch (ShoppingCartException e) {
-
 
145
			// TODO Auto-generated catch block
-
 
146
			e.printStackTrace();
-
 
147
		} catch (TException e) {
-
 
148
			// TODO Auto-generated catch block
-
 
149
			e.printStackTrace();
-
 
150
		}
-
 
151
		for (Line line : lineItems) {
-
 
152
			orderDetails.append("Item Id : " + line.getItemId() + "\n");
-
 
153
			orderDetails.append("Item Name : " + Utils.getItemName(line.getItemId()) + "\n");
-
 
154
			orderDetails.append("Item Quantity : " + line.getQuantity() + "\n");
-
 
155
		}
-
 
156
		return orderDetails.toString();
-
 
157
	}
-
 
158
	
-
 
159
	public static String getContactNumber(long cartId){
-
 
160
//		in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
-
 
161
//		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
-
 
162
		
-
 
163
//		userClient.getPrimaryInfo(userId, false).getAddresses()
-
 
164
//		cartClient.getCart(cartId).getAddressId()
-
 
165
//TODO		write function to get address from addressId
-
 
166
		return "";
-
 
167
	}
-
 
168
 
-
 
169
	public static String getBillingAddress(long cartId){
-
 
170
		in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
-
 
171
		//TODO		write function to get shipping and billing address
-
 
172
		return "";
-
 
173
	}
-
 
174
	
-
 
175
	public static String getItemName(long itemId){
-
 
176
		//TODO		write function to get item name given item id
-
 
177
		return "";
-
 
178
	}
-
 
179
	
119
}
180
}
-
 
181
	
120
182