Projekt

Obecné

Profil

Stáhnout (2.81 KB) Statistiky
| Větev: | Revize:
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.parameter.Parameter;
10
import vldc.aswi.service.AssemblyManager;
11
import vldc.aswi.service.ConfigurationManager;
12
import vldc.aswi.service.SqlQueryManager;
13

    
14
import javax.validation.Valid;
15
import java.util.ArrayList;
16
import java.util.List;
17

    
18
/**
19
 * Controller for index page
20
 */
21
@Controller
22
public class IndexController extends BasicController {
23

    
24
	/**
25
	 * Length when attribute string generation stops after its exceeded
26
	 */
27
	private static final int ATTRIBUTES_LENGTH = 50;
28

    
29
	/**
30
	 * Autowired SQL query manager
31
	 */
32
	@Autowired
33
	private SqlQueryManager sqlQueryManager;
34

    
35
	/**
36
	 * Autowired assembly manager
37
	 */
38
	@Autowired
39
	private AssemblyManager assemblyManager;
40

    
41
	/**
42
	 * Autowired configuration manager
43
	 */
44
	@Autowired
45
	private ConfigurationManager configurationManager;
46

    
47
	/**
48
	 * Shows index page
49
	 * @return modelAndView with index page
50
	 */
51
	@GetMapping("/")
52
	public ModelAndView index() {
53
		ModelAndView modelAndView = new ModelAndView("index");
54

    
55
		ModelMap modelMap = modelAndView.getModelMap();
56

    
57
		List<Assembly> assemblies = assemblyManager.getAssembliesOrdered();
58

    
59

    
60
		modelMap.addAttribute("assemblies", assemblies);
61
		modelMap.addAttribute("attributes", createAttributesString(assemblies));
62
		modelMap.addAttribute("configurations", configurationManager.getConfigurations());
63

    
64
		return modelAndView;
65
	}
66

    
67
	@PostMapping("/")
68
	public ModelAndView indexPost(@Valid @ModelAttribute("assemblyID") String id) {
69
		ModelAndView modelAndView = new ModelAndView();
70
		modelAndView.setViewName("redirect:/assembly");
71

    
72
		return modelAndView;
73
	}
74

    
75
	/**
76
	 * Generates list of strings with attributes for each assembly
77
	 * @param assemblies list of assemblies
78
	 * @return list of strings with attributes
79
	 */
80
	private List<String> createAttributesString(List<Assembly> assemblies) {
81
		List<String> attributes = new ArrayList<>();
82
		for(Assembly assembly : assemblies) {
83
			int i = 0;
84
			StringBuilder stringBuilder = new StringBuilder();
85
			for(Parameter parameter : assembly.getParameters()) {
86
				stringBuilder.append(parameter.getName());
87
				i++;
88
				if (i == assembly.getParameters().size()) {
89
					break;
90
				}
91
				else if (stringBuilder.toString().length() > ATTRIBUTES_LENGTH) {
92
					stringBuilder.append(",...");
93
					break;
94
				}
95
				else {
96
					stringBuilder.append(", ");
97
				}
98
			}
99
			attributes.add(stringBuilder.toString());
100
		}
101

    
102
		return attributes;
103
	}
104

    
105
	@ResponseBody
106
	@RequestMapping("/saveOrder")
107
	public String saveOrder(@RequestBody String data) {
108
		assemblyManager.updateAssemblyOrder(data);
109

    
110
		return "Ok";
111
	}
112

    
113
}
(4-4/5)