Java tutorial
/* Copyright 2013-2015 www.snakerflow.com * * 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.fantasia.snakerflow.web; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang.StringUtils; import org.snaker.engine.access.Page; import org.snaker.engine.access.QueryFilter; import org.snaker.engine.entity.HistoryOrder; 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.StreamHelper; import org.snaker.engine.helper.StringHelper; import org.snaker.engine.model.ProcessModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import com.fantasia.snakerflow.engine.SnakerEngineFacets; import com.fantasia.snakerflow.engine.SnakerHelper; /** * ? * @author yuqs * @since 0.1 */ @Controller @RequestMapping(value = "/snaker/process") public class ProcessController { @Autowired private SnakerEngineFacets facets; /** * ? * @param model * @return */ @RequestMapping(value = "list", method = RequestMethod.GET) public String processList(Model model, Page<Process> page, String displayName) { QueryFilter filter = new QueryFilter(); if (StringHelper.isNotEmpty(displayName)) { filter.setDisplayName(displayName); } facets.getEngine().process().getProcesss(page, filter); model.addAttribute("page", page); return "snaker/processList"; } /** * ?? * @return */ @RequestMapping(value = "init", method = RequestMethod.GET) public String processInit() { facets.initFlows(); return "redirect:/snaker/process/list"; } /** * ?? * @param model * @return */ @RequestMapping(value = "deploy", method = RequestMethod.GET) public String processDeploy(Model model) { return "snaker/processDeploy"; } /** * ?[web?] * @param model * @return */ @RequestMapping(value = "designer", method = RequestMethod.GET) public String processDesigner(String processId, Model model) { if (StringUtils.isNotEmpty(processId)) { Process process = facets.getEngine().process().getProcessById(processId); AssertHelper.notNull(process); ProcessModel processModel = process.getModel(); if (processModel != null) { String json = SnakerHelper.getModelJson(processModel); model.addAttribute("process", json); } model.addAttribute("processId", processId); } return "snaker/processDesigner"; } /** * ? * @param model * @return */ @RequestMapping(value = "edit/{id}", method = RequestMethod.GET) public String processEdit(Model model, @PathVariable("id") String id) { Process process = facets.getEngine().process().getProcessById(id); model.addAttribute("process", process); if (process.getDBContent() != null) { try { model.addAttribute("content", StringHelper.textXML(new String(process.getDBContent(), "UTF-8"))); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return "snaker/processEdit"; } /** * ??ID? * @param id * @return */ @RequestMapping(value = "delete/{id}", method = RequestMethod.GET) public String processDelete(@PathVariable("id") String id) { facets.getEngine().process().undeploy(id); return "redirect:/snaker/process/list"; } /** * ?? * @param snakerFile * @param id * @return */ @RequestMapping(value = "deploy", method = RequestMethod.POST) public String processDeploy(@RequestParam(value = "snakerFile") MultipartFile snakerFile, String id) { InputStream input = null; try { input = snakerFile.getInputStream(); if (StringUtils.isNotEmpty(id)) { facets.getEngine().process().redeploy(id, input); } else { facets.getEngine().process().deploy(input); } } catch (IOException e) { e.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } return "redirect:/snaker/process/list"; } /** * ??[web?] * @param model * @return */ @RequestMapping(value = "deployXml", method = RequestMethod.POST) @ResponseBody public boolean processDeploy(String model, String id) { InputStream input = null; try { String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + SnakerHelper.convertXml(model); System.out.println("model xml=\n" + xml); input = StreamHelper.getStreamFromString(xml); if (StringUtils.isNotEmpty(id)) { facets.getEngine().process().redeploy(id, input); } else { facets.getEngine().process().deploy(input); } } catch (Exception e) { e.printStackTrace(); return false; } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; } @RequestMapping(value = "json", method = RequestMethod.GET) @ResponseBody public Object json(String processId, String orderId) { List<Task> tasks = null; if (StringUtils.isNotEmpty(orderId)) { tasks = facets.getEngine().query().getActiveTasks(new QueryFilter().setOrderId(orderId)); } Process process = facets.getEngine().process().getProcessById(processId); AssertHelper.notNull(process); ProcessModel model = process.getModel(); Map<String, String> jsonMap = new HashMap<String, String>(); if (model != null) { jsonMap.put("process", SnakerHelper.getModelJson(model)); } //{"activeRects":{"rects":[{"paths":[],"name":"3"},{"paths":[],"name":"4"},{"paths":[],"name":"2"}]},"historyRects":{"rects":[{"paths":["TO 1"],"name":""},{"paths":["TO "],"name":"1"},{"paths":["TO 3","TO 4","TO 2"],"name":""}]}} if (tasks != null && !tasks.isEmpty()) { jsonMap.put("active", SnakerHelper.getActiveJson(tasks)); } return jsonMap; } @RequestMapping(value = "display", method = RequestMethod.GET) public String display(Model model, String processId, String orderId) { HistoryOrder order = facets.getEngine().query().getHistOrder(orderId); model.addAttribute("processId", processId); model.addAttribute("order", order); List<HistoryTask> tasks = facets.getEngine().query().getHistoryTasks(new QueryFilter().setOrderId(orderId)); if (tasks != null && tasks.size() > 0) { for (HistoryTask historyTask : tasks) { String result = ""; if (historyTask.getVariableMap().get("method") == null) { result = "???"; historyTask.setVariable(""); } else { if (historyTask.getVariableMap().get("method").toString().equals("-1")) { result = "???"; } if (historyTask.getVariableMap().get("method").toString().equals("0")) { result = "??"; } if (historyTask.getVariableMap().get("approveDept.suggest") == null) { historyTask.setVariable((String) historyTask.getVariableMap().get("approveBoss.suggest")); } else { historyTask.setVariable((String) historyTask.getVariableMap().get("approveDept.suggest")); } } historyTask.setParentTaskId(result); } } model.addAttribute("tasks", tasks); return "snaker/processView"; } /** * ? */ @RequestMapping(value = "diagram", method = RequestMethod.GET) public String diagram(Model model, String processId, String orderId) { model.addAttribute("processId", processId); model.addAttribute("orderId", orderId); return "snaker/diagram"; } }