1
|
package vldc.aswi.web.controller;
|
2
|
|
3
|
import org.springframework.beans.factory.annotation.Autowired;
|
4
|
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
5
|
import org.springframework.security.core.Authentication;
|
6
|
import org.springframework.security.core.context.SecurityContextHolder;
|
7
|
import org.springframework.stereotype.Controller;
|
8
|
import org.springframework.ui.ModelMap;
|
9
|
import org.springframework.validation.BindingResult;
|
10
|
import org.springframework.web.bind.annotation.GetMapping;
|
11
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
12
|
import org.springframework.web.bind.annotation.PostMapping;
|
13
|
import org.springframework.web.bind.annotation.RequestParam;
|
14
|
import org.springframework.web.servlet.ModelAndView;
|
15
|
import vldc.aswi.domain.Configuration;
|
16
|
import vldc.aswi.domain.Location;
|
17
|
import vldc.aswi.domain.Operator;
|
18
|
import vldc.aswi.domain.parameter.ParameterInConfiguration;
|
19
|
import vldc.aswi.service.ConfigurationManager;
|
20
|
import vldc.aswi.service.SqlQueryManager;
|
21
|
import vldc.aswi.utils.AuthControl;
|
22
|
import vldc.aswi.utils.Utils;
|
23
|
|
24
|
import javax.validation.Valid;
|
25
|
import java.util.ArrayList;
|
26
|
import java.util.Comparator;
|
27
|
import java.util.List;
|
28
|
|
29
|
/**
|
30
|
* Manager for configuration.
|
31
|
*/
|
32
|
@Controller
|
33
|
public class ConfigurationController extends BasicController{
|
34
|
|
35
|
/** Autowired sql query manager. */
|
36
|
@Autowired
|
37
|
private SqlQueryManager sqlQueryManager;
|
38
|
|
39
|
/** Autowired configuration manager */
|
40
|
@Autowired
|
41
|
private ConfigurationManager configurationManager;
|
42
|
|
43
|
/**
|
44
|
* Opens an configuration page after configuration name was clicked
|
45
|
* @param id ID of configuration
|
46
|
* @return modelAndView of page to be shown
|
47
|
*/
|
48
|
@GetMapping("/configuration")
|
49
|
public ModelAndView configurationGet(@RequestParam("configurationID") String id) {
|
50
|
ModelAndView modelAndView = new ModelAndView("assembly");
|
51
|
|
52
|
ModelMap modelMap = modelAndView.getModelMap();
|
53
|
|
54
|
Configuration configuration = configurationManager.getConfigurationById(Long.parseLong(id));
|
55
|
|
56
|
// TODO: 04.05.2020 error page when id doesn't exist
|
57
|
|
58
|
String userName = AuthControl.getUserName();
|
59
|
|
60
|
if (userName == null) {
|
61
|
// TODO: 04.05.2020 error message, user not authenticated
|
62
|
}
|
63
|
else if (!userName.equals(configuration.getUser().getUsername())) {
|
64
|
// TODO: 04.05.2020 error page wrong user
|
65
|
return new ModelAndView("redirect:/");
|
66
|
}
|
67
|
|
68
|
List<ParameterInConfiguration> parametersInConfiguration = new ArrayList<>(configuration.getParametersInConfiguration());
|
69
|
configuration.setParametersInConfiguration(parametersInConfiguration);
|
70
|
|
71
|
initializeFields(configuration);
|
72
|
Comparator<ParameterInConfiguration> comparator = Comparator.comparingInt(o -> o.getParameter().getParameterOrder());
|
73
|
|
74
|
modelMap.addAttribute("configuration", configuration);
|
75
|
modelMap.addAttribute("comparator", comparator);
|
76
|
modelMap.addAttribute("formAction", "/configuration?configurationID=" + configuration.getId());
|
77
|
|
78
|
return modelAndView;
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* Saves or edits configuration and redirects to the page with configuration
|
83
|
* @param newConfiguration contained with configuration data
|
84
|
* @param id ID of configuration, if empty new configuration is created
|
85
|
* @param bindingResult binding result
|
86
|
* @return modelAndView with redirection
|
87
|
*/
|
88
|
@PostMapping("/configuration")
|
89
|
public ModelAndView configurationPost( Configuration newConfiguration,
|
90
|
BindingResult bindingResult,
|
91
|
@RequestParam("configurationID") String id,
|
92
|
@RequestParam(required=false, value="generateTable") String generateTable,
|
93
|
@RequestParam(required=false, value="exportXls") String exportXls,
|
94
|
@RequestParam(required=false, value="exportPdf") String exportPdf,
|
95
|
@RequestParam(required=false, value="saveConfiguration") String saveConfiguration)
|
96
|
{
|
97
|
if (generateTable != null)
|
98
|
{
|
99
|
System.out.println("Generuj tabulku");
|
100
|
}
|
101
|
else if (exportXls != null)
|
102
|
{
|
103
|
System.out.println("Generuj XLS");
|
104
|
}
|
105
|
else if (exportPdf != null)
|
106
|
{
|
107
|
System.out.println("Generuj PDF");
|
108
|
}
|
109
|
else if (saveConfiguration != null)
|
110
|
{
|
111
|
System.out.println("ulož konfiguraci");
|
112
|
}
|
113
|
|
114
|
|
115
|
ModelAndView modelAndView = new ModelAndView();
|
116
|
|
117
|
if (bindingResult.hasErrors()) {
|
118
|
// TODO: 04.05.2020 Error message
|
119
|
modelAndView.setViewName("redirect:/");
|
120
|
|
121
|
return modelAndView;
|
122
|
}
|
123
|
|
124
|
|
125
|
|
126
|
Configuration configuration = configurationManager.saveConfiguration(newConfiguration, id);
|
127
|
|
128
|
this.sqlQueryManager.getContingencyTableRow(configuration.getAssembly().getSQLQuery());
|
129
|
|
130
|
initializeFields(configuration);
|
131
|
|
132
|
modelAndView.setViewName("redirect:/configuration?configurationID=" + configuration.getId());
|
133
|
|
134
|
return modelAndView;
|
135
|
}
|
136
|
|
137
|
/**
|
138
|
* Get method for configuration delete.
|
139
|
* @param id - ID of configuration.
|
140
|
* @return ModelAndView for index.
|
141
|
*/
|
142
|
@GetMapping("/configuration_delete")
|
143
|
public ModelAndView configurationDeleteGet(@RequestParam("configurationID") String id) {
|
144
|
ModelAndView modelAndView = new ModelAndView("redirect:/");
|
145
|
|
146
|
Long configurationId = Utils.tryParseLong(id);
|
147
|
|
148
|
if (configurationId == null) {
|
149
|
// TODO: print error in index.
|
150
|
}
|
151
|
|
152
|
boolean success = this.configurationManager.deleteConfiguration(configurationId);
|
153
|
// TODO: check success.
|
154
|
|
155
|
return modelAndView;
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* Initializes fields of a given configuration
|
160
|
* @param configuration configuration which fields should be initialized
|
161
|
*/
|
162
|
private void initializeFields(Configuration configuration) {
|
163
|
for(ParameterInConfiguration parameterInConfiguration : configuration.getParametersInConfiguration()) {
|
164
|
if(parameterInConfiguration.getLocation() == null) {
|
165
|
parameterInConfiguration.setLocation(new Location());
|
166
|
}
|
167
|
if(parameterInConfiguration.getOperator() == null) {
|
168
|
parameterInConfiguration.setOperator(new Operator());
|
169
|
}
|
170
|
if(parameterInConfiguration.getFunctions() == null) {
|
171
|
parameterInConfiguration.setFunctions(new ArrayList<>());
|
172
|
}
|
173
|
}
|
174
|
}
|
175
|
}
|