5
5
import org .springframework .beans .factory .annotation .Autowired ;
6
6
import org .springframework .core .io .ClassPathResource ;
7
7
import org .springframework .core .io .InputStreamResource ;
8
+ import org .springframework .data .domain .Page ;
8
9
import org .springframework .http .HttpHeaders ;
9
10
import org .springframework .http .ResponseEntity ;
10
11
import org .springframework .core .io .Resource ;
@@ -127,7 +128,6 @@ public String profile(Model model, HttpServletRequest request) {
127
128
128
129
//edit profile page
129
130
@ GetMapping ("/MONeditProfile" )
130
-
131
131
public String editProfile (Model model , HttpServletRequest request ) {
132
132
String emailName = request .getUserPrincipal ().getName ();
133
133
Optional <User > mon = monServ .findByEmail (emailName );
@@ -147,7 +147,6 @@ public String addEditedMonitor(Model model, HttpServletRequest request,
147
147
@ RequestParam String phone ,
148
148
@ RequestParam String birthdayDate ,
149
149
@ RequestParam String hiringDate ,
150
- @ RequestParam (required = false ) Long activityName ,
151
150
@ RequestParam String description ,
152
151
@ RequestParam (name = "image" , required = false ) MultipartFile image ) throws IOException {
153
152
String emailName = request .getUserPrincipal ().getName ();
@@ -163,42 +162,10 @@ public String addEditedMonitor(Model model, HttpServletRequest request,
163
162
user .setAddress (address );
164
163
user .setPostalCode (postalCode );
165
164
user .setPhone (phone );
166
- user .getBirthday ().setDay (birthdayDate .substring (8 , 10 ));
167
- user .getBirthday ().setMonth (birthdayDate .substring (5 , 7 ));
168
- user .getBirthday ().setYear (birthdayDate .substring (0 ,4 ));
169
- user .getBirthday ().generateSpanishFormat ();
170
- user .getHiringDate ().setDay (hiringDate .substring (8 , 10 ));
171
- user .getHiringDate ().setMonth (hiringDate .substring (5 , 7 ));
172
- user .getHiringDate ().setYear (hiringDate .substring (0 , 4 ));
173
- user .getHiringDate ().generateSpanishFormat ();
174
-
175
- if (activityName == -1 ) {
176
- //Delete association to an activity
177
- Activity act = user .getACT1 ();
178
- act .setMonitorName (null );
179
- actServ .save (act );
180
- user .setACT1 (null );
181
- } else if (activityName != -1 && user .getACT1 () == null ) {
182
- //Add activity to a monitor without previous activity
183
- Optional <Activity > activityOptional = actServ .findById (activityName );
184
- activityOptional .orElseThrow ().setMonitorName (user .getName ());
185
- actServ .save (activityOptional .get ());
186
- user .setACT1 (activityOptional .get ());
187
- } else if (activityName != -1 && user .getACT1 () != null && !user .getACT1 ().getId ().equals (activityName )) {
188
- //Add activity to monitor with previous activity associated
189
- //1 -> Change monitorName in old activity
190
- if (user .getACT1 () != null ) {
191
- user .getACT1 ().setMonitorName (null );
192
- actServ .save (user .getACT1 ());
193
- }
194
- //2 -> Change monitor name in new Activity
195
- Optional <Activity > activityOptional = actServ .findById (activityName );
196
- activityOptional .orElseThrow ().setMonitorName (user .getName ());
197
- actServ .save (activityOptional .get ());
198
- //3 -> Add activity to user
199
- user .setACT1 (activityOptional .get ());
200
- }
201
-
165
+ DateType date = new DateType (birthdayDate .substring (0 ,4 ), birthdayDate .substring (5 , 7 ), birthdayDate .substring (8 , 10 ));
166
+ user .setBirthday (date );
167
+ date = new DateType (hiringDate .substring (0 ,4 ), hiringDate .substring (5 , 7 ), hiringDate .substring (8 , 10 ));
168
+ user .setHiringDate (date );
202
169
user .setDescription (description );
203
170
if (!image .isEmpty ())
204
171
user .setImage (BlobProxy .generateProxy (image .getInputStream (), image .getSize ()));
@@ -215,8 +182,11 @@ public String addEditedMonitor(Model model, HttpServletRequest request,
215
182
@ GetMapping ("/MONexerciseTable" )
216
183
public String exerciseTable (Model model , HttpServletRequest request ) {
217
184
List <ExerciseTable > all = exerciseTableServ .findAll ();
185
+ Page <ExerciseTable > exerTabPage = exerciseTableServ .findPage (0 );
218
186
model .addAttribute ("exerciseTableList" , all );
219
187
model .addAttribute ("monitor" , monServ .findByEmail (request .getUserPrincipal ().getName ()).orElseThrow ());
188
+ model .addAttribute ("listMON" , exerTabPage .toList ());
189
+ model .addAttribute ("last" , exerTabPage .getTotalPages ());
220
190
return "USRMON_03ExerciseTable" ;
221
191
}
222
192
@@ -231,6 +201,15 @@ public String seeExerciseTableInfo(Model model, @PathVariable long id, HttpServl
231
201
return "USRMON_03ExerciseTable" ;
232
202
}
233
203
204
+ //ajax
205
+ @ GetMapping ("/MONexerciseTable/page/{page}" )
206
+ public String getExerciseTablePageMonitor (Model model , @ PathVariable int page ){
207
+ Page <ExerciseTable > exerTabPage = exerciseTableServ .findPage (page );
208
+ model .addAttribute ("listMON" , exerTabPage .toList ());
209
+
210
+ return "USRMON_03ExerciseTableAJAX" ;
211
+ }
212
+
234
213
@ GetMapping ("/MONexerciseTable/{id}/image" )
235
214
public ResponseEntity <Object > downloadExerciseImage (@ PathVariable long id ) throws SQLException {
236
215
Optional <ExerciseTable > optExerTab = exerciseTableServ .findById (id );
@@ -249,20 +228,48 @@ public ResponseEntity<Object> downloadExerciseImage(@PathVariable long id) throw
249
228
//add exercise table page
250
229
@ GetMapping ("/MONaddNewExerciseTable" )
251
230
public String newExerciseTable (Model model , HttpServletRequest request ) {
231
+ List <User > all = monServ .findByUserType ("monitor" );
232
+ Page <User > userPage = monServ .findPageClient (0 , "monitor" );
233
+
234
+ List <Exercise > allEx = exerServ .findAll ();
235
+ Page <Exercise > exerPage = exerServ .findPage (0 );
236
+
252
237
model .addAttribute ("monitor" , monServ .findByEmail (request .getUserPrincipal ().getName ()).orElseThrow ());
253
238
model .addAttribute ("exercises" , exerServ .findAll ());
239
+
240
+ model .addAttribute ("exerList" , allEx );
241
+ model .addAttribute ("clientList" , all );
242
+ model .addAttribute ("list" , exerPage .toList ());
243
+ model .addAttribute ("last" , exerPage .getTotalPages ());
244
+
254
245
return "USRMON_06AddExerciseTable" ;
255
246
}
256
247
248
+ //ajax for adding new exercises
249
+ @ GetMapping ("/MONaddNewExercise/page/{page}" )
250
+ public String getExercisesPageMonitor (Model model , @ PathVariable int page ){
251
+ Page <Exercise > exerTabPage = exerServ .findPage (page );
252
+ model .addAttribute ("list" , exerTabPage .toList ());
253
+
254
+ return "USRMON_06AddExerciseTableAJAX" ;
255
+
256
+ }
257
+
257
258
@ PostMapping ("/MONaddNewExerciseTable" )
258
- public String addNewExerciseTable (Model model , @ RequestParam String name , @ RequestParam String description ,
259
- @ RequestParam List <Long > id ,@ RequestParam ("image" ) MultipartFile image ) throws IOException {
259
+ public String addNewExerciseTable (Model model ,
260
+ @ RequestParam (required = false ) List <Long > id ,
261
+ @ RequestParam String name ,
262
+ @ RequestParam String description ,
263
+ @ RequestParam ("image" ) MultipartFile image ) throws IOException {
260
264
ExerciseTable exerciseTable = new ExerciseTable (name , description );
261
- List <Exercise > auxList = new ArrayList <>(id .size ());
262
- for (Long l : id ){
263
- auxList .add (exerServ .findById (l ).orElseThrow ());
265
+ if (!id .isEmpty ()){
266
+ List <Exercise > auxList = new ArrayList <>(id .size ());
267
+
268
+ for (Long l : id ){
269
+ auxList .add (exerServ .findById (l ).orElseThrow ());
270
+ }
271
+ exerciseTable .setExercises (auxList );
264
272
}
265
- exerciseTable .setExercises (auxList );
266
273
if (image .isEmpty ()) {
267
274
Resource imageNotAdded = new ClassPathResource ("/sample_images/imageNotAddedActivity.jpeg" );
268
275
exerciseTable .setImage (BlobProxy .generateProxy (imageNotAdded .getInputStream (), imageNotAdded .contentLength ()));
@@ -331,7 +338,6 @@ public String deleteExerciseTable(Model model, @PathVariable long id) {
331
338
return "redirect:/exerciseTable" ;
332
339
}
333
340
334
-
335
341
//grupal activities page
336
342
@ GetMapping ("/MONactivities" )
337
343
public String activities (Model model , HttpServletRequest request ) {
@@ -367,5 +373,4 @@ public ResponseEntity<Object> downloadImage(@PathVariable long id) throws SQLExc
367
373
return ResponseEntity .notFound ().build ();
368
374
}
369
375
370
-
371
376
}
0 commit comments