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.annotation.*;
|
9
|
import org.springframework.web.servlet.ModelAndView;
|
10
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
11
|
import vldc.aswi.domain.Assembly;
|
12
|
import vldc.aswi.domain.Configuration;
|
13
|
import vldc.aswi.domain.Location;
|
14
|
import vldc.aswi.domain.Operator;
|
15
|
import vldc.aswi.domain.parameter.ParameterInConfiguration;
|
16
|
import vldc.aswi.model.ExportType;
|
17
|
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow;
|
18
|
import vldc.aswi.service.*;
|
19
|
import vldc.aswi.service.parameter.ParameterManager;
|
20
|
import vldc.aswi.utils.AuthControl;
|
21
|
import vldc.aswi.utils.Utils;
|
22
|
|
23
|
import javax.servlet.http.HttpServletRequest;
|
24
|
import javax.servlet.http.HttpServletResponse;
|
25
|
import vldc.aswi.validators.ConfigurationValidator;
|
26
|
|
27
|
import javax.validation.Valid;
|
28
|
import java.util.ArrayList;
|
29
|
import java.util.Comparator;
|
30
|
import java.util.List;
|
31
|
|
32
|
/**
|
33
|
* Manager for configuration.
|
34
|
*/
|
35
|
@Controller
|
36
|
public class ConfigurationController extends BasicController{
|
37
|
|
38
|
/** Autowired location manager. */
|
39
|
@Autowired
|
40
|
private LocationManager locationManager;
|
41
|
|
42
|
/** Autowired assembly manager. */
|
43
|
@Autowired
|
44
|
private AssemblyManager assemblyManager;
|
45
|
|
46
|
/** Autowired sql query manager. */
|
47
|
@Autowired
|
48
|
private SqlQueryManager sqlQueryManager;
|
49
|
|
50
|
/** Autowired export manager. */
|
51
|
@Autowired
|
52
|
private ExportManager exportManager;
|
53
|
|
54
|
/** Autowired parameter manager. */
|
55
|
@Autowired
|
56
|
private ParameterManager parameterManager;
|
57
|
|
58
|
/** Autowired configuration manager */
|
59
|
@Autowired
|
60
|
private ConfigurationManager configurationManager;
|
61
|
|
62
|
/** Autowired configuration validator */
|
63
|
@Autowired
|
64
|
private ConfigurationValidator configurationValidator;
|
65
|
|
66
|
/** Name of thymeleaf parameter, which will contain text in assembly form. */
|
67
|
private String assemblyTitleName = "title";
|
68
|
|
69
|
/** Title text for table generation form. */
|
70
|
private String generateTableTitleText = "Generování tabulky - ";
|
71
|
|
72
|
/**
|
73
|
* Opens an configuration page after configuration name was clicked
|
74
|
* @param id ID of configuration
|
75
|
* @return modelAndView of page to be shown
|
76
|
*/
|
77
|
@GetMapping("/configuration")
|
78
|
public ModelAndView configurationGet(@RequestParam("configurationID") String id,
|
79
|
@ModelAttribute("successText") String successMsg,
|
80
|
@ModelAttribute("errorText") String errorMsg) {
|
81
|
ModelAndView modelAndView = new ModelAndView("assembly");
|
82
|
|
83
|
ModelMap modelMap = modelAndView.getModelMap();
|
84
|
|
85
|
Configuration configuration = configurationManager.getConfigurationById(Long.parseLong(id));
|
86
|
|
87
|
// TODO: 04.05.2020 error page when id doesn't exist
|
88
|
|
89
|
String userName = AuthControl.getUserName();
|
90
|
|
91
|
if (userName == null) {
|
92
|
// TODO: 04.05.2020 error message, user not authenticated
|
93
|
}
|
94
|
else if (!userName.equals(configuration.getUser().getUsername())) {
|
95
|
// TODO: 04.05.2020 error page wrong user
|
96
|
return new ModelAndView("redirect:/");
|
97
|
}
|
98
|
|
99
|
List<ParameterInConfiguration> parametersInConfiguration = new ArrayList<>(configuration.getParametersInConfiguration());
|
100
|
configuration.setParametersInConfiguration(parametersInConfiguration);
|
101
|
|
102
|
initializeFields(configuration);
|
103
|
|
104
|
modelMap.addAttribute("configuration", configuration);
|
105
|
modelMap.addAttribute("formAction", "/configuration?configurationID=" + configuration.getId());
|
106
|
|
107
|
modelMap.addAttribute(assemblySuccessName, successMsg);
|
108
|
modelMap.addAttribute(assemblyErrorName, errorMsg);
|
109
|
|
110
|
return modelAndView;
|
111
|
}
|
112
|
|
113
|
/**
|
114
|
* Saves or edits configuration and redirects to the page with configuration
|
115
|
* @param newConfiguration contained with configuration data
|
116
|
* @param id ID of configuration, if empty new configuration is created
|
117
|
* @param bindingResult binding result
|
118
|
* @return modelAndView with redirection
|
119
|
*/
|
120
|
@PostMapping("/configuration")
|
121
|
public ModelAndView configurationPost(@Valid Configuration newConfiguration,
|
122
|
BindingResult bindingResult,
|
123
|
@RequestParam("isNotRemoveEmpty") String isNotRemoveEmptyString,
|
124
|
@RequestParam("configurationID") String id,
|
125
|
@RequestParam(required=false, value="generateTable") String generateTable,
|
126
|
@RequestParam(required=false, value="exportXlsx") String exportXlsx,
|
127
|
@RequestParam(required=false, value="exportPdf") String exportPdf,
|
128
|
@RequestParam(required=false, value="saveConfiguration") String saveConfiguration,
|
129
|
RedirectAttributes redirectAttributes,
|
130
|
HttpServletRequest request,
|
131
|
HttpServletResponse response
|
132
|
)
|
133
|
{
|
134
|
ModelAndView modelAndView = new ModelAndView();
|
135
|
ModelMap modelMap = modelAndView.getModelMap();
|
136
|
boolean isNotRemoveEmpty = isNotRemoveEmptyString == null || !isNotRemoveEmptyString.equals("");
|
137
|
|
138
|
if (bindingResult.hasErrors()) {
|
139
|
// TODO: 04.05.2020 Error message
|
140
|
modelAndView.setViewName("redirect:/");
|
141
|
|
142
|
return modelAndView;
|
143
|
}
|
144
|
|
145
|
if (generateTable != null)
|
146
|
{
|
147
|
prepareForTable(newConfiguration);
|
148
|
BindingResult errors = new BeanPropertyBindingResult(newConfiguration, newConfiguration.getClass().getName());
|
149
|
configurationValidator.validate(newConfiguration, errors);
|
150
|
if (errors.hasErrors()) {
|
151
|
addConfigurationDataIntoModelAndView(newConfiguration, modelAndView, modelMap);
|
152
|
|
153
|
modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(errors));
|
154
|
modelMap.addAttribute(assemblyTitleName, generateTableTitleText + " " + newConfiguration.getName());
|
155
|
}
|
156
|
else {
|
157
|
List<ContingencyTableRow> rows = this.sqlQueryManager.getContingencyTableRow(newConfiguration.getAssembly(),
|
158
|
newConfiguration.getParametersInConfiguration(), isNotRemoveEmpty);
|
159
|
|
160
|
modelMap.addAttribute("configurationID", id);
|
161
|
modelMap.addAttribute("contingencyTableRows", rows);
|
162
|
addConfigurationDataIntoModelAndView(newConfiguration, modelAndView, modelMap);
|
163
|
}
|
164
|
}
|
165
|
else if (exportXlsx != null)
|
166
|
{
|
167
|
prepareForTable(newConfiguration);
|
168
|
this.exportManager.exportContingencyTable(newConfiguration, isNotRemoveEmpty, response, ExportType.XLSX);
|
169
|
|
170
|
// Return null, because we want to continue on our page with selected parameters / attributes.
|
171
|
return null;
|
172
|
}
|
173
|
else if (exportPdf != null)
|
174
|
{
|
175
|
prepareForTable(newConfiguration);
|
176
|
this.exportManager.exportContingencyTable(newConfiguration, isNotRemoveEmpty, response, ExportType.PDF);
|
177
|
|
178
|
// Return null, because we want to continue on our page with selected parameters / attributes.
|
179
|
return null;
|
180
|
}
|
181
|
else if (saveConfiguration != null)
|
182
|
{
|
183
|
Configuration configuration = configurationManager.saveConfiguration(newConfiguration, id);
|
184
|
|
185
|
initializeFields(configuration);
|
186
|
|
187
|
redirectAttributes.addFlashAttribute(assemblySuccessName, "Šablona byla úspěšně uložena");
|
188
|
modelAndView.setViewName("redirect:/configuration?configurationID=" + configuration.getId());
|
189
|
|
190
|
}
|
191
|
|
192
|
return modelAndView;
|
193
|
}
|
194
|
|
195
|
/**
|
196
|
* Get method for configuration delete.
|
197
|
* @param id - ID of configuration.
|
198
|
* @return ModelAndView for index.
|
199
|
*/
|
200
|
@GetMapping("/configuration_delete")
|
201
|
public ModelAndView configurationDeleteGet(@RequestParam("configurationID") String id, RedirectAttributes redirectAttributes) {
|
202
|
ModelAndView modelAndView = new ModelAndView("redirect:/");
|
203
|
|
204
|
Long configurationId = Utils.tryParseLong(id);
|
205
|
|
206
|
if (configurationId == null) {
|
207
|
redirectAttributes.addFlashAttribute(assemblyErrorName, "Sestavu se nepodařilo odstranit");
|
208
|
}
|
209
|
|
210
|
boolean success = this.configurationManager.deleteConfiguration(configurationId);
|
211
|
|
212
|
if (success)
|
213
|
{
|
214
|
redirectAttributes.addFlashAttribute(assemblySuccessName, "Sestava byla úspěšně odstraněna");
|
215
|
}
|
216
|
else
|
217
|
{
|
218
|
redirectAttributes.addFlashAttribute(assemblyErrorName, "Sestavu se nepodařilo odstranit");
|
219
|
}
|
220
|
|
221
|
return modelAndView;
|
222
|
}
|
223
|
|
224
|
/**
|
225
|
* Initializes fields of a given configuration
|
226
|
* @param configuration configuration which fields should be initialized
|
227
|
*/
|
228
|
private void initializeFields(Configuration configuration) {
|
229
|
for(ParameterInConfiguration parameterInConfiguration : configuration.getParametersInConfiguration()) {
|
230
|
if(parameterInConfiguration.getLocation() == null) {
|
231
|
parameterInConfiguration.setLocation(new Location());
|
232
|
}
|
233
|
if(parameterInConfiguration.getOperator() == null) {
|
234
|
parameterInConfiguration.setOperator(new Operator());
|
235
|
}
|
236
|
if(parameterInConfiguration.getFunctions() == null) {
|
237
|
parameterInConfiguration.setFunctions(new ArrayList<>());
|
238
|
}
|
239
|
}
|
240
|
}
|
241
|
|
242
|
/**
|
243
|
* Prepares the configuration for contingency table generation. Initializes assembly and parameters of parameters in
|
244
|
* configuration.
|
245
|
* @param configuration configuration to be prepared.
|
246
|
*/
|
247
|
private void prepareForTable(Configuration configuration) {
|
248
|
Assembly assembly = assemblyManager.getAssemblyById(configuration.getAssembly().getId());
|
249
|
configuration.setAssembly(assembly);
|
250
|
|
251
|
for(int i = 0; i < configuration.getParametersInConfiguration().size(); i++) {
|
252
|
ParameterInConfiguration parameterInConfiguration = configuration.getParametersInConfiguration().get(i);
|
253
|
parameterInConfiguration.setParameter(parameterManager.getParameterById(parameterInConfiguration.getParameter().getId()));
|
254
|
parameterInConfiguration.setParameter(assembly.getParameters().get(i));
|
255
|
if (parameterInConfiguration.getLocation() != null) {
|
256
|
parameterInConfiguration.setLocation(locationManager.getLocationById(parameterInConfiguration.getLocation().getId()));
|
257
|
} else {
|
258
|
parameterInConfiguration.setLocation(new Location());
|
259
|
}
|
260
|
parameterInConfiguration.setConfiguration(configuration);
|
261
|
}
|
262
|
|
263
|
Comparator<ParameterInConfiguration> comparator = Comparator.comparingInt(o -> o.getParameter().getParameterOrder());
|
264
|
configuration.getParametersInConfiguration().sort(comparator);
|
265
|
}
|
266
|
|
267
|
/**
|
268
|
* Adds configuration and other data to modelAndView.
|
269
|
* @param newConfiguration Configuration to be added to the modelAndView.
|
270
|
* @param modelAndView ModelAndView to which to which modelMap belongs.
|
271
|
* @param modelMap ModelMap to which data is added.
|
272
|
*/
|
273
|
private void addConfigurationDataIntoModelAndView(Configuration newConfiguration, ModelAndView modelAndView, ModelMap modelMap) {
|
274
|
initializeFields(newConfiguration);
|
275
|
modelMap.addAttribute("configuration", newConfiguration);
|
276
|
modelMap.addAttribute("formAction", "/configuration?configurationID=" + newConfiguration.getAssembly().getId());
|
277
|
modelAndView.setViewName("assembly");
|
278
|
}
|
279
|
}
|