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.GetMapping;
|
8
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
9
|
import org.springframework.web.bind.annotation.PostMapping;
|
10
|
import org.springframework.web.bind.annotation.RequestParam;
|
11
|
import org.springframework.web.servlet.ModelAndView;
|
12
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
13
|
import vldc.aswi.domain.Assembly;
|
14
|
import vldc.aswi.domain.Configuration;
|
15
|
import vldc.aswi.domain.Location;
|
16
|
import vldc.aswi.domain.Operator;
|
17
|
import vldc.aswi.domain.parameter.Parameter;
|
18
|
import vldc.aswi.domain.parameter.ParameterInConfiguration;
|
19
|
import vldc.aswi.service.AssemblyManager;
|
20
|
import vldc.aswi.service.ConfigurationManager;
|
21
|
import vldc.aswi.service.SqlQueryManager;
|
22
|
|
23
|
import javax.validation.Valid;
|
24
|
import java.util.ArrayList;
|
25
|
|
26
|
/**
|
27
|
* Controller for assemblies and configurations
|
28
|
*/
|
29
|
@Controller
|
30
|
public class AssemblyController {
|
31
|
|
32
|
/**
|
33
|
* Autowired SQL query manager
|
34
|
*/
|
35
|
@Autowired
|
36
|
private SqlQueryManager sqlQueryManager;
|
37
|
|
38
|
/**
|
39
|
* Autowired assembly manager
|
40
|
*/
|
41
|
@Autowired
|
42
|
private AssemblyManager assemblyManager;
|
43
|
|
44
|
/**
|
45
|
* Autowired configuration manager
|
46
|
*/
|
47
|
@Autowired
|
48
|
private ConfigurationManager configurationManager;
|
49
|
|
50
|
/**
|
51
|
* Opens an configuration page after assembly name was clicked
|
52
|
* @param id ID of assembly
|
53
|
* @return modelAndView of page to be shown
|
54
|
*/
|
55
|
@GetMapping("/assembly")
|
56
|
public ModelAndView assemblyIndex(@Valid @ModelAttribute("assemblyID") String id) {
|
57
|
ModelAndView modelAndView = new ModelAndView("assembly");
|
58
|
|
59
|
ModelMap modelMap = modelAndView.getModelMap();
|
60
|
|
61
|
Assembly assembly = assemblyManager.getAssemblyById(Long.parseLong(id));
|
62
|
|
63
|
Configuration configuration = new Configuration();
|
64
|
|
65
|
configuration.setAssembly(assembly);
|
66
|
configuration.setParametersInConfiguration(new ArrayList<>());
|
67
|
for(Parameter parameter : assembly.getParameters()) {
|
68
|
ParameterInConfiguration parameterInConfiguration = new ParameterInConfiguration();
|
69
|
parameterInConfiguration.setParameter(parameter);
|
70
|
parameterInConfiguration.setConfiguration(configuration);
|
71
|
parameterInConfiguration.setOperator(new Operator());
|
72
|
parameterInConfiguration.setLocation(new Location());
|
73
|
configuration.getParametersInConfiguration().add(parameterInConfiguration);
|
74
|
}
|
75
|
|
76
|
modelMap.addAttribute("configuration", configuration);
|
77
|
modelMap.addAttribute("isAssembly", false);
|
78
|
|
79
|
return modelAndView;
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Opens an configuration page after configuration name was clicked
|
84
|
* @param id ID of configuration
|
85
|
* @return modelAndView of page to be shown
|
86
|
*/
|
87
|
@GetMapping("/configuration")
|
88
|
public ModelAndView configurationIndex(@Valid @ModelAttribute("configurationID") String id) {
|
89
|
ModelAndView modelAndView = new ModelAndView("assembly");
|
90
|
|
91
|
ModelMap modelMap = modelAndView.getModelMap();
|
92
|
|
93
|
Configuration configuration = configurationManager.getConfigurationById(Long.parseLong(id));
|
94
|
|
95
|
for(ParameterInConfiguration parameterInConfiguration : configuration.getParametersInConfiguration()) {
|
96
|
if(parameterInConfiguration.getLocation() == null) {
|
97
|
parameterInConfiguration.setLocation(new Location());
|
98
|
}
|
99
|
if(parameterInConfiguration.getOperator() == null) {
|
100
|
parameterInConfiguration.setOperator(new Operator());
|
101
|
}
|
102
|
}
|
103
|
|
104
|
modelMap.addAttribute("configuration", configuration);
|
105
|
modelMap.addAttribute("isAssembly", "0");
|
106
|
|
107
|
return modelAndView;
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Saves or edits configuration and redirects to the page with configuration
|
112
|
* @param newConfiguration contained with configuration data
|
113
|
* @param id ID of configuration, if empty new configuration is created
|
114
|
* @param bindingResult binding result
|
115
|
* @return modelAndView with redirection
|
116
|
*/
|
117
|
@PostMapping("/configuration")
|
118
|
public ModelAndView saveConfiguration(@Valid Configuration newConfiguration, BindingResult bindingResult,
|
119
|
@RequestParam("configurationID") String id) {
|
120
|
ModelAndView modelAndView = new ModelAndView();
|
121
|
|
122
|
if (bindingResult.hasErrors()) {
|
123
|
modelAndView.setViewName("redirect:/");
|
124
|
|
125
|
return modelAndView;
|
126
|
}
|
127
|
|
128
|
Configuration configuration = configurationManager.saveConfiguration(newConfiguration, id);
|
129
|
|
130
|
for(ParameterInConfiguration parameterInConfiguration : configuration.getParametersInConfiguration()) {
|
131
|
if(parameterInConfiguration.getLocation() == null) {
|
132
|
parameterInConfiguration.setLocation(new Location());
|
133
|
}
|
134
|
if(parameterInConfiguration.getOperator() == null) {
|
135
|
parameterInConfiguration.setOperator(new Operator());
|
136
|
}
|
137
|
}
|
138
|
|
139
|
modelAndView.setViewName("redirect:/configuration?configurationID=" + configuration.getId());
|
140
|
|
141
|
return modelAndView;
|
142
|
}
|
143
|
|
144
|
|
145
|
@PostMapping("/assembly")
|
146
|
public ModelAndView indexPost(@Valid Assembly assembly, BindingResult bindingResult, RedirectAttributes atts) {
|
147
|
ModelAndView modelAndView = new ModelAndView();
|
148
|
|
149
|
ModelMap modelMap = modelAndView.getModelMap();
|
150
|
|
151
|
long assemblyID = assembly.getId();
|
152
|
System.out.println(assemblyID);
|
153
|
Assembly assembly2 = assemblyManager.getAssemblyById(assemblyID);
|
154
|
|
155
|
|
156
|
modelMap.addAttribute("assemblies", assemblyManager.getAssemblies());
|
157
|
modelMap.addAttribute("assembly", assembly2);
|
158
|
modelMap.addAttribute("contingencyTableRows", sqlQueryManager.getContingencyTableRow(assembly2.getSQLQuery()));
|
159
|
|
160
|
return modelAndView;
|
161
|
}
|
162
|
|
163
|
@GetMapping("/assembly_edit")
|
164
|
public ModelAndView assemblyEditIndex(@Valid @ModelAttribute("assemblyID") String id) {
|
165
|
ModelAndView modelAndView = new ModelAndView("assembly_edit");
|
166
|
|
167
|
ModelMap modelMap = modelAndView.getModelMap();
|
168
|
|
169
|
Assembly assembly = assemblyManager.getAssemblyById(Long.parseLong(id));
|
170
|
|
171
|
modelMap.addAttribute("assemblies", assemblyManager.getAssemblies());
|
172
|
modelMap.addAttribute("assembly", assembly);
|
173
|
|
174
|
return modelAndView;
|
175
|
}
|
176
|
}
|