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