Rev 517 | Blame | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;import in.shop2020.logistics.LogisticsInfo;import in.shop2020.logistics.LogisticsServiceException;import in.shop2020.model.v1.catalog.InventoryServiceException;import in.shop2020.model.v1.catalog.Item;import in.shop2020.model.v1.order.LineItem;import in.shop2020.model.v1.order.Order;import in.shop2020.model.v1.order.OrderStatus;import in.shop2020.model.v1.order.Payment;import in.shop2020.model.v1.order.PaymentInfo;import in.shop2020.model.v1.order.PaymentStatus;import in.shop2020.model.v1.order.Transaction;import in.shop2020.model.v1.order.TransactionStatus;import in.shop2020.model.v1.shoppingcart.Cart;import in.shop2020.model.v1.shoppingcart.Line;import in.shop2020.model.v1.shoppingcart.LineStatus;import in.shop2020.model.v1.shoppingcart.ShoppingCartException;import in.shop2020.model.v1.user.Address;import in.shop2020.model.v1.user.UserContextException;import in.shop2020.payments.PaymentException;import in.shop2020.serving.controllers.BaseController;import in.shop2020.serving.pages.PageContentKeys;import in.shop2020.serving.pages.PageEnum;import in.shop2020.serving.pages.PageManager;import in.shop2020.serving.services.PageLoaderHandler;import in.shop2020.thrift.clients.CatalogServiceClient;import in.shop2020.thrift.clients.LogisticsServiceClient;import in.shop2020.thrift.clients.ShoppingCartClient;import in.shop2020.thrift.clients.TransactionServiceClient;import in.shop2020.thrift.clients.UserContextServiceClient;import java.util.*;import org.apache.juli.logging.Log;import org.apache.juli.logging.LogFactory;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;import org.apache.struts2.interceptor.ParameterAware;import org.apache.struts2.rest.DefaultHttpHeaders;import org.apache.struts2.rest.HttpHeaders;import org.apache.thrift.TException;@Results({@Result(name="redirect", location="${url}", type="redirect")})public class OrderController extends BaseController implements ParameterAware {private static final long serialVersionUID = 1L;private PageManager pageManager = null;private Map<String, String[]> reqparams;private String redirectUrl;private static Log log = LogFactory.getLog(OrderController.class);private String id;private Map<String,String> htmlSnippets;private String message;public OrderController(){super();pageManager = PageManager.getPageManager();}// GET /order/ orderidpublic String show() {log.info("id=" + id);if(!userinfo.isLoggedIn()){this.redirectUrl = "login";return "redirect";}Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();params.put(PageContentKeys.ORDER_ID, id);params.put(PageContentKeys.USER_ID, new Long(userinfo.getUserId()).toString());params.put(PageContentKeys.CART_ID, new Long(userinfo.getCartId()).toString());params.put(PageContentKeys.SESSION_ID, new Long(userinfo.getSessionId()).toString());params.put(PageContentKeys.USER_NAME, userinfo.getNameOfUser());params.put(PageContentKeys.ITEM_COUNT, new Long(userinfo.getTotalItems()).toString());htmlSnippets = pageManager.getPageContents(PageEnum.ORDER_DETAILS_PAGE, params);return "show";}public String getId(){return id;}public void setId(String id){this.id = id;}@Overridepublic void setParameters(Map<String, String[]> reqmap) {log.info("setParameters:" + reqmap);this.reqparams = reqmap;}public String getHeaderSnippet(){return htmlSnippets.get("HEADER");}public String getMainMenuSnippet(){return htmlSnippets.get("MAIN_MENU");}public String getSearchBarSnippet(){return htmlSnippets.get("SEARCH_BAR");}public String getCustomerServiceSnippet(){return htmlSnippets.get("CUSTOMER_SERVICE");}public String getMyaccountHeaderSnippet(){return htmlSnippets.get("MYACCOUNT_HEADER");}public String getOrderDetailsSnippet(){return htmlSnippets.get("ORDER_DETAILS");}public String getMyResearchSnippet(){return htmlSnippets.get("MY_RESEARCH");}public String getFooterSnippet(){return htmlSnippets.get("FOOTER");}public String getJsFileSnippet(){return htmlSnippets.get("JS_FILES");}public String getCssFileSnippet(){return htmlSnippets.get("CSS_FILES");}// POST /order/public String create() throws Exception {long addressId = Long.parseLong(this.request.getParameter("addressid"));ShoppingCartClient cl = new ShoppingCartClient();cl.getClient().addAddressToCart(userinfo.getCartId(), addressId);Transaction t = getTransaction();TransactionServiceClient tsc = new TransactionServiceClient();long transaction_id = tsc.getClient().createTransaction(t);List<Order> orders = tsc.getClient().getOrdersForTransaction(transaction_id);this.message = "Your order numbers are as below:";for(Order order: orders){this.message = this.message + "<br> <a href=\"./order/" + order.getId() + "\">" + order.getId() + "</a>";}cl.getClient().commitCart(userinfo.getCartId());userinfo.setCartId(-1);userinfo.setTotalItems(0);this.session.setAttribute("userinfo", userinfo);PageLoaderHandler pageLoader = new PageLoaderHandler();htmlSnippets = new HashMap<String, String>();htmlSnippets.put("HEADER", pageLoader.getHeaderHtml(userinfo.getUserId(), userinfo.isSessionId(), userinfo.getNameOfUser()));htmlSnippets.put("MAIN_MENU", pageLoader.getMainMenuHtml());htmlSnippets.put("SEARCH_BAR", pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000));htmlSnippets.put("CUSTOMER_SERVICE", pageLoader.getCustomerServiceHtml());htmlSnippets.put("FOOTER",pageLoader.getFooterHtml());return "success";}public String getMessage(){return this.message;}private Transaction getTransaction(){Transaction t = new Transaction();PaymentInfo paymentInfo = new PaymentInfo();t.setPaymentInfo(paymentInfo);t.setShoppingCartid(userinfo.getCartId());t.setCustomer_id(userinfo.getUserId());t.setCreatedOn((new Date()).getTime());t.setTransactionStatus(TransactionStatus.INIT);t.setStatusDescription("New order");t.setOrders(getOrders());return t;}private List<Order> getOrders(){List<Order> orders = new ArrayList<Order>();ShoppingCartClient cl = null;Cart c = null;try {cl = new ShoppingCartClient();c = cl.getClient().getCart(userinfo.getCartId());} catch (ShoppingCartException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}List<Line> lines = c.getLines();for(Line line : lines){if(line.getLineStatus() == LineStatus.LINE_ACTIVE){//line is activeLineItem lineItem = getLineItem(line.getItemId());// create orders equivalent to quantity. Create one order per item.for(int i= 0; i<line.getQuantity(); i++){// set orderOrder order = getOrder(c.getAddressId(), lineItem);order.addToLineitems(lineItem);orders.add(order);}}}return orders;}private Order getOrder(long addressId, LineItem lineItem){String sku_id = lineItem.getSku_id();double total_amount = lineItem.getTotal_price();double total_weight = lineItem.getTotal_weight();long customer_id = userinfo.getUserId();Address address = getAddress(addressId);String customer_email = userinfo.getEmail();String customer_name = address.getName();String customer_address = getAddressString(address);String customer_mobilenumber = address.getPhone();String customer_pincode = address.getPin();// get logistics informationLogisticsInfo logistics_info = null;try {LogisticsServiceClient lsc = new LogisticsServiceClient();logistics_info = lsc.getClient().getLogisticsInfo(customer_pincode, sku_id);} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (LogisticsServiceException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}long warehouse_id = logistics_info.getWarehouse_id();long logistics_provider_id= logistics_info.getProvider_id();String airwaybill_no = String.valueOf(logistics_info.getAirway_billno()); // should it be really long ?String tracking_id = airwaybill_no; //right now both are samelong expected_delivery_time = (new Date()).getTime() + 60*60*1000*logistics_info.getDelivery_estimate();long created_timestamp = (new Date()).getTime();OrderStatus status = OrderStatus.SUBMITTED_FOR_PROCESSING; // to get from orderstatus fileString statusDescription = "Submitted to warehouse";Order order = new Order();order.setLogistics_provider_id(logistics_provider_id);order.setAirwaybill_no(airwaybill_no);order.setExpected_delivery_time(expected_delivery_time);order.setTracking_id(tracking_id);order.setWarehouse_id(warehouse_id);order.setCreated_timestamp(created_timestamp);order.setStatus(status);order.setStatusDescription(statusDescription);order.setCustomer_id(customer_id);order.setCustomer_name(customer_name);order.setCustomer_address(customer_address);order.setCustomer_email(customer_email);order.setCustomer_mobilenumber(customer_mobilenumber);order.setCustomer_pincode(customer_pincode);order.setTotal_amount(total_amount);order.setTotal_weight(total_weight);return order;}private Address getAddress(long addressId){List<Address> addresses = null;try {UserContextServiceClient usc = new UserContextServiceClient();addresses = usc.getClient().getPrimaryInfo(userinfo.getUserId(), false).getAddresses();} catch (UserContextException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}Address address = null;for(Address a : addresses){if(a.getId() == addressId){address = a;}}return address;}private LineItem getLineItem(long catelogItemId){LineItem lineItem = new LineItem();Item item = null;try {CatalogServiceClient csc = new CatalogServiceClient();item = csc.getClient().getItemByCatalogId(catelogItemId);} catch (InventoryServiceException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}String model_name = item.getModelName();String model_number = item.getModelNumber();String brand = item.getManufacturerName();String sku_id = item.getVendorItemId();double unit_price = item.getSellingPrice();double unit_weight = item.getWeight();double total_price = item.getSellingPrice();double total_weight = item.getWeight();String extra_info = item.getFeatureDescription();double quantity = 1; // because now we will create one order per itemlineItem.setSku_id(sku_id);lineItem.setBrand(brand);lineItem.setExtra_info(extra_info);lineItem.setModel_name(model_name);lineItem.setModel_number(model_number);lineItem.setQuantity(quantity);lineItem.setSku_id(sku_id);lineItem.setUnit_price(unit_price);lineItem.setUnit_weight(unit_weight);lineItem.setTotal_price(total_price);lineItem.setTotal_weight(total_weight);return lineItem;}private String getAddressString(Address a){String add = a.getLine1()+",\n"+a.getLine2()+",\n Landmark"+a.getLandmark()+",/n"+a.getCity()+",\n"+a.getState()+",\n"+a.getCountry();return add;}private String getAddress(Set<Address> address, long id){for(Address a : address){if(a.getId() == id){//Prepare StringString 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();return add;}}return "";}}