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.context.SecurityContextHolder;
|
7
|
import org.springframework.stereotype.Controller;
|
8
|
import org.springframework.ui.ModelMap;
|
9
|
import org.springframework.validation.BindingResult;
|
10
|
import org.springframework.web.bind.annotation.GetMapping;
|
11
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
12
|
import org.springframework.web.bind.annotation.PostMapping;
|
13
|
import org.springframework.web.bind.annotation.RequestParam;
|
14
|
import org.springframework.web.servlet.ModelAndView;
|
15
|
import vldc.aswi.domain.Configuration;
|
16
|
import vldc.aswi.domain.Location;
|
17
|
import vldc.aswi.domain.Operator;
|
18
|
import vldc.aswi.domain.parameter.ParameterInConfiguration;
|
19
|
import vldc.aswi.service.ConfigurationManager;
|
20
|
import vldc.aswi.utils.AuthControl;
|
21
|
import vldc.aswi.utils.Utils;
|
22
|
|
23
|
import javax.validation.Valid;
|
24
|
import java.util.ArrayList;
|
25
|
import java.util.Comparator;
|
26
|
import java.util.List;
|
27
|
|
28
|
/**
|
29
|
* Manager for configuration.
|
30
|
*/
|
31
|
@Controller
|
32
|
public class ConfigurationController extends BasicController{
|
33
|
|
34
|
/** Autowired configuration manager */
|
35
|
@Autowired
|
36
|
private ConfigurationManager configurationManager;
|
37
|
|
38
|
/**
|
39
|
* Opens an configuration page after configuration name was clicked
|
40
|
* @param id ID of configuration
|
41
|
* @return modelAndView of page to be shown
|
42
|
*/
|
43
|
@GetMapping("/configuration")
|
44
|
public ModelAndView configurationGet(@RequestParam("configurationID") String id) {
|
45
|
ModelAndView modelAndView = new ModelAndView("assembly");
|
46
|
|
47
|
ModelMap modelMap = modelAndView.getModelMap();
|
48
|
|
49
|
Configuration configuration = configurationManager.getConfigurationById(Long.parseLong(id));
|
50
|
|
51
|
// TODO: 04.05.2020 error page when id doesn't exist
|
52
|
|
53
|
String userName = AuthControl.getUserName();
|
54
|
|
55
|
if (userName == null) {
|
56
|
// TODO: 04.05.2020 error message, user not authenticated
|
57
|
}
|
58
|
else if (!userName.equals(configuration.getUser().getUsername())) {
|
59
|
// TODO: 04.05.2020 error page wrong user
|
60
|
return new ModelAndView("redirect:/");
|
61
|
}
|
62
|
|
63
|
List<ParameterInConfiguration> parametersInConfiguration = new ArrayList<>(configuration.getParametersInConfiguration());
|
64
|
configuration.setParametersInConfiguration(parametersInConfiguration);
|
65
|
|
66
|
initializeFields(configuration);
|
67
|
Comparator<ParameterInConfiguration> comparator = Comparator.comparingInt(o -> o.getParameter().getParameterOrder());
|
68
|
|
69
|
modelMap.addAttribute("configuration", configuration);
|
70
|
modelMap.addAttribute("comparator", comparator);
|
71
|
|
72
|
return modelAndView;
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* Saves or edits configuration and redirects to the page with configuration
|
77
|
* @param newConfiguration contained with configuration data
|
78
|
* @param id ID of configuration, if empty new configuration is created
|
79
|
* @param bindingResult binding result
|
80
|
* @return modelAndView with redirection
|
81
|
*/
|
82
|
@PostMapping("/configuration")
|
83
|
public ModelAndView configurationPost(@Valid Configuration newConfiguration, BindingResult bindingResult,
|
84
|
@RequestParam("configurationID") String id) {
|
85
|
ModelAndView modelAndView = new ModelAndView();
|
86
|
|
87
|
if (bindingResult.hasErrors()) {
|
88
|
// TODO: 04.05.2020 Error message
|
89
|
modelAndView.setViewName("redirect:/");
|
90
|
|
91
|
return modelAndView;
|
92
|
}
|
93
|
|
94
|
Configuration configuration = configurationManager.saveConfiguration(newConfiguration, id);
|
95
|
|
96
|
initializeFields(configuration);
|
97
|
|
98
|
modelAndView.setViewName("redirect:/configuration?configurationID=" + configuration.getId());
|
99
|
|
100
|
return modelAndView;
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Get method for configuration delete.
|
105
|
* @param id - ID of configuration.
|
106
|
* @return ModelAndView for index.
|
107
|
*/
|
108
|
@GetMapping("/configuration_delete")
|
109
|
public ModelAndView configurationDeleteGet(@RequestParam("configurationID") String id) {
|
110
|
ModelAndView modelAndView = new ModelAndView("redirect:/");
|
111
|
|
112
|
Long configurationId = Utils.tryParseLong(id);
|
113
|
|
114
|
if (configurationId == null) {
|
115
|
// TODO: print error in index.
|
116
|
}
|
117
|
|
118
|
boolean success = this.configurationManager.deleteConfiguration(configurationId);
|
119
|
// TODO: check success.
|
120
|
|
121
|
return modelAndView;
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* Initializes fields of a given configuration
|
126
|
* @param configuration configuration which fields should be initialized
|
127
|
*/
|
128
|
private void initializeFields(Configuration configuration) {
|
129
|
for(ParameterInConfiguration parameterInConfiguration : configuration.getParametersInConfiguration()) {
|
130
|
if(parameterInConfiguration.getLocation() == null) {
|
131
|
parameterInConfiguration.setLocation(new Location());
|
132
|
}
|
133
|
if(parameterInConfiguration.getOperator() == null) {
|
134
|
parameterInConfiguration.setOperator(new Operator());
|
135
|
}
|
136
|
if(parameterInConfiguration.getFunctions() == null) {
|
137
|
parameterInConfiguration.setFunctions(new ArrayList<>());
|
138
|
}
|
139
|
}
|
140
|
}
|
141
|
}
|