|
| 1 | +package org.woehlke.simpleworklist.project; |
| 2 | + |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | +import org.springframework.beans.factory.annotation.Autowired; |
| 5 | +import org.springframework.stereotype.Controller; |
| 6 | +import org.springframework.ui.Model; |
| 7 | +import org.springframework.validation.BindingResult; |
| 8 | +import org.springframework.validation.ObjectError; |
| 9 | +import org.springframework.web.bind.annotation.*; |
| 10 | +import org.woehlke.simpleworklist.breadcrumb.Breadcrumb; |
| 11 | +import org.woehlke.simpleworklist.common.AbstractController; |
| 12 | +import org.woehlke.simpleworklist.context.Context; |
| 13 | +import org.woehlke.simpleworklist.task.*; |
| 14 | +import org.woehlke.simpleworklist.user.UserSessionBean; |
| 15 | +import org.woehlke.simpleworklist.user.account.UserAccount; |
| 16 | + |
| 17 | +import javax.validation.Valid; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Locale; |
| 20 | + |
| 21 | +@Slf4j |
| 22 | +@Controller |
| 23 | +@RequestMapping(path = "/project/task") |
| 24 | +public class ProjectTaskController extends AbstractController { |
| 25 | + |
| 26 | + private final ProjectControllerService projectControllerService; |
| 27 | + private final TaskService taskService; |
| 28 | + |
| 29 | + @Autowired |
| 30 | + public ProjectTaskController(ProjectControllerService projectControllerService, TaskService taskService) { |
| 31 | + this.projectControllerService = projectControllerService; |
| 32 | + this.taskService = taskService; |
| 33 | + } |
| 34 | + |
| 35 | + @RequestMapping(path = "/add", method = RequestMethod.GET) |
| 36 | + public final String addNewTaskToInboxGet( |
| 37 | + @ModelAttribute("userSession") UserSessionBean userSession, |
| 38 | + Locale locale, Model model |
| 39 | + ) { |
| 40 | + log.info("addNewTaskToInboxGet"); |
| 41 | + UserAccount userAccount = userAccountLoginSuccessService.retrieveCurrentUser(); |
| 42 | + Task task = new Task(); |
| 43 | + task.setTaskState(TaskState.INBOX); |
| 44 | + task.setTaskEnergy(TaskEnergy.NONE); |
| 45 | + task.setTaskTime(TaskTime.NONE); |
| 46 | + Boolean mustChooseContext = false; |
| 47 | + if(userSession.getContextId() == 0L){ |
| 48 | + mustChooseContext = true; |
| 49 | + task.setContext(userAccount.getDefaultContext()); |
| 50 | + } else { |
| 51 | + Context context = contextService.findByIdAndUserAccount(userSession.getContextId(), userAccount); |
| 52 | + task.setContext(context); |
| 53 | + } |
| 54 | + Breadcrumb breadcrumb = breadcrumbService.getBreadcrumbForTaskstate(TaskState.INBOX,locale); |
| 55 | + model.addAttribute("breadcrumb", breadcrumb); |
| 56 | + model.addAttribute("mustChooseArea", mustChooseContext); |
| 57 | + model.addAttribute("breadcrumb", breadcrumb); |
| 58 | + model.addAttribute("task", task); |
| 59 | + return "project/task/add"; |
| 60 | + } |
| 61 | + |
| 62 | + @RequestMapping(path = "/add", method = RequestMethod.POST) |
| 63 | + public final String addNewTaskToInboxPost( |
| 64 | + @ModelAttribute("userSession") UserSessionBean userSession, |
| 65 | + @Valid Task task, |
| 66 | + BindingResult result, |
| 67 | + Locale locale, |
| 68 | + Model model |
| 69 | + ) { |
| 70 | + log.info("addNewTaskToInboxPost"); |
| 71 | + Context context = super.getContext(userSession); |
| 72 | + if (result.hasErrors()) { |
| 73 | + for (ObjectError e : result.getAllErrors()) { |
| 74 | + log.info(e.toString()); |
| 75 | + } |
| 76 | + Boolean mustChooseArea = false; |
| 77 | + task.setContext(context); |
| 78 | + Breadcrumb breadcrumb = breadcrumbService.getBreadcrumbForTaskstate(TaskState.INBOX,locale); |
| 79 | + model.addAttribute("mustChooseArea", mustChooseArea); |
| 80 | + model.addAttribute("breadcrumb", breadcrumb); |
| 81 | + model.addAttribute("task", task); |
| 82 | + return "project/task/add"; |
| 83 | + } else { |
| 84 | + task = taskService.addToInbox(task); |
| 85 | + log.info(task.toString()); |
| 86 | + return "redirect:/taskstate/" + task.getTaskState().name().toLowerCase(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @RequestMapping(path = "/{taskId}/edit", method = RequestMethod.GET) |
| 91 | + public final String editTaskGet( |
| 92 | + @PathVariable("taskId") Task task, |
| 93 | + @ModelAttribute("userSession") UserSessionBean userSession, |
| 94 | + Locale locale, Model model |
| 95 | + ) { |
| 96 | + log.info("editTaskGet"); |
| 97 | + if(task != null) { |
| 98 | + UserAccount userAccount = userAccountLoginSuccessService.retrieveCurrentUser(); |
| 99 | + List<Context> contexts = contextService.getAllForUser(userAccount); |
| 100 | + Project thisProject; |
| 101 | + if (task.getContext() == null) { |
| 102 | + thisProject = new Project(); |
| 103 | + thisProject.setId(0L); |
| 104 | + } else { |
| 105 | + thisProject = task.getProject(); |
| 106 | + } |
| 107 | + Context thisContext = task.getContext(); |
| 108 | + Breadcrumb breadcrumb = breadcrumbService.getBreadcrumbForTaskstate(task.getTaskState(),locale); |
| 109 | + model.addAttribute("breadcrumb", breadcrumb); |
| 110 | + model.addAttribute("thisProject", thisProject); |
| 111 | + model.addAttribute("thisContext", thisContext); |
| 112 | + model.addAttribute("task", task); |
| 113 | + model.addAttribute("areas", contexts); |
| 114 | + return "project/task/edit"; |
| 115 | + } else { |
| 116 | + return "redirect:/taskstate/inbox"; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @RequestMapping(path = "/{taskId}/edit", method = RequestMethod.POST) |
| 121 | + public final String editTaskPost( |
| 122 | + @PathVariable long taskId, |
| 123 | + @Valid Task task, |
| 124 | + @ModelAttribute("userSession") UserSessionBean userSession, |
| 125 | + BindingResult result, |
| 126 | + Locale locale, |
| 127 | + Model model |
| 128 | + ) { |
| 129 | + log.info("editTaskPost"); |
| 130 | + if (result.hasErrors() ) { |
| 131 | + log.warn("result.hasErrors"); |
| 132 | + for (ObjectError e : result.getAllErrors()) { |
| 133 | + log.error(e.toString()); |
| 134 | + } |
| 135 | + Task persistentTask = taskService.findOne(taskId); |
| 136 | + persistentTask.merge(task); |
| 137 | + task = persistentTask; |
| 138 | + UserAccount userAccount = userAccountLoginSuccessService.retrieveCurrentUser(); |
| 139 | + List<Context> contexts = contextService.getAllForUser(userAccount); |
| 140 | + Project thisProject; |
| 141 | + if (task.getContext() == null) { |
| 142 | + thisProject = new Project(); |
| 143 | + thisProject.setId(0L); |
| 144 | + } else { |
| 145 | + thisProject = task.getProject(); |
| 146 | + } |
| 147 | + Context thisContext = task.getContext(); |
| 148 | + Breadcrumb breadcrumb = breadcrumbService.getBreadcrumbForShowOneProject(thisProject,locale); |
| 149 | + model.addAttribute("breadcrumb", breadcrumb); |
| 150 | + model.addAttribute("thisProject", thisProject); |
| 151 | + model.addAttribute("thisContext", thisContext); |
| 152 | + model.addAttribute("task", task); |
| 153 | + model.addAttribute("areas", contexts); |
| 154 | + return "project/task/edit"; |
| 155 | + } else { |
| 156 | + task.unsetFocus(); |
| 157 | + task.setRootProject(); |
| 158 | + Task persistentTask = taskService.findOne(task.getId()); |
| 159 | + persistentTask.merge(task); |
| 160 | + task = taskService.updatedViaTaskstate(persistentTask); |
| 161 | + return task.getTaskState().getUrl(); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + @RequestMapping(path = "/{sourceTaskId}/changeorderto/{destinationTaskId}", method = RequestMethod.GET) |
| 166 | + public String changeTaskOrderId( |
| 167 | + @PathVariable("sourceTaskId") Task sourceTask, |
| 168 | + @PathVariable("destinationTaskId") Task destinationTask, |
| 169 | + @ModelAttribute("userSession") UserSessionBean userSession, |
| 170 | + Model model |
| 171 | + ){ |
| 172 | + userSession.setLastTaskState(sourceTask.getTaskState()); |
| 173 | + model.addAttribute("userSession", userSession); |
| 174 | + log.info("------------- changeTaskOrderId -------------"); |
| 175 | + log.info("source Task: "+sourceTask.toString()); |
| 176 | + log.info("---------------------------------------------"); |
| 177 | + log.info("destination Task: "+destinationTask.toString()); |
| 178 | + log.info("---------------------------------------------"); |
| 179 | + taskService.moveOrderIdTaskState(sourceTask, destinationTask); |
| 180 | + return "redirect:/project/" + sourceTask.getTaskState().name().toLowerCase(); |
| 181 | + } |
| 182 | + |
| 183 | + @RequestMapping(path = "/{taskId}/move/to/project/{projectId}", method = RequestMethod.GET) |
| 184 | + public final String moveTaskToAnotherProject( |
| 185 | + @PathVariable("taskId") Task task, |
| 186 | + @PathVariable long projectId |
| 187 | + ) { |
| 188 | + if(projectId == 0) { |
| 189 | + task = taskService.moveTaskToRootProject(task); |
| 190 | + } else { |
| 191 | + Project project = projectService.findByProjectId(projectId); |
| 192 | + task = taskService.moveTaskToAnotherProject(task,project); |
| 193 | + } |
| 194 | + return "redirect:/project/" + projectId + "/"; |
| 195 | + } |
| 196 | + |
| 197 | + @RequestMapping(path = "/{taskId}/move/to/taskstate/inbox", method = RequestMethod.GET) |
| 198 | + public final String moveTaskToInbox(@PathVariable("taskId") Task task) { |
| 199 | + log.info("dragged and dropped "+task.getId()+" to inbox"); |
| 200 | + task.moveToInbox(); |
| 201 | + taskService.updatedViaTaskstate(task); |
| 202 | + return "redirect:/taskstate/inbox"; |
| 203 | + } |
| 204 | + |
| 205 | + @RequestMapping(path = "/{taskId}/move/to/taskstate/today", method = RequestMethod.GET) |
| 206 | + public final String moveTaskToToday(@PathVariable("taskId") Task task) { |
| 207 | + log.info("dragged and dropped "+task.getId()+" to today"); |
| 208 | + task.moveToToday(); |
| 209 | + taskService.updatedViaTaskstate(task); |
| 210 | + return "redirect:/taskstate/today"; |
| 211 | + } |
| 212 | + |
| 213 | + @RequestMapping(path = "/{taskId}/move/to/taskstate/next", method = RequestMethod.GET) |
| 214 | + public final String moveTaskToNext(@PathVariable("taskId") Task task) { |
| 215 | + log.info("dragged and dropped "+task.getId()+" to next"); |
| 216 | + task.moveToNext(); |
| 217 | + taskService.updatedViaTaskstate(task); |
| 218 | + return "redirect:/taskstate/next"; |
| 219 | + } |
| 220 | + |
| 221 | + @RequestMapping(path = "/{taskId}/move/to/taskstate/waiting", method = RequestMethod.GET) |
| 222 | + public final String moveTaskToWaiting(@PathVariable("taskId") Task task) { |
| 223 | + log.info("dragged and dropped "+task.getId()+" to waiting"); |
| 224 | + task.moveToWaiting(); |
| 225 | + taskService.updatedViaTaskstate(task); |
| 226 | + return "redirect:/taskstate/waiting"; |
| 227 | + } |
| 228 | + |
| 229 | + @RequestMapping(path = "/{taskId}/move/to/taskstate/someday", method = RequestMethod.GET) |
| 230 | + public final String moveTaskToSomeday(@PathVariable("taskId") Task task) { |
| 231 | + log.info("dragged and dropped "+task.getId()+" to someday"); |
| 232 | + task.moveToSomeday(); |
| 233 | + taskService.updatedViaTaskstate(task); |
| 234 | + return "redirect:/taskstate/someday"; |
| 235 | + } |
| 236 | + |
| 237 | + @RequestMapping(path = "/{taskId}/move/to/taskstate/focus", method = RequestMethod.GET) |
| 238 | + public final String moveTaskToFocus(@PathVariable("taskId") Task task) { |
| 239 | + log.info("dragged and dropped "+task.getId()+" to focus"); |
| 240 | + task.moveToFocus(); |
| 241 | + taskService.updatedViaTaskstate(task); |
| 242 | + return "redirect:/taskstate/focus"; |
| 243 | + } |
| 244 | + |
| 245 | + @RequestMapping(path = "/{taskId}/move/to/taskstate/completed", method = RequestMethod.GET) |
| 246 | + public final String moveTaskToCompleted(@PathVariable("taskId") Task task) { |
| 247 | + log.info("dragged and dropped "+task.getId()+" to completed"); |
| 248 | + task.moveToCompletedTasks(); |
| 249 | + taskService.updatedViaTaskstate(task); |
| 250 | + return "redirect:/taskstate/completed"; |
| 251 | + } |
| 252 | + |
| 253 | + @RequestMapping(path = "/{taskId}/move/to/trash", method = RequestMethod.GET) |
| 254 | + public final String moveTaskToTrash(@PathVariable("taskId") Task task) { |
| 255 | + log.info("dragged and dropped "+task.getId()+" to trash"); |
| 256 | + task.moveToTrash(); |
| 257 | + taskService.updatedViaTaskstate(task); |
| 258 | + return "redirect:/taskstate/trash"; |
| 259 | + } |
| 260 | + |
| 261 | + @RequestMapping(path = "/completed/move/to/trash", method = RequestMethod.GET) |
| 262 | + public final String moveAllCompletedToTrash( |
| 263 | + @ModelAttribute("userSession") UserSessionBean userSession |
| 264 | + ) { |
| 265 | + Context context = super.getContext(userSession); |
| 266 | + taskService.moveAllCompletedToTrash(context); |
| 267 | + return "redirect:/taskstate/trash"; |
| 268 | + } |
| 269 | + |
| 270 | + @RequestMapping(path = "/trash/empty", method = RequestMethod.GET) |
| 271 | + public final String emptyTrash( |
| 272 | + @ModelAttribute("userSession") UserSessionBean userSession |
| 273 | + ) { |
| 274 | + Context context = super.getContext(userSession); |
| 275 | + taskService.emptyTrash(context); |
| 276 | + return "redirect:/taskstate/trash"; |
| 277 | + } |
| 278 | + |
| 279 | + |
| 280 | + @RequestMapping(path = "/{taskId}/delete", method = RequestMethod.GET) |
| 281 | + public final String deleteTaskGet(@PathVariable("taskId") Task task) { |
| 282 | + log.info("deleteTaskGet"); |
| 283 | + if(task!= null){ |
| 284 | + task.delete(); |
| 285 | + taskService.updatedViaTaskstate(task); |
| 286 | + } |
| 287 | + return "redirect:/taskstate/trash"; |
| 288 | + } |
| 289 | + |
| 290 | + @RequestMapping(path = "/{taskId}/undelete", method = RequestMethod.GET) |
| 291 | + public final String undeleteTaskGet(@PathVariable("taskId") Task task) { |
| 292 | + log.info("undeleteTaskGet"); |
| 293 | + if(task!= null) { |
| 294 | + task.undelete(); |
| 295 | + taskService.updatedViaTaskstate(task); |
| 296 | + return "redirect:/taskstate/completed"; |
| 297 | + } else { |
| 298 | + return "redirect:/taskstate/trash"; |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | + @RequestMapping(path = "/{taskId}/transform", method = RequestMethod.GET) |
| 303 | + public final String transformTaskIntoProjectGet(@PathVariable("taskId") Task task) { |
| 304 | + log.info("transformTaskIntoProjectGet"); |
| 305 | + return transformTaskIntoProjectGet(task); |
| 306 | + } |
| 307 | + |
| 308 | + @RequestMapping(path = "/{taskId}/complete", method = RequestMethod.GET) |
| 309 | + public final String setDoneTaskGet( |
| 310 | + @PathVariable("taskId") Task task |
| 311 | + ) { |
| 312 | + if(task != null){ |
| 313 | + task.complete(); |
| 314 | + long maxOrderIdTaskState = taskService.getMaxOrderIdTaskState(TaskState.COMPLETED,task.getContext()); |
| 315 | + task.setOrderIdTaskState(++maxOrderIdTaskState); |
| 316 | + task = taskService.updatedViaTaskstate(task); |
| 317 | + return task.getUrl(); |
| 318 | + } |
| 319 | + return "redirect:/taskstate/completed"; |
| 320 | + } |
| 321 | + |
| 322 | + @RequestMapping(path = "/{taskId}/incomplete", method = RequestMethod.GET) |
| 323 | + public final String unsetDoneTaskGet( |
| 324 | + @PathVariable("taskId") Task task |
| 325 | + ) { |
| 326 | + if(task !=null) { |
| 327 | + task.incomplete(); |
| 328 | + long maxOrderIdTaskState = taskService.getMaxOrderIdTaskState(task.getTaskState(),task.getContext()); |
| 329 | + task.setOrderIdTaskState(++maxOrderIdTaskState); |
| 330 | + task = taskService.updatedViaTaskstate(task); |
| 331 | + return task.getUrl(); |
| 332 | + } else { |
| 333 | + return "redirect:/taskstate/inbox"; |
| 334 | + } |
| 335 | + } |
| 336 | + |
| 337 | + @RequestMapping(path = "/{taskId}/setfocus", method = RequestMethod.GET) |
| 338 | + public final String setFocusGet( |
| 339 | + @PathVariable("taskId") Task task, |
| 340 | + @RequestParam(required=false) String back |
| 341 | + ){ |
| 342 | + if(task !=null) { |
| 343 | + task.setFocus(); |
| 344 | + task = taskService.updatedViaTaskstate(task); |
| 345 | + return task.getUrl(); |
| 346 | + } else { |
| 347 | + return "redirect:/taskstate/inbox"; |
| 348 | + } |
| 349 | + } |
| 350 | + |
| 351 | + @RequestMapping(path = "/{taskId}/unsetfocus", method = RequestMethod.GET) |
| 352 | + public final String unsetFocusGet( |
| 353 | + @PathVariable("taskId") Task task, |
| 354 | + @RequestParam(required=false) String back |
| 355 | + ){ |
| 356 | + if(task !=null) { |
| 357 | + task.unsetFocus(); |
| 358 | + task = taskService.updatedViaTaskstate(task); |
| 359 | + return task.getUrl(); |
| 360 | + } else { |
| 361 | + return "redirect:/taskstate/inbox"; |
| 362 | + } |
| 363 | + } |
| 364 | +} |
0 commit comments