22
33import lombok .extern .slf4j .Slf4j ;
44
5+ import org .springframework .beans .factory .annotation .Autowired ;
56import org .springframework .stereotype .Controller ;
7+ import org .springframework .ui .Model ;
8+ import org .springframework .validation .BindingResult ;
9+ import org .springframework .validation .ObjectError ;
610import org .springframework .web .bind .annotation .*;
11+ import org .woehlke .simpleworklist .breadcrumb .Breadcrumb ;
712import org .woehlke .simpleworklist .common .AbstractController ;
13+ import org .woehlke .simpleworklist .context .Context ;
814import org .woehlke .simpleworklist .project .Project ;
15+ import org .woehlke .simpleworklist .taskstate .TaskMoveService ;
16+ import org .woehlke .simpleworklist .taskstate .TaskState ;
17+ import org .woehlke .simpleworklist .user .UserSessionBean ;
18+ import org .woehlke .simpleworklist .user .account .UserAccount ;
19+
20+ import javax .validation .Valid ;
21+ import java .util .List ;
22+ import java .util .Locale ;
923
1024@ Slf4j
1125@ Controller
1226@ RequestMapping (path = "/task" )
1327public class TaskController extends AbstractController {
1428
15- @ RequestMapping (path = "/delete/{taskId}" , method = RequestMethod .GET )
29+ private final TaskMoveService taskMoveService ;
30+
31+ @ Autowired
32+ public TaskController (TaskMoveService taskMoveService ) {
33+ this .taskMoveService = taskMoveService ;
34+ }
35+
36+ @ RequestMapping (path = "/add" , method = RequestMethod .GET )
37+ public final String addNewTaskToInboxGet (
38+ @ ModelAttribute ("userSession" ) UserSessionBean userSession ,
39+ Locale locale , Model model
40+ ) {
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 "task/addToInbox" ;
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+ Context context = super .getContext (userSession );
71+ if (result .hasErrors ()) {
72+ for (ObjectError e : result .getAllErrors ()) {
73+ log .info (e .toString ());
74+ }
75+ Boolean mustChooseArea = false ;
76+ task .setContext (context );
77+ Breadcrumb breadcrumb = breadcrumbService .getBreadcrumbForTaskstate (TaskState .INBOX ,locale );
78+ model .addAttribute ("mustChooseArea" , mustChooseArea );
79+ model .addAttribute ("breadcrumb" , breadcrumb );
80+ model .addAttribute ("task" , task );
81+ return "task/addToProject" ;
82+ } else {
83+ task .setProject (null );
84+ if (task .getDueDate ()==null ){
85+ task .setTaskState (TaskState .INBOX );
86+ } else {
87+ task .setTaskState (TaskState .SCHEDULED );
88+ }
89+ task .setFocus (false );
90+ task .setContext (context );
91+ long maxOrderIdProject = taskMoveService .getMaxOrderIdProject (task .getProject (),context );
92+ task .setOrderIdProject (++maxOrderIdProject );
93+ long maxOrderIdTaskState = taskMoveService .getMaxOrderIdTaskState (task .getTaskState (),task .getContext ());
94+ task .setOrderIdTaskState (++maxOrderIdTaskState );
95+ task = taskService .saveAndFlush (task );
96+ log .info (task .toString ());
97+ return "redirect:/taskstate/" + task .getTaskState ().name ().toLowerCase ();
98+ }
99+ }
100+
101+ @ RequestMapping (path = "/{taskId}/edit" , method = RequestMethod .GET )
102+ public final String editTaskGet (
103+ @ PathVariable ("taskId" ) Task task ,
104+ @ ModelAttribute ("userSession" ) UserSessionBean userSession ,
105+ Locale locale , Model model
106+ ) {
107+ log .info ("editTaskGet" );
108+ UserAccount userAccount = userAccountLoginSuccessService .retrieveCurrentUser ();
109+ List <Context > contexts = contextService .getAllForUser (userAccount );
110+ if (task != null ) {
111+ Project thisProject = null ;
112+ if (task .getProject () == null ) {
113+ thisProject = new Project ();
114+ thisProject .setId (0L );
115+ } else {
116+ thisProject = task .getProject ();
117+ }
118+ model .addAttribute ("thisProject" , thisProject );
119+ Breadcrumb breadcrumb = breadcrumbService .getBreadcrumbForShowOneProject (thisProject ,locale );
120+ model .addAttribute ("breadcrumb" , breadcrumb );
121+ model .addAttribute ("task" , task );
122+ model .addAttribute ("areas" , contexts );
123+ return "task/edit" ;
124+ } else {
125+ return "redirect:/taskstate/inbox" ;
126+ }
127+ }
128+
129+ @ RequestMapping (path = "/{taskId}/edit" , method = RequestMethod .POST )
130+ public final String editTaskPost (
131+ @ PathVariable long taskId ,
132+ @ Valid Task task ,
133+ @ ModelAttribute ("userSession" ) UserSessionBean userSession ,
134+ BindingResult result ,
135+ Locale locale ,
136+ Model model
137+ ) {
138+ log .info ("editTaskPost" );
139+ Task persistentTask = taskService .findOne (taskId );
140+ long projectId = 0 ;
141+ Project thisProject ;
142+ if (persistentTask .getProject () == null ) {
143+ thisProject = new Project ();
144+ thisProject .setId (0L );
145+ } else {
146+ thisProject = persistentTask .getProject ();
147+ projectId = thisProject .getId ();
148+ }
149+ if (result .hasErrors ()) {
150+ for (ObjectError e : result .getAllErrors ()) {
151+ log .info (e .toString ());
152+ }
153+ model .addAttribute ("thisProject" , thisProject );
154+ Breadcrumb breadcrumb = breadcrumbService .getBreadcrumbForShowOneProject (thisProject ,locale );
155+ model .addAttribute ("breadcrumb" , breadcrumb );
156+ model .addAttribute ("task" , task );
157+ return "task/edit" ;
158+ } else {
159+ persistentTask .setTitle (task .getTitle ());
160+ persistentTask .setText (task .getText ());
161+ if (task .getDueDate ()==null ){
162+ persistentTask .setDueDate (null );
163+ if (persistentTask .getTaskState ().compareTo (TaskState .SCHEDULED )==0 ){
164+ persistentTask .setTaskState (TaskState .INBOX );
165+ }
166+ } else {
167+ persistentTask .setDueDate (task .getDueDate ());
168+ persistentTask .setTaskState (TaskState .SCHEDULED );
169+ }
170+ persistentTask .setTaskTime (task .getTaskTime ());
171+ persistentTask .setTaskEnergy (task .getTaskEnergy ());
172+ boolean contextChanged = persistentTask .getContext ().equalsById (task .getContext ());
173+ if (contextChanged ){
174+ persistentTask .setContext (task .getContext ());
175+ if (thisProject .getId ()==0L ) {
176+ persistentTask .setRootProject ();
177+ } else if (thisProject .getContext ().equalsById (task .getContext ())){
178+ persistentTask .setProject (thisProject );
179+ }
180+ userSession .setContextId (task .getContext ().getId ());
181+ model .addAttribute ("userSession" , userSession );
182+ return "redirect:/project/0/" ;
183+ }
184+ taskService .saveAndFlush (persistentTask );
185+ return "redirect:/project/" + projectId + "/" ;
186+ }
187+ }
188+
189+ @ RequestMapping (path = "/{taskId}/delete" , method = RequestMethod .GET )
16190 public final String deleteTaskGet (@ PathVariable ("taskId" ) Task task ) {
17191 log .info ("deleteTaskGet" );
18192 if (task != null ){
@@ -21,7 +195,7 @@ public final String deleteTaskGet(@PathVariable("taskId") Task task) {
21195 return "redirect:/taskstate/trash" ;
22196 }
23197
24- @ RequestMapping (path = "/task/undelete/ {taskId}" , method = RequestMethod .GET )
198+ @ RequestMapping (path = "/{taskId}/undelete " , method = RequestMethod .GET )
25199 public final String undeleteTaskGet (@ PathVariable ("taskId" ) Task task ) {
26200 log .info ("undeleteTaskGet" );
27201 if (task != null ) {
@@ -32,7 +206,7 @@ public final String undeleteTaskGet(@PathVariable("taskId") Task task) {
32206 }
33207 }
34208
35- @ RequestMapping (path = "/transform/ {taskId}" , method = RequestMethod .GET )
209+ @ RequestMapping (path = "/{taskId}/transform " , method = RequestMethod .GET )
36210 public final String transformTaskIntoProjectGet (@ PathVariable ("taskId" ) Task task ) {
37211 log .info ("transformTaskIntoProjectGet" );
38212 if (task != null ) {
0 commit comments