package com.blueland.bpm.manage.config.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.blueland.bpm.common.base.BaseSpringController;
import com.blueland.bpm.common.page.Page;
import com.blueland.bpm.engine.config.model.ActivityRule;
import com.blueland.bpm.engine.config.service.ActivityRuleManager;
import com.blueland.bpm.engine.core.service.ActivityRuleService;
import com.blueland.bpm.manage.util.BaseUser;
/**
 * @author lw
 * @version 1.0
 * @since  
 */

@Controller
@RequestMapping(value = "/actInfo/activityResourceRule")
public class ActivityResourceRuleController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	@Autowired
	private ActivityRuleManager activityRuleManager;
	@Autowired
	private ActivityRuleService activityRuleService;
	private final String LIST_ACTION = "redirect:/actInfo/activityResourceRule/list.do";
	
	public ActivityResourceRuleController() {
	}
	

	/**
	 * 增加了@ModelAttribute的方法可以在本controller的方法调用前执行,可以存放一些共享变量,如枚举值
	 */
	@ModelAttribute
	public void init(ModelMap model) {
		//model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
	
	/** 
	 * 执行搜索 
	 **/
	@RequestMapping(value = "list")
	public ModelAndView list(HttpServletRequest request,ActivityRule query) {
		query.setRuleType(ActivityRule.RULE_TYPE_ACT_RES);
		Page page = this.activityRuleManager.findPage(query);
		ModelAndView result = new ModelAndView("/actInfo/activityResourceRule/list");
		result.addAllObjects(toModelMap(page, query));
		this.saveQueryParams(request, query);
		return result;
	}
	
	/** 
	 * 执行搜索 
	 **/
	@RequestMapping(value = "getActivityRule")
	@ResponseBody
	public ActivityRule getActivityRule(HttpServletRequest request,String ruleId) {
		ActivityRule activityRule=activityRuleManager.getById(ruleId);
		return activityRule;
	}

	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping(value = "create")
	public ModelAndView create(HttpServletRequest request,ActivityRule activityRule)  {
		activityRule.setRuleType(ActivityRule.RULE_TYPE_ACT_RES);
		return new ModelAndView("/actInfo/activityResourceRule/create","activityRule",activityRule);
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping(value = "save")
	public ModelAndView save(HttpServletRequest request,ActivityRule activityRule,RedirectAttributes redirectAttributes)  {
		activityRule.setRuleId("");
		//activityRule.setTenantId(BaseUser.getInstance().getLoginUserAppId(request));
		activityRule.setRuleType(ActivityRule.RULE_TYPE_ACT_RES);
		activityRuleManager.save(activityRule);
		redirectAttributes.addFlashAttribute(ACTION_MESSAGE, CREATED_SUCCESS);
		activityRule.clear();
		return create(request,activityRule);
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping(value = "update")
	public ModelAndView update(HttpServletRequest request,ActivityRule activityRule,RedirectAttributes redirectAttributes)  {
		activityRule.setRuleType(ActivityRule.RULE_TYPE_ACT_RES);
		if(StringUtils.isNotEmpty(activityRule.getRuleId())){
			activityRuleManager.update(activityRule);
		}
		return create(request,activityRule);
	}
	
	/**
	 *删除对象
	 **/
	@RequestMapping(value = "delete")
	public ModelAndView delete(String ruleId,RedirectAttributes redirectAttributes,HttpServletRequest request) {
		activityRuleManager.removeById(ruleId);
		redirectAttributes.addFlashAttribute(ACTION_MESSAGE, DELETE_SUCCESS);
		return new ModelAndView(LIST_ACTION+"?"+this.getQueryParams(request));
	}
	
	
	/**
	 *验证规则
	 **/
	@RequestMapping(value = "validate")
	public ModelAndView validate(String ruleScript,HttpServletRequest request) {
		String validateMsg="";
		String ruleScriptValueType="";
		Object ruleScriptValue=null;
		try{
			ruleScriptValue=activityRuleService.validateActivityRule(ruleScript);
			if(ruleScriptValue!=null){
				ruleScriptValueType=ruleScriptValue.getClass().toString();
			}
			validateMsg="脚本验证成功！";
		}catch(Exception e){
			String errorMsg=e.getMessage();
			validateMsg="脚本验证失败:"+(StringUtils.isNotEmpty(errorMsg)?errorMsg:e.getCause().getMessage());
		}
		ModelAndView result = new ModelAndView("/actInfo/activityResourceRule/showValidate");
		result.addObject("validateMsg",validateMsg);
		result.addObject("ruleScriptValue",ruleScriptValue);
		result.addObject("ruleScriptValueType",ruleScriptValueType);
		return result;
	}
	
}

