Subversion Repositories SmartDukaan

Rev

Rev 27693 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.CustomAddress;
import com.spice.profitmandi.common.model.CustomCustomer;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.fofo.Customer;
import com.spice.profitmandi.dao.entity.fofo.CustomerAddress;
import com.spice.profitmandi.dao.repository.catalog.CustomerOfferRepository;
import com.spice.profitmandi.dao.repository.fofo.CustomerAddressRepository;
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
import com.spice.profitmandi.service.CustomerService;

@Controller
@Transactional
public class CustomerController {

        private static final Logger LOGGER = LogManager.getLogger(CustomerController.class);

        @Autowired
        private ResponseSender<?> responseSender;

        @Autowired
        private CustomerRepository customerRepository;

        @Autowired
        private CustomerService customerService;

        @Autowired
        private CustomerAddressRepository customerAddressRepository;

        @RequestMapping(value = "/customer/address", method = RequestMethod.POST)
        public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestParam int customerId,
                        @RequestBody CustomAddress customAddress) {
                CustomerAddress customerAddress = this.toCustomerAddress(customerId, customAddress);
                customerAddressRepository.persist(customerAddress);
                return responseSender.ok(this.toCustomAddress(customerAddress));

        }

        @RequestMapping(value = "/customer/update/address", method = RequestMethod.POST)
        public ResponseEntity<?> updateAddress(HttpServletRequest request, @RequestBody CustomAddress customAddress)
                        throws ProfitMandiBusinessException {
                CustomerAddress customerAddress = customerAddressRepository.selectById(customAddress.getId());

                customerAddress.setCity(customAddress.getCity());
                customerAddress.setCountry(customAddress.getCountry());
                customerAddress.setLandmark(customAddress.getLandmark());
                customerAddress.setLine1(customAddress.getLine1());
                customerAddress.setLine2(customAddress.getLine2());
                customerAddress.setName(customAddress.getName());
                customerAddress.setLastName(customAddress.getLastName());
                customerAddress.setPhoneNumber(customAddress.getPhoneNumber());
                customerAddress.setPinCode(customAddress.getPinCode());
                customerAddress.setState(customAddress.getState());
                return responseSender.ok(this.toCustomAddress(customerAddress));

        }

        @RequestMapping(value = "/customer/add", method = RequestMethod.POST)
        public ResponseEntity<?> addCustomer(HttpServletRequest request, @RequestBody CustomCustomer customCustomer)
                        throws ProfitMandiBusinessException {
                Customer customer = new Customer();
                if (StringUtils.isEmpty(customCustomer.getFirstName())) {
                        throw new ProfitMandiBusinessException("First Name", "Empty", "First Name required");
                }
                customer.setEmailId(customCustomer.getEmailId());
                customer.setFirstName(customCustomer.getFirstName());
                customer.setLastName(customCustomer.getLastName());
                customer.setMobileNumber(customCustomer.getMobileNumber());
                customer = customerService.addCustomer(customer);
                customCustomer.setCustomerId(customer.getId());
                return responseSender.ok(customCustomer);

        }

        @RequestMapping(value = "/customer/add-email", method = RequestMethod.POST)
        public ResponseEntity<?> addEmail(HttpServletRequest request, @RequestBody CustomCustomer customCustomer)
                        throws ProfitMandiBusinessException {
                String emailId = customCustomer.getEmailId();
                int customerId = customCustomer.getCustomerId();
                Customer customer = customerRepository.selectById(customerId);
                customer.setEmailId(emailId);
                return this.getCustomerByMobileNumber(request, customer.getMobileNumber());

        }

        @RequestMapping(value = "/customer/mobileNumber", method = RequestMethod.GET)
        public ResponseEntity<?> getCustomerByMobileNumber(HttpServletRequest request,
                        @RequestParam(name = ProfitMandiConstants.MOBILE_NUMBER) String mobileNumber)
                        throws ProfitMandiBusinessException {
                CustomCustomer customCustomer = null;
                LOGGER.info("Request Received at url {}", request.getRequestURI());
                try {
                        Customer customer = customerRepository.selectByMobileNumber(mobileNumber);
                        customCustomer = new CustomCustomer();
                        customCustomer.setCustomerId(customer.getId());
                        customCustomer.setEmailId(customer.getEmailId());
                        customCustomer.setFirstName(customer.getFirstName());
                        customCustomer.setLastName(customer.getLastName());
                        customCustomer.setMobileNumber(customer.getMobileNumber());
                        LOGGER.info(customer.getCustomerAddress());
                        List<CustomerAddress> customerAddresses = customer.getCustomerAddress().stream()
                                        .sorted((CustomerAddress c1, CustomerAddress c2) -> {
                                                return c1.getCreateTimestamp().isBefore(c2.getCreateTimestamp()) ? 1 : -1;
                                        }).limit(5).collect(Collectors.toList());
                        LOGGER.info(customerAddresses);
                        if (!customerAddresses.isEmpty()) {
                                List<CustomAddress> customAddresses = new ArrayList<>();
                                for (CustomerAddress customerAddress : customerAddresses) {
                                        customAddresses.add(this.toCustomAddress(customerAddress));
                                }
                                customCustomer.setAddresses(customAddresses);
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return responseSender.ok(customCustomer);
        }

        private CustomAddress toCustomAddress(CustomerAddress customerAddress) {
                CustomAddress customAddress = new CustomAddress();
                customAddress.setCity(customerAddress.getCity());
                customAddress.setCountry(customerAddress.getCountry());
                customAddress.setLandmark(customerAddress.getLandmark());
                customAddress.setLine1(customerAddress.getLine1());
                customAddress.setLine2(customerAddress.getLine2());
                customAddress.setName(customerAddress.getName());
                customAddress.setLastName(customerAddress.getLastName());
                customAddress.setPhoneNumber(customerAddress.getPhoneNumber());
                customAddress.setPinCode(customerAddress.getPinCode());
                customAddress.setState(customerAddress.getState());
                customAddress.setId(customerAddress.getId());
                return customAddress;
        }

        private CustomerAddress toCustomerAddress(int customerId, CustomAddress customAddress) {
                CustomerAddress customerAddress = new CustomerAddress();
                customerAddress.setCustomerId(customerId);
                customerAddress.setName(customAddress.getName());
                customerAddress.setLastName(customAddress.getLastName());
                customerAddress.setLine1(customAddress.getLine1());
                customerAddress.setLine2(customAddress.getLine2());
                customerAddress.setLandmark(customAddress.getLandmark());
                customerAddress.setCity(customAddress.getCity());
                customerAddress.setPinCode(customAddress.getPinCode());
                customerAddress.setState(customAddress.getState());
                customerAddress.setCountry(customAddress.getCountry());
                customerAddress.setPhoneNumber(customAddress.getPhoneNumber());

                return customerAddress;
        }
}