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