net.groupbuy.controller.admin.BaseController.java Source code

Java tutorial

Introduction

Here is the source code for net.groupbuy.controller.admin.BaseController.java

Source

/*
 * Copyright 2005-2013 shopxx.net. All rights reserved.
 * Support: http://www.shopxx.net
 * License: http://www.shopxx.net/license
 */
package net.groupbuy.controller.admin;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Set;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import net.groupbuy.DateEditor;
import net.groupbuy.Message;
import net.groupbuy.Setting;
import net.groupbuy.entity.Log;
import net.groupbuy.template.directive.FlashMessageDirective;
import net.groupbuy.util.SettingUtils;
import net.groupbuy.util.SpringUtils;

import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * Controller - 
 * 
 * @author SHOP++ Team
 * @version 3.0
 */
public class BaseController {

    /**  */
    protected static final String ERROR_VIEW = "/admin/common/error";

    /** ? */
    protected static final Message ERROR_MESSAGE = Message.error("admin.message.error");

    /** ?? */
    protected static final Message SUCCESS_MESSAGE = Message.success("admin.message.success");

    /** "?"??? */
    private static final String CONSTRAINT_VIOLATIONS_ATTRIBUTE_NAME = "constraintViolations";

    @Resource(name = "validator")
    private Validator validator;

    /**
     * ?
     * 
     * @param binder
     *            WebDataBinder
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
        binder.registerCustomEditor(Date.class, new DateEditor(true));
    }

    /**
     * ??
     * 
     * @param target
     *            ?
     * @param groups
     *            ?
     * @return ?
     */
    protected boolean isValid(Object target, Class<?>... groups) {
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(target, groups);
        if (constraintViolations.isEmpty()) {
            return true;
        } else {
            RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
            requestAttributes.setAttribute(CONSTRAINT_VIOLATIONS_ATTRIBUTE_NAME, constraintViolations,
                    RequestAttributes.SCOPE_REQUEST);
            return false;
        }
    }

    /**
     * ??
     * 
     * @param type
     *            
     * @param property
     *            
     * @param value
     *            
     * @param groups
     *            ?
     * @return ?
     */
    protected boolean isValid(Class<?> type, String property, Object value, Class<?>... groups) {
        Set<?> constraintViolations = validator.validateValue(type, property, value, groups);
        if (constraintViolations.isEmpty()) {
            return true;
        } else {
            RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
            requestAttributes.setAttribute(CONSTRAINT_VIOLATIONS_ATTRIBUTE_NAME, constraintViolations,
                    RequestAttributes.SCOPE_REQUEST);
            return false;
        }
    }

    /**
     * ??
     * 
     * @param amount
     *            ?
     * @param showSign
     *            
     * @param showUnit
     *            ??
     * @return ??
     */
    protected String currency(BigDecimal amount, boolean showSign, boolean showUnit) {
        Setting setting = SettingUtils.get();
        String price = setting.setScale(amount).toString();
        if (showSign) {
            price = setting.getCurrencySign() + price;
        }
        if (showUnit) {
            price += setting.getCurrencyUnit();
        }
        return price;
    }

    /**
     * ??
     * 
     * @param code
     *            ?
     * @param args
     *            ?
     * @return ?
     */
    protected String message(String code, Object... args) {
        return SpringUtils.getMessage(code, args);
    }

    /**
     * ?
     * 
     * @param redirectAttributes
     *            RedirectAttributes
     * @param message
     *            ?
     */
    protected void addFlashMessage(RedirectAttributes redirectAttributes, Message message) {
        if (redirectAttributes != null && message != null) {
            redirectAttributes.addFlashAttribute(FlashMessageDirective.FLASH_MESSAGE_ATTRIBUTE_NAME, message);
        }
    }

    /**
     * 
     * 
     * @param content
     *            
     */
    protected void addLog(String content) {
        if (content != null) {
            RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
            requestAttributes.setAttribute(Log.LOG_CONTENT_ATTRIBUTE_NAME, content,
                    RequestAttributes.SCOPE_REQUEST);
        }
    }

}