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.*;
|
11
|
import org.springframework.web.servlet.ModelAndView;
|
12
|
import vldc.aswi.domain.Assembly;
|
13
|
import vldc.aswi.domain.Configuration;
|
14
|
import vldc.aswi.domain.Location;
|
15
|
import vldc.aswi.domain.Operator;
|
16
|
import vldc.aswi.domain.parameter.ParameterInConfiguration;
|
17
|
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow;
|
18
|
import vldc.aswi.service.AssemblyManager;
|
19
|
import vldc.aswi.service.ConfigurationManager;
|
20
|
import vldc.aswi.service.LocationManager;
|
21
|
import vldc.aswi.service.SqlQueryManager;
|
22
|
import vldc.aswi.utils.AuthControl;
|
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.List;
|
29
|
|
30
|
/**
|
31
|
* Manager for configuration.
|
32
|
*/
|
33
|
@Controller
|
34
|
public class ConfigurationController extends BasicController{
|
35
|
|
36
|
/** Autowired location manager. */
|
37
|
@Autowired
|
38
|
private LocationManager locationManager;
|
39
|
|
40
|
/** Autowired assembly manager. */
|
41
|
@Autowired
|
42
|
private AssemblyManager assemblyManager;
|
43
|
|
44
|
/** Autowired sql query manager. */
|
45
|
@Autowired
|
46
|
private SqlQueryManager sqlQueryManager;
|
47
|
|
48
|
/** Autowired configuration manager */
|
49
|
@Autowired
|
50
|
private ConfigurationManager configurationManager;
|
51
|
|
52
|
/**
|
53
|
* Opens an configuration page after configuration name was clicked
|
54
|
* @param id ID of configuration
|
55
|
* @return modelAndView of page to be shown
|
56
|
*/
|
57
|
@GetMapping("/configuration")
|
58
|
public ModelAndView configurationGet(@RequestParam("configurationID") String id) {
|
59
|
ModelAndView modelAndView = new ModelAndView("assembly");
|
60
|
|
61
|
ModelMap modelMap = modelAndView.getModelMap();
|
62
|
|
63
|
Configuration configuration = configurationManager.getConfigurationById(Long.parseLong(id));
|
64
|
|
65
|
// TODO: 04.05.2020 error page when id doesn't exist
|
66
|
|
67
|
String userName = AuthControl.getUserName();
|
68
|
|
69
|
if (userName == null) {
|
70
|
// TODO: 04.05.2020 error message, user not authenticated
|
71
|
}
|
72
|
else if (!userName.equals(configuration.getUser().getUsername())) {
|
73
|
// TODO: 04.05.2020 error page wrong user
|
74
|
return new ModelAndView("redirect:/");
|
75
|
}
|
76
|
|
77
|
List<ParameterInConfiguration> parametersInConfiguration = new ArrayList<>(configuration.getParametersInConfiguration());
|
78
|
configuration.setParametersInConfiguration(parametersInConfiguration);
|
79
|
|
80
|
initializeFields(configuration);
|
81
|
Comparator<ParameterInConfiguration> comparator = Comparator.comparingInt(o -> o.getParameter().getParameterOrder());
|
82
|
|
83
|
modelMap.addAttribute("configuration", configuration);
|
84
|
modelMap.addAttribute("comparator", comparator);
|
85
|
modelMap.addAttribute("formAction", "/configuration?configurationID=" + configuration.getId());
|
86
|
|
87
|
return modelAndView;
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Saves or edits configuration and redirects to the page with configuration
|
92
|
* @param newConfiguration contained with configuration data
|
93
|
* @param id ID of configuration, if empty new configuration is created
|
94
|
* @param bindingResult binding result
|
95
|
* @return modelAndView with redirection
|
96
|
*/
|
97
|
@PostMapping("/configuration")
|
98
|
public ModelAndView configurationPost(Configuration newConfiguration,
|
99
|
BindingResult bindingResult,
|
100
|
@RequestParam("configurationID") String id,
|
101
|
@RequestParam(required=false, value="generateTable") String generateTable,
|
102
|
@RequestParam(required=false, value="exportXls") String exportXls,
|
103
|
@RequestParam(required=false, value="exportPdf") String exportPdf,
|
104
|
@RequestParam(required=false, value="saveConfiguration") String saveConfiguration)
|
105
|
{
|
106
|
ModelAndView modelAndView = new ModelAndView();
|
107
|
|
108
|
if (bindingResult.hasErrors()) {
|
109
|
// TODO: 04.05.2020 Error message
|
110
|
modelAndView.setViewName("redirect:/");
|
111
|
|
112
|
return modelAndView;
|
113
|
}
|
114
|
|
115
|
if (generateTable != null)
|
116
|
{
|
117
|
prepareForTable(newConfiguration);
|
118
|
System.out.println("Generuj tabulku");
|
119
|
ModelMap modelMap = modelAndView.getModelMap();
|
120
|
modelMap.addAttribute("configurationID", id);
|
121
|
modelMap.addAttribute("contingencyTableRows", this.sqlQueryManager.getContingencyTableRow(newConfiguration));
|
122
|
Comparator<ParameterInConfiguration> comparator = Comparator.comparingInt(o -> o.getParameter().getParameterOrder());
|
123
|
initializeFields(newConfiguration);
|
124
|
modelMap.addAttribute("configuration", newConfiguration);
|
125
|
modelMap.addAttribute("comparator", comparator);
|
126
|
modelMap.addAttribute("formAction", "/configuration?configurationID=" + id);
|
127
|
modelAndView.setViewName("assembly");
|
128
|
}
|
129
|
else if (exportXls != null)
|
130
|
{
|
131
|
System.out.println("Generuj XLS");
|
132
|
}
|
133
|
else if (exportPdf != null)
|
134
|
{
|
135
|
System.out.println("Generuj PDF");
|
136
|
}
|
137
|
else if (saveConfiguration != null)
|
138
|
{
|
139
|
System.out.println("ulož konfiguraci");
|
140
|
Configuration configuration = configurationManager.saveConfiguration(newConfiguration, id);
|
141
|
|
142
|
initializeFields(configuration);
|
143
|
|
144
|
modelAndView.setViewName("redirect:/configuration?configurationID=" + configuration.getId());
|
145
|
}
|
146
|
|
147
|
return modelAndView;
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* Get method for configuration delete.
|
152
|
* @param id - ID of configuration.
|
153
|
* @return ModelAndView for index.
|
154
|
*/
|
155
|
@GetMapping("/configuration_delete")
|
156
|
public ModelAndView configurationDeleteGet(@RequestParam("configurationID") String id) {
|
157
|
ModelAndView modelAndView = new ModelAndView("redirect:/");
|
158
|
|
159
|
Long configurationId = Utils.tryParseLong(id);
|
160
|
|
161
|
if (configurationId == null) {
|
162
|
// TODO: print error in index.
|
163
|
}
|
164
|
|
165
|
boolean success = this.configurationManager.deleteConfiguration(configurationId);
|
166
|
// TODO: check success.
|
167
|
|
168
|
return modelAndView;
|
169
|
}
|
170
|
|
171
|
/**
|
172
|
* Initializes fields of a given configuration
|
173
|
* @param configuration configuration which fields should be initialized
|
174
|
*/
|
175
|
private void initializeFields(Configuration configuration) {
|
176
|
for(ParameterInConfiguration parameterInConfiguration : configuration.getParametersInConfiguration()) {
|
177
|
if(parameterInConfiguration.getLocation() == null) {
|
178
|
parameterInConfiguration.setLocation(new Location());
|
179
|
}
|
180
|
if(parameterInConfiguration.getOperator() == null) {
|
181
|
parameterInConfiguration.setOperator(new Operator());
|
182
|
}
|
183
|
if(parameterInConfiguration.getFunctions() == null) {
|
184
|
parameterInConfiguration.setFunctions(new ArrayList<>());
|
185
|
}
|
186
|
}
|
187
|
}
|
188
|
|
189
|
/**
|
190
|
* Prepares the configuration for contingency table generation. Initializes assembly and parameters of parameters in
|
191
|
* configuration.
|
192
|
* @param configuration configuration to be prepared.
|
193
|
*/
|
194
|
private void prepareForTable(Configuration configuration) {
|
195
|
Assembly assembly = assemblyManager.getAssemblyById(configuration.getAssembly().getId());
|
196
|
configuration.setAssembly(assembly);
|
197
|
|
198
|
for(int i = 0; i < configuration.getParametersInConfiguration().size(); i++) {
|
199
|
ParameterInConfiguration parameterInConfiguration = configuration.getParametersInConfiguration().get(i);
|
200
|
parameterInConfiguration.setParameter(assembly.getParameters().get(i));
|
201
|
if (parameterInConfiguration.getLocation() != null) {
|
202
|
parameterInConfiguration.setLocation(locationManager.getLocationById(parameterInConfiguration.getLocation().getId()));
|
203
|
}
|
204
|
}
|
205
|
}
|
206
|
}
|