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.Assembly;
|
16
|
import vldc.aswi.domain.Configuration;
|
17
|
import vldc.aswi.domain.Location;
|
18
|
import vldc.aswi.domain.Operator;
|
19
|
import vldc.aswi.domain.parameter.Parameter;
|
20
|
import vldc.aswi.domain.parameter.ParameterInConfiguration;
|
21
|
import vldc.aswi.service.*;
|
22
|
import vldc.aswi.service.parameter.ParameterManager;
|
23
|
import vldc.aswi.service.parameter.ParameterTypeManager;
|
24
|
import vldc.aswi.validators.AssemblyValidator;
|
25
|
|
26
|
import javax.validation.Valid;
|
27
|
import java.util.ArrayList;
|
28
|
|
29
|
/**
|
30
|
* Controller for assemblies and configurations
|
31
|
*/
|
32
|
@Controller
|
33
|
public class AssemblyController extends BasicController {
|
34
|
|
35
|
/** Autowired sql query manager. */
|
36
|
@Autowired
|
37
|
private SqlQueryManager sqlQueryManager;
|
38
|
|
39
|
/** Autowired assembly manager. */
|
40
|
@Autowired
|
41
|
private AssemblyManager assemblyManager;
|
42
|
|
43
|
/** Autowired role manager. */
|
44
|
@Autowired
|
45
|
private RoleManager roleManager;
|
46
|
|
47
|
/** Autowired parameterType manager. */
|
48
|
@Autowired
|
49
|
private ParameterTypeManager parameterTypeManager;
|
50
|
|
51
|
/** Autowired operator manager. */
|
52
|
@Autowired
|
53
|
private OperatorManager operatorManager;
|
54
|
|
55
|
/** Autowired function manager. */
|
56
|
@Autowired
|
57
|
private FunctionManager functionManager;
|
58
|
|
59
|
/** Autowired location manager. */
|
60
|
@Autowired
|
61
|
private LocationManager locationManager;
|
62
|
|
63
|
/** Autowired parameter manager. */
|
64
|
@Autowired
|
65
|
private ParameterManager parameterManager;
|
66
|
|
67
|
/** Autowired assembly validator */
|
68
|
@Autowired
|
69
|
private AssemblyValidator assemblyValidator;
|
70
|
|
71
|
@Autowired
|
72
|
private ConfigurationManager configurationManager;
|
73
|
|
74
|
/** Name of thymeleaf parameter, which will contain text in assembly form. */
|
75
|
private String assemblyTitleName = "title";
|
76
|
|
77
|
/** Title text for assembly edit form. */
|
78
|
private String assemblyEditTitleText = "Editace - ";
|
79
|
|
80
|
/** Title text for new assembly form. */
|
81
|
private String assemblyNewTitleText = "Vytvoření nové sestavy";
|
82
|
|
83
|
/** Name of thymeleaf parameter, which will contain error text from validator. */
|
84
|
private String assemblyErrorName = "errorText";
|
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
|
@GetMapping("/assembly")
|
96
|
public ModelAndView assemblyIndex(@Valid @ModelAttribute("assemblyID") String id) {
|
97
|
ModelAndView modelAndView = new ModelAndView("assembly");
|
98
|
|
99
|
ModelMap modelMap = modelAndView.getModelMap();
|
100
|
|
101
|
Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
|
102
|
|
103
|
Configuration configuration = new Configuration();
|
104
|
|
105
|
configuration.setAssembly(assembly);
|
106
|
configuration.setParametersInConfiguration(new ArrayList<>());
|
107
|
for(Parameter parameter : assembly.getParameters()) {
|
108
|
ParameterInConfiguration parameterInConfiguration = new ParameterInConfiguration();
|
109
|
parameterInConfiguration.setParameter(parameter);
|
110
|
parameterInConfiguration.setConfiguration(configuration);
|
111
|
parameterInConfiguration.setOperator(new Operator());
|
112
|
parameterInConfiguration.setLocation(new Location());
|
113
|
configuration.getParametersInConfiguration().add(parameterInConfiguration);
|
114
|
}
|
115
|
|
116
|
modelMap.addAttribute("configuration", configuration);
|
117
|
modelMap.addAttribute("isAssembly", false);
|
118
|
|
119
|
return modelAndView;
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Opens an configuration page after configuration name was clicked
|
124
|
* @param id ID of configuration
|
125
|
* @return modelAndView of page to be shown
|
126
|
*/
|
127
|
@GetMapping("/configuration")
|
128
|
public ModelAndView configurationIndex(@Valid @ModelAttribute("configurationID") String id) {
|
129
|
ModelAndView modelAndView = new ModelAndView("assembly");
|
130
|
|
131
|
ModelMap modelMap = modelAndView.getModelMap();
|
132
|
|
133
|
Configuration configuration = configurationManager.getConfigurationById(Long.parseLong(id));
|
134
|
|
135
|
initializeFields(configuration);
|
136
|
|
137
|
modelMap.addAttribute("configuration", configuration);
|
138
|
modelMap.addAttribute("isAssembly", "0");
|
139
|
|
140
|
return modelAndView;
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* Saves or edits configuration and redirects to the page with configuration
|
145
|
* @param newConfiguration contained with configuration data
|
146
|
* @param id ID of configuration, if empty new configuration is created
|
147
|
* @param bindingResult binding result
|
148
|
* @return modelAndView with redirection
|
149
|
*/
|
150
|
@PostMapping("/configuration")
|
151
|
public ModelAndView saveConfiguration(@Valid Configuration newConfiguration, BindingResult bindingResult,
|
152
|
@RequestParam("configurationID") String id) {
|
153
|
ModelAndView modelAndView = new ModelAndView();
|
154
|
|
155
|
if (bindingResult.hasErrors()) {
|
156
|
modelAndView.setViewName("redirect:/");
|
157
|
|
158
|
return modelAndView;
|
159
|
}
|
160
|
|
161
|
Configuration configuration = configurationManager.saveConfiguration(newConfiguration, id);
|
162
|
|
163
|
initializeFields(configuration);
|
164
|
|
165
|
modelAndView.setViewName("redirect:/configuration?configurationID=" + configuration.getId());
|
166
|
|
167
|
return modelAndView;
|
168
|
}
|
169
|
|
170
|
@PostMapping("/assembly")
|
171
|
public ModelAndView indexPost(@Valid Assembly assembly, BindingResult bindingResult, RedirectAttributes atts) {
|
172
|
ModelAndView modelAndView = new ModelAndView();
|
173
|
|
174
|
ModelMap modelMap = modelAndView.getModelMap();
|
175
|
|
176
|
long assemblyID = assembly.getId();
|
177
|
System.out.println(assemblyID);
|
178
|
|
179
|
Assembly assembly2 = this.assemblyManager.getAssemblyById(assemblyID);
|
180
|
|
181
|
modelMap.addAttribute("assemblies", this.assemblyManager.getAssemblies());
|
182
|
modelMap.addAttribute("assembly", assembly2);
|
183
|
modelMap.addAttribute("contingencyTableRows", this.sqlQueryManager.getContingencyTableRow(assembly2.getSQLQuery()));
|
184
|
|
185
|
return modelAndView;
|
186
|
}
|
187
|
|
188
|
/**
|
189
|
* Get method for assembly edit.
|
190
|
* @param id - Id of assembly.
|
191
|
* @return ModelAndView for assembly edit.
|
192
|
*/
|
193
|
@GetMapping("/assembly_edit")
|
194
|
public ModelAndView assemblyEditIndex(@Valid @ModelAttribute("assemblyID") String id) {
|
195
|
ModelAndView modelAndView = new ModelAndView("assembly_manage");
|
196
|
|
197
|
ModelMap modelMap = modelAndView.getModelMap();
|
198
|
|
199
|
Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
|
200
|
|
201
|
modelMap.addAttribute("assembly", assembly);
|
202
|
|
203
|
modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
|
204
|
|
205
|
modelMap.addAttribute("allRoles", this.roleManager.getRoles());
|
206
|
modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
|
207
|
modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
|
208
|
modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
|
209
|
modelMap.addAttribute("allLocations", this.locationManager.getLocations());
|
210
|
|
211
|
return modelAndView;
|
212
|
}
|
213
|
|
214
|
/**
|
215
|
* Post method for assembly edit.
|
216
|
* @param assembly - Edited assembly values.
|
217
|
* @param result - Validator results.
|
218
|
* @param assemblyIsPublic - Attribute indicating if assembly is public or not.
|
219
|
* @return ModelAndView for assembly edit.
|
220
|
*/
|
221
|
@PostMapping("/assembly_edit")
|
222
|
public ModelAndView assemblyEditPostIndex(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result,
|
223
|
@Valid @ModelAttribute("checkboxPublic") String assemblyIsPublic) {
|
224
|
|
225
|
ModelAndView modelAndView = new ModelAndView("assembly_manage");
|
226
|
ModelMap modelMap = modelAndView.getModelMap();
|
227
|
|
228
|
if (result.hasErrors()) {
|
229
|
modelMap.addAttribute("assembly", assembly);
|
230
|
|
231
|
modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
|
232
|
modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
|
233
|
}
|
234
|
else {
|
235
|
if (assemblyIsPublic.equals("on")) assembly.setIsPublic(1);
|
236
|
else assembly.setIsPublic(0);
|
237
|
|
238
|
// TODO: chybí defaultValue.
|
239
|
Long assemblyID = this.assemblyManager.updateAssembly(assembly);
|
240
|
this.parameterManager.updateParameters(assemblyID, assembly.getParameters());
|
241
|
Assembly updatedAssembly = this.assemblyManager.getAssemblyById(assembly.getId());
|
242
|
|
243
|
modelMap.addAttribute("assembly", updatedAssembly);
|
244
|
modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + updatedAssembly.getName());
|
245
|
}
|
246
|
|
247
|
modelMap.addAttribute("allRoles", this.roleManager.getRoles());
|
248
|
modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
|
249
|
modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
|
250
|
modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
|
251
|
modelMap.addAttribute("allLocations", this.locationManager.getLocations());
|
252
|
|
253
|
return modelAndView;
|
254
|
}
|
255
|
|
256
|
/**
|
257
|
* Get method for new assembly form.
|
258
|
* @return ModelAndView for new assembly.
|
259
|
*/
|
260
|
@GetMapping("/assembly_new")
|
261
|
public ModelAndView assemblyNewIndex() {
|
262
|
ModelAndView modelAndView = new ModelAndView("assembly_manage");
|
263
|
|
264
|
ModelMap modelMap = modelAndView.getModelMap();
|
265
|
|
266
|
modelMap.addAttribute("assembly", new Assembly());
|
267
|
|
268
|
modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
|
269
|
|
270
|
modelMap.addAttribute("allRoles", this.roleManager.getRoles());
|
271
|
modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
|
272
|
modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
|
273
|
modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
|
274
|
modelMap.addAttribute("allLocations", this.locationManager.getLocations());
|
275
|
|
276
|
return modelAndView;
|
277
|
}
|
278
|
|
279
|
/**
|
280
|
* Post method for new assembly form.
|
281
|
* @param assembly - Assembly values.
|
282
|
* @param result - Validator results.
|
283
|
* @return
|
284
|
*/
|
285
|
@PostMapping("/assembly_new")
|
286
|
public ModelAndView assemblyNewIndexPost(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result) {
|
287
|
ModelAndView modelAndView = new ModelAndView();
|
288
|
|
289
|
if (result.hasErrors()) {
|
290
|
ModelMap modelMap = modelAndView.getModelMap();
|
291
|
modelAndView.setViewName("assembly_manage");
|
292
|
|
293
|
modelMap.addAttribute("assembly", assembly);
|
294
|
|
295
|
modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
|
296
|
modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
|
297
|
|
298
|
modelMap.addAttribute("allRoles", this.roleManager.getRoles());
|
299
|
modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
|
300
|
modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
|
301
|
modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
|
302
|
modelMap.addAttribute("allLocations", this.locationManager.getLocations());
|
303
|
}
|
304
|
|
305
|
ModelMap modelMap = modelAndView.getModelMap();
|
306
|
|
307
|
// TODO: chybí získání default value.
|
308
|
Long assemblyId = this.assemblyManager.createAssembly(assembly);
|
309
|
this.parameterManager.addParameters(assemblyId, assembly.getParameters());
|
310
|
|
311
|
modelAndView.setViewName("redirect:/assembly_edit?assemblyID=" + assemblyId);
|
312
|
|
313
|
return modelAndView;
|
314
|
}
|
315
|
|
316
|
/**
|
317
|
* Initializes fields of a given configuration
|
318
|
* @param configuration configuration which fields should be initialized
|
319
|
*/
|
320
|
private void initializeFields(Configuration configuration) {
|
321
|
for(ParameterInConfiguration parameterInConfiguration : configuration.getParametersInConfiguration()) {
|
322
|
if(parameterInConfiguration.getLocation() == null) {
|
323
|
parameterInConfiguration.setLocation(new Location());
|
324
|
}
|
325
|
if(parameterInConfiguration.getOperator() == null) {
|
326
|
parameterInConfiguration.setOperator(new Operator());
|
327
|
}
|
328
|
if(parameterInConfiguration.getFunctions() == null) {
|
329
|
parameterInConfiguration.setFunctions(new ArrayList<>());
|
330
|
}
|
331
|
}
|
332
|
}
|
333
|
}
|