PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/OnlineShoppingApplication/src/main/java/com/dh/web/controller/ApplicationController.java

https://gitlab.com/deep060494/OnlineShoppingApplication
Java | 1050 lines | 602 code | 244 blank | 204 comment | 22 complexity | 257c39e7212887d65047c69a54a04f3b MD5 | raw file
  1. package com.dh.web.controller;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Collection;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import java.util.ListIterator;
  10. import java.util.Map;
  11. import java.util.Random;
  12. import javax.xml.bind.JAXBException;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.context.ApplicationContext;
  15. import org.springframework.context.support.FileSystemXmlApplicationContext;
  16. import org.springframework.data.domain.Page;
  17. import org.springframework.data.domain.PageRequest;
  18. import org.springframework.data.domain.Sort;
  19. import org.springframework.data.jpa.repository.JpaRepository;
  20. import org.springframework.security.access.method.P;
  21. import org.springframework.security.authentication.AnonymousAuthenticationToken;
  22. import org.springframework.security.core.GrantedAuthority;
  23. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  24. import org.springframework.security.core.context.SecurityContext;
  25. import org.springframework.security.core.context.SecurityContextHolder;
  26. import org.springframework.stereotype.Service;
  27. import org.springframework.web.bind.annotation.PathVariable;
  28. import org.springframework.web.bind.annotation.RequestBody;
  29. import org.springframework.web.bind.annotation.RequestMapping;
  30. import org.springframework.web.bind.annotation.RequestParam;
  31. import org.springframework.web.bind.annotation.ResponseBody;
  32. import org.springframework.web.bind.annotation.RestController;
  33. import org.springframework.web.multipart.MultipartFile;
  34. import org.xml.sax.SAXException;
  35. import com.dh.web.model.Brand;
  36. import com.dh.web.model.Cart;
  37. import com.dh.web.model.CartItem;
  38. import com.dh.web.model.Category;
  39. import com.dh.web.model.Customer;
  40. import com.dh.web.model.Order;
  41. import com.dh.web.model.OrderDetail;
  42. import com.dh.web.model.Product;
  43. import com.dh.web.model.ProductImage;
  44. import com.dh.web.model.ShippingAddress;
  45. import com.dh.web.model.StateVat;
  46. import com.dh.web.repository.BrandRepository;
  47. import com.dh.web.repository.CartItemRepository;
  48. import com.dh.web.repository.CartRepository;
  49. import com.dh.web.repository.CategoryRepository;
  50. import com.dh.web.repository.CustomerRepository;
  51. import com.dh.web.repository.OrderDetailRepository;
  52. import com.dh.web.repository.OrderRepository;
  53. import com.dh.web.repository.ProductImageRepository;
  54. import com.dh.web.repository.ProductRepository;
  55. import com.dh.web.repository.ShippingAddressRepository;
  56. import com.dh.web.repository.StateVatRepository;
  57. @RestController
  58. public class ApplicationController {
  59. @Autowired
  60. BrandRepository brandRepository;
  61. @Autowired
  62. CartItemRepository cartItemRepository ;
  63. @Autowired
  64. CartRepository cartRepository ;
  65. @Autowired
  66. CustomerRepository customerRepository ;
  67. @Autowired
  68. OrderRepository orderRepository ;
  69. @Autowired
  70. OrderDetailRepository orderDetailRepository ;
  71. @Autowired
  72. ShippingAddressRepository shippingAddressRepository ;
  73. @Autowired
  74. ShippingAddressRepository shippingAddressRepository2;
  75. @Autowired
  76. ProductRepository productRepository ;
  77. @Autowired
  78. StateVatRepository stateVatRepository ;
  79. @Autowired
  80. CategoryRepository categoryRepository ;
  81. @Autowired
  82. ProductImageRepository productImageRepository;
  83. @RequestMapping("/public/log")
  84. public HashMap<String, String> getLogin() {
  85. HashMap<String, String> returnParams = new HashMap<String, String>();
  86. //System.out.println(SecurityContextHolder.getContext().getAuthentication().getName());
  87. // System.out.println(SecurityContextHolder.getContext().getAuthentication().getCredentials());
  88. // System.out.println(SecurityContextHolder.getContext().getAuthentication().getDetails());
  89. //System.out.println(SecurityContextHolder.getContext().getAuthentication().isAuthenticated());
  90. // System.out.println(SecurityContextHolder.getContext().getAuthentication().getAuthorities());
  91. //System.out.println(SecurityContextHolder.getContext().getAuthentication());
  92. if(!(SecurityContextHolder.getContext().getAuthentication() instanceof AnonymousAuthenticationToken) && SecurityContextHolder.getContext().getAuthentication().isAuthenticated()==true)
  93. {
  94. returnParams.put("status", "true");
  95. returnParams.put("name", SecurityContextHolder.getContext().getAuthentication().getName());
  96. Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
  97. Boolean authority = authorities.contains(new SimpleGrantedAuthority("USER"));
  98. Boolean auth = true;
  99. //System.out.println(authority);
  100. if(authority==auth)
  101. returnParams.put("role", "user");
  102. else
  103. returnParams.put("role", "admin");
  104. }
  105. else
  106. {
  107. returnParams.put("status", "false");
  108. }
  109. return returnParams;
  110. }
  111. @RequestMapping("/public/brands")
  112. public List<Brand> getBrands() {
  113. return (List<Brand>) brandRepository.findAll();
  114. }
  115. @RequestMapping("/user/cart")
  116. public Cart getCart() {
  117. Customer customer = getCustomer();
  118. try
  119. {
  120. int cartid = customer.getCart().getCartId();
  121. return cartRepository.findOne(cartid);
  122. } catch (Exception e) {
  123. return null;
  124. }
  125. }
  126. @RequestMapping("/user/cart/{cartId}")
  127. public Cart getCart (@PathVariable("cartId") int cartId){
  128. return cartRepository.findOne(cartId);
  129. }
  130. @RequestMapping("/user/cartitems")
  131. public List<CartItem> getCartItem() {
  132. return (List<CartItem>) cartItemRepository.findAll();
  133. }
  134. @RequestMapping("/user/customers")
  135. public List<Customer> getCustomers() {
  136. return (List<Customer>) customerRepository.findAll();
  137. }
  138. @RequestMapping("/user/customers/one")
  139. public Customer getCustomer (){
  140. String userName = SecurityContextHolder.getContext().getAuthentication().getName();
  141. return customerRepository.findByuserName(userName);
  142. }
  143. @RequestMapping("/admin/orders")
  144. public Page<Order> getOrders(@RequestParam("pc")int pc, @RequestParam("ps")int ps) {
  145. int pageCount;
  146. if(pc==0){
  147. pageCount=0;
  148. }
  149. else{
  150. pageCount=pc-1;
  151. }
  152. PageRequest pageRequest=new PageRequest(pageCount,ps);
  153. return (Page<Order>) orderRepository.findAll(pageRequest);
  154. }
  155. @RequestMapping("/admin/orders/{orderId}")
  156. public Customer getOrder1(@PathVariable("orderId")int orderId) {
  157. return customerRepository.findByOrderOrderId(orderId);
  158. }
  159. @RequestMapping("/autosearchbyname/{userName}")
  160. public List<String> getOrdersByName(@PathVariable("userName") String userName){
  161. return customerRepository.findbyusername("%"+userName+"%");
  162. }
  163. @RequestMapping("/admin/ordercost/{orderId}")
  164. public Order getOrder2(@PathVariable("orderId")int orderId) {
  165. return orderRepository.findOne(orderId);
  166. }
  167. @RequestMapping("/admin/orders/search/{userName}")
  168. public List<Order> getCustomerbyfirstName(@PathVariable("userName") String userName) {
  169. //HashMap<String, Object> returnParams=new HashMap<String, Object>();
  170. return customerRepository.findByuserName(userName).getOrder();
  171. /*return (List<Customer>) customerRepository.findByFirstName(firstName);
  172. */
  173. }
  174. @RequestMapping("/user/myorders")
  175. public List<Order> getmyOrders() {
  176. List<Order> order = getCustomer().getOrder();
  177. Customer customer=new Customer();
  178. customer.getCustomerId();
  179. int customerId=getCustomer().getCustomerId();
  180. return (List<Order>) orderRepository.findByCustomerCustomerId(customerId, sortByOrderIdDesc());
  181. }
  182. private Sort sortByOrderIdDesc() {
  183. return new Sort(Sort.Direction.DESC, "orderId");
  184. }
  185. @RequestMapping("/admin/order/{orderId}")
  186. public List<OrderDetail> getOrderDetail(@PathVariable("orderId") int orderId) {
  187. Order order = orderRepository.findOne(orderId);
  188. return order.getOrderdetail();
  189. }
  190. @RequestMapping("/user/order/{orderId}")
  191. public List<OrderDetail> getOrderDetail1(@PathVariable("orderId") int orderId) {
  192. Order order = orderRepository.findOne(orderId);
  193. return order.getOrderdetail();
  194. }
  195. /*@RequestMapping("/public/productsearch")
  196. public List<Product> getProducts() {
  197. return (List<Product>) productRepository.findAll();
  198. }
  199. */
  200. @RequestMapping("/public/products")
  201. public Page<Product> getProducts(@RequestParam("pc")int pc, @RequestParam("ps")int ps) {
  202. int pageCount;
  203. if(pc==0){
  204. pageCount=0;
  205. }
  206. else{
  207. pageCount=pc-1;
  208. }
  209. PageRequest pageRequest=new PageRequest(pageCount,ps);
  210. return (Page<Product>) productRepository.findAll(pageRequest);
  211. }
  212. @RequestMapping("/public/productsearch")
  213. public List<Product> getProduct(@RequestBody HashMap<String , Object> map ) {
  214. List<Product> p2=null;
  215. Category cat = categoryRepository.findOne((String) map.get("category"));
  216. List<Object> list = (List<Object>) map.get("brand");
  217. ListIterator<Object> listIterator = list.listIterator();
  218. List<Brand> bList = new ArrayList<>();
  219. while (listIterator.hasNext()) {
  220. String name = (String) listIterator.next();
  221. Brand brand = brandRepository.findOne(name);
  222. bList.add(brand);
  223. }
  224. List<Product> pList = productRepository.findByCategoryAndBrandIn(cat,bList);
  225. System.out.println(pList.size());
  226. return pList;
  227. /* PageRequest pageRequest=new PageRequest(pageCount,pageSize);
  228. System.out.println(productRepository.count());
  229. return (Page<Product>) productRepository.findAll(pageRequest);*/
  230. }
  231. /*@RequestMapping("/public/productsearch")
  232. public List<Product> getProduct(@RequestBody HashMap<String , Object> map ) {
  233. List<Product> p2=null;
  234. String category= (String) map.get("category");
  235. String [] brand=(String[]) map.get("brand");
  236. Product p1=(Product) productRepository.findByCategory(category);
  237. for(int i=0; i<brand.length;i++){
  238. p2= productRepository.findByBrand(p1, brand[i]);
  239. }
  240. return p2;
  241. }
  242. */
  243. @RequestMapping("/public/products/brand/{brandId}")
  244. public List<Product> getProductByBrand(@PathVariable("brandId") int brandId) {
  245. return (List<Product>) productRepository.findByBrandBrandId(brandId);
  246. }
  247. @RequestMapping("/public/products/category/{categoryId}")
  248. public List<Product> getProductByCategory(@PathVariable("categoryId") int categoryId) {
  249. return (List<Product>) productRepository.findByCategoryCategoryId(categoryId);
  250. }
  251. @RequestMapping("public/product/{productId}")
  252. public Product getProduct(@PathVariable("productId") int productId){
  253. return productRepository.findOne(productId);
  254. }
  255. @RequestMapping("/public/products/{productName}")
  256. public List<Product> getProductbyName (@PathVariable("productName") String productName){
  257. return productRepository.findByProductName(productName);
  258. }
  259. /*@RequestMapping("/public/products/{productName}")
  260. public List<Product> getProductbyName (@PathVariable("productName") String productName){
  261. return productRepository.findByProductName(productName);
  262. }
  263. */
  264. @RequestMapping("/autosearchbytitle/{productName}")
  265. public List<String> getProductbyName1(@PathVariable("productName") String productName){
  266. return productRepository.findByproductName("%"+productName+"%");
  267. }
  268. @RequestMapping("/public/viewproducts")
  269. public List<Product> getviewProducts() {
  270. return (List<Product>) productRepository.findAll();
  271. }
  272. @RequestMapping("/public/categories")
  273. public List<Category> getCategories() {
  274. return (List<Category>) categoryRepository.findAll();
  275. }
  276. @RequestMapping("/user/shippingaddress/{shippingId}")
  277. public ShippingAddress getShippingAddress(@PathVariable("shippingId") int shippingId) {
  278. return shippingAddressRepository2.findOne(shippingId);
  279. }
  280. @RequestMapping("/user/shippingaddresses")
  281. public List<ShippingAddress> getShippingAddress() {
  282. return (List<ShippingAddress>) shippingAddressRepository.findAll();
  283. }
  284. @RequestMapping("/user/shippingaddresses/one")
  285. public List<ShippingAddress> getShippingAddressOne() {
  286. String userName = SecurityContextHolder.getContext().getAuthentication().getName();
  287. Customer customer= customerRepository.findByuserName(userName);
  288. int id=customer.getCustomerId();
  289. System.out.println("---------------->Customer object is :"+customer);
  290. return (List<ShippingAddress>) shippingAddressRepository.findByCustomer(customer);
  291. }
  292. @RequestMapping("/user/displaystate")
  293. public List<StateVat> getStateVat() {
  294. return (List<StateVat>) stateVatRepository.findAll();
  295. }
  296. @RequestMapping("/public/savecustomer")
  297. public HashMap<String, Object> savecustomer(@RequestBody Customer customer) {
  298. HashMap<String, Object> returnParams = new HashMap<String, Object>();
  299. try {
  300. String userName=customer.getUserName();
  301. String emailId=customer.getEmailId();
  302. Customer customer1=customerRepository.findByuserName(userName);
  303. if(customer1 != null){
  304. returnParams.put("msg1", "Username/EmailId exists");
  305. }
  306. else{
  307. customerRepository.save(customer);
  308. returnParams.put("status", true);
  309. }
  310. } catch (Exception e) {
  311. returnParams.put("status", false);
  312. returnParams.put("msg", "customer Addition Failed User Exists!!!!!!");
  313. }
  314. return returnParams;
  315. }
  316. @RequestMapping("/admin/addcategory")
  317. public HashMap<String, Object> category(@RequestBody Category category) {
  318. HashMap<String, Object> returnParams = new HashMap<String, Object>();
  319. try {
  320. String CategoryName= category.getCategoryName();
  321. Category cat=categoryRepository.findByCategoryName(CategoryName);
  322. if(cat!=null)
  323. {
  324. returnParams.put("msg1", "Category exists");
  325. }
  326. else{
  327. categoryRepository.save(category);
  328. returnParams.put("status", true);
  329. }
  330. }
  331. catch (Exception e) {
  332. returnParams.put("status", false);
  333. }
  334. return returnParams;
  335. }
  336. @RequestMapping("/admin/addbrand")
  337. public HashMap<String, Object> category(@RequestBody Brand brand) {
  338. HashMap<String, Object> returnParams = new HashMap<String, Object>();
  339. try {
  340. String BrandName= brand.getBrandName();
  341. Brand bran=brandRepository.findByBrandName(BrandName);
  342. if(bran!=null)
  343. {
  344. returnParams.put("msg1", "Brand exists");
  345. }
  346. else{
  347. brandRepository.save(brand);
  348. returnParams.put("status", true);
  349. }
  350. }
  351. catch (Exception e) {
  352. returnParams.put("status", false);
  353. }
  354. return returnParams;
  355. }
  356. @RequestMapping("/user/addshippingaddress")
  357. public HashMap<String, Object> addShippingaddress(@RequestBody ShippingAddress shippingaddress) {
  358. HashMap<String, Object> returnParams = new HashMap<String, Object>();
  359. try{
  360. String userName = SecurityContextHolder.getContext().getAuthentication().getName();
  361. Customer customer= customerRepository.findByuserName(userName);
  362. System.out.println("Customer Object:"+customer);
  363. int id=customer.getCustomerId();
  364. System.out.println("-----------------------> Customer Id:"+id);
  365. shippingaddress.setCustomer(customer);
  366. String state=shippingaddress.getState();
  367. System.out.println("------------------->State is:"+state);
  368. StateVat stateVat=(StateVat) stateVatRepository.findBystate(state);
  369. int stateId=stateVat.getStateId();
  370. System.out.println("------------------------> State Id :"+stateId);
  371. shippingaddress.setStateVat(stateVat);
  372. System.out.println(customer);
  373. shippingAddressRepository2.save(shippingaddress);
  374. returnParams.put("status", true);
  375. } catch (Exception e) {
  376. returnParams.put("status", false);
  377. returnParams.put("msg", "Shippingaddress Addition Failed!!!!!!");
  378. }
  379. return returnParams;
  380. }
  381. @RequestMapping("/user/editshippingaddress")
  382. public HashMap<String, Object> editShippingaddress(@RequestBody ShippingAddress shippingaddress) {
  383. HashMap<String, Object> returnParams = new HashMap<String, Object>();
  384. try{
  385. String userName = SecurityContextHolder.getContext().getAuthentication().getName();
  386. Customer customer= customerRepository.findByuserName(userName);
  387. System.out.println("Customer Object:"+customer);
  388. int id=customer.getCustomerId();
  389. System.out.println("-----------------------> Customer Id:"+id);
  390. shippingaddress.setCustomer(customer);
  391. String state=shippingaddress.getState();
  392. System.out.println("------------------->State is:"+state);
  393. StateVat stateVat=(StateVat) stateVatRepository.findBystate(state);
  394. int stateId=stateVat.getStateId();
  395. System.out.println("------------------------> State Id :"+stateId);
  396. shippingaddress.setStateVat(stateVat);
  397. System.out.println(customer);
  398. /*String userName = SecurityContextHolder.getContext().getAuthentication().getName();
  399. Customer customer= customerRepository.findByuserName(userName);*/
  400. /*int id=customer.getCustomerId();
  401. */
  402. shippingaddress.setCustomer(customer);
  403. /*String state=shippingaddress.getState();
  404. StateVat stateVat=(StateVat) stateVatRepository.findBystate(state);
  405. int stateId=stateVat.getStateId();*/
  406. shippingaddress.setStateVat(stateVat);
  407. shippingAddressRepository2.save(shippingaddress);
  408. returnParams.put("status", true);
  409. } catch (Exception e) {
  410. returnParams.put("status", false);
  411. returnParams.put("msg", "Shippingaddress Addition Failed!!!!!!");
  412. }
  413. return returnParams;
  414. }
  415. @RequestMapping("/admin/saveproduct")
  416. public HashMap<String, Object> saveproduct(@RequestBody Product product) throws IOException {
  417. HashMap<String, Object> returnParams = new HashMap<String, Object>();
  418. try {
  419. // byte[] b = file.getBytes();
  420. // String imageFile = StringUtils.newStringUtf8(Base64.encodeBase64(b));
  421. // System.out.println(imageFile);
  422. // System.out.println(product.getCategory());
  423. productRepository.save(product);
  424. ProductImage productImage=new ProductImage();
  425. productImage.setProduct(product);
  426. productImage.setImage(product.getImage());
  427. productImageRepository.save(productImage);
  428. returnParams.put("status", true);
  429. } catch (Exception e) {
  430. returnParams.put("status", false);
  431. returnParams.put("msg", "product Addition Failed!!!!!!");
  432. }
  433. return returnParams;
  434. }
  435. @RequestMapping("/user/savedetails")
  436. public HashMap<String, Object> savedetails(@RequestBody Customer customer) {
  437. HashMap<String, Object> returnParams = new HashMap<String, Object>();
  438. try {
  439. customerRepository.save(customer);
  440. returnParams.put("status", true);
  441. } catch (Exception e) {
  442. e.printStackTrace();
  443. returnParams.put("status", false);
  444. returnParams.put("msg", "Details Addition Failed!!!!!!");
  445. }
  446. return returnParams;
  447. }
  448. /* @RequestMapping("/addtocart")
  449. public HashMap<String, Object> addtoCart(@RequestBody CartItem cartitem) { }
  450. HashMap<String, Object> returnParams = new HashMap<String, Object>();
  451. try
  452. {
  453. Customer customer= getCustomer();
  454. Cart cartcheck = customer.getCart();
  455. if(cartcheck != null)
  456. {
  457. int cartid = customer.getCart().getCartId();
  458. Cart cart = cartRepository.findOne(cartid);
  459. cartitem.setCart(cart);
  460. }
  461. List<CartItem> cartitems = cart.getCartitem();
  462. Iterator<CartItem> it = cartitems.iterator();
  463. while(it.hasNext()){
  464. CartItem cartitem= (CartItem)it.next();
  465. Product product = cartitem.getProduct();
  466. product.setCartitem(cartitem);
  467. cartitem.setCart(cart);
  468. }
  469. else
  470. {
  471. Cart cart1 = customer.getCart();
  472. cartitem.setCart(cart1);
  473. }
  474. // cart.setCustomer(getCustomer());
  475. cartItemRepository.save(cartitem);
  476. returnParams.put("status", true);
  477. } catch (Exception e) {
  478. e.printStackTrace();
  479. returnParams.put("status", false);
  480. returnParams.put("msg", "Cart Addition Failed!");
  481. }
  482. return returnParams;
  483. }
  484. }
  485. */
  486. /*
  487. @RequestMapping(value = "/upload")
  488. public HashMap<String, Object> handleFormUpload(@RequestParam("file") MultipartFile file) throws IOException{
  489. File convertedFile=convert(file);
  490. System.out.println(convertedFile);
  491. Product prodcut=new Product();
  492. //int id=product.getProductId();
  493. HashMap<String, Object> returnParams = new HashMap<String, Object>();
  494. byte[] b = file.getBytes();
  495. String imageFile = StringUtils.newStringUtf8(Base64.encodeBase64(b));
  496. System.out.println(imageFile);
  497. Product uploadFile = new Product();
  498. uploadFile.setImage(imageFile);
  499. uploadFile.setExtn("jpg");
  500. //uploadFile.setProduct(product);
  501. productRepository.save(uploadFile);
  502. System.out.println("Image Successfully Manipulated!");
  503. return returnParams;
  504. }
  505. }*/
  506. @RequestMapping("/user/cartcheck")
  507. public HashMap<String, String> cartCheck() {
  508. HashMap<String, String> returnParams = new HashMap<String, String>();
  509. Customer customer= getCustomer();
  510. Cart cartcheck = customer.getCart();
  511. if(cartcheck != null)
  512. returnParams.put("status", "true");
  513. else
  514. returnParams.put("status", "false");
  515. return returnParams;
  516. }
  517. @RequestMapping("/user/addcartitem")
  518. public HashMap<String, Object> addCartitem(@RequestBody CartItem cartitem) {
  519. HashMap<String, Object> returnParams = new HashMap<String, Object>();
  520. try
  521. {
  522. int qty = cartitem.getQuantity();
  523. int productQty=cartitem.getProduct().getQuantity();
  524. System.out.println(productQty+":"+qty);
  525. Product product=cartitem.getProduct();
  526. Cart cart = getCustomer().getCart();
  527. int productId = product.getProductId();
  528. if(qty>productQty){
  529. System.out.println("Quantity exceeded");
  530. returnParams.put("msg", "quantity exceeded");
  531. }
  532. if(qty<productQty)
  533. {
  534. List<CartItem> cartitems = cart.getCartitem();
  535. Iterator<CartItem> it = cartitems.iterator();
  536. boolean stat;
  537. while(it.hasNext()){
  538. CartItem cartit= (CartItem)it.next();
  539. stat = cartit.getProduct().getProductId()==productId;
  540. if(stat==true)
  541. {
  542. cartitem.setQuantity(cartit.getQuantity()+1);
  543. cartitem.setCartitemId(cartit.getCartitemId());
  544. }
  545. }
  546. cartitem.setCart(cart);
  547. cartItemRepository.save(cartitem);
  548. returnParams.put("status", true);
  549. }
  550. } catch (Exception e) {
  551. e.printStackTrace();
  552. returnParams.put("status", false);
  553. returnParams.put("msg", "Cart Addition Failed!");
  554. }
  555. return returnParams;
  556. }
  557. @RequestMapping("/user/addcartitemquantity")
  558. public HashMap<String, Object> addCartitemQuantity(@RequestBody CartItem cartitem) {
  559. HashMap<String, Object> returnParams = new HashMap<String, Object>();
  560. try
  561. {
  562. int qty = cartitem.getQuantity();
  563. int productQty=cartitem.getProduct().getQuantity();
  564. System.out.println(productQty+":"+qty);
  565. if(qty>productQty){
  566. System.out.println("Quantity exceeded");
  567. returnParams.put("msg", "quantity exceeded");
  568. }
  569. else
  570. {
  571. /*Product product=cartitem.getProduct();
  572. CartItem productexist=cartItemRepository.findByProduct(product);
  573. if(productexist!=null){
  574. productexist.setQuantity(qty+1);
  575. }*/
  576. Customer customer= getCustomer();
  577. Cart cartcheck = customer.getCart();
  578. if(cartcheck != null)
  579. {
  580. int cartid = customer.getCart().getCartId();
  581. Cart cart = cartRepository.findOne(cartid);
  582. cartitem.setCart(cart);
  583. }
  584. cartItemRepository.save(cartitem);
  585. returnParams.put("status", true);
  586. }
  587. } catch (Exception e) {
  588. e.printStackTrace();
  589. returnParams.put("status", false);
  590. returnParams.put("msg", "Cart Addition Failed!");
  591. }
  592. return returnParams;
  593. }
  594. @RequestMapping("/user/addcart")
  595. public HashMap<String, Object> addCart(@RequestBody Cart cart) {
  596. HashMap<String, Object> returnParams = new HashMap<String, Object>();
  597. try
  598. {
  599. List<CartItem> cartitems = cart.getCartitem();
  600. Iterator<CartItem> it = cartitems.iterator();
  601. while(it.hasNext()){
  602. CartItem cartitem= (CartItem)it.next();
  603. /*Product product = cartitem.getProduct();
  604. product.setCartitem(cartitem);*/
  605. cartitem.setCart(cart);
  606. }
  607. cart.setCustomer(getCustomer());
  608. cartRepository.save(cart);
  609. returnParams.put("status", true);
  610. } catch (Exception e) {
  611. e.printStackTrace();
  612. returnParams.put("status", false);
  613. returnParams.put("msg", "Cart Addition Failed!");
  614. }
  615. return returnParams;
  616. }
  617. @RequestMapping("/user/calculatevat")
  618. public HashMap<String, Double> setShipping(@RequestBody ShippingAddress shippingaddress) {
  619. HashMap<String, Double> returnParams = new HashMap<String, Double>();
  620. Cart cart = getCustomer().getCart();
  621. cart.setShippingaddress(shippingaddress);
  622. cartRepository.save(cart);
  623. returnParams= calculateVat();
  624. return returnParams;
  625. }
  626. @RequestMapping("/user/getvat")
  627. public HashMap<String, Double> calculateVat() {
  628. HashMap<String, Double> returnParams = new HashMap<String, Double>();
  629. if(getCustomer().getCart()!=null)
  630. {
  631. Cart cart = getCustomer().getCart();
  632. if(cart.getShippingaddress()==null)
  633. {
  634. returnParams.put("status", 0.0);
  635. }
  636. if(cart.getShippingaddress()!=null)
  637. {
  638. ShippingAddress shippingaddress = cart.getShippingaddress();
  639. float total=cart.getTotalcost();
  640. String state = shippingaddress.getState();
  641. StateVat statevat= stateVatRepository.findBystate(state);
  642. StateVat country = stateVatRepository.findBystate("india");
  643. double cst = (country.getVatPercent()*total)/100;
  644. double vat= (statevat.getVatPercent()*total)/100;
  645. double finalprice = total+cst+vat;
  646. returnParams.put("cst", cst);
  647. returnParams.put("vat", vat);
  648. returnParams.put("finalprice", finalprice);
  649. }
  650. }
  651. return returnParams;
  652. }
  653. @RequestMapping("/user/placetheorder")
  654. public Object placeOrder() {
  655. HashMap<String, Object> returnStatus = new HashMap<String, Object>();
  656. try
  657. {
  658. Order order = new Order();
  659. Cart cart = getCustomer().getCart();
  660. List<CartItem> cartitems = cart.getCartitem();
  661. Iterator<CartItem> it = cartitems.iterator();
  662. while(it.hasNext()){
  663. CartItem cartitem= (CartItem)it.next();
  664. Product product = cartitem.getProduct();
  665. int productid = product.getProductId();
  666. String productname = product.getProductName();
  667. int quantity = cartitem.getQuantity();
  668. int productquantity = product.getQuantity();
  669. int updatedquantity = productquantity-quantity;
  670. if(updatedquantity==0){
  671. System.out.println("Product cannot be purchased");
  672. returnStatus.put("msg","Sorry!! Product Out of Stock");
  673. break;
  674. }
  675. product.setQuantity(updatedquantity);
  676. productRepository.save(product);
  677. float initialprice = cartitem.getInitialprice();
  678. OrderDetail orderdetail = new OrderDetail();
  679. orderdetail.setProductId(productid);
  680. orderdetail.setProductName(productname);
  681. orderdetail.setQuantity(quantity);
  682. orderdetail.setPrice(initialprice);
  683. orderRepository.save(order);
  684. orderdetail.setOrder(order);
  685. /* ProductImage productImage=(ProductImage) cartitem.getProduct().getProductimage();
  686. orderdetail.setProductImage(productImage);*/
  687. orderDetailRepository.save(orderdetail);
  688. }
  689. HashMap<String, Double> returnParams = calculateVat();
  690. order.setShippingAddress(cart.getShippingaddress());
  691. order.setCustomer(getCustomer());
  692. float totalcost = cart.getTotalcost();
  693. order.setCost(totalcost);
  694. Double cst = returnParams.get("cst");
  695. Double vat = returnParams.get("vat");
  696. float Tax = (float) (cst+vat);
  697. order.setTax(Tax);
  698. Double finalprice = returnParams.get("finalprice");
  699. float finalcost = finalprice.floatValue();
  700. order.setTotalCost(finalcost);
  701. orderRepository.save(order);
  702. cartRepository.delete(cart);
  703. returnStatus.put("status", true);
  704. // status = "order placed";
  705. } catch (Exception e){
  706. //status = "notallowed";
  707. returnStatus.put("status", false);
  708. }
  709. return returnStatus;
  710. }
  711. /*@RequestMapping("/user/removecartitem/{cartitemId}")
  712. public void removeCartitem(@PathVariable("cartitemId") int cartitemId) {
  713. //HashMap<String, Double> returnParams = new HashMap<String, Double>();
  714. cartItemRepository.delete(cartitemId);
  715. return;
  716. }*/
  717. @RequestMapping("/user/removecartitem")
  718. public void removeCartitem(@RequestBody CartItem cartitem) {
  719. //HashMap<String, Double> returnParams = new HashMap<String, Double>();
  720. int cartitemId = cartitem.getCartitemId();
  721. cartItemRepository.delete(cartitemId);
  722. return;
  723. }
  724. /*@RequestMapping("/public/user/verifytoken")
  725. public HashMap<String, Object> verufyToken(@RequestBody Customer customer){
  726. HashMap<String, Object> returnParams=new HashMap<String, Object>();
  727. int token=customer.getToken();
  728. String userName = SecurityContextHolder.getContext().getAuthentication().getName();
  729. Customer customer1= customerRepository.findByuserName(userName);
  730. int token1=customer1.getToken();
  731. if(token==token1){
  732. returnParams.put("msg", "Token matched succesfully");
  733. }
  734. else{
  735. returnParams.put("msg","Token didn't match" );
  736. }
  737. return returnParams;
  738. }
  739. @RequestMapping("/public/user/verifyemail")
  740. public HashMap<String, Object> verifyEmail(@RequestBody Customer customer){
  741. HashMap<String, Object> returnParams=new HashMap<String, Object>();
  742. Random generator = new Random();
  743. generator.setSeed(System.currentTimeMillis());
  744. int token = generator.nextInt(99999) + 99999;
  745. if (token < 100000 || token > 999999) {
  746. token = generator.nextInt(99999) + 99999;
  747. if (token < 100000 || token > 999999) {
  748. try {
  749. throw new Exception("Unable to generate PIN at this time..");
  750. } catch (Exception e) {
  751. // TODO Auto-generated catch block
  752. e.printStackTrace();
  753. }
  754. }
  755. }
  756. String email=customer.getEmailId();
  757. String ft=Integer.toString(token);
  758. String msg="Verification OTP is:";
  759. String finaltoken=msg.concat(ft);
  760. customer.setToken(token);
  761. customerRepository.save(customer);
  762. MailService mailService=new MailService();
  763. mailService.init(email, "Mail Verification", finaltoken);
  764. return (HashMap<String, Object>) returnParams.put("msg","Message sent Succesfully");
  765. }
  766. @RequestMapping("/public/user/changepassword")
  767. public HashMap<String, Object> savePassword(@RequestBody Customer customer){
  768. HashMap<String, Object> returnParams=new HashMap<String, Object>();
  769. String userName = SecurityContextHolder.getContext().getAuthentication().getName();
  770. customer= customerRepository.findByuserName(userName);
  771. String password=customer.getPassword();
  772. customer.setPassword(password);
  773. customerRepository.save(customer);
  774. return (HashMap<String, Object>) returnParams.put("msg","Password Succesfully Changed");
  775. }*/
  776. /*public void init(String to, String subject, String body){
  777. //Create the application context
  778. ApplicationContext context = new FileSystemXmlApplicationContext("src/main/resources/application-context.xml");
  779. //Get the mailer instance
  780. ApplicationMailer mailer = (ApplicationMailer) context.getBean("mailService");
  781. //Send a composed mail
  782. mailer.sendMail(to,"Mail Verification",body);
  783. //Send a pre-configured mail
  784. mailer.sendPreConfiguredMail("Mail succesfully sent");
  785. }*/
  786. }