/*CopyRright (c)2014: <www.usoftchina.com>
 */
package com.uas.search.console.b2b.core.advice;

import java.sql.SQLRecoverableException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.uas.search.b2b.exception.SearchException;

/**
 * <p>
 * 基于Application的异常处理,以AOP的形式注册到SpringMVC的处理链
 * </p>
 * <p>
 * 正常的业务流程,只需抛出对应的异常和相关的信息
 * </P>
 * <p>
 * 不同的错误，对应不同的方法来处理
 * </p>
 * <p>
 * 可以用不同的HttpStatus来表示具体的异常类型
 * </p>
 * <p>
 * 客户端可以基于对应的HttpStatus Code做出最有利于自己的处理
 * </p>
 * 
 * @author yingp
 * 
 */
@ControllerAdvice
public class ExceptionHandlerAdvice {

	private final static Logger logger = LoggerFactory.getLogger(ExceptionHandlerAdvice.class);

	/**
	 * 处理运行时抛出异常
	 * 
	 * @param ex
	 * @return
	 */
	@ExceptionHandler(RuntimeException.class)
	public ResponseEntity<String> handleUnexpectedServerError(RuntimeException ex) {
		logger.error("RuntimeException", ex);
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/text; charset=utf-8");
		ex.printStackTrace();
		return new ResponseEntity<String>("\u7CFB\u7EDF\u9519\u8BEF", headers, HttpStatus.INTERNAL_SERVER_ERROR);
	}

	/**
	 * 处理连接池的连接失效抛出异常
	 * 
	 * @see SQLRecoverableException
	 * @param ex
	 * @return
	 */
	@ExceptionHandler(SQLRecoverableException.class)
	public ResponseEntity<String> handleSQLRecoverableException(SQLRecoverableException ex) {
		logger.error("SQLRecoverableException", ex);
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/text; charset=utf-8");
		return new ResponseEntity<String>("\u7CFB\u7EDF\u9519\u8BEF", headers, HttpStatus.INTERNAL_SERVER_ERROR);
	}

	/**
	 * 处理参数错误抛出异常
	 * 
	 * @see IllegalArgumentException
	 * @param ex
	 * @return
	 */
	@ExceptionHandler(IllegalArgumentException.class)
	public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException ex) {
		logger.error("IllegalArgumentException", ex);
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/text; charset=utf-8");
		return new ResponseEntity<String>("\u53C2\u6570\u9519\u8BEF", headers, HttpStatus.INTERNAL_SERVER_ERROR);
	}

	/**
	 * 处理已捕获异常，明确传达给客户端错误码、错误信息
	 * 
	 * @see SearchException
	 * @param ex
	 * @return
	 */
	@ExceptionHandler(SearchException.class)
	public ResponseEntity<ModelMap> handleSearchException(SearchException ex) {
		if (!StringUtils.isEmpty(ex.getDetailedMessage())) {
			logger.error(ex.getDetailedMessage());
		} else {
			logger.error(ex.getMessage());
		}
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/json; charset=utf-8");
		ModelMap map = new ModelMap();
		map.put("success", false);
		map.put("message", ex.getMessage());
		return new ResponseEntity<ModelMap>(map, headers, HttpStatus.BAD_REQUEST);
	}

}
