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.GrantedAuthority;
|
7
|
import org.springframework.security.core.context.SecurityContextHolder;
|
8
|
import org.springframework.stereotype.Controller;
|
9
|
import org.springframework.ui.ModelMap;
|
10
|
import org.springframework.validation.BindingResult;
|
11
|
import org.springframework.web.bind.WebDataBinder;
|
12
|
import org.springframework.web.bind.annotation.GetMapping;
|
13
|
import org.springframework.web.bind.annotation.InitBinder;
|
14
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
15
|
import org.springframework.web.bind.annotation.PostMapping;
|
16
|
import org.springframework.web.bind.annotation.RequestParam;
|
17
|
import org.springframework.web.servlet.ModelAndView;
|
18
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
19
|
import vldc.aswi.domain.*;
|
20
|
import vldc.aswi.domain.parameter.Parameter;
|
21
|
import vldc.aswi.domain.parameter.ParameterInConfiguration;
|
22
|
import vldc.aswi.service.*;
|
23
|
import vldc.aswi.service.parameter.ParameterManager;
|
24
|
import vldc.aswi.service.parameter.ParameterTypeManager;
|
25
|
import vldc.aswi.utils.AuthControl;
|
26
|
import vldc.aswi.validators.AssemblyValidator;
|
27
|
import vldc.aswi.utils.Utils;
|
28
|
|
29
|
import javax.validation.Valid;
|
30
|
import java.util.ArrayList;
|
31
|
import java.util.Comparator;
|
32
|
import java.util.Set;
|
33
|
import java.util.stream.Collectors;
|
34
|
|
35
|
/**
|
36
|
* Controller for assemblies and configurations
|
37
|
*/
|
38
|
@Controller
|
39
|
public class AssemblyController extends BasicController {
|
40
|
|
41
|
/** Autowired sql query manager. */
|
42
|
@Autowired
|
43
|
private SqlQueryManager sqlQueryManager;
|
44
|
|
45
|
/** Autowired assembly manager. */
|
46
|
@Autowired
|
47
|
private AssemblyManager assemblyManager;
|
48
|
|
49
|
/** Autowired role manager. */
|
50
|
@Autowired
|
51
|
private RoleManager roleManager;
|
52
|
|
53
|
/** Autowired parameterType manager. */
|
54
|
@Autowired
|
55
|
private ParameterTypeManager parameterTypeManager;
|
56
|
|
57
|
/** Autowired operator manager. */
|
58
|
@Autowired
|
59
|
private OperatorManager operatorManager;
|
60
|
|
61
|
/** Autowired function manager. */
|
62
|
@Autowired
|
63
|
private FunctionManager functionManager;
|
64
|
|
65
|
/** Autowired location manager. */
|
66
|
@Autowired
|
67
|
private LocationManager locationManager;
|
68
|
|
69
|
/** Autowired parameter manager. */
|
70
|
@Autowired
|
71
|
private ParameterManager parameterManager;
|
72
|
|
73
|
/** Autowired assembly validator */
|
74
|
@Autowired
|
75
|
private AssemblyValidator assemblyValidator;
|
76
|
|
77
|
/** Name of thymeleaf parameter, which will contain text in assembly form. */
|
78
|
private String assemblyTitleName = "title";
|
79
|
|
80
|
/** Title text for assembly edit form. */
|
81
|
private String assemblyEditTitleText = "Editace - ";
|
82
|
|
83
|
/** Title text for new assembly form. */
|
84
|
private String assemblyNewTitleText = "Vytvoření nové sestavy";
|
85
|
|
86
|
/** Name of thymeleaf parameter, which will contain error text from validator. */
|
87
|
private String assemblyErrorName = "errorText";
|
88
|
|
89
|
/**
|
90
|
* Bind assembly validator.
|
91
|
* @param binder Binder.
|
92
|
*/
|
93
|
@InitBinder("assembly")
|
94
|
protected void userBinder(WebDataBinder binder) {
|
95
|
binder.addValidators(this.assemblyValidator);
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Get method for form, where configuration is created from assembly.
|
100
|
* @param id - ID of assembly.
|
101
|
* @return ModelAndView for assembly.
|
102
|
*/
|
103
|
@GetMapping("/assembly")
|
104
|
public ModelAndView assemblyGet(@Valid @ModelAttribute("assemblyID") String id) {
|
105
|
ModelAndView modelAndView = new ModelAndView("assembly");
|
106
|
|
107
|
ModelMap modelMap = modelAndView.getModelMap();
|
108
|
|
109
|
Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
|
110
|
|
111
|
// TODO: 04.05.2020 error page when id doesn't exist
|
112
|
|
113
|
String roleName = AuthControl.getRoleName();
|
114
|
|
115
|
if (roleName == null) {
|
116
|
// TODO: 04.05.2020 error message, user not authenticated
|
117
|
}
|
118
|
|
119
|
Role role = roleManager.getRole(roleName);
|
120
|
|
121
|
if (!assembly.getRoles().contains(role)) {
|
122
|
// TODO: 04.05.2020 Error page, wrong role
|
123
|
return new ModelAndView("redirect:/");
|
124
|
}
|
125
|
|
126
|
Configuration configuration = new Configuration();
|
127
|
|
128
|
configuration.setAssembly(assembly);
|
129
|
configuration.setTableName(assembly.getName());
|
130
|
configuration.setParametersInConfiguration(new ArrayList<>());
|
131
|
for(Parameter parameter : assembly.getParameters()) {
|
132
|
ParameterInConfiguration parameterInConfiguration = new ParameterInConfiguration();
|
133
|
parameterInConfiguration.setParameter(parameter);
|
134
|
parameterInConfiguration.setConfiguration(configuration);
|
135
|
parameterInConfiguration.setOperator(new Operator());
|
136
|
parameterInConfiguration.setLocation(new Location());
|
137
|
parameterInConfiguration.setParameterOrder(parameter.getParameterOrder());
|
138
|
configuration.getParametersInConfiguration().add(parameterInConfiguration);
|
139
|
}
|
140
|
|
141
|
Comparator<ParameterInConfiguration> comparator = Comparator.comparingInt(o -> o.getParameter().getParameterOrder());
|
142
|
|
143
|
modelMap.addAttribute("configuration", configuration);
|
144
|
modelMap.addAttribute("comparator", comparator);
|
145
|
|
146
|
return modelAndView;
|
147
|
}
|
148
|
|
149
|
|
150
|
/**
|
151
|
* Post method for form, where configuration is created from assembly.
|
152
|
* @param assembly - Assembly values.
|
153
|
* @param bindingResult - Error results from assembly validators.
|
154
|
* @param atts - Redirect attributes.
|
155
|
* @return ModelAndView for assembly.
|
156
|
*/
|
157
|
@PostMapping("/assembly")
|
158
|
public ModelAndView assemblyPost(@Valid Assembly assembly, BindingResult bindingResult, RedirectAttributes atts) {
|
159
|
ModelAndView modelAndView = new ModelAndView();
|
160
|
|
161
|
ModelMap modelMap = modelAndView.getModelMap();
|
162
|
|
163
|
long assemblyID = assembly.getId();
|
164
|
System.out.println(assemblyID);
|
165
|
|
166
|
Assembly assembly2 = this.assemblyManager.getAssemblyById(assemblyID);
|
167
|
|
168
|
modelMap.addAttribute("assemblies", this.assemblyManager.getAssemblies());
|
169
|
modelMap.addAttribute("assembly", assembly2);
|
170
|
modelMap.addAttribute("contingencyTableRows", this.sqlQueryManager.getContingencyTableRow(assembly2.getSQLQuery()));
|
171
|
|
172
|
return modelAndView;
|
173
|
}
|
174
|
|
175
|
/**
|
176
|
* Get method for assembly delete.
|
177
|
* @param id - ID of assembly.
|
178
|
* @return ModelAndView for index.
|
179
|
*/
|
180
|
@GetMapping("/assembly_delete")
|
181
|
public ModelAndView assemblyDeleteGet(@RequestParam("assemblyID") String id) {
|
182
|
ModelAndView modelAndView = new ModelAndView("redirect:/");
|
183
|
|
184
|
Long assemblyId = Utils.tryParseLong(id);
|
185
|
|
186
|
if (assemblyId == null) {
|
187
|
// TODO: print error in index.
|
188
|
}
|
189
|
boolean success = this.assemblyManager.deleteAssembly(assemblyId);
|
190
|
// TODO: check success.
|
191
|
|
192
|
return modelAndView;
|
193
|
}
|
194
|
|
195
|
/**
|
196
|
* Get method for assembly edit.
|
197
|
* @param id - Id of assembly.
|
198
|
* @return ModelAndView for assembly edit.
|
199
|
*/
|
200
|
@GetMapping("/assembly_edit")
|
201
|
public ModelAndView assemblyEditGet(@Valid @ModelAttribute("assemblyID") String id) {
|
202
|
long startTime = System.currentTimeMillis();
|
203
|
ModelAndView modelAndView = new ModelAndView("assembly_manage");
|
204
|
|
205
|
ModelMap modelMap = modelAndView.getModelMap();
|
206
|
|
207
|
Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
|
208
|
|
209
|
modelMap.addAttribute("assembly", assembly);
|
210
|
|
211
|
modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
|
212
|
|
213
|
modelMap.addAttribute("allRoles", this.roleManager.getRoles());
|
214
|
modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
|
215
|
modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
|
216
|
modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
|
217
|
modelMap.addAttribute("allLocations", this.locationManager.getLocations());
|
218
|
long estimatedTime = System.currentTimeMillis() - startTime;
|
219
|
System.out.println("EDIT GET estimated - " + ((double)estimatedTime / 1000) + " sekund ");
|
220
|
|
221
|
return modelAndView;
|
222
|
}
|
223
|
|
224
|
/**
|
225
|
* Post method for assembly edit.
|
226
|
* @param assembly - Edited assembly values.
|
227
|
* @param result - Validator results.
|
228
|
* @param assemblyIsPublic - Attribute indicating if assembly is public or not.
|
229
|
* @return ModelAndView for assembly edit.
|
230
|
*/
|
231
|
@PostMapping("/assembly_edit")
|
232
|
public ModelAndView assemblyEditPost(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result,
|
233
|
@Valid @ModelAttribute("checkboxPublic") String assemblyIsPublic) {
|
234
|
long startTime = System.currentTimeMillis();
|
235
|
ModelAndView modelAndView = new ModelAndView("assembly_manage");
|
236
|
ModelMap modelMap = modelAndView.getModelMap();
|
237
|
|
238
|
if (result.hasErrors()) {
|
239
|
modelMap.addAttribute("assembly", assembly);
|
240
|
|
241
|
modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
|
242
|
modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
|
243
|
}
|
244
|
else {
|
245
|
if (assemblyIsPublic.equals("on")) assembly.setIsPublic(1);
|
246
|
else assembly.setIsPublic(0);
|
247
|
|
248
|
// TODO: chybí defaultValue.
|
249
|
Long assemblyID = this.assemblyManager.updateAssembly(assembly);
|
250
|
this.parameterManager.updateParameters(assemblyID, assembly.getParameters());
|
251
|
Assembly updatedAssembly = this.assemblyManager.getAssemblyById(assemblyID);
|
252
|
|
253
|
modelMap.addAttribute("assembly", updatedAssembly);
|
254
|
modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + updatedAssembly.getName());
|
255
|
}
|
256
|
|
257
|
modelMap.addAttribute("allRoles", this.roleManager.getRoles());
|
258
|
modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
|
259
|
modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
|
260
|
modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
|
261
|
modelMap.addAttribute("allLocations", this.locationManager.getLocations());
|
262
|
long estimatedTime = System.currentTimeMillis() - startTime;
|
263
|
System.out.println("EDIT POST estimated - " + ((double)estimatedTime / 1000) + " sekund ");
|
264
|
|
265
|
return modelAndView;
|
266
|
}
|
267
|
|
268
|
/**
|
269
|
* Get method for new assembly form.
|
270
|
* @return ModelAndView for new assembly.
|
271
|
*/
|
272
|
@GetMapping("/assembly_new")
|
273
|
public ModelAndView assemblyNewGet() {
|
274
|
ModelAndView modelAndView = new ModelAndView("assembly_manage");
|
275
|
|
276
|
ModelMap modelMap = modelAndView.getModelMap();
|
277
|
|
278
|
modelMap.addAttribute("assembly", new Assembly());
|
279
|
|
280
|
modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
|
281
|
|
282
|
modelMap.addAttribute("allRoles", this.roleManager.getRoles());
|
283
|
modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
|
284
|
modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
|
285
|
modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
|
286
|
modelMap.addAttribute("allLocations", this.locationManager.getLocations());
|
287
|
|
288
|
return modelAndView;
|
289
|
}
|
290
|
|
291
|
/**
|
292
|
* Post method for new assembly form.
|
293
|
* @param assembly - Assembly values.
|
294
|
* @param result - Validator results.
|
295
|
* @return
|
296
|
*/
|
297
|
@PostMapping("/assembly_new")
|
298
|
public ModelAndView assemblyNewPost(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result) {
|
299
|
ModelAndView modelAndView = new ModelAndView();
|
300
|
ModelMap modelMap = modelAndView.getModelMap();
|
301
|
|
302
|
if (result.hasErrors()) {
|
303
|
modelAndView.setViewName("assembly_manage");
|
304
|
modelMap.addAttribute("assembly", assembly);
|
305
|
|
306
|
modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
|
307
|
modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
|
308
|
}
|
309
|
else {
|
310
|
// TODO: chybí získání default value.
|
311
|
Long assemblyId = this.assemblyManager.createAssembly(assembly);
|
312
|
this.parameterManager.addParameters(assemblyId, assembly.getParameters());
|
313
|
|
314
|
modelAndView.setViewName("redirect:/assembly_edit?assemblyID=" + assemblyId);
|
315
|
}
|
316
|
|
317
|
return modelAndView;
|
318
|
}
|
319
|
}
|