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