| Line 1... |
Line 1... |
| 1 |
package in.shop2020.serving.controllers;
|
1 |
package in.shop2020.serving.controllers;
|
| 2 |
|
2 |
|
| 3 |
import in.shop2020.model.v1.shoppingcart.Cart;
|
3 |
import in.shop2020.model.v1.user.Cart;
|
| 4 |
import in.shop2020.model.v1.shoppingcart.ShoppingCartException;
|
4 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
| 5 |
import in.shop2020.model.v1.user.Address;
|
5 |
import in.shop2020.model.v1.user.Address;
|
| 6 |
import in.shop2020.model.v1.user.AddressType;
|
6 |
import in.shop2020.model.v1.user.AddressType;
|
| 7 |
import in.shop2020.model.v1.user.UserContextService.Client;
|
7 |
import in.shop2020.model.v1.user.UserContextService.Client;
|
| 8 |
import in.shop2020.serving.controllers.BaseController;
|
8 |
import in.shop2020.serving.controllers.BaseController;
|
| 9 |
import in.shop2020.serving.pages.PageContentKeys;
|
9 |
import in.shop2020.serving.pages.PageContentKeys;
|
| 10 |
import in.shop2020.serving.pages.PageEnum;
|
10 |
import in.shop2020.serving.pages.PageEnum;
|
| 11 |
import in.shop2020.serving.pages.PageManager;
|
11 |
import in.shop2020.serving.pages.PageManager;
|
| 12 |
import in.shop2020.serving.utils.Utils;
|
12 |
import in.shop2020.serving.utils.Utils;
|
| 13 |
import in.shop2020.thrift.clients.ShoppingCartClient;
|
- |
|
| 14 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
13 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
| 15 |
|
14 |
|
| 16 |
import java.util.*;
|
15 |
import java.util.*;
|
| 17 |
|
16 |
|
| 18 |
import org.apache.juli.logging.Log;
|
17 |
import org.apache.juli.logging.Log;
|
| Line 39... |
Line 38... |
| 39 |
pageManager = PageManager.getPageManager();
|
38 |
pageManager = PageManager.getPageManager();
|
| 40 |
}
|
39 |
}
|
| 41 |
|
40 |
|
| 42 |
// GET /shipping
|
41 |
// GET /shipping
|
| 43 |
public HttpHeaders index() {
|
42 |
public HttpHeaders index() {
|
| 44 |
long userId = 0;
|
43 |
long userId = userinfo.getUserId();
|
| 45 |
boolean isSessionId = true;
|
44 |
boolean isLoggedIn = userinfo.isLoggedIn();
|
| 46 |
Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
|
45 |
Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
|
| 47 |
|
46 |
|
| 48 |
if(userinfo.isLoggedIn()){
|
- |
|
| 49 |
userId = userinfo.getUserId();
|
- |
|
| 50 |
isSessionId = false;
|
- |
|
| 51 |
}else {
|
- |
|
| 52 |
userId = userinfo.getSessionId();
|
- |
|
| 53 |
isSessionId = true;
|
- |
|
| 54 |
}
|
- |
|
| 55 |
|
- |
|
| 56 |
|
- |
|
| 57 |
params.put(PageContentKeys.CUSTOMER_ID, userId+"");
|
47 |
params.put(PageContentKeys.CUSTOMER_ID, userId+"");
|
| 58 |
params.put(PageContentKeys.IS_SESSION_ID, isSessionId+"");
|
48 |
params.put(PageContentKeys.IS_LOGGED_IN, isLoggedIn+"");
|
| 59 |
params.put(PageContentKeys.CART_ID, userinfo.getCartId()+"");
|
49 |
params.put(PageContentKeys.CART_ID, userinfo.getCartId()+"");
|
| 60 |
params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
|
50 |
params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
|
| - |
|
51 |
params.put(PageContentKeys.USER_NAME, userinfo.getNameOfUser());
|
| 61 |
|
52 |
|
| 62 |
htmlSnippets = pageManager.getPageContents(PageEnum.SHIPPING_PAGE, params);
|
53 |
htmlSnippets = pageManager.getPageContents(PageEnum.SHIPPING_PAGE, params);
|
| 63 |
|
54 |
|
| 64 |
return new DefaultHttpHeaders("index").disableCaching();
|
55 |
return new DefaultHttpHeaders("index").disableCaching();
|
| 65 |
}
|
56 |
}
|
| 66 |
|
57 |
|
| 67 |
// POST /entity
|
58 |
// POST /entity
|
| 68 |
public HttpHeaders create(){
|
59 |
public HttpHeaders create(){
|
| 69 |
UserContextServiceClient userContextServiceClient;
|
60 |
UserContextServiceClient userContextServiceClient = null;
|
| 70 |
ShoppingCartClient shoppingCartClient;
|
61 |
in.shop2020.model.v1.user.UserContextService.Client userClient = null;
|
| 71 |
|
62 |
|
| 72 |
printParams();
|
63 |
printParams();
|
| 73 |
|
64 |
|
| 74 |
String action = this.request.getParameter("action");
|
65 |
String action = this.request.getParameter("action");
|
| 75 |
if(action == null){
|
66 |
if(action == null){
|
| 76 |
return new DefaultHttpHeaders("failure");
|
67 |
return new DefaultHttpHeaders("failure");
|
| 77 |
}
|
68 |
}
|
| 78 |
|
69 |
|
| - |
|
70 |
try {
|
| - |
|
71 |
userContextServiceClient = new UserContextServiceClient();
|
| - |
|
72 |
userClient = userContextServiceClient.getClient();
|
| - |
|
73 |
} catch (Exception e) {
|
| - |
|
74 |
return new DefaultHttpHeaders("failure");
|
| - |
|
75 |
}
|
| - |
|
76 |
|
| 79 |
if(action.equals("addnew")){
|
77 |
if(action.equals("addnew")){
|
| 80 |
|
78 |
|
| 81 |
Address address = new Address();
|
79 |
Address address = new Address();
|
| 82 |
address.setName(this.request.getParameter("customername"));
|
80 |
address.setName(this.request.getParameter("customername"));
|
| 83 |
address.setLine1(this.request.getParameter("line1"));
|
81 |
address.setLine1(this.request.getParameter("line1"));
|
| Line 90... |
Line 88... |
| 90 |
address.setEnabled(true);
|
88 |
address.setEnabled(true);
|
| 91 |
address.setType(AddressType.HOME);
|
89 |
address.setType(AddressType.HOME);
|
| 92 |
|
90 |
|
| 93 |
if(userinfo.isLoggedIn()){
|
91 |
if(userinfo.isLoggedIn()){
|
| 94 |
try {
|
92 |
try {
|
| 95 |
userContextServiceClient = new UserContextServiceClient();
|
93 |
userClient.addAddressForUser(userinfo.getUserId(), address, (new Date()).getTime(), false);
|
| 96 |
Client userClient = userContextServiceClient.getClient();
|
94 |
// FIXME: This is a tortuous way of finding the id of the
|
| 97 |
userClient.addAddressForUser(address, userinfo.getUserId(), (new Date()).getTime(), false);
|
95 |
// last address that was added. Also, there is no guarantee
|
| - |
|
96 |
// that the maxAddressId will hold the id of the address
|
| - |
|
97 |
// that was just added. Instead, make addAddressForUser
|
| - |
|
98 |
// return the id of the address that was just added.
|
| 98 |
List<Address> addresses = userClient.getPrimaryInfo(userinfo.getUserId(), userinfo.isSessionId()).getAddresses();
|
99 |
List<Address> addresses = userClient.getUserById(userinfo.getUserId()).getAddresses();
|
| 99 |
long maxAddressId = 0;
|
100 |
long maxAddressId = 0;
|
| 100 |
for(Address tempaddress: addresses){
|
101 |
for(Address tempaddress: addresses){
|
| 101 |
if(tempaddress.getId() > maxAddressId){
|
102 |
if(tempaddress.getId() > maxAddressId){
|
| 102 |
maxAddressId = tempaddress.getId();
|
103 |
maxAddressId = tempaddress.getId();
|
| 103 |
}
|
104 |
}
|
| 104 |
}
|
105 |
}
|
| 105 |
this.addressId = maxAddressId;
|
106 |
this.addressId = maxAddressId;
|
| 106 |
// to set the address in cart
|
107 |
// to set the address in cart
|
| 107 |
shoppingCartClient = new ShoppingCartClient();
|
- |
|
| 108 |
in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
|
- |
|
| 109 |
cartClient.addAddressToCart(userinfo.getCartId(), maxAddressId);
|
108 |
userClient.addAddressToCart(userinfo.getCartId(), maxAddressId);
|
| 110 |
|
- |
|
| 111 |
} catch (Exception e) {
|
109 |
} catch (Exception e) {
|
| 112 |
e.printStackTrace();
|
110 |
e.printStackTrace();
|
| 113 |
return new DefaultHttpHeaders("failure");
|
111 |
return new DefaultHttpHeaders("failure");
|
| 114 |
}
|
112 |
}
|
| 115 |
|
113 |
|
| Line 118... |
Line 116... |
| 118 |
return new DefaultHttpHeaders("failure");
|
116 |
return new DefaultHttpHeaders("failure");
|
| 119 |
}
|
117 |
}
|
| 120 |
}
|
118 |
}
|
| 121 |
if(action.equals("change")){
|
119 |
if(action.equals("change")){
|
| 122 |
try {
|
120 |
try {
|
| 123 |
shoppingCartClient = new ShoppingCartClient();
|
- |
|
| 124 |
in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
|
- |
|
| 125 |
long addressId = Long.parseLong(this.request.getParameter("addressid"));
|
121 |
long addressId = Long.parseLong(this.request.getParameter("addressid"));
|
| 126 |
cartClient.addAddressToCart(userinfo.getCartId(), addressId);
|
122 |
userClient.addAddressToCart(userinfo.getCartId(), addressId);
|
| 127 |
} catch (TException e) {
|
123 |
} catch (TException e) {
|
| 128 |
e.printStackTrace();
|
124 |
e.printStackTrace();
|
| 129 |
} catch (Exception e) {
|
125 |
} catch (Exception e) {
|
| 130 |
// TODO Auto-generated catch block
|
- |
|
| 131 |
e.printStackTrace();
|
126 |
e.printStackTrace();
|
| 132 |
}
|
127 |
}
|
| 133 |
return new DefaultHttpHeaders("success");
|
128 |
return new DefaultHttpHeaders("success");
|
| 134 |
}
|
129 |
}
|
| 135 |
return new DefaultHttpHeaders("failure");
|
130 |
return new DefaultHttpHeaders("failure");
|