net.shopxx.controller.admin.SpecificationController.java Source code

Java tutorial

Introduction

Here is the source code for net.shopxx.controller.admin.SpecificationController.java

Source

/*
 * Copyright 2005-2015 shopxx.net. All rights reserved.
 * Support: http://3936242.01p.com/
 * License: http://3936242.01p.com/license
 */
package net.shopxx.controller.admin;

import javax.annotation.Resource;

import net.shopxx.Message;
import net.shopxx.Pageable;
import net.shopxx.entity.BaseEntity;
import net.shopxx.entity.Specification;
import net.shopxx.service.ProductCategoryService;
import net.shopxx.service.SpecificationService;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.functors.AndPredicate;
import org.apache.commons.collections.functors.UniquePredicate;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller("adminSpecificationController")
@RequestMapping("/admin/specification")
public class SpecificationController extends BaseController {

    @Resource(name = "specificationServiceImpl")
    private SpecificationService specificationService;
    @Resource(name = "productCategoryServiceImpl")
    private ProductCategoryService productCategoryService;

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Long sampleId, ModelMap model) {
        model.addAttribute("sample", specificationService.find(sampleId));
        model.addAttribute("productCategoryTree", productCategoryService.findTree());
        return "/admin/specification/add";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Specification specification, Long productCategoryId, RedirectAttributes redirectAttributes) {
        CollectionUtils.filter(specification.getOptions(), new AndPredicate(new UniquePredicate(), new Predicate() {
            public boolean evaluate(Object object) {
                String option = (String) object;
                return StringUtils.isNotEmpty(option);
            }
        }));
        specification.setProductCategory(productCategoryService.find(productCategoryId));
        if (!isValid(specification, BaseEntity.Save.class)) {
            return ERROR_VIEW;
        }
        specificationService.save(specification);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.jhtml";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        model.addAttribute("productCategoryTree", productCategoryService.findTree());
        model.addAttribute("specification", specificationService.find(id));
        return "/admin/specification/edit";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Specification specification, RedirectAttributes redirectAttributes) {
        CollectionUtils.filter(specification.getOptions(), new AndPredicate(new UniquePredicate(), new Predicate() {
            public boolean evaluate(Object object) {
                String option = (String) object;
                return StringUtils.isNotEmpty(option);
            }
        }));
        if (!isValid(specification)) {
            return ERROR_VIEW;
        }
        specificationService.update(specification, "productCategory");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.jhtml";
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model) {
        model.addAttribute("page", specificationService.findPage(pageable));
        return "/admin/specification/list";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody Message delete(Long[] ids) {
        specificationService.delete(ids);
        return SUCCESS_MESSAGE;
    }

}