0
@Controller
public class CustomerController {
    @Autowired
    private CustomerBS customerBS;

    @Autowired
    private CustomerExtService customerExt;

    /**
    * This method fetches CustomerCategory details
    * @Method getCustomerCategory 
    * @param  request
    * @return categoryMap
    * @throws BusinessException
    */
    @ModelAttribute("categoryMap")  
    public  Map<Integer, String> getCustomerCategory(HttpServletRequest request) throws BusinessException{
        HttpSession session=request.getSession();
        UserDetails userDetails=(UserDetails) session.getAttribute(CollaborationConstants.USERSESSION);
        Map<Integer, String> categoryMap= customerBS.fetchCategoryList(userDetails.getTenantId(),userDetails.getLocaleId());
        return categoryMap;
    }

    /**
       * This method fetches CustomerType details
       * @Method getCustomerType 
       * @param  request
       * @return customer type
       * @throws BusinessException
     */
    @ModelAttribute(value = "custType")
    public Map<Integer, String> getCustomerType(HttpServletRequest request)
            throws BusinessException {
        HttpSession session = request.getSession();
        UserDetails userDetails = (UserDetails) session
                .getAttribute(CollaborationConstants.USERSESSION);
        Map<Integer, String> customertype = customerBS
                .fetchCustomerType(userDetails.getTenantId(), userDetails.getLocaleId());
        return customertype;
    }

    /**
     * This method fetches Customer SearchView details
     * @Method customerSearchView
     * @param  customer
     * @throws BusinessException
     * @return search customer
     */
    @PreAuthorize("hasAnyRole('CUSTOMER MANAGEMENT_SEARCH')")
    @RequestMapping(value="/searchCustomerView.do")
    public String customerSearchView(@ModelAttribute("customer") Customer customer) throws BusinessException{
        return "customer/searchcustomer";
    }

    /**
     * This method  Create Retail CustomerView 
     * @Method createRetailCustomerView
     * @param  customer
     * @param  categoryName
     * @param  categoryId
     * @return ModelAndView
     */
    @PreAuthorize("hasRole('CUSTOMER MANAGEMENT_CREATE')")
    @RequestMapping(value="/createCustomerView.do")
    public ModelAndView createRetailCustomerView(@ModelAttribute("customer") Customer customer, @RequestParam String categoryName,@RequestParam String categoryId){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("categoryName", categoryName);
        modelAndView.addObject("categoryId", categoryId);
        modelAndView.addObject("addressList", new ArrayList());
        modelAndView.setViewName("customer/createcustomer");
        return modelAndView;
    }

    /**
     * This method  fetch List of Customers details
     * @Method fetchCustomerList
     * @param customer
     * @param customerCode
     * @param customerName
     * @param categoryId
     * @param session
     * @throws CustomerBusinessException
     * @return List of Customers
     */
    @PreAuthorize("hasAnyRole('CUSTOMER MANAGEMENT_SEARCH','CUSTOMER MANAGEMENT_VIEW')")
    @RequestMapping(value = "fetchCustomerList.do")
        public @ResponseBody JsonResult fetchCustomerList(@ModelAttribute("customer") Customer customer,@RequestParam("custCode") String customerCode, @RequestParam("custName") String customerName, @RequestParam("categoryId") Integer categoryId, HttpSession session) throws CustomerBusinessException {
        UserDetails userDetails = (UserDetails) session.getAttribute(CollaborationConstants.USERSESSION);
            JsonResult jsonResult = new JsonResult();
           List<Customer> customerList= customerBS.fetchCustomerList( customer ,customerCode, customerName,userDetails,categoryId, 0, -1);
           jsonResult.setResult(customerList);
            jsonResult.setPage(customer.getPage());
            jsonResult.setTotal(customer.getTotalRows());
            jsonResult.setRecords(customer.getTotalRecords());
            // jsonResult.setRows(saemployee.getRows());

            jsonResult.setResult(customerList);
            return  jsonResult;
     }

    /**
     * This method  fetch List of Country details
     * @Method fetchCountryList
     * @param  session
     * @throws CustomerBusinessException
     * @return List of Country
     */
    @PreAuthorize("hasAnyRole('CUSTOMER MANAGEMENT_CREATE','CUSTOMER MANAGEMENT_EDIT')")
     @RequestMapping(value="fetchCountries.do")
     public @ResponseBody JsonResult fetchCountryList(HttpSession session) throws CustomerBusinessException{
         JsonResult jsonResult = new JsonResult();
        UserDetails userDetails = (UserDetails) session.getAttribute(CollaborationConstants.USERSESSION);
        List<Country> countryList= customerBS.fetchCountryList(userDetails.getTenantId(), userDetails.getLocaleId());
        jsonResult.setResult(countryList);
        return jsonResult;
     }


    /**
     * This method  fetch List of AddressType details
     * @Method fetchAddressTypeList
     * @param  request
     * @throws CustomerBusinessException
     * @return List of AddressType
     */
    @PreAuthorize("hasAnyRole('CUSTOMER MANAGEMENT_CREATE','CUSTOMER MANAGEMENT_EDIT')")
    @RequestMapping(value="fetchAddressTypeList.do")
     public @ResponseBody JsonResult fetchAddressTypeList(HttpServletRequest request) throws CustomerBusinessException{
         JsonResult jsonResult = new JsonResult();
         HttpSession session = request.getSession();
         UserDetails userDetails = (UserDetails) session
                    .getAttribute(CollaborationConstants.USERSESSION);
        List<AddressType> addressTypeList= customerBS.fetchAddressTypeList(userDetails.getTenantId(), userDetails.getLocaleId());
        jsonResult.setResult(addressTypeList);
        return jsonResult;
     }


    /**
     * This method View Customer Details 
     * @Method viewCustomerDetails
     * @param  customerId
     * @param  categoryName
     * @param  request
     * @throws BusinessException
     * @return ModelAndView
     */
    @PreAuthorize("hasAnyRole('CUSTOMER MANAGEMENT_VIEW','CUSTOMER MANAGEMENT_EDIT')")
    @RequestMapping(value="viewCustomerDetails.do")
    public ModelAndView viewCustomerDetails(@RequestParam("custId") Integer customerId, @RequestParam("categoryName") String categoryName, HttpServletRequest request) throws BusinessException{
        HttpSession session = request.getSession();
        UserDetails userDetails = (UserDetails) session
                .getAttribute(CollaborationConstants.USERSESSION);
        Map<String, Integer> alignmentList = customerExt.fetchAlgmntTemplates(userDetails, customerId);
        Customer customerDetails=customerBS.fetchCustomerDetails(customerId,categoryName,0,-1);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("customer", customerDetails);
        modelAndView.addObject("alignmentList", alignmentList);
        if(customerDetails.getAddressList()!=null)
        {
        modelAndView.addObject("addressList", JSONUtil.toJSON(customerDetails.getAddressList()));
        }
        else
        {
            modelAndView.addObject("addressList",new ArrayList());
        }
        modelAndView.setViewName("customer/viewcustomerwithalignment");
        return modelAndView;
    }

    /**
     * This method fetch Customer Details
     * @Method fetchCustomerDetails
     * @param  customerId
     * @param  categoryName
     * @param  request
     * @throws BusinessException
     * @return ModelAndView
     */
    @PreAuthorize("hasRole('CUSTOMER MANAGEMENT_EDIT')")
    @RequestMapping(value = "/editCustomerView.do", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView fetchCustomerDetails(@RequestParam("custId") Integer customerId, @RequestParam("categoryName") String categoryName, HttpServletRequest request)
            throws BusinessException {
        HttpSession session = request.getSession();
        UserDetails userDetails = (UserDetails) session
                .getAttribute(CollaborationConstants.USERSESSION);
        Map<String, Integer> alignmentList = customerExt.fetchAlgmntTemplates(userDetails, customerId);
        Customer customerDetails=customerBS.fetchCustomerDetails(customerId,categoryName,0,-1);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("customer", customerDetails);
        modelAndView.addObject("alignmentList", alignmentList);
        if(customerDetails.getAddressList()!=null)
        {
        modelAndView.addObject("addressList", JSONUtil.toJSON(customerDetails.getAddressList()));
        }
        else
        {
            modelAndView.addObject("addressList",new ArrayList());
        }
        modelAndView.setViewName("customer/modifycustomer");
        return modelAndView;
    }


    /**
      * This method Add Customer
      * @Method addCustomer
      * @param  customer
      * @param  request
      * @param  session
      * @throws BusinessException
      * @return ModelAndView
      */
     @PreAuthorize("hasRole('CUSTOMER MANAGEMENT_CREATE')")
     @RequestMapping(value = "/addCustomer.do", method = RequestMethod.POST)
    public ModelAndView addCustomer(@ModelAttribute("customer") Customer customer, HttpServletRequest request, HttpSession session)
            throws BusinessException {
        UserDetails userDetails = (UserDetails) session.getAttribute(CollaborationConstants.USERSESSION);
        if((customer.getCustId()==null)){
         customer =customerBS.addCustomer(customer,userDetails);
         ModelAndView modelAndView=new ModelAndView();
         modelAndView.addObject("customer", customer);
         modelAndView.addObject("categoryName", customer.getCategoryName());
         modelAndView.addObject("categoryId", customer.getCategoryId());
         if(customer.getAddressList()!=null)
         {
         modelAndView.addObject("addressList", JSONUtil.toJSON(customer.getAddressList()));

         }else
         {
             modelAndView.addObject("addressList", new ArrayList());     
         }
         if(customer.getCustCodeFlag()!=null && customer.getCustCodeFlag().equals("Y"))
         {
             modelAndView.setViewName("customer/createcustomer");
         }else
          modelAndView.setViewName("customer/viewcustomer");

        return modelAndView;
        }
        else{
            customerBS.updateCustomer(customer,userDetails);
            customer=customerBS.fetchCustomerDetails(customer.getCustId(),customer.getCategoryName(),0,-1);
        }
         ModelAndView modelAndView=new ModelAndView();
         modelAndView.addObject("customer", customer);
         if(customer.getAddressList()!=null)
         {
         modelAndView.addObject("addressList", JSONUtil.toJSON(customer.getAddressList()));
         }else
            {
                modelAndView.addObject("addressList",new ArrayList());
            }
         modelAndView.setViewName("customer/viewcustomer");
        return modelAndView;
    }




}

when i was going through some code, got some confusion in @ModelAttribute annotation.After googeling it i come to know why this annotation is being used and it is used by two types. but here @ModelAttribute("categoryMap") is places before getCustomerCategory().It be called before any controller get called but where we are calling that method in our code i am failed to figure it out.If we are not using that method in our code then what is the use of this method.Is this any programmer's fault or behind the screen we are using that method some where else.

i can share other code as needed ,in case please let me know.

Piyush Mittal
  • 1,860
  • 1
  • 21
  • 39

0 Answers0