| 419 |
rajveer |
1 |
package in.shop2020.serving.controllers;
|
|
|
2 |
|
| 517 |
rajveer |
3 |
import in.shop2020.logistics.LogisticsInfo;
|
|
|
4 |
import in.shop2020.logistics.LogisticsServiceException;
|
|
|
5 |
import in.shop2020.model.v1.catalog.InventoryServiceException;
|
|
|
6 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
7 |
import in.shop2020.model.v1.order.LineItem;
|
|
|
8 |
import in.shop2020.model.v1.order.Order;
|
|
|
9 |
import in.shop2020.model.v1.order.OrderStatus;
|
|
|
10 |
import in.shop2020.model.v1.order.Payment;
|
|
|
11 |
import in.shop2020.model.v1.order.PaymentInfo;
|
|
|
12 |
import in.shop2020.model.v1.order.PaymentStatus;
|
|
|
13 |
import in.shop2020.model.v1.order.Transaction;
|
|
|
14 |
import in.shop2020.model.v1.order.TransactionStatus;
|
| 555 |
chandransh |
15 |
import in.shop2020.model.v1.user.Cart;
|
|
|
16 |
import in.shop2020.model.v1.user.LineStatus;
|
|
|
17 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
| 517 |
rajveer |
18 |
import in.shop2020.model.v1.user.Address;
|
|
|
19 |
import in.shop2020.model.v1.user.UserContextException;
|
|
|
20 |
import in.shop2020.payments.PaymentException;
|
| 419 |
rajveer |
21 |
import in.shop2020.serving.controllers.BaseController;
|
|
|
22 |
import in.shop2020.serving.pages.PageContentKeys;
|
|
|
23 |
import in.shop2020.serving.pages.PageEnum;
|
|
|
24 |
import in.shop2020.serving.pages.PageManager;
|
| 536 |
rajveer |
25 |
import in.shop2020.serving.services.PageLoaderHandler;
|
| 517 |
rajveer |
26 |
import in.shop2020.thrift.clients.CatalogServiceClient;
|
|
|
27 |
import in.shop2020.thrift.clients.LogisticsServiceClient;
|
|
|
28 |
import in.shop2020.thrift.clients.TransactionServiceClient;
|
|
|
29 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
|
|
30 |
|
| 419 |
rajveer |
31 |
import java.util.*;
|
|
|
32 |
|
|
|
33 |
import org.apache.juli.logging.Log;
|
|
|
34 |
import org.apache.juli.logging.LogFactory;
|
|
|
35 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
36 |
import org.apache.struts2.convention.annotation.Results;
|
|
|
37 |
import org.apache.struts2.interceptor.ParameterAware;
|
|
|
38 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
39 |
import org.apache.struts2.rest.HttpHeaders;
|
| 517 |
rajveer |
40 |
import org.apache.thrift.TException;
|
| 419 |
rajveer |
41 |
|
|
|
42 |
@Results({
|
| 507 |
rajveer |
43 |
@Result(name="redirect", location="${url}", type="redirect")
|
| 419 |
rajveer |
44 |
})
|
|
|
45 |
public class OrderController extends BaseController implements ParameterAware {
|
|
|
46 |
|
|
|
47 |
private static final long serialVersionUID = 1L;
|
|
|
48 |
|
| 507 |
rajveer |
49 |
private PageManager pageManager = null;
|
| 419 |
rajveer |
50 |
private Map<String, String[]> reqparams;
|
| 507 |
rajveer |
51 |
private String redirectUrl;
|
| 419 |
rajveer |
52 |
private static Log log = LogFactory.getLog(OrderController.class);
|
| 507 |
rajveer |
53 |
private String id;
|
| 419 |
rajveer |
54 |
private Map<String,String> htmlSnippets;
|
| 517 |
rajveer |
55 |
|
|
|
56 |
private String message;
|
| 419 |
rajveer |
57 |
|
|
|
58 |
public OrderController(){
|
| 507 |
rajveer |
59 |
super();
|
|
|
60 |
pageManager = PageManager.getPageManager();
|
| 419 |
rajveer |
61 |
}
|
|
|
62 |
|
| 507 |
rajveer |
63 |
// GET /order/ orderid
|
|
|
64 |
public String show() {
|
|
|
65 |
log.info("id=" + id);
|
| 424 |
rajveer |
66 |
if(!userinfo.isLoggedIn()){
|
| 507 |
rajveer |
67 |
this.redirectUrl = "login";
|
|
|
68 |
return "redirect";
|
| 424 |
rajveer |
69 |
}
|
| 419 |
rajveer |
70 |
|
| 507 |
rajveer |
71 |
Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
|
|
|
72 |
|
|
|
73 |
params.put(PageContentKeys.ORDER_ID, id);
|
|
|
74 |
params.put(PageContentKeys.USER_ID, new Long(userinfo.getUserId()).toString());
|
|
|
75 |
params.put(PageContentKeys.CART_ID, new Long(userinfo.getCartId()).toString());
|
|
|
76 |
params.put(PageContentKeys.USER_NAME, userinfo.getNameOfUser());
|
|
|
77 |
params.put(PageContentKeys.ITEM_COUNT, new Long(userinfo.getTotalItems()).toString());
|
|
|
78 |
|
|
|
79 |
htmlSnippets = pageManager.getPageContents(PageEnum.ORDER_DETAILS_PAGE, params);
|
|
|
80 |
return "show";
|
| 419 |
rajveer |
81 |
}
|
| 517 |
rajveer |
82 |
|
| 507 |
rajveer |
83 |
public String getId(){
|
|
|
84 |
return id;
|
|
|
85 |
}
|
| 419 |
rajveer |
86 |
|
| 507 |
rajveer |
87 |
public void setId(String id){
|
|
|
88 |
this.id = id;
|
|
|
89 |
}
|
| 419 |
rajveer |
90 |
|
| 507 |
rajveer |
91 |
@Override
|
| 419 |
rajveer |
92 |
public void setParameters(Map<String, String[]> reqmap) {
|
|
|
93 |
log.info("setParameters:" + reqmap);
|
|
|
94 |
|
|
|
95 |
this.reqparams = reqmap;
|
|
|
96 |
}
|
| 507 |
rajveer |
97 |
|
|
|
98 |
public String getHeaderSnippet(){
|
|
|
99 |
return htmlSnippets.get("HEADER");
|
|
|
100 |
}
|
| 419 |
rajveer |
101 |
|
| 507 |
rajveer |
102 |
public String getMainMenuSnippet(){
|
|
|
103 |
return htmlSnippets.get("MAIN_MENU");
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
public String getSearchBarSnippet(){
|
|
|
107 |
return htmlSnippets.get("SEARCH_BAR");
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
public String getCustomerServiceSnippet(){
|
|
|
112 |
return htmlSnippets.get("CUSTOMER_SERVICE");
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
public String getMyaccountHeaderSnippet(){
|
|
|
116 |
return htmlSnippets.get("MYACCOUNT_HEADER");
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public String getOrderDetailsSnippet(){
|
|
|
120 |
return htmlSnippets.get("ORDER_DETAILS");
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
public String getMyResearchSnippet(){
|
|
|
124 |
return htmlSnippets.get("MY_RESEARCH");
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
public String getFooterSnippet(){
|
|
|
128 |
return htmlSnippets.get("FOOTER");
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
public String getJsFileSnippet(){
|
|
|
132 |
return htmlSnippets.get("JS_FILES");
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
public String getCssFileSnippet(){
|
|
|
136 |
return htmlSnippets.get("CSS_FILES");
|
|
|
137 |
}
|
| 517 |
rajveer |
138 |
|
|
|
139 |
// POST /order/
|
|
|
140 |
public String create() throws Exception {
|
|
|
141 |
long addressId = Long.parseLong(this.request.getParameter("addressid"));
|
| 555 |
chandransh |
142 |
UserContextServiceClient cl = new UserContextServiceClient();
|
| 517 |
rajveer |
143 |
cl.getClient().addAddressToCart(userinfo.getCartId(), addressId);
|
|
|
144 |
|
|
|
145 |
Transaction t = getTransaction();
|
|
|
146 |
TransactionServiceClient tsc = new TransactionServiceClient();
|
|
|
147 |
long transaction_id = tsc.getClient().createTransaction(t);
|
|
|
148 |
|
|
|
149 |
List<Order> orders = tsc.getClient().getOrdersForTransaction(transaction_id);
|
| 536 |
rajveer |
150 |
this.message = "Your order numbers are as below:";
|
|
|
151 |
|
| 517 |
rajveer |
152 |
for(Order order: orders){
|
| 536 |
rajveer |
153 |
this.message = this.message + "<br> <a href=\"./order/" + order.getId() + "\">" + order.getId() + "</a>";
|
| 517 |
rajveer |
154 |
}
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
cl.getClient().commitCart(userinfo.getCartId());
|
|
|
158 |
|
|
|
159 |
userinfo.setCartId(-1);
|
|
|
160 |
userinfo.setTotalItems(0);
|
|
|
161 |
this.session.setAttribute("userinfo", userinfo);
|
|
|
162 |
|
| 536 |
rajveer |
163 |
PageLoaderHandler pageLoader = new PageLoaderHandler();
|
|
|
164 |
htmlSnippets = new HashMap<String, String>();
|
| 555 |
chandransh |
165 |
htmlSnippets.put("HEADER", pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getNameOfUser()));
|
| 536 |
rajveer |
166 |
htmlSnippets.put("MAIN_MENU", pageLoader.getMainMenuHtml());
|
|
|
167 |
htmlSnippets.put("SEARCH_BAR", pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000));
|
|
|
168 |
htmlSnippets.put("CUSTOMER_SERVICE", pageLoader.getCustomerServiceHtml());
|
|
|
169 |
htmlSnippets.put("FOOTER",pageLoader.getFooterHtml());
|
|
|
170 |
|
| 517 |
rajveer |
171 |
return "success";
|
|
|
172 |
}
|
| 507 |
rajveer |
173 |
|
| 517 |
rajveer |
174 |
public String getMessage(){
|
|
|
175 |
return this.message;
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
private Transaction getTransaction(){
|
|
|
179 |
Transaction t = new Transaction();
|
|
|
180 |
PaymentInfo paymentInfo = new PaymentInfo();
|
|
|
181 |
t.setPaymentInfo(paymentInfo);
|
|
|
182 |
t.setShoppingCartid(userinfo.getCartId());
|
|
|
183 |
t.setCustomer_id(userinfo.getUserId());
|
|
|
184 |
t.setCreatedOn((new Date()).getTime());
|
|
|
185 |
t.setTransactionStatus(TransactionStatus.INIT);
|
|
|
186 |
t.setStatusDescription("New order");
|
|
|
187 |
|
|
|
188 |
t.setOrders(getOrders());
|
|
|
189 |
|
|
|
190 |
return t;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
private List<Order> getOrders(){
|
|
|
194 |
List<Order> orders = new ArrayList<Order>();
|
| 555 |
chandransh |
195 |
UserContextServiceClient usc = null;
|
| 517 |
rajveer |
196 |
Cart c = null;
|
|
|
197 |
try {
|
| 555 |
chandransh |
198 |
usc = new UserContextServiceClient();
|
|
|
199 |
c = usc.getClient().getCart(userinfo.getCartId());
|
| 517 |
rajveer |
200 |
} catch (ShoppingCartException e) {
|
|
|
201 |
e.printStackTrace();
|
|
|
202 |
} catch (TException e) {
|
|
|
203 |
e.printStackTrace();
|
|
|
204 |
} catch (Exception e) {
|
|
|
205 |
e.printStackTrace();
|
|
|
206 |
}
|
| 555 |
chandransh |
207 |
List<in.shop2020.model.v1.user.Line> cartLines = c.getLines();
|
| 517 |
rajveer |
208 |
|
| 555 |
chandransh |
209 |
for(in.shop2020.model.v1.user.Line cartLine : cartLines){
|
|
|
210 |
if(cartLine.getLineStatus() == LineStatus.LINE_ACTIVE){
|
| 517 |
rajveer |
211 |
//line is active
|
| 555 |
chandransh |
212 |
LineItem lineItem = getLineItem(cartLine.getItemId());
|
| 517 |
rajveer |
213 |
// create orders equivalent to quantity. Create one order per item.
|
| 555 |
chandransh |
214 |
for(int i= 0; i<cartLine.getQuantity(); i++){
|
| 517 |
rajveer |
215 |
// set order
|
|
|
216 |
Order order = getOrder(c.getAddressId(), lineItem);
|
|
|
217 |
order.addToLineitems(lineItem);
|
|
|
218 |
orders.add(order);
|
|
|
219 |
}
|
|
|
220 |
}
|
|
|
221 |
}
|
|
|
222 |
return orders;
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
|
|
|
226 |
private Order getOrder(long addressId, LineItem lineItem){
|
|
|
227 |
String sku_id = lineItem.getSku_id();
|
|
|
228 |
double total_amount = lineItem.getTotal_price();
|
|
|
229 |
double total_weight = lineItem.getTotal_weight();
|
|
|
230 |
|
|
|
231 |
long customer_id = userinfo.getUserId();
|
|
|
232 |
Address address = getAddress(addressId);
|
|
|
233 |
String customer_email = userinfo.getEmail();
|
|
|
234 |
String customer_name = address.getName();
|
|
|
235 |
String customer_address = getAddressString(address);
|
|
|
236 |
String customer_mobilenumber = address.getPhone();
|
|
|
237 |
String customer_pincode = address.getPin();
|
|
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
|
241 |
// get logistics information
|
|
|
242 |
LogisticsInfo logistics_info = null;
|
|
|
243 |
try {
|
|
|
244 |
LogisticsServiceClient lsc = new LogisticsServiceClient();
|
|
|
245 |
logistics_info = lsc.getClient().getLogisticsInfo(customer_pincode, sku_id);
|
|
|
246 |
} catch (TException e) {
|
|
|
247 |
// TODO Auto-generated catch block
|
|
|
248 |
e.printStackTrace();
|
|
|
249 |
} catch (LogisticsServiceException e) {
|
|
|
250 |
// TODO Auto-generated catch block
|
|
|
251 |
e.printStackTrace();
|
|
|
252 |
} catch (Exception e) {
|
|
|
253 |
// TODO Auto-generated catch block
|
|
|
254 |
e.printStackTrace();
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
long warehouse_id = logistics_info.getWarehouse_id();
|
|
|
258 |
long logistics_provider_id= logistics_info.getProvider_id();
|
|
|
259 |
String airwaybill_no = String.valueOf(logistics_info.getAirway_billno()); // should it be really long ?
|
|
|
260 |
String tracking_id = airwaybill_no; //right now both are same
|
|
|
261 |
long expected_delivery_time = (new Date()).getTime() + 60*60*1000*logistics_info.getDelivery_estimate();
|
|
|
262 |
|
|
|
263 |
long created_timestamp = (new Date()).getTime();
|
|
|
264 |
OrderStatus status = OrderStatus.SUBMITTED_FOR_PROCESSING; // to get from orderstatus file
|
|
|
265 |
String statusDescription = "Submitted to warehouse";
|
|
|
266 |
|
|
|
267 |
Order order = new Order();
|
|
|
268 |
order.setLogistics_provider_id(logistics_provider_id);
|
|
|
269 |
order.setAirwaybill_no(airwaybill_no);
|
|
|
270 |
order.setExpected_delivery_time(expected_delivery_time);
|
|
|
271 |
order.setTracking_id(tracking_id);
|
|
|
272 |
|
|
|
273 |
order.setWarehouse_id(warehouse_id);
|
|
|
274 |
order.setCreated_timestamp(created_timestamp);
|
|
|
275 |
order.setStatus(status);
|
|
|
276 |
order.setStatusDescription(statusDescription);
|
|
|
277 |
|
|
|
278 |
order.setCustomer_id(customer_id);
|
|
|
279 |
order.setCustomer_name(customer_name);
|
|
|
280 |
order.setCustomer_address(customer_address);
|
|
|
281 |
order.setCustomer_email(customer_email);
|
|
|
282 |
order.setCustomer_mobilenumber(customer_mobilenumber);
|
|
|
283 |
order.setCustomer_pincode(customer_pincode);
|
|
|
284 |
|
|
|
285 |
order.setTotal_amount(total_amount);
|
|
|
286 |
order.setTotal_weight(total_weight);
|
|
|
287 |
|
|
|
288 |
return order;
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
|
|
|
292 |
private Address getAddress(long addressId){
|
|
|
293 |
List<Address> addresses = null;
|
|
|
294 |
|
|
|
295 |
try {
|
|
|
296 |
UserContextServiceClient usc = new UserContextServiceClient();
|
| 555 |
chandransh |
297 |
addresses = usc.getClient().getUserById(userinfo.getUserId()).getAddresses();
|
| 517 |
rajveer |
298 |
} catch (UserContextException e) {
|
|
|
299 |
e.printStackTrace();
|
|
|
300 |
} catch (TException e) {
|
|
|
301 |
e.printStackTrace();
|
|
|
302 |
} catch (Exception e) {
|
|
|
303 |
e.printStackTrace();
|
|
|
304 |
}
|
|
|
305 |
Address address = null;
|
|
|
306 |
for(Address a : addresses){
|
|
|
307 |
if(a.getId() == addressId){
|
|
|
308 |
address = a;
|
|
|
309 |
}
|
|
|
310 |
}
|
|
|
311 |
return address;
|
|
|
312 |
}
|
|
|
313 |
|
|
|
314 |
private LineItem getLineItem(long catelogItemId){
|
|
|
315 |
|
|
|
316 |
LineItem lineItem = new LineItem();
|
|
|
317 |
Item item = null;
|
|
|
318 |
try {
|
|
|
319 |
CatalogServiceClient csc = new CatalogServiceClient();
|
|
|
320 |
item = csc.getClient().getItemByCatalogId(catelogItemId);
|
|
|
321 |
} catch (InventoryServiceException e) {
|
|
|
322 |
e.printStackTrace();
|
|
|
323 |
} catch (TException e) {
|
|
|
324 |
e.printStackTrace();
|
|
|
325 |
} catch (Exception e) {
|
|
|
326 |
e.printStackTrace();
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
String model_name = item.getModelName();
|
|
|
330 |
String model_number = item.getModelNumber();
|
|
|
331 |
String brand = item.getManufacturerName();
|
|
|
332 |
String sku_id = item.getVendorItemId();
|
|
|
333 |
double unit_price = item.getSellingPrice();
|
|
|
334 |
double unit_weight = item.getWeight();
|
|
|
335 |
double total_price = item.getSellingPrice();
|
|
|
336 |
double total_weight = item.getWeight();
|
|
|
337 |
String extra_info = item.getFeatureDescription();
|
|
|
338 |
double quantity = 1; // because now we will create one order per item
|
|
|
339 |
|
|
|
340 |
lineItem.setSku_id(sku_id);
|
|
|
341 |
lineItem.setBrand(brand);
|
|
|
342 |
lineItem.setExtra_info(extra_info);
|
|
|
343 |
lineItem.setModel_name(model_name);
|
|
|
344 |
lineItem.setModel_number(model_number);
|
|
|
345 |
lineItem.setQuantity(quantity);
|
|
|
346 |
lineItem.setSku_id(sku_id);
|
|
|
347 |
lineItem.setUnit_price(unit_price);
|
|
|
348 |
lineItem.setUnit_weight(unit_weight);
|
|
|
349 |
lineItem.setTotal_price(total_price);
|
|
|
350 |
lineItem.setTotal_weight(total_weight);
|
|
|
351 |
return lineItem;
|
|
|
352 |
}
|
|
|
353 |
|
|
|
354 |
private String getAddressString(Address a){
|
|
|
355 |
String add = a.getLine1()+",\n"+a.getLine2()+",\n Landmark"+a.getLandmark()+",/n"+a.getCity()+",\n"+a.getState()+",\n"+a.getCountry();
|
|
|
356 |
return add;
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
private String getAddress(Set<Address> address, long id){
|
|
|
360 |
for(Address a : address){
|
|
|
361 |
if(a.getId() == id){
|
|
|
362 |
//Prepare String
|
|
|
363 |
String add = a.getName()+",\n"+a.getLine1()+",\n"+a.getLine2()+",\n Landmark"+a.getLandmark()+",/n"+a.getCity()+",\n"+a.getState()+",\n"+a.getCountry()+",\n"+a.getPin()+",\n Phone :- "+a.getPhone();
|
|
|
364 |
return add;
|
|
|
365 |
}
|
|
|
366 |
}
|
|
|
367 |
return "";
|
|
|
368 |
}
|
|
|
369 |
|
| 419 |
rajveer |
370 |
}
|