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.common.util.HttpUtils;
/**
 * @author lw
 * @version 1.0
 * @since  
 */
 






import java.util.*;

import com.blueland.bpm.engine.config.model.*;
import com.blueland.bpm.engine.config.dao.*;
import com.blueland.bpm.engine.config.service.*;
import com.blueland.bpm.engine.core.model.ActivityDefinitionModel;
import com.blueland.bpm.engine.core.model.ProcessDefinitionModel;
import com.blueland.bpm.engine.core.service.ProcessDefinitionService;
import com.blueland.bpm.manage.service.AgainSaveSessionService;
import com.blueland.bpm.manage.util.BaseUser;

@Controller
@RequestMapping(value = "dataObjectRelation")
public class DataObjectRelationController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	@Autowired
	private DataObjectRelationManager dataObjectRelationManager;
	@Autowired
	private ProcessDefinitionService processDefinitionService;
	@Autowired
	private AgainSaveSessionService againSaveSessionService;
	
	private final String LIST_ACTION = "redirect:/dataObjectRelation/list.do";
	
	public DataObjectRelationController() {
	}
	

	/**
	 * 增加了@ModelAttribute的方法可以在本controller的方法调用前执行,可以存放一些共享变量,如枚举值
	 */
	@ModelAttribute
	public void init(ModelMap model) {
		//model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
	
	/** 
	 * 执行搜索 
	 **/
	@RequestMapping(value = "sequenceFlowList")
	public ModelAndView sequenceFlowList(HttpServletRequest request,DataObjectRelation query) {
		List<DataObjectRelation> dataObjectRelations = this.dataObjectRelationManager.findDataObjectRelationsBySequenceFlow(query.getProcessDefId(), query.getActivityDefId(),query.getTargetActId());
		ModelAndView result = new ModelAndView("/dataObjectRelation/sequenceFlowList");
		result.addObject("dataObjectRelations",dataObjectRelations);
		result.addObject("query",query);
		return result;
	}
	
	/** 
	 * 执行搜索 
	 **/
	@RequestMapping(value = "processList")
	public ModelAndView processList(HttpServletRequest request,DataObjectRelation query) {
		List<DataObjectRelation> dataObjectRelations = this.dataObjectRelationManager.findDataObjectRelationsByProcessDefId(query.getProcessDefId());
		ModelAndView result = new ModelAndView("/dataObjectRelation/processList");
		result.addObject("dataObjectRelations",dataObjectRelations);
		result.addObject("query",query);
		return result;
	}

	/** 
	 * 执行搜索 
	 **/
	@RequestMapping(value = "activityList")
	public ModelAndView activityList(HttpServletRequest request,DataObjectRelation query) {
		List<DataObjectRelation> dataObjectRelations = this.dataObjectRelationManager.findDataObjectRelationsByActivityDefId(query.getProcessDefId(), query.getActivityDefId());
		ModelAndView result = new ModelAndView("/dataObjectRelation/activityList");
		result.addObject("dataObjectRelations",dataObjectRelations);
		result.addObject("query",query);
		return result;
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value = "show")
	public ModelAndView show(HttpServletRequest request)  {
		String id = new String(request.getParameter("dorId"));
		DataObjectRelation dataObjectRelation = (DataObjectRelation)dataObjectRelationManager.getById(id);
		return new ModelAndView("/dataObjectRelation/show","dataObjectRelation",dataObjectRelation);
	}
	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping(value = "create")
	public ModelAndView create(HttpServletRequest request,DataObjectRelation dataObjectRelation)  {
		ProcessDefinitionModel processDef =processDefinitionService.getProcessDef(dataObjectRelation.getProcessDefId());
		
		dataObjectRelation.setProcessDefName(processDef.getProcDefName());
		if(!StringUtils.isEmpty(dataObjectRelation.getActivityDefId())) {
			ActivityDefinitionModel actDef =processDefinitionService.getActivity(processDef.getProcDefId(), dataObjectRelation.getActivityDefId());
			dataObjectRelation.setActivityDefName(actDef.getActDefName());
			if(!StringUtils.isEmpty(dataObjectRelation.getTargetActId())) {
				ActivityDefinitionModel destActDef =processDefinitionService.getActivity(processDef.getProcDefId(), dataObjectRelation.getTargetActId());
				dataObjectRelation.setTargetActName(destActDef.getActDefName());
			}
		}
		return new ModelAndView("/dataObjectRelation/create","dataObjectRelation",dataObjectRelation);
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping(value = "save")
	@ResponseBody
	public String save(HttpServletRequest request,DataObjectRelation dataObjectRelation)  {
		dataObjectRelation.getDataObject().setTenantId(BaseUser.getInstance().getLoginUserAppId(request));
		dataObjectRelationManager.save(dataObjectRelation);
		return CREATED_SUCCESS;
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping(value = "edit")
	public ModelAndView edit(HttpServletRequest request)  {
		String id = new String(request.getParameter("dorId"));
		DataObjectRelation dataObjectRelation = (DataObjectRelation)dataObjectRelationManager.getById(id);
		return new ModelAndView("/dataObjectRelation/edit","dataObjectRelation",dataObjectRelation);
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping(value = "update")
	@ResponseBody
	public String update(HttpServletRequest request,DataObjectRelation dataObjectRelation)  {
		String id = new String(request.getParameter("dorId"));
		dataObjectRelationManager.update(dataObjectRelation);
		return UPDATE_SUCCESS;
	}
	
	/**
	 *删除对象
	 **/
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(HttpServletRequest request) {
		String[] items = request.getParameterValues("items");
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			String id = new String((String)params.get("dorId"));
			dataObjectRelationManager.removeById(id);
		}
		return DELETE_SUCCESS;
	}
	
}

