package com.blueland.bpm.manage.web;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.Gateway;
import org.activiti.bpmn.model.ScriptTask;
import org.activiti.bpmn.model.SequenceFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.blueland.bpm.engine.config.model.ActivityInfoConfig;
import com.blueland.bpm.engine.config.service.ActivityInfoConfigManager;
import com.blueland.bpm.engine.config.service.DataObjectManager;
import com.blueland.bpm.engine.config.service.DataObjectRelationManager;
import com.blueland.bpm.engine.core.service.ProcessDefinitionService;
import com.blueland.bpm.engine.core.service.ProcessModelService;
import com.blueland.bpm.engine.core.service.ProcessTraceService;
import com.blueland.bpm.manage.util.ComparatorTransition;

@Controller
@RequestMapping(value = "/actInfo/gateway")
public class GatewayController extends BaseProcessController{
	// 默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;
	@Autowired
	private ActivityInfoConfigManager activityInfoConfigManager;
	@Autowired
	private ProcessDefinitionService processDefinitionService;
	@Autowired
	private ProcessTraceService processTraceService;
	@Autowired
	private DataObjectManager dataObjectManager;
	@Autowired
	private DataObjectRelationManager dataObjectRelationManager;
	@Autowired
	private ProcessModelService processModelService;
	private final String LIST_ACTION = "redirect:/otherTask/list.do";

	public GatewayController() {
	}



	/**
	 * 进入更新页面
	 **/
	@RequestMapping(value = "editGatewayByCd")
	public ModelAndView editGatewayByCd(HttpServletRequest request,
			ActivityInfoConfig params) {
		ModelAndView modelAndView=new ModelAndView("/actInfo/gateway/editGatewayByCd");
		BpmnModel bpmnModel=processModelService.getBpmnModelByProcDefId(params.getProcessDefId());
		Gateway gateway=(Gateway) bpmnModel.getFlowElement(params.getActivityDefId());;
		List<SequenceFlow> sequenceFlows=gateway.getOutgoingFlows();
		Collections.sort(sequenceFlows,new ComparatorTransition());
		for (SequenceFlow sequenceFlow : sequenceFlows) {
			sequenceFlow.setSourceRefName(bpmnModel.getFlowElement(sequenceFlow.getSourceRef()).getName());
			sequenceFlow.setTargetRefName(bpmnModel.getFlowElement(sequenceFlow.getTargetRef()).getName());
		}
		modelAndView.addObject("gateway", gateway);
		modelAndView.addObject("params", params);
		return modelAndView;
	}
	
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping(value = "editGatewayByIc")
	public ModelAndView editGatewayByIc(HttpServletRequest request,
			ActivityInfoConfig params) {
		ModelAndView modelAndView=new ModelAndView("/actInfo/gateway/editGatewayByCd");
		BpmnModel bpmnModel=processModelService.getBpmnModelByProcDefId(params.getProcessDefId());
		Gateway gateway=(Gateway) bpmnModel.getFlowElement(params.getActivityDefId());;
		List<SequenceFlow> sequenceFlows=gateway.getOutgoingFlows();
		Collections.sort(sequenceFlows,new ComparatorTransition());
		for (SequenceFlow sequenceFlow : sequenceFlows) {
			sequenceFlow.setSourceRefName(bpmnModel.getFlowElement(sequenceFlow.getSourceRef()).getName());
			sequenceFlow.setTargetRefName(bpmnModel.getFlowElement(sequenceFlow.getTargetRef()).getName());
		}
		modelAndView.addObject("gateway", gateway);
		modelAndView.addObject("params", params);
		return modelAndView;
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping(value = "updateGateway")
	@ResponseBody
	public String updateGateway(HttpServletRequest request,
			ScriptTask scriptTask,ActivityInfoConfig params) {
		BpmnModel bpmnModel = processModelService
				.getBpmnModelByProcDefId(params.getProcessDefId());
		ScriptTask tempScriptTask = (ScriptTask) bpmnModel.getFlowElement(params.getActivityDefId());
		tempScriptTask.setScript(scriptTask.getScript());
		tempScriptTask.setScriptFormat(scriptTask.getScriptFormat());
		tempScriptTask.setDocumentation(scriptTask.getDocumentation());
		updateBpmnModel(bpmnModel, params.getProcessDefId(), true);
		return this.ACTION_SUCCESS;
	}

}
