com.matel.service.impl.OrderServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.matel.service.impl.OrderServiceImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.matel.service.impl;

import java.io.Serializable;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.matel.components.BillingInformation;
import com.matel.dao.OrderDAO;
import com.matel.data.ShoppingCartData;
import com.matel.data.ShoppingCartItemData;
import com.matel.domain.OrderProduct;
import com.matel.domain.Orders;
import com.matel.domain.UserAddress;
import com.matel.domain.UserPayment;
import com.matel.domain.UserProfile;
import com.matel.service.OrderService;
import com.matel.utils.SpringComponent;

/**
 *
 * @author badri
 */
@Service(OrderServiceImpl.SERVICE_NAME)
@Scope(SpringComponent.SCOPE_REQUEST)
public class OrderServiceImpl implements OrderService, Serializable {
    private static final long serialVersionUID = 1L;
    public static final String SERVICE_NAME = "service_impl_orderServiceImpl";

    @Autowired
    OrderDAO orderDAO;

    @Override
    public Orders createNewOrder(ShoppingCartData cartData, String salesReceiptId) {
        Orders order = null;
        if (null != cartData) {
            UserPayment uPayment = new UserPayment();
            order = new Orders();

            order = prepareNewOrder(cartData, order);
            order.setPaymentId(Integer.parseInt(salesReceiptId));

            if (cartData.getBillingInfo() != null) {
                UserAddress billingAddress = prepareBillingAddress(cartData.getBillingInfo(),
                        cartData.getCustomer());
                billingAddress = orderDAO.saveBillingAddress(billingAddress);
                order.setBillingAddressId(billingAddress);
            }
            if (cartData.getShippingInfo() != null) {
                UserAddress shippingAddress = prepareShippingAddress(cartData.getShippingInfo(),
                        cartData.getCustomer());
                shippingAddress = orderDAO.saveShippingAddress(shippingAddress);
                order.setShippingAddressId(shippingAddress);
            }
            if (cartData.getCardInfo() != null) {
                String cCard = cartData.getCardInfo().getCreditCard();
                byte[] cCardBytes = cCard.getBytes(StandardCharsets.UTF_8);
                uPayment.setCreditCardNumber(Base64.getEncoder().encodeToString(cCardBytes)); //Encode Credit Card

                /*
                 * To Decode Credit Card use the following code snippet
                 * 
                    byte[] ccDecodeBytes = Base64.getDecoder().decode(uPayment.getCreditCardNumber()); 
                   String cCardDecoded = new String(ccDecodeBytes, StandardCharsets.UTF_8);
                */
                uPayment.setCardType(cartData.getCardInfo().getCardType());
                uPayment.setNameOnTheCard(cartData.getCardInfo().getCardHolderName());
            }
            order.setComments(cartData.getShippingInfo().getInstructions());
            order.setShippingAmount(cartData.getShippingMethodData().getShippingCharges());
            order = orderDAO.saveOrder(order);
            uPayment.setOrderId(order);
            uPayment = orderDAO.saveUserPayment(uPayment);
            saveOrderProducts(order, cartData);
        }
        return order;
    }

    private Orders prepareNewOrder(ShoppingCartData cartData, Orders order) {
        order.setTotalAmount(cartData.getTotal());
        order.setOrderDate(new Date());
        order.setUserId(cartData.getCustomer());
        return order;
    }
    /* 
     private Set<OrderProduct> prepareSaveOrderProduct(Orders order, ShoppingCartData cartData) {
    Set<OrderProduct> orderProducts = new HashSet<>();
    for (ShoppingCartItemData cartItem : cartData.getCartItems()) {
        OrderProduct product = new OrderProduct();
        product.setAmount(cartItem.getProductPrice().multiply(new BigDecimal(cartItem.getQuantity())));
        product.setProductId(cartItem.getProduct());
        product.setQuantity(cartItem.getQuantity());
        product.setUnitPrice(cartItem.getProductPrice());
        product.setProductDescription(cartItem.getName());
        product.setOrderId(order);
       // orderDAO.saveOrderProduct(product);
        orderProducts.add(product);
    }
    return orderProducts;
        // order.setOrderProductSet(orderProducts);
     }*/

    private void saveOrderProducts(Orders order, ShoppingCartData cartData) {
        Set<OrderProduct> orderProducts = new HashSet<>();
        for (ShoppingCartItemData cartItem : cartData.getCartItems()) {
            OrderProduct product = new OrderProduct();
            product.setAmount(cartItem.getProductPrice().multiply(new BigDecimal(cartItem.getQuantity())));
            product.setProductId(cartItem.getProduct());
            product.setQuantity(cartItem.getQuantity());
            product.setUnitPrice(cartItem.getProductPrice());
            product.setProductDescription(cartItem.getName());
            product.setOrderId(order);
            orderDAO.saveOrderProduct(product);
            orderProducts.add(product);
        }
        order.setOrderProductSet(orderProducts);
    }

    private UserAddress prepareBillingAddress(BillingInformation billingInfo, UserProfile userProfile) {
        UserAddress billingAddress = new UserAddress();
        billingAddress.setAddress1(billingInfo.getAddress());
        billingAddress.setAddressType("B");
        billingAddress.setCity(billingInfo.getCity());
        billingAddress.setState(billingInfo.getState());
        billingAddress.setZip(billingInfo.getPostalCode());
        billingAddress.setPhone(billingInfo.getTelePhone());
        if (billingInfo.getFax() != null && !billingInfo.getFax().isEmpty()) {
            billingAddress.setFax(billingInfo.getFax());
        }
        billingAddress.setEmail(billingInfo.getEmail());
        billingAddress.setUserId(userProfile);
        return billingAddress;

    }

    private UserAddress prepareShippingAddress(BillingInformation shippingInfo, UserProfile userProfile) {
        UserAddress shippingAddress = new UserAddress();
        shippingAddress.setAddress1(shippingInfo.getAddress());
        shippingAddress.setAddressType("S");
        shippingAddress.setCity(shippingInfo.getCity());
        shippingAddress.setState(shippingInfo.getState());
        shippingAddress.setZip(shippingInfo.getPostalCode());
        shippingAddress.setPhone(
                shippingInfo.getPhoneNumber1() + shippingInfo.getPhoneNumber2() + shippingInfo.getPhoneNumber3());
        if (shippingInfo.getFax() != null && !shippingInfo.getFax().isEmpty()) {
            shippingAddress.setFax(shippingInfo.getFax());
        }
        shippingAddress.setEmail(shippingInfo.getEmail());
        shippingAddress.setUserId(userProfile);
        return shippingAddress;

    }

}