Newer
Older
task-bldg / src / main / java / osm / surveyor / task / city / TaskController.java
@haya4 haya4 on 15 Aug 2022 8 KB "/admin/download"
  1. package osm.surveyor.task.city;
  2.  
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.util.List;
  6. import java.util.UUID;
  7.  
  8. import javax.servlet.http.HttpServletResponse;
  9.  
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.http.HttpStatus;
  12. import org.springframework.security.core.annotation.AuthenticationPrincipal;
  13. import org.springframework.security.core.userdetails.UserDetails;
  14. import org.springframework.stereotype.Controller;
  15. import org.springframework.ui.Model;
  16. import org.springframework.validation.BindingResult;
  17. import org.springframework.validation.annotation.Validated;
  18. import org.springframework.web.bind.annotation.ExceptionHandler;
  19. import org.springframework.web.bind.annotation.GetMapping;
  20. import org.springframework.web.bind.annotation.ModelAttribute;
  21. import org.springframework.web.bind.annotation.PostMapping;
  22. import org.springframework.web.bind.annotation.RequestParam;
  23.  
  24. import lombok.RequiredArgsConstructor;
  25. import osm.surveyor.task.city.model.City;
  26. import osm.surveyor.task.city.model.Citymesh;
  27. import osm.surveyor.task.city.model.CitymeshPK;
  28. import osm.surveyor.task.city.model.Operation;
  29. import osm.surveyor.task.city.model.Status;
  30. import osm.surveyor.task.city.model.Task;
  31.  
  32. @RequiredArgsConstructor
  33. @Controller
  34. public class TaskController {
  35. private final CityRepository cityRepository;
  36. private final CitymeshRepository meshRepository;
  37. private final TaskRepository taskRepository;
  38. @Autowired
  39. private TaskService service;
  40. /**
  41. * ログインユーザーが関係しているTASKリスト
  42. * @param model
  43. * @return
  44. */
  45. @GetMapping("/tasks")
  46. public String showList(@AuthenticationPrincipal UserDetails user, Model model,
  47. @RequestParam(name="citycode") String citycode,
  48. @RequestParam(name="meshcode") String meshcode)
  49. {
  50. // ログイン名を取得
  51. String loginName = "";
  52. if (user != null) {
  53. loginName = user.getUsername();
  54. }
  55. model.addAttribute("username", loginName);
  56.  
  57. City city = cityRepository.getById(citycode);
  58. model.addAttribute("citycode", citycode);
  59. model.addAttribute("cityname", city.getCityname());
  60. model.addAttribute("meshcode", meshcode);
  61. CitymeshPK pk = new CitymeshPK();
  62. pk.setCitycode(citycode);
  63. pk.setMeshcode(meshcode);
  64. Citymesh mesh = meshRepository.getById(pk);
  65. model.addAttribute("mesh", mesh);
  66. List<Task> tasks = taskRepository.serchByMesh(citycode, meshcode);
  67. model.addAttribute("tasks", tasks);
  68. return "tasks";
  69. }
  70.  
  71. /**
  72. * 「タスク操作」
  73. * @param citycode
  74. * @param meshcode
  75. * @param task
  76. * @return
  77. */
  78. @GetMapping("/task/add")
  79. public String addTask(@AuthenticationPrincipal UserDetails user,
  80. Model model,
  81. @RequestParam(name="op") String op,
  82. @RequestParam(name="citycode") String citycode,
  83. @RequestParam(name="meshcode") String meshcode)
  84. {
  85. String next = "task";
  86. Operation operation = Operation.NOP;
  87. Status nextStatus = Status.PREPARATION;
  88. if (op.equals(Operation.RESERVE.toString())) {
  89. model.addAttribute("command", "タスク予約");
  90. operation = Operation.RESERVE;
  91. nextStatus = Status.RESERVED;
  92. }
  93. else if (op.equals(Operation.CANCEL.toString())) {
  94. model.addAttribute("command", "タスク予約取消");
  95. operation = Operation.CANCEL;
  96. nextStatus = Status.ACCEPTING;
  97. }
  98. else if (op.equals(Operation.DONE.toString())) {
  99. model.addAttribute("command", "編集完了");
  100. operation = Operation.DONE;
  101. nextStatus = Status.IMPORTED;
  102. next = "task_done";
  103. }
  104. else if (op.equals(Operation.NG.toString())) {
  105. model.addAttribute("command", "検証(NG)");
  106. operation = Operation.NG;
  107. nextStatus = Status.ACCEPTING;
  108. }
  109. else if (op.equals(Operation.OK.toString())) {
  110. model.addAttribute("command", "検証(OK)");
  111. operation = Operation.OK;
  112. nextStatus = Status.END;
  113. }
  114.  
  115. // ログイン名を取得
  116. String loginName = "";
  117. if (user != null) {
  118. loginName = user.getUsername();
  119. }
  120. model.addAttribute("username", loginName);
  121. City city = cityRepository.getById(citycode);
  122. model.addAttribute("citycode", citycode);
  123. model.addAttribute("cityname", city.getCityname());
  124. model.addAttribute("meshcode", meshcode);
  125. CitymeshPK pk = new CitymeshPK();
  126. pk.setCitycode(citycode);
  127. pk.setMeshcode(meshcode);
  128. Citymesh mesh = meshRepository.getById(pk);
  129. Task pre = service.getTaskByMesh(citycode, meshcode);
  130. if (pre != null) {
  131. pre.setOperation(operation);
  132. pre.setStatus(nextStatus);
  133. if (op.equals(Operation.OK.toString()) || op.equals(Operation.NG.toString())) {
  134. pre.setValidator(loginName);
  135. }
  136. else {
  137. pre.setUsername(loginName);
  138. }
  139. model.addAttribute("task", pre);
  140. return next;
  141. }
  142. else {
  143. // 既存Taskが無い場合は生成する
  144. String uuid = UUID.randomUUID().toString();
  145. Task task = new Task();
  146. task.setCurrentId(uuid);
  147. task.setPreId(uuid);
  148. task.setCitycode(citycode);
  149. task.setMeshcode(meshcode);
  150. task.setMesh(mesh);
  151. task.setStatus(nextStatus);
  152. task.setUsername(loginName);
  153. if (op.equals(Operation.OK.toString()) || op.equals(Operation.NG.toString())) {
  154. task.setValidator(loginName);
  155. }
  156. task.setOperation(operation);
  157. model.addAttribute("task", task);
  158. return next;
  159. }
  160. }
  161. @PostMapping("/task/process")
  162. public String process(@AuthenticationPrincipal UserDetails user,
  163. @Validated @ModelAttribute Task task,
  164. BindingResult result)
  165. {
  166. if (result.hasErrors()) {
  167. // エラーがある場合
  168. return nextPage(task);
  169. }
  170. service.add(task, user);
  171. return "redirect:/tasks?citycode="+ task.getCitycode() +"&meshcode="+ task.getMeshcode();
  172. }
  173. @GetMapping("/admin")
  174. public String admin()
  175. {
  176. return "admin";
  177. }
  178.  
  179. @PostMapping("/admin/download")
  180. public String download(HttpServletResponse response) {
  181. try (OutputStream os = response.getOutputStream();) {
  182. List<Task> list = taskRepository.findAll();
  183. StringBuffer sb = new StringBuffer();
  184. boolean c1 = false;
  185. sb.append("[");
  186. sb.append(System.lineSeparator());
  187. for (Task task : list) {
  188. if (c1) {
  189. sb.append(",");
  190. }
  191. else {
  192. c1 = true;
  193. }
  194. sb.append(task.toString());
  195. sb.append(System.lineSeparator());
  196. }
  197. sb.append("]");
  198. byte[] fb1 = String.valueOf(sb).getBytes();
  199.  
  200. response.setContentType("application/octet-stream");
  201. response.setHeader("Content-Disposition", "attachment; filename="+ "task-bldg.json");
  202. response.setContentLength(fb1.length);
  203. os.write(fb1);
  204. os.flush();
  205. } catch (IOException e) {
  206. e.printStackTrace();
  207. }
  208. return null;
  209. }
  210. /**
  211. * 400 Bad Request
  212. *
  213. * @param e
  214. * @param model
  215. * @param citycode
  216. * @param meshcode
  217. * @return
  218. */
  219. @ExceptionHandler(TaskException.class)
  220. public String taskExceptionHandler(TaskException e, Model model)
  221. {
  222. model.addAttribute("error", "400 Bad Request");
  223. model.addAttribute("message", e.getMessage());
  224. model.addAttribute("status", HttpStatus.BAD_REQUEST);
  225. return exceptionHandler(e.getTask(), model);
  226. }
  227.  
  228. /**
  229. * 406 Not Acceptable
  230. * "ACCEPTIONGではないため予約できません"
  231. *
  232. * @param e
  233. * @param model
  234. * @param citycode
  235. * @param meshcode
  236. * @return
  237. */
  238. @ExceptionHandler(NotAcceptableException.class)
  239. public String notAcceptableExceptionHandler(NotAcceptableException e, Model model)
  240. {
  241. model.addAttribute("error", "406 Not Acceptable");
  242. model.addAttribute("message", e.getMessage());
  243. model.addAttribute("status", HttpStatus.NOT_ACCEPTABLE);
  244. return exceptionHandler(e.getTask(), model);
  245. }
  246.  
  247. /**
  248. * 409 Conflict
  249. * "タスクが変更されたため更新できません"
  250. *
  251. * @param e
  252. * @param model
  253. * @param citycode
  254. * @param meshcode
  255. * @return
  256. */
  257. @ExceptionHandler(ConflictException.class)
  258. public String conflictExceptionHandler(ConflictException e, Model model)
  259. {
  260. model.addAttribute("error", "409 Conflict");
  261. model.addAttribute("message", e.getMessage());
  262. model.addAttribute("status", HttpStatus.CONFLICT);
  263. return exceptionHandler(e.getTask(), model);
  264. }
  265. private String exceptionHandler(Task task, Model model) {
  266. if (task == null) {
  267. return "error";
  268. }
  269. if (task.getOperation() == Operation.RESERVE) {
  270. model.addAttribute("command", "タスク予約");
  271. }
  272. else if (task.getOperation() == Operation.CANCEL) {
  273. model.addAttribute("command", "タスク予約取消");
  274. }
  275. else if (task.getOperation() == Operation.DONE) {
  276. model.addAttribute("command", "編集済み");
  277. }
  278. else if (task.getOperation() == Operation.NG) {
  279. model.addAttribute("command", "検証(NG)");
  280. }
  281. else if (task.getOperation() == Operation.OK) {
  282. model.addAttribute("command", "検証(OK)");
  283. }
  284. model.addAttribute("citycode", task.getCitycode());
  285. model.addAttribute("meshcode", task.getMeshcode());
  286. model.addAttribute("task", task);
  287. return nextPage(task);
  288. }
  289. private String nextPage(Task task) {
  290. if (task.getOperation() == Operation.DONE) {
  291. return "task_done";
  292. }
  293. return "task";
  294. }
  295. }