/* Copyright 2012-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ym.mec.workfow.controller.flow;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.snaker.engine.SnakerEngine;
import org.snaker.engine.access.Page;
import org.snaker.engine.access.QueryFilter;
import org.snaker.engine.entity.HistoryTask;
import org.snaker.engine.entity.Process;
import org.snaker.engine.entity.Task;
import org.snaker.engine.helper.AssertHelper;
import org.snaker.engine.helper.StringHelper;
import org.snaker.engine.model.ProcessModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ym.mec.common.controller.BaseController;

/**
 * 流程定义
 * @author yuqs
 * @since 0.1
 */
@RestController
@RequestMapping(value = "/snaker/process")
public class ProcessController extends BaseController {

	@Autowired
	private SnakerEngine snakerEngine;

	/**
	 * 流程定义查询列表
	 * @param model
	 * @return
	 */
	@GetMapping(value = "list")
	public Object processList(Page<Process> page, String displayName) {
		QueryFilter filter = new QueryFilter();
		if (StringHelper.isNotEmpty(displayName)) {
			filter.setDisplayName(displayName);
		}
		return succeed(snakerEngine.process().getProcesss(page, filter));
	}

	/**
	 * 根据流程定义ID，删除流程定义
	 * @param id
	 * @return
	 */
	@PostMapping(value = "delete/{id}")
	public Object processDelete(@PathVariable("id") String id) {
		snakerEngine.process().undeploy(id);
		return succeed();
	}

	/**
	 * 添加流程定义后的部署
	 * @param snakerFile
	 * @param id
	 * @return
	 * @throws IOException 
	 */
	@PostMapping(value = "deploy")
	public Object processDeploy(@RequestParam(value = "snakerFile") MultipartFile snakerFile, String id) throws IOException {
		InputStream input = null;
		try {
			input = snakerFile.getInputStream();
			if (StringUtils.isNotEmpty(id)) {
				snakerEngine.process().redeploy(id, input);
			} else {
				snakerEngine.process().deploy(input);
			}
		} finally {
			if (input != null) {
				input.close();
			}
		}
		return succeed();
	}

}
