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.web.bind.annotation.*;
|
7
|
import org.springframework.web.servlet.ModelAndView;
|
8
|
import vldc.aswi.domain.Assembly;
|
9
|
import vldc.aswi.domain.Role;
|
10
|
import vldc.aswi.domain.parameter.Parameter;
|
11
|
import vldc.aswi.service.AssemblyManager;
|
12
|
import vldc.aswi.service.ConfigurationManager;
|
13
|
import vldc.aswi.service.RoleManager;
|
14
|
import vldc.aswi.service.SqlQueryManager;
|
15
|
import vldc.aswi.utils.AuthControl;
|
16
|
|
17
|
import javax.validation.Valid;
|
18
|
import java.util.ArrayList;
|
19
|
import java.util.List;
|
20
|
|
21
|
/**
|
22
|
* Controller for index page
|
23
|
*/
|
24
|
@Controller
|
25
|
public class IndexController extends BasicController {
|
26
|
|
27
|
/**
|
28
|
* Length when attribute string generation stops after its exceeded
|
29
|
*/
|
30
|
private static final int ATTRIBUTES_LENGTH = 50;
|
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
|
* Autowired role manager
|
52
|
*/
|
53
|
@Autowired
|
54
|
private RoleManager roleManager;
|
55
|
|
56
|
/**
|
57
|
* Shows index page
|
58
|
* @return modelAndView with index page
|
59
|
*/
|
60
|
@GetMapping("/")
|
61
|
public ModelAndView index(@ModelAttribute("successText") String successMsg, @ModelAttribute("errorText") String errorMsg) {
|
62
|
ModelAndView modelAndView = new ModelAndView("index");
|
63
|
|
64
|
ModelMap modelMap = modelAndView.getModelMap();
|
65
|
|
66
|
String roleName = AuthControl.getRoleName();
|
67
|
|
68
|
if (roleName == null) {
|
69
|
// TODO: 04.05.2020 error message, user not authenticated
|
70
|
}
|
71
|
|
72
|
Role role = roleManager.getRole(roleName);
|
73
|
|
74
|
List<Assembly> assemblies;
|
75
|
if (role.getName().equals("Administrátor")) {
|
76
|
assemblies = assemblyManager.getAssembliesOrdered();
|
77
|
}
|
78
|
else {
|
79
|
assemblies = assemblyManager.getAssembliesWithRoleOrdered(role);
|
80
|
}
|
81
|
|
82
|
modelMap.addAttribute("assemblies", assemblies);
|
83
|
modelMap.addAttribute("attributes", createAttributesString(assemblies));
|
84
|
modelMap.addAttribute("configurations", configurationManager.getConfigurations());
|
85
|
|
86
|
modelMap.addAttribute(assemblySuccessName, successMsg);
|
87
|
modelMap.addAttribute(assemblyErrorName, errorMsg);
|
88
|
|
89
|
|
90
|
return modelAndView;
|
91
|
}
|
92
|
|
93
|
@PostMapping("/")
|
94
|
public ModelAndView indexPost(@Valid @ModelAttribute("assemblyID") String id) {
|
95
|
ModelAndView modelAndView = new ModelAndView();
|
96
|
modelAndView.setViewName("redirect:/assembly");
|
97
|
|
98
|
return modelAndView;
|
99
|
}
|
100
|
|
101
|
/**
|
102
|
* Generates list of strings with attributes for each assembly
|
103
|
* @param assemblies list of assemblies
|
104
|
* @return list of strings with attributes
|
105
|
*/
|
106
|
private List<String> createAttributesString(List<Assembly> assemblies) {
|
107
|
List<String> attributes = new ArrayList<>();
|
108
|
for(Assembly assembly : assemblies) {
|
109
|
int i = 0;
|
110
|
StringBuilder stringBuilder = new StringBuilder();
|
111
|
for(Parameter parameter : assembly.getParameters()) {
|
112
|
stringBuilder.append(parameter.getName());
|
113
|
i++;
|
114
|
if (i == assembly.getParameters().size()) {
|
115
|
break;
|
116
|
}
|
117
|
else if (stringBuilder.toString().length() > ATTRIBUTES_LENGTH) {
|
118
|
stringBuilder.append(",...");
|
119
|
break;
|
120
|
}
|
121
|
else {
|
122
|
stringBuilder.append(", ");
|
123
|
}
|
124
|
}
|
125
|
attributes.add(stringBuilder.toString());
|
126
|
}
|
127
|
|
128
|
return attributes;
|
129
|
}
|
130
|
|
131
|
@ResponseBody
|
132
|
@RequestMapping("/saveOrder")
|
133
|
public String saveOrder(@RequestBody String data) {
|
134
|
assemblyManager.updateAssemblyOrder(data);
|
135
|
|
136
|
return "Pořadí sestav bylo uloženo";
|
137
|
}
|
138
|
|
139
|
}
|