Package com.jpoweredcart.admin.controller.localisation

Source Code of com.jpoweredcart.admin.controller.localisation.CurrencyAdminController

package com.jpoweredcart.admin.controller.localisation;

import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.jpoweredcart.admin.form.localisation.CurrencyForm;
import com.jpoweredcart.admin.model.localisation.CurrencyAdminModel;
import com.jpoweredcart.common.BaseController;
import com.jpoweredcart.common.PageParam;
import com.jpoweredcart.common.entity.localisation.Currency;
import com.jpoweredcart.common.exception.admin.UnauthorizedAdminException;
import com.jpoweredcart.common.security.UserPermissions;
import com.jpoweredcart.common.view.Pagination;


@Controller
@RequestMapping(value="/admin/localisation/currency")
public class CurrencyAdminController extends BaseController {
 
  @Inject
  private CurrencyAdminModel currencyAdminModel;
 
  @RequestMapping(value={"", "/"})
  public String index(Model model, HttpServletRequest request){
   
    PageParam pageParam = createPageParam(request);
    List<Currency> currencyList = currencyAdminModel.getList(pageParam);
    model.addAttribute("currencies", currencyList);
   
    int total = currencyAdminModel.getTotal();
    Pagination pagination = new Pagination();
    pagination.setTotal(total).setPageParam(pageParam)
      .setText(message(request, "text.pagination"))
      .setUrl(uri("/admin/localisation/currency"));
    model.addAttribute("pagination", pagination.render());
   
    return "/admin/localisation/currencyList";
  }
 
  @RequestMapping(value="/create")
  public String create(Model model){
   
    checkModifyPermission();
   
    model.addAttribute("currencyForm", currencyAdminModel.newForm());
   
    return "/admin/localisation/currencyForm";
  }
 
  @RequestMapping(value="/edit/{id}")
  public String edit(@PathVariable("id") Integer id, Model model){
   
    checkModifyPermission();
   
    CurrencyForm currencyForm = currencyAdminModel.getForm(id);
    model.addAttribute("currencyForm", currencyForm);
    return "/admin/localisation/currencyForm";
  }
 
  @RequestMapping(value="/save", method=RequestMethod.POST)
  public String save(@Valid CurrencyForm currencyForm, BindingResult result, Model model,
      RedirectAttributes redirect){
   
    checkModifyPermission();
   
    if(result.hasErrors()){
      model.addAttribute("currencyForm", currencyForm);
      return "/admin/localisation/currencyForm";
    }
   
    if(currencyForm.getId()!=null){
      currencyAdminModel.update(currencyForm);
    }else{
      currencyAdminModel.create(currencyForm);
    }
   
    redirect.addFlashAttribute("msg_success", "text.success");
   
    return "redirect:/admin/localisation/currency";
  }
 
  @RequestMapping(value="/delete", method=RequestMethod.POST)
  public String delete(@RequestParam(value="selected", required=false) Integer[] ids, Model model,
      RedirectAttributes redirect){
    checkModifyPermission();
    boolean error = false;
    if(ids!=null){
      if(!error) for(Integer id: ids){
        currencyAdminModel.delete(id);
      }
      if(!error) redirect.addFlashAttribute("msg_success", "text.success");
    }
   
    return "redirect:/admin/localisation/currency";
  }
 
  private void checkModifyPermission(){
    UserPermissions.checkModify("localisation/currency",
        new UnauthorizedAdminException());
  }
 
}
TOP

Related Classes of com.jpoweredcart.admin.controller.localisation.CurrencyAdminController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.