Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. package B_servlets;
  2.  
  3. import EntityManager.MemberEntity;
  4. import HelperClasses.ShoppingCartLineItem;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.util.ArrayList;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.http.*;
  10. import javax.ws.rs.client.Client;
  11. import javax.ws.rs.client.ClientBuilder;
  12. import javax.ws.rs.client.Entity;
  13. import javax.ws.rs.client.Invocation;
  14. import javax.ws.rs.client.WebTarget;
  15. import javax.ws.rs.core.MediaType;
  16. import javax.ws.rs.core.Response;
  17. public class ECommerce_PaymentServlet extends HttpServlet {
  18.  
  19. private String URLprefix = "";
  20.  
  21. protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  22. response.setContentType("text/html;charset=UTF-8");
  23. PrintWriter out = response.getWriter();
  24. System.out.println("ECommerce_AddFurnitureToListServlet");
  25. try {
  26. HttpSession session = request.getSession();
  27.  
  28. URLprefix = (String) session.getAttribute("URLprefix");
  29. if (URLprefix == null) {
  30. response.sendRedirect("/IS3102_Project-war/B/selectCountry.jsp");
  31. return;
  32. }
  33.  
  34. Long countryID = (Long) session.getAttribute("countryID");
  35. MemberEntity m = (MemberEntity) session.getAttribute("member");
  36. Long memberId = m.getId();
  37.  
  38. ArrayList<ShoppingCartLineItem> shoppingCart = (ArrayList<ShoppingCartLineItem>) session.getAttribute("shoppingCart");
  39.  
  40. // double amountPaid = 0.0;
  41. // for (ShoppingCartLineItem item : shoppingCart) {
  42. // amountPaid += item.getPrice() * item.getQuantity();
  43. // }
  44. // String salesRecordID = createECommerceTransactionRecord(memberId, amountPaid, countryID);
  45. // if (salesRecordID.equals("0")) {
  46. // //error
  47. // System.out.println("Error creating ECommerce Transaction Record. Sales record ID returned 0.");
  48. // response.sendRedirect("/IS3102_Project-war/B/" + URLprefix + "shoppingCart.jsp?errMsg=Error processing transaction.");
  49. //
  50. // return;
  51. // }
  52. // for (ShoppingCartLineItem item : shoppingCart) {
  53. // String itemID = item.getId();
  54. // int quantity = item.getQuantity();
  55. // //call ws to insert lineitem and salesrecordentity_lineitementity based on salesRecordID and lineItemID
  56. // String result = createECommerceLineItemRecord(salesRecordID, itemID, quantity, countryID);
  57. // if (!result.equals("0")) {
  58. // System.out.println("createECommerceLineItemRecord successful");
  59. // } else {
  60. // System.out.println("Error creating createECommerceLineItemRecord, returned 0.");
  61. // response.sendRedirect("/IS3102_Project-war/B/" + URLprefix + "shoppingCart.jsp?errMsg=Error checking out.");
  62. // return;
  63. // }
  64. // }
  65.  
  66. session.setAttribute("shoppingCart", null);
  67.  
  68. response.sendRedirect("/IS3102_Project-war/B/" + URLprefix + "shoppingCart.jsp?goodMsg=Thank you for shopping at Island Furniture. You have checkout successfully!");
  69. } catch (Exception ex) {
  70. out.println(ex);
  71. ex.printStackTrace();
  72. response.sendRedirect("/IS3102_Project-war/B/" + URLprefix + "shoppingCart.jsp?errMsg=Error checking out.");
  73. }
  74. }
  75.  
  76. public String createECommerceLineItemRecord(String salesRecordID, String itemID, int quantity, Long countryID) {
  77. // try {
  78. // Client client = ClientBuilder.newClient();
  79. // WebTarget target = client
  80. // .target("http://dmit.bulletplus.com/WebService_Mobile/webresources/commerce")
  81. // .path("createECommerceLineItemRecord")
  82. // .queryParam("salesRecordID", salesRecordID)
  83. // .queryParam("itemID", itemID)
  84. // .queryParam("quantity", quantity)
  85. // .queryParam("countryID", countryID);
  86. //
  87. // Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON);
  88. // Response response = invocationBuilder.put(Entity.entity("", "application/json"));
  89. // if (response.getStatus() != 201) {
  90. // return "0";
  91. // }
  92. // String path = response.getLocation().getPath();
  93. // String result = path.substring(path.indexOf('=') + 1);
  94. // return result + "";
  95. //
  96. // } catch (Exception e) {
  97. // e.printStackTrace();
  98. // return "0";
  99. // }
  100. return null;
  101. }
  102.  
  103. public String createECommerceTransactionRecord(Long memberId, Double amountPaid, Long countryId) {
  104. // try {
  105. // Client client = ClientBuilder.newClient();
  106. // WebTarget target = client
  107. // .target("http://dmit.bulletplus.com/WebService_Mobile/webresources/commerce")
  108. // .path("createECommerceTransactionRecord")
  109. // .queryParam("memberID", memberId)
  110. // .queryParam("amountPaid", amountPaid)
  111. // .queryParam("countryID", countryId);
  112. //
  113. // Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON);
  114. // Response response = invocationBuilder.put(Entity.entity("", "application/json"));
  115. // if (response.getStatus() != 201) {
  116. // return "0";
  117. // }
  118. // String path = response.getLocation().getPath();
  119. // String result = path.substring(path.indexOf('=') + 1);
  120. // return result + "";
  121. //
  122. // } catch (Exception e) {
  123. // e.printStackTrace();
  124. // return "0";
  125. // }
  126. return null;
  127. }
  128.  
  129. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  130. /**
  131. * Handles the HTTP <code>GET</code> method.
  132. *
  133. * @param request servlet request
  134. * @param response servlet response
  135. * @throws ServletException if a servlet-specific error occurs
  136. * @throws IOException if an I/O error occurs
  137. */
  138. @Override
  139. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  140. throws ServletException, IOException {
  141. processRequest(request, response);
  142. }
  143.  
  144. /**
  145. * Handles the HTTP <code>POST</code> method.
  146. *
  147. * @param request servlet request
  148. * @param response servlet response
  149. * @throws ServletException if a servlet-specific error occurs
  150. * @throws IOException if an I/O error occurs
  151. */
  152. @Override
  153. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  154. throws ServletException, IOException {
  155. processRequest(request, response);
  156. }
  157.  
  158. /**
  159. * Returns a short description of the servlet.
  160. *
  161. * @return a String containing servlet description
  162. */
  163. @Override
  164. public String getServletInfo() {
  165. return "Short description";
  166. }// </editor-fold>
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement