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
|
initializeFields(configuration);
|
96
|
|
97
|
modelMap.addAttribute("configuration", configuration);
|
98
|
modelMap.addAttribute("isAssembly", "0");
|
99
|
|
100
|
return modelAndView;
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Saves or edits configuration and redirects to the page with configuration
|
105
|
* @param newConfiguration contained with configuration data
|
106
|
* @param id ID of configuration, if empty new configuration is created
|
107
|
* @param bindingResult binding result
|
108
|
* @return modelAndView with redirection
|
109
|
*/
|
110
|
@PostMapping("/configuration")
|
111
|
public ModelAndView saveConfiguration(@Valid Configuration newConfiguration, BindingResult bindingResult,
|
112
|
@RequestParam("configurationID") String id) {
|
113
|
ModelAndView modelAndView = new ModelAndView();
|
114
|
|
115
|
if (bindingResult.hasErrors()) {
|
116
|
modelAndView.setViewName("redirect:/");
|
117
|
|
118
|
return modelAndView;
|
119
|
}
|
120
|
|
121
|
Configuration configuration = configurationManager.saveConfiguration(newConfiguration, id);
|
122
|
|
123
|
initializeFields(configuration);
|
124
|
|
125
|
modelAndView.setViewName("redirect:/configuration?configurationID=" + configuration.getId());
|
126
|
|
127
|
return modelAndView;
|
128
|
}
|
129
|
|
130
|
@PostMapping("/assembly")
|
131
|
public ModelAndView indexPost(@Valid Assembly assembly, BindingResult bindingResult, RedirectAttributes atts) {
|
132
|
ModelAndView modelAndView = new ModelAndView();
|
133
|
|
134
|
ModelMap modelMap = modelAndView.getModelMap();
|
135
|
|
136
|
long assemblyID = assembly.getId();
|
137
|
System.out.println(assemblyID);
|
138
|
Assembly assembly2 = assemblyManager.getAssemblyById(assemblyID);
|
139
|
|
140
|
|
141
|
modelMap.addAttribute("assemblies", assemblyManager.getAssemblies());
|
142
|
modelMap.addAttribute("assembly", assembly2);
|
143
|
modelMap.addAttribute("contingencyTableRows", sqlQueryManager.getContingencyTableRow(assembly2.getSQLQuery()));
|
144
|
|
145
|
return modelAndView;
|
146
|
}
|
147
|
|
148
|
@GetMapping("/assembly_edit")
|
149
|
public ModelAndView assemblyEditIndex(@Valid @ModelAttribute("assemblyID") String id) {
|
150
|
ModelAndView modelAndView = new ModelAndView("assembly_edit");
|
151
|
|
152
|
ModelMap modelMap = modelAndView.getModelMap();
|
153
|
|
154
|
Assembly assembly = assemblyManager.getAssemblyById(Long.parseLong(id));
|
155
|
|
156
|
modelMap.addAttribute("assemblies", assemblyManager.getAssemblies());
|
157
|
modelMap.addAttribute("assembly", assembly);
|
158
|
|
159
|
return modelAndView;
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* Initializes fields of a given configuration
|
164
|
* @param configuration configuration which fields should be initialized
|
165
|
*/
|
166
|
private void initializeFields(Configuration configuration) {
|
167
|
for(ParameterInConfiguration parameterInConfiguration : configuration.getParametersInConfiguration()) {
|
168
|
if(parameterInConfiguration.getLocation() == null) {
|
169
|
parameterInConfiguration.setLocation(new Location());
|
170
|
}
|
171
|
if(parameterInConfiguration.getOperator() == null) {
|
172
|
parameterInConfiguration.setOperator(new Operator());
|
173
|
}
|
174
|
if(parameterInConfiguration.getFunctions() == null) {
|
175
|
parameterInConfiguration.setFunctions(new ArrayList<>());
|
176
|
}
|
177
|
}
|
178
|
}
|
179
|
}
|