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