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
|
System.out.println("Generuj tabulku");
|
179
|
prepareForTable(newConfiguration);
|
180
|
System.out.println("Generuj tabulku");
|
181
|
ModelMap modelMap = modelAndView.getModelMap();
|
182
|
modelMap.addAttribute("configurationID", id);
|
183
|
modelMap.addAttribute("contingencyTableRows", this.sqlQueryManager.getContingencyTableRow(newConfiguration));
|
184
|
Comparator<ParameterInConfiguration> comparator = Comparator.comparingInt(o -> o.getParameter().getParameterOrder());
|
185
|
initializeFields(newConfiguration);
|
186
|
modelMap.addAttribute("configuration", newConfiguration);
|
187
|
modelMap.addAttribute("comparator", comparator);
|
188
|
modelMap.addAttribute("formAction", "/assembly?assemblyID=" + newConfiguration.getAssembly().getId());
|
189
|
modelAndView.setViewName("assembly");
|
190
|
}
|
191
|
else if (exportXls != null)
|
192
|
{
|
193
|
System.out.println("Generuj XLS");
|
194
|
}
|
195
|
else if (exportPdf != null)
|
196
|
{
|
197
|
System.out.println("Generuj PDF");
|
198
|
}
|
199
|
else if (saveConfiguration != null)
|
200
|
{
|
201
|
System.out.println("ulož konfiguraci");
|
202
|
|
203
|
Configuration configuration = configurationManager.saveConfiguration(newConfiguration, "");
|
204
|
|
205
|
initializeFields(configuration);
|
206
|
|
207
|
redirectAttributes.addFlashAttribute(assemblySuccessName, "Šablona byla úspěšně uložena");
|
208
|
|
209
|
modelAndView.setViewName("redirect:/configuration?configurationID=" + configuration.getId());
|
210
|
}
|
211
|
|
212
|
return modelAndView;
|
213
|
/*
|
214
|
ModelMap modelMap = modelAndView.getModelMap();
|
215
|
|
216
|
long assemblyID = assembly.getId();
|
217
|
System.out.println(assemblyID);
|
218
|
|
219
|
Assembly assembly2 = this.assemblyManager.getAssemblyById(assemblyID);
|
220
|
|
221
|
modelMap.addAttribute("assemblies", this.assemblyManager.getAssemblies());
|
222
|
modelMap.addAttribute("assembly", assembly2);
|
223
|
modelMap.addAttribute("contingencyTableRows", this.sqlQueryManager.getContingencyTableRow(assembly2.getSQLQuery()));
|
224
|
|
225
|
return modelAndView;
|
226
|
|
227
|
*/
|
228
|
}
|
229
|
|
230
|
/**
|
231
|
* Get method for assembly delete.
|
232
|
* @param id - ID of assembly.
|
233
|
* @return ModelAndView for index.
|
234
|
*/
|
235
|
@GetMapping("/assembly_delete")
|
236
|
public ModelAndView assemblyDeleteGet(@RequestParam("assemblyID") String id, RedirectAttributes redirectAttributes) {
|
237
|
ModelAndView modelAndView = new ModelAndView("redirect:/");
|
238
|
|
239
|
Long assemblyId = Utils.tryParseLong(id);
|
240
|
|
241
|
if (assemblyId == null) {
|
242
|
redirectAttributes.addFlashAttribute(assemblyErrorName, "Sestavu se nepodařilo odstranit.");
|
243
|
}
|
244
|
boolean success = this.assemblyManager.deleteAssembly(assemblyId);
|
245
|
|
246
|
if (success)
|
247
|
{
|
248
|
redirectAttributes.addFlashAttribute(assemblySuccessName, "Sestava byla úspěšně odstraněna");
|
249
|
}
|
250
|
else
|
251
|
{
|
252
|
redirectAttributes.addFlashAttribute(assemblyErrorName, "Sestavu se nepodařilo odstranit");
|
253
|
}
|
254
|
|
255
|
return modelAndView;
|
256
|
}
|
257
|
|
258
|
/**
|
259
|
* Get method for assembly edit.
|
260
|
* @param id - Id of assembly.
|
261
|
* @return ModelAndView for assembly edit.
|
262
|
*/
|
263
|
@GetMapping("/assembly_edit")
|
264
|
public ModelAndView assemblyEditGet(@RequestParam("assemblyID") String id) {
|
265
|
long startTime = System.currentTimeMillis();
|
266
|
ModelAndView modelAndView = new ModelAndView("assembly_manage");
|
267
|
|
268
|
ModelMap modelMap = modelAndView.getModelMap();
|
269
|
|
270
|
Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
|
271
|
|
272
|
modelMap.addAttribute("assembly", assembly);
|
273
|
|
274
|
modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
|
275
|
|
276
|
modelMap.addAttribute("allRoles", this.roleManager.getRoles());
|
277
|
modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
|
278
|
modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
|
279
|
modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
|
280
|
modelMap.addAttribute("allLocations", this.locationManager.getLocations());
|
281
|
long estimatedTime = System.currentTimeMillis() - startTime;
|
282
|
System.out.println("EDIT GET estimated - " + ((double)estimatedTime / 1000) + " sekund ");
|
283
|
|
284
|
return modelAndView;
|
285
|
}
|
286
|
|
287
|
/**
|
288
|
* Post method for assembly edit.
|
289
|
* @param assembly - Edited assembly values.
|
290
|
* @param result - Validator results.
|
291
|
* @param assemblyIsPublic - Attribute indicating if assembly is public or not.
|
292
|
* @return ModelAndView for assembly edit.
|
293
|
*/
|
294
|
@PostMapping("/assembly_edit")
|
295
|
public ModelAndView assemblyEditPost(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result,
|
296
|
@Valid @ModelAttribute("checkboxPublic") String assemblyIsPublic) {
|
297
|
long startTime = System.currentTimeMillis();
|
298
|
ModelAndView modelAndView = new ModelAndView("assembly_manage");
|
299
|
ModelMap modelMap = modelAndView.getModelMap();
|
300
|
|
301
|
if (result.hasErrors()) {
|
302
|
modelMap.addAttribute("assembly", assembly);
|
303
|
|
304
|
modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
|
305
|
modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
|
306
|
}
|
307
|
else {
|
308
|
if (assemblyIsPublic.equals("on")) assembly.setIsPublic(1);
|
309
|
else assembly.setIsPublic(0);
|
310
|
|
311
|
// TODO: chybí defaultValue.
|
312
|
Long assemblyID = this.assemblyManager.updateAssembly(assembly);
|
313
|
this.parameterManager.updateParameters(assemblyID, assembly.getParameters());
|
314
|
Assembly updatedAssembly = this.assemblyManager.getAssemblyById(assemblyID);
|
315
|
|
316
|
modelMap.addAttribute("assembly", updatedAssembly);
|
317
|
modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + updatedAssembly.getName());
|
318
|
modelMap.addAttribute(assemblySuccessName, "Úspěšně uloženo");
|
319
|
}
|
320
|
|
321
|
modelMap.addAttribute("allRoles", this.roleManager.getRoles());
|
322
|
modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
|
323
|
modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
|
324
|
modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
|
325
|
modelMap.addAttribute("allLocations", this.locationManager.getLocations());
|
326
|
long estimatedTime = System.currentTimeMillis() - startTime;
|
327
|
System.out.println("EDIT POST estimated - " + ((double)estimatedTime / 1000) + " sekund ");
|
328
|
|
329
|
return modelAndView;
|
330
|
}
|
331
|
|
332
|
/**
|
333
|
* Get method for new assembly form.
|
334
|
* @return ModelAndView for new assembly.
|
335
|
*/
|
336
|
@GetMapping("/assembly_new")
|
337
|
public ModelAndView assemblyNewGet() {
|
338
|
ModelAndView modelAndView = new ModelAndView("assembly_manage");
|
339
|
|
340
|
ModelMap modelMap = modelAndView.getModelMap();
|
341
|
|
342
|
modelMap.addAttribute("assembly", new Assembly());
|
343
|
|
344
|
modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
|
345
|
|
346
|
modelMap.addAttribute("allRoles", this.roleManager.getRoles());
|
347
|
modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
|
348
|
modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
|
349
|
modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
|
350
|
modelMap.addAttribute("allLocations", this.locationManager.getLocations());
|
351
|
|
352
|
return modelAndView;
|
353
|
}
|
354
|
|
355
|
/**
|
356
|
* Post method for new assembly form.
|
357
|
* @param assembly - Assembly values.
|
358
|
* @param result - Validator results.
|
359
|
* @return
|
360
|
*/
|
361
|
@PostMapping("/assembly_new")
|
362
|
public ModelAndView assemblyNewPost(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result) {
|
363
|
ModelAndView modelAndView = new ModelAndView();
|
364
|
ModelMap modelMap = modelAndView.getModelMap();
|
365
|
|
366
|
if (result.hasErrors()) {
|
367
|
modelAndView.setViewName("assembly_manage");
|
368
|
modelMap.addAttribute("assembly", assembly);
|
369
|
|
370
|
modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
|
371
|
modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
|
372
|
}
|
373
|
else {
|
374
|
// TODO: chybí získání default value.
|
375
|
Long assemblyId = this.assemblyManager.createAssembly(assembly);
|
376
|
this.parameterManager.addParameters(assemblyId, assembly.getParameters());
|
377
|
|
378
|
modelAndView.setViewName("redirect:/assembly_edit?assemblyID=" + assemblyId);
|
379
|
modelMap.addAttribute(assemblySuccessName, "Sestava byla úspěšně vytvořena");
|
380
|
}
|
381
|
|
382
|
modelMap.addAttribute("allRoles", this.roleManager.getRoles());
|
383
|
modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
|
384
|
modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
|
385
|
modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
|
386
|
modelMap.addAttribute("allLocations", this.locationManager.getLocations());
|
387
|
|
388
|
return modelAndView;
|
389
|
}
|
390
|
|
391
|
/**
|
392
|
* Initializes fields of a given configuration
|
393
|
* @param configuration configuration which fields should be initialized
|
394
|
*/
|
395
|
private void initializeFields(Configuration configuration) {
|
396
|
for(ParameterInConfiguration parameterInConfiguration : configuration.getParametersInConfiguration()) {
|
397
|
if(parameterInConfiguration.getLocation() == null) {
|
398
|
parameterInConfiguration.setLocation(new Location());
|
399
|
}
|
400
|
if(parameterInConfiguration.getOperator() == null) {
|
401
|
parameterInConfiguration.setOperator(new Operator());
|
402
|
}
|
403
|
if(parameterInConfiguration.getFunctions() == null) {
|
404
|
parameterInConfiguration.setFunctions(new ArrayList<>());
|
405
|
}
|
406
|
}
|
407
|
}
|
408
|
|
409
|
/**
|
410
|
* Prepares the configuration for contingency table generation. Initializes assembly and parameters of parameters in
|
411
|
* configuration.
|
412
|
* @param configuration configuration to be prepared.
|
413
|
*/
|
414
|
private void prepareForTable(Configuration configuration) {
|
415
|
Assembly assembly = assemblyManager.getAssemblyById(configuration.getAssembly().getId());
|
416
|
configuration.setAssembly(assembly);
|
417
|
|
418
|
for(int i = 0; i < configuration.getParametersInConfiguration().size(); i++) {
|
419
|
ParameterInConfiguration parameterInConfiguration = configuration.getParametersInConfiguration().get(i);
|
420
|
parameterInConfiguration.setParameter(assembly.getParameters().get(i));
|
421
|
if (parameterInConfiguration.getLocation() != null) {
|
422
|
parameterInConfiguration.setLocation(locationManager.getLocationById(parameterInConfiguration.getLocation().getId()));
|
423
|
}
|
424
|
}
|
425
|
}
|
426
|
}
|