| 419 |
rajveer |
1 |
package in.shop2020.serving.utils;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.model.v1.shoppingcart.ShoppingCartException;
|
|
|
4 |
import in.shop2020.model.v1.user.UserContextException;
|
|
|
5 |
import in.shop2020.thrift.clients.ShoppingCartClient;
|
|
|
6 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
|
|
7 |
|
|
|
8 |
import org.apache.thrift.TException;
|
|
|
9 |
|
|
|
10 |
public class Utils {
|
|
|
11 |
private static UserContextServiceClient userContextServiceClient;
|
|
|
12 |
private static in.shop2020.model.v1.user.UserContextService.Client userClient;
|
|
|
13 |
|
|
|
14 |
private static ShoppingCartClient shoppingCartClient;
|
|
|
15 |
private static in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient;
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
static {
|
|
|
19 |
try {
|
|
|
20 |
userContextServiceClient = new UserContextServiceClient();
|
|
|
21 |
userClient = userContextServiceClient.getClient();
|
|
|
22 |
|
|
|
23 |
shoppingCartClient = new ShoppingCartClient();
|
|
|
24 |
cartClient = shoppingCartClient.getClient();
|
|
|
25 |
|
|
|
26 |
} catch (Exception e) {
|
|
|
27 |
// TODO Auto-generated catch block
|
|
|
28 |
e.printStackTrace();
|
|
|
29 |
}
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
public static boolean isUserLoggedIn(long userId){
|
|
|
33 |
boolean isLoggedin = false;
|
|
|
34 |
try {
|
|
|
35 |
isLoggedin = userClient.getState(userId, false).isIsLoggedIn();
|
|
|
36 |
} catch (UserContextException e) {
|
|
|
37 |
// TODO Auto-generated catch block
|
|
|
38 |
e.printStackTrace();
|
|
|
39 |
} catch (TException e) {
|
|
|
40 |
// TODO Auto-generated catch block
|
|
|
41 |
e.printStackTrace();
|
|
|
42 |
}
|
|
|
43 |
return isLoggedin;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
public static String getEmailId(long userId){
|
|
|
48 |
String email = "";
|
|
|
49 |
|
|
|
50 |
try {
|
|
|
51 |
email = userClient.getPrimaryInfo(userId, false).getEmail();
|
|
|
52 |
} catch (UserContextException e) {
|
|
|
53 |
// TODO Auto-generated catch block
|
|
|
54 |
e.printStackTrace();
|
|
|
55 |
} catch (TException e) {
|
|
|
56 |
// TODO Auto-generated catch block
|
|
|
57 |
e.printStackTrace();
|
|
|
58 |
}
|
|
|
59 |
return email;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
public static int getNumberOfItemsInCart(long cartId) {
|
|
|
63 |
int numberOfItems = 0;
|
|
|
64 |
try {
|
|
|
65 |
numberOfItems = cartClient.getShadowCart(cartId).getLinesSize();
|
|
|
66 |
} catch (ShoppingCartException 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 numberOfItems;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
public static long getCartId(long userId) {
|
|
|
78 |
long cartId = 0;
|
|
|
79 |
try {
|
|
|
80 |
cartId = cartClient.getCurrentCart(userId, false).getId();
|
|
|
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 cartId;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
|
|
|
92 |
public static long addItemToCart(long catalogItemId, long userId, boolean isSessionId){
|
|
|
93 |
long cartId = -1;
|
|
|
94 |
|
|
|
95 |
try {
|
|
|
96 |
userContextServiceClient = new UserContextServiceClient();
|
|
|
97 |
userClient = userContextServiceClient.getClient();
|
|
|
98 |
|
|
|
99 |
shoppingCartClient = new ShoppingCartClient();
|
|
|
100 |
cartClient = shoppingCartClient.getClient();
|
|
|
101 |
|
|
|
102 |
cartId = cartClient.createCart(userId, isSessionId);
|
|
|
103 |
cartClient.addItemToCart(cartId, catalogItemId, 1);
|
|
|
104 |
|
|
|
105 |
} catch (ShoppingCartException e) {
|
|
|
106 |
// TODO Auto-generated catch block
|
|
|
107 |
e.printStackTrace();
|
|
|
108 |
} catch (TException e) {
|
|
|
109 |
// TODO Auto-generated catch block
|
|
|
110 |
e.printStackTrace();
|
|
|
111 |
} catch (Exception e) {
|
|
|
112 |
// TODO Auto-generated catch block
|
|
|
113 |
e.printStackTrace();
|
|
|
114 |
}
|
|
|
115 |
return cartId;
|
|
|
116 |
//if user is logged in create new cart
|
|
|
117 |
//if( userClient.getState(userId, false).isIsLoggedIn()){
|
|
|
118 |
}
|
|
|
119 |
}
|