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.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.Task; import org.snaker.engine.entity.WorkItem; import org.snaker.engine.model.TaskModel.TaskType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.fantasia.core.DbcContext; import com.fantasia.snakerflow.engine.SnakerEngineFacets; /** * Snaker?Controller * @author yuqs * @since 0.1 */ @Controller @RequestMapping(value = "/snaker/task") public class TaskController { @Autowired private SnakerEngineFacets facets; @RequestMapping(value = "active", method = RequestMethod.GET) public String homeTaskList(Model model) { String[] assignees = new String[] { DbcContext.getUser().getUserName() }; Page<WorkItem> majorPage = new Page<WorkItem>(5); Page<WorkItem> aidantPage = new Page<WorkItem>(3); Page<HistoryOrder> ccorderPage = new Page<HistoryOrder>(3); List<WorkItem> majorWorks = facets.getEngine().query().getWorkItems(majorPage, new QueryFilter().setOperators(assignees).setTaskType(TaskType.Major.ordinal())); List<WorkItem> aidantWorks = facets.getEngine().query().getWorkItems(aidantPage, new QueryFilter().setOperators(assignees).setTaskType(TaskType.Aidant.ordinal())); List<HistoryOrder> ccWorks = facets.getEngine().query().getCCWorks(ccorderPage, new QueryFilter().setOperators(assignees).setState(1)); model.addAttribute("majorWorks", majorWorks); model.addAttribute("majorTotal", majorPage.getTotalCount()); model.addAttribute("aidantWorks", aidantWorks); model.addAttribute("aidantTotal", aidantPage.getTotalCount()); model.addAttribute("ccorderWorks", ccWorks); model.addAttribute("ccorderTotal", ccorderPage.getTotalCount()); return "snaker/activeTask"; } /** * ?? * @param model * @return */ @RequestMapping(value = "user", method = RequestMethod.GET) public String userTaskList(Model model, Page<WorkItem> page) { facets.getEngine().query().getWorkItems(page, new QueryFilter().setOperator(DbcContext.getUser().getUserName())); model.addAttribute("page", page); return "snaker/userTask"; } @RequestMapping(value = "actor/add", method = RequestMethod.GET) public String addTaskActor(Model model, String orderId, String taskName) { model.addAttribute("orderId", orderId); model.addAttribute("taskName", taskName); return "snaker/actor"; } @RequestMapping(value = "actor/add", method = RequestMethod.POST) @ResponseBody public String addTaskActor(Model model, String orderId, String taskName, String operator) { List<Task> tasks = facets.getEngine().query().getActiveTasks(new QueryFilter().setOrderId(orderId)); for (Task task : tasks) { if (task.getTaskName().equalsIgnoreCase(taskName) && StringUtils.isNotEmpty(operator)) { facets.getEngine().task().addTaskActor(task.getId(), operator); } } return "success"; } @RequestMapping(value = "tip", method = RequestMethod.GET) @ResponseBody public Map<String, String> addTaskActor(String orderId, String taskName) { List<Task> tasks = facets.getEngine().query().getActiveTasks(new QueryFilter().setOrderId(orderId)); StringBuilder builder = new StringBuilder(); String createTime = ""; for (Task task : tasks) { if (task.getTaskName().equalsIgnoreCase(taskName)) { String[] actors = facets.getEngine().query().getTaskActorsByTaskId(task.getId()); for (String actor : actors) { builder.append(actor).append(","); } createTime = task.getCreateTime(); } } if (builder.length() > 0) { builder.deleteCharAt(builder.length() - 1); } Map<String, String> data = new HashMap<String, String>(); data.put("actors", builder.toString()); data.put("createTime", createTime); return data; } /** * * @param model * @return */ @RequestMapping(value = "active/more", method = RequestMethod.GET) public String activeTaskList(Model model, Page<WorkItem> page, Integer taskType) { String[] assignees = new String[] { DbcContext.getUser().getUserName() }; facets.getEngine().query().getWorkItems(page, new QueryFilter().setOperators(assignees).setTaskType(taskType)); model.addAttribute("page", page); model.addAttribute("taskType", taskType); return "snaker/activeTaskMore"; } /** * * @param model * @return */ @RequestMapping(value = "active/ccmore", method = RequestMethod.GET) public String activeCCList(Model model, Page<HistoryOrder> page) { String[] assignees = new String[] { DbcContext.getUser().getUserName() }; facets.getEngine().query().getCCWorks(page, new QueryFilter().setOperators(assignees).setState(1)); model.addAttribute("page", page); return "snaker/activeCCMore"; } /** * * @param model * @return */ @RequestMapping(value = "exec", method = RequestMethod.GET) public String activeTaskExec(Model model, String taskId) { facets.execute(taskId, DbcContext.getUser().getUserName(), null); return "redirect:/snaker/task/active"; } /** * * @param model * @param taskId * @return */ @RequestMapping(value = "reject", method = RequestMethod.GET) public String activeTaskReject(Model model, String taskId) { String error = ""; try { facets.executeAndJump(taskId, DbcContext.getUser().getUserName(), null, null); } catch (Exception e) { error = "?error=1"; } return "redirect:/snaker/task/active" + error; } /** * ?? * @param model * @return */ @RequestMapping(value = "history", method = RequestMethod.GET) public String historyTaskList(Model model, Page<WorkItem> page) { facets.getEngine().query().getHistoryWorkItems(page, new QueryFilter().setOperator(DbcContext.getUser().getUserName())); model.addAttribute("page", page); return "snaker/historyTask"; } /** * ? * @param taskId * @return */ @RequestMapping(value = "undo", method = RequestMethod.GET) public String historyTaskUndo(Model model, String taskId) { String returnMessage = ""; try { facets.getEngine().task().withdrawTask(taskId, DbcContext.getUser().getUserName()); returnMessage = "?."; } catch (Exception e) { returnMessage = e.getMessage(); } model.addAttribute("returnMessage", returnMessage); return "redirect:/snaker/task/history"; } }