1
|
package vldc.aswi.web.controller;
|
2
|
|
3
|
import org.springframework.beans.factory.annotation.Autowired;
|
4
|
import org.springframework.stereotype.Controller;
|
5
|
import org.springframework.ui.ModelMap;
|
6
|
import org.springframework.validation.BeanPropertyBindingResult;
|
7
|
import org.springframework.validation.BindingResult;
|
8
|
import org.springframework.web.bind.WebDataBinder;
|
9
|
import org.springframework.web.bind.annotation.*;
|
10
|
import org.springframework.web.servlet.ModelAndView;
|
11
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
12
|
import vldc.aswi.domain.*;
|
13
|
import vldc.aswi.domain.parameter.Parameter;
|
14
|
import vldc.aswi.domain.parameter.ParameterInConfiguration;
|
15
|
import vldc.aswi.model.ExportType;
|
16
|
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow;
|
17
|
import vldc.aswi.service.*;
|
18
|
import vldc.aswi.service.parameter.ParameterManager;
|
19
|
import vldc.aswi.service.parameter.ParameterTypeManager;
|
20
|
import vldc.aswi.utils.AuthControl;
|
21
|
import vldc.aswi.validators.AssemblyValidator;
|
22
|
import vldc.aswi.utils.Utils;
|
23
|
import vldc.aswi.validators.ConfigurationValidator;
|
24
|
import javax.servlet.http.HttpServletResponse;
|
25
|
import javax.validation.Valid;
|
26
|
import java.util.ArrayList;
|
27
|
import java.util.Comparator;
|
28
|
import java.util.List;
|
29
|
|
30
|
/**
|
31
|
* Controller for assemblies and configurations
|
32
|
*/
|
33
|
@Controller
|
34
|
public class AssemblyController extends BasicController {
|
35
|
|
36
|
/** Autowired sql query manager. */
|
37
|
@Autowired
|
38
|
private SqlQueryManager sqlQueryManager;
|
39
|
|
40
|
/** Autowired assembly manager. */
|
41
|
@Autowired
|
42
|
private AssemblyManager assemblyManager;
|
43
|
|
44
|
/** Autowired parameter manager. */
|
45
|
@Autowired
|
46
|
private ParameterManager parameterManager;
|
47
|
|
48
|
/** Autowired export manager. */
|
49
|
@Autowired
|
50
|
private ExportManager exportManager;
|
51
|
|
52
|
/** Autowired configuration manager */
|
53
|
@Autowired
|
54
|
private ConfigurationManager configurationManager;
|
55
|
|
56
|
/** Autowired role manager. */
|
57
|
@Autowired
|
58
|
private RoleManager roleManager;
|
59
|
|
60
|
/** Autowired parameterType manager. */
|
61
|
@Autowired
|
62
|
private ParameterTypeManager parameterTypeManager;
|
63
|
|
64
|
/** Autowired operator manager. */
|
65
|
@Autowired
|
66
|
private OperatorManager operatorManager;
|
67
|
|
68
|
/** Autowired function manager. */
|
69
|
@Autowired
|
70
|
private FunctionManager functionManager;
|
71
|
|
72
|
/** Autowired location manager. */
|
73
|
@Autowired
|
74
|
private LocationManager locationManager;
|
75
|
|
76
|
/** Autowired assembly validator */
|
77
|
@Autowired
|
78
|
private AssemblyValidator assemblyValidator;
|
79
|
|
80
|
/** Autowired configuration validator */
|
81
|
@Autowired
|
82
|
private ConfigurationValidator configurationValidator;
|
83
|
|
84
|
/** Name of thymeleaf parameter, which will contain text in assembly form. */
|
85
|
private String assemblyTitleName = "title";
|
86
|
|
87
|
/** Title text for assembly edit form. */
|
88
|
private String assemblyEditTitleText = "Editace - ";
|
89
|
|
90
|
/** Title text for new assembly form. */
|
91
|
private String assemblyNewTitleText = "Vytvoření nové sestavy";
|
92
|
|
93
|
/** Title text for table generation form. */
|
94
|
private String generateTableTitleText = "Generování tabulky - ";
|
95
|
|
96
|
/**
|
97
|
* Bind assembly validator.
|
98
|
* @param binder Binder.
|
99
|
*/
|
100
|
@InitBinder("assembly")
|
101
|
protected void userBinder(WebDataBinder binder) {
|
102
|
binder.addValidators(this.assemblyValidator);
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* Get method for form, where configuration is created from assembly.
|
107
|
* @param id - ID of assembly.
|
108
|
* @return ModelAndView for assembly.
|
109
|
*/
|
110
|
@GetMapping("/assembly")
|
111
|
public ModelAndView assemblyGet(@RequestParam("assemblyID") String id) {
|
112
|
ModelAndView modelAndView = new ModelAndView("assembly");
|
113
|
|
114
|
ModelMap modelMap = modelAndView.getModelMap();
|
115
|
|
116
|
Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
|
117
|
|
118
|
// TODO: 04.05.2020 error page when id doesn't exist
|
119
|
|
120
|
String roleName = AuthControl.getRoleName();
|
121
|
|
122
|
if (roleName == null) {
|
123
|
// TODO: 04.05.2020 error message, user not authenticated
|
124
|
}
|
125
|
|
126
|
Role role = roleManager.getRole(roleName);
|
127
|
|
128
|
if (!assembly.getRoles().contains(role) && !role.getName().equals("Administrátor")) {
|
129
|
// TODO: 04.05.2020 Error page, wrong role
|
130
|
return new ModelAndView("redirect:/");
|
131
|
}
|
132
|
|
133
|
Configuration configuration = new Configuration();
|
134
|
|
135
|
configuration.setAssembly(assembly);
|
136
|
configuration.setTableName(assembly.getName());
|
137
|
configuration.setParametersInConfiguration(new ArrayList<>());
|
138
|
for(Parameter parameter : assembly.getParameters()) {
|
139
|
ParameterInConfiguration parameterInConfiguration = new ParameterInConfiguration();
|
140
|
parameterInConfiguration.setParameter(parameter);
|
141
|
parameterInConfiguration.setConfiguration(configuration);
|
142
|
parameterInConfiguration.setOperator(new Operator());
|
143
|
parameterInConfiguration.setLocation(new Location());
|
144
|
parameterInConfiguration.setParameterOrder(parameter.getParameterOrder());
|
145
|
configuration.getParametersInConfiguration().add(parameterInConfiguration);
|
146
|
}
|
147
|
|
148
|
Comparator<ParameterInConfiguration> comparator = Comparator.comparingInt(o -> o.getParameter().getParameterOrder());
|
149
|
|
150
|
boolean isNotRemoveEmpty = false;
|
151
|
modelMap.addAttribute("isNotRemoveEmpty", isNotRemoveEmpty);
|
152
|
modelMap.addAttribute("configuration", configuration);
|
153
|
modelMap.addAttribute("comparator", comparator);
|
154
|
modelMap.addAttribute("formAction", "/assembly?assemblyID=" + assembly.getId());
|
155
|
|
156
|
return modelAndView;
|
157
|
}
|
158
|
|
159
|
/**
|
160
|
* Post method for form, where configuration is created from assembly.
|
161
|
* @param newConfiguration - Configuration values.
|
162
|
* @param bindingResult - Error results from assembly validators.
|
163
|
* @return ModelAndView for assembly.
|
164
|
*/
|
165
|
@PostMapping("/assembly")
|
166
|
public ModelAndView assemblyPost(Configuration newConfiguration,
|
167
|
BindingResult bindingResult,
|
168
|
RedirectAttributes atts,
|
169
|
@RequestParam("isNotRemoveEmpty") String isNotRemoveEmptyString,
|
170
|
@RequestParam("assemblyID") String id,
|
171
|
@RequestParam(required=false, value="generateTable") String generateTable,
|
172
|
@RequestParam(required=false, value="exportXlsx") String exportXlsx,
|
173
|
@RequestParam(required=false, value="exportPdf") String exportPdf,
|
174
|
@RequestParam(required=false, value="saveConfiguration") String saveConfiguration,
|
175
|
RedirectAttributes redirectAttributes,
|
176
|
HttpServletResponse response
|
177
|
)
|
178
|
{
|
179
|
|
180
|
ModelAndView modelAndView = new ModelAndView();
|
181
|
ModelMap modelMap = modelAndView.getModelMap();
|
182
|
boolean isNotRemoveEmpty = isNotRemoveEmptyString == null || !isNotRemoveEmptyString.equals("");
|
183
|
|
184
|
if (bindingResult.hasErrors()) {
|
185
|
// TODO: 04.05.2020 Error message
|
186
|
modelAndView.setViewName("redirect:/");
|
187
|
|
188
|
return modelAndView;
|
189
|
}
|
190
|
|
191
|
if (generateTable != null)
|
192
|
{
|
193
|
Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
|
194
|
newConfiguration.setAssembly(assembly);
|
195
|
newConfiguration.setTableName(assembly.getName());
|
196
|
|
197
|
// If configuration name is empty, then replace it by assembly name.
|
198
|
if (newConfiguration.getName().equals("")) {
|
199
|
newConfiguration.setName(assembly.getName());
|
200
|
}
|
201
|
|
202
|
prepareForTable(newConfiguration);
|
203
|
BindingResult errors = new BeanPropertyBindingResult(newConfiguration, newConfiguration.getClass().getName());
|
204
|
configurationValidator.validate(newConfiguration, errors);
|
205
|
if (errors.hasErrors()) {
|
206
|
addConfigurationDataIntoModelAndView(newConfiguration, modelAndView, modelMap);
|
207
|
|
208
|
modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(errors));
|
209
|
modelMap.addAttribute(assemblyTitleName, generateTableTitleText + " " + newConfiguration.getName());
|
210
|
}
|
211
|
else {
|
212
|
List<ContingencyTableRow> rows = this.sqlQueryManager.getContingencyTableRow(newConfiguration.getAssembly(),
|
213
|
newConfiguration.getParametersInConfiguration(), isNotRemoveEmpty);
|
214
|
|
215
|
modelMap.addAttribute("configurationID", id);
|
216
|
modelMap.addAttribute("contingencyTableRows", rows);
|
217
|
modelMap.addAttribute("isNotRemoveEmpty", isNotRemoveEmpty);
|
218
|
addConfigurationDataIntoModelAndView(newConfiguration, modelAndView, modelMap);
|
219
|
}
|
220
|
}
|
221
|
else if (exportXlsx != null)
|
222
|
{
|
223
|
prepareForTable(newConfiguration);
|
224
|
this.exportManager.exportContingencyTable(newConfiguration, isNotRemoveEmpty, response, ExportType.XLSX);
|
225
|
|
226
|
// Return null, because we want to continue on our page with selected parameters / attributes.
|
227
|
return null;
|
228
|
}
|
229
|
else if (exportPdf != null)
|
230
|
{
|
231
|
prepareForTable(newConfiguration);
|
232
|
this.exportManager.exportContingencyTable(newConfiguration, isNotRemoveEmpty, response, ExportType.PDF);
|
233
|
|
234
|
// Return null, because we want to continue on our page with selected parameters / attributes.
|
235
|
return null;
|
236
|
|
237
|
}
|
238
|
else if (saveConfiguration != null)
|
239
|
{
|
240
|
Configuration configuration = configurationManager.saveConfiguration(newConfiguration, "");
|
241
|
|
242
|
initializeFields(configuration);
|
243
|
|
244
|
redirectAttributes.addFlashAttribute(assemblySuccessName, "Šablona byla úspěšně uložena");
|
245
|
|
246
|
modelAndView.setViewName("redirect:/configuration?configurationID=" + configuration.getId());
|
247
|
}
|
248
|
|
249
|
return modelAndView;
|
250
|
}
|
251
|
|
252
|
/**
|
253
|
* Adds configuration and other data to modelAndView.
|
254
|
* @param newConfiguration Configuration to be added to the modelAndView.
|
255
|
* @param modelAndView ModelAndView to which to which modelMap belongs.
|
256
|
* @param modelMap ModelMap to which data is added.
|
257
|
*/
|
258
|
private void addConfigurationDataIntoModelAndView(Configuration newConfiguration, ModelAndView modelAndView, ModelMap modelMap) {
|
259
|
Comparator<ParameterInConfiguration> comparator = Comparator.comparingInt(o -> o.getParameter().getParameterOrder());
|
260
|
initializeFields(newConfiguration);
|
261
|
modelMap.addAttribute("configuration", newConfiguration);
|
262
|
modelMap.addAttribute("comparator", comparator);
|
263
|
modelMap.addAttribute("formAction", "/assembly?assemblyID=" + newConfiguration.getAssembly().getId());
|
264
|
modelAndView.setViewName("assembly");
|
265
|
}
|
266
|
|
267
|
/**
|
268
|
* Get method for assembly delete.
|
269
|
* @param id - ID of assembly.
|
270
|
* @return ModelAndView for index.
|
271
|
*/
|
272
|
@GetMapping("/assembly_delete")
|
273
|
public ModelAndView assemblyDeleteGet(@RequestParam("assemblyID") String id, RedirectAttributes redirectAttributes) {
|
274
|
ModelAndView modelAndView = new ModelAndView("redirect:/");
|
275
|
|
276
|
Long assemblyId = Utils.tryParseLong(id);
|
277
|
|
278
|
if (assemblyId == null) {
|
279
|
redirectAttributes.addFlashAttribute(assemblyErrorName, "Sestavu se nepodařilo odstranit.");
|
280
|
}
|
281
|
boolean success = this.assemblyManager.deleteAssembly(assemblyId);
|
282
|
|
283
|
if (success)
|
284
|
{
|
285
|
redirectAttributes.addFlashAttribute(assemblySuccessName, "Sestava byla úspěšně odstraněna");
|
286
|
}
|
287
|
else
|
288
|
{
|
289
|
redirectAttributes.addFlashAttribute(assemblyErrorName, "Sestavu se nepodařilo odstranit");
|
290
|
}
|
291
|
|
292
|
return modelAndView;
|
293
|
}
|
294
|
|
295
|
/**
|
296
|
* Get method for assembly edit.
|
297
|
* @param id - Id of assembly.
|
298
|
* @return ModelAndView for assembly edit.
|
299
|
*/
|
300
|
@GetMapping("/assembly_edit")
|
301
|
public ModelAndView assemblyEditGet(@RequestParam("assemblyID") String id) {
|
302
|
long startTime = System.currentTimeMillis();
|
303
|
ModelAndView modelAndView = new ModelAndView("assembly_manage");
|
304
|
|
305
|
ModelMap modelMap = modelAndView.getModelMap();
|
306
|
|
307
|
Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
|
308
|
|
309
|
modelMap.addAttribute("assembly", assembly);
|
310
|
|
311
|
modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
|
312
|
|
313
|
modelMap.addAttribute("allRoles", this.roleManager.getRoles());
|
314
|
modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
|
315
|
modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
|
316
|
modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
|
317
|
modelMap.addAttribute("allLocations", this.locationManager.getLocations());
|
318
|
long estimatedTime = System.currentTimeMillis() - startTime;
|
319
|
System.out.println("EDIT GET estimated - " + ((double)estimatedTime / 1000) + " sekund ");
|
320
|
|
321
|
return modelAndView;
|
322
|
}
|
323
|
|
324
|
/**
|
325
|
* Post method for assembly edit.
|
326
|
* @param assembly - Edited assembly values.
|
327
|
* @param result - Validator results.
|
328
|
* @param assemblyIsPublic - Attribute indicating if assembly is public or not.
|
329
|
* @return ModelAndView for assembly edit.
|
330
|
*/
|
331
|
@PostMapping("/assembly_edit")
|
332
|
public ModelAndView assemblyEditPost(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result,
|
333
|
@Valid @ModelAttribute("checkboxPublic") String assemblyIsPublic) {
|
334
|
long startTime = System.currentTimeMillis();
|
335
|
ModelAndView modelAndView = new ModelAndView("assembly_manage");
|
336
|
ModelMap modelMap = modelAndView.getModelMap();
|
337
|
|
338
|
if (result.hasErrors()) {
|
339
|
modelMap.addAttribute("assembly", assembly);
|
340
|
|
341
|
modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
|
342
|
modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
|
343
|
}
|
344
|
else {
|
345
|
if (assemblyIsPublic.equals("on")) assembly.setIsPublic(1);
|
346
|
else assembly.setIsPublic(0);
|
347
|
|
348
|
// TODO: chybí defaultValue.
|
349
|
Long assemblyID = this.assemblyManager.updateAssembly(assembly);
|
350
|
|
351
|
this.parameterManager.updateParameters(assemblyID, assembly.getParameters());
|
352
|
Assembly updatedAssembly = this.assemblyManager.getAssemblyById(assemblyID);
|
353
|
|
354
|
modelMap.addAttribute("assembly", updatedAssembly);
|
355
|
modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + updatedAssembly.getName());
|
356
|
modelMap.addAttribute(assemblySuccessName, "Úspěšně uloženo");
|
357
|
}
|
358
|
|
359
|
modelMap.addAttribute("allRoles", this.roleManager.getRoles());
|
360
|
modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
|
361
|
modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
|
362
|
modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
|
363
|
modelMap.addAttribute("allLocations", this.locationManager.getLocations());
|
364
|
long estimatedTime = System.currentTimeMillis() - startTime;
|
365
|
System.out.println("EDIT POST estimated - " + ((double)estimatedTime / 1000) + " sekund ");
|
366
|
|
367
|
return modelAndView;
|
368
|
}
|
369
|
|
370
|
/**
|
371
|
* Get method for new assembly form.
|
372
|
* @return ModelAndView for new assembly.
|
373
|
*/
|
374
|
@GetMapping("/assembly_new")
|
375
|
public ModelAndView assemblyNewGet() {
|
376
|
ModelAndView modelAndView = new ModelAndView("assembly_manage");
|
377
|
|
378
|
ModelMap modelMap = modelAndView.getModelMap();
|
379
|
|
380
|
modelMap.addAttribute("assembly", new Assembly());
|
381
|
|
382
|
modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
|
383
|
|
384
|
modelMap.addAttribute("allRoles", this.roleManager.getRoles());
|
385
|
modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
|
386
|
modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
|
387
|
modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
|
388
|
modelMap.addAttribute("allLocations", this.locationManager.getLocations());
|
389
|
|
390
|
return modelAndView;
|
391
|
}
|
392
|
|
393
|
/**
|
394
|
* Post method for new assembly form.
|
395
|
* @param assembly - Assembly values.
|
396
|
* @param result - Validator results.
|
397
|
* @return
|
398
|
*/
|
399
|
@PostMapping("/assembly_new")
|
400
|
public ModelAndView assemblyNewPost(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result) {
|
401
|
ModelAndView modelAndView = new ModelAndView();
|
402
|
ModelMap modelMap = modelAndView.getModelMap();
|
403
|
|
404
|
if (result.hasErrors()) {
|
405
|
modelAndView.setViewName("assembly_manage");
|
406
|
modelMap.addAttribute("assembly", assembly);
|
407
|
|
408
|
modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
|
409
|
modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
|
410
|
}
|
411
|
else {
|
412
|
// TODO: chybí získání default value.
|
413
|
Long assemblyId = this.assemblyManager.createAssembly(assembly);
|
414
|
this.parameterManager.addParameters(assemblyId, assembly.getParameters());
|
415
|
|
416
|
modelAndView.setViewName("redirect:/assembly_edit?assemblyID=" + assemblyId);
|
417
|
modelMap.addAttribute(assemblySuccessName, "Sestava byla úspěšně vytvořena");
|
418
|
}
|
419
|
|
420
|
modelMap.addAttribute("allRoles", this.roleManager.getRoles());
|
421
|
modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
|
422
|
modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
|
423
|
modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
|
424
|
modelMap.addAttribute("allLocations", this.locationManager.getLocations());
|
425
|
|
426
|
return modelAndView;
|
427
|
}
|
428
|
|
429
|
/**
|
430
|
* Initializes fields of a given configuration
|
431
|
* @param configuration configuration which fields should be initialized
|
432
|
*/
|
433
|
private void initializeFields(Configuration configuration) {
|
434
|
for(ParameterInConfiguration parameterInConfiguration : configuration.getParametersInConfiguration()) {
|
435
|
if(parameterInConfiguration.getLocation() == null) {
|
436
|
parameterInConfiguration.setLocation(new Location());
|
437
|
}
|
438
|
if(parameterInConfiguration.getOperator() == null) {
|
439
|
parameterInConfiguration.setOperator(new Operator());
|
440
|
}
|
441
|
if(parameterInConfiguration.getFunctions() == null) {
|
442
|
parameterInConfiguration.setFunctions(new ArrayList<>());
|
443
|
}
|
444
|
}
|
445
|
}
|
446
|
|
447
|
/**
|
448
|
* Prepares the configuration for contingency table generation. Initializes assembly and parameters of parameters in
|
449
|
* configuration.
|
450
|
* @param configuration configuration to be prepared.
|
451
|
*/
|
452
|
private void prepareForTable(Configuration configuration) {
|
453
|
Assembly assembly = assemblyManager.getAssemblyById(configuration.getAssembly().getId());
|
454
|
configuration.setAssembly(assembly);
|
455
|
|
456
|
for(int i = 0; i < configuration.getParametersInConfiguration().size(); i++) {
|
457
|
ParameterInConfiguration parameterInConfiguration = configuration.getParametersInConfiguration().get(i);
|
458
|
parameterInConfiguration.setParameter(parameterManager.getParameterById(parameterInConfiguration.getParameter().getId()));
|
459
|
if (parameterInConfiguration.getLocation() != null) {
|
460
|
parameterInConfiguration.setLocation(locationManager.getLocationById(parameterInConfiguration.getLocation().getId()));
|
461
|
} else {
|
462
|
parameterInConfiguration.setLocation(new Location());
|
463
|
}
|
464
|
parameterInConfiguration.setConfiguration(configuration);
|
465
|
}
|
466
|
|
467
|
Comparator<ParameterInConfiguration> comparator = Comparator.comparingInt(o -> o.getParameter().getParameterOrder());
|
468
|
configuration.getParametersInConfiguration().sort(comparator);
|
469
|
}
|
470
|
}
|