Projekt

Obecné

Profil

Stáhnout (9.23 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.validation.BindingResult;
7
import org.springframework.web.bind.WebDataBinder;
8
import org.springframework.web.bind.annotation.GetMapping;
9
import org.springframework.web.bind.annotation.InitBinder;
10
import org.springframework.web.bind.annotation.ModelAttribute;
11
import org.springframework.web.bind.annotation.PostMapping;
12
import org.springframework.web.servlet.ModelAndView;
13
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
14
import vldc.aswi.domain.Assembly;
15
import vldc.aswi.service.*;
16
import vldc.aswi.service.parameter.ParameterManager;
17
import vldc.aswi.service.parameter.ParameterTypeManager;
18
import vldc.aswi.validators.AssemblyValidator;
19

    
20
import javax.validation.Valid;
21

    
22
/**
23
 * Controller for assembly.
24
 */
25
@Controller
26
public class AssemblyController extends BasicController {
27

    
28
    /** Autowired sql query manager. */
29
    @Autowired
30
    private SqlQueryManager sqlQueryManager;
31

    
32
    /** Autowired assembly manager. */
33
    @Autowired
34
    private AssemblyManager assemblyManager;
35

    
36
    /** Autowired role manager. */
37
    @Autowired
38
    private RoleManager roleManager;
39

    
40
    /** Autowired parameterType manager. */
41
    @Autowired
42
    private ParameterTypeManager parameterTypeManager;
43

    
44
    /** Autowired operator manager. */
45
    @Autowired
46
    private OperatorManager operatorManager;
47

    
48
    /** Autowired function manager. */
49
    @Autowired
50
    private FunctionManager functionManager;
51

    
52
    /** Autowired location manager. */
53
    @Autowired
54
    private LocationManager locationManager;
55

    
56
    /** Autowired parameter manager. */
57
    @Autowired
58
    private ParameterManager parameterManager;
59

    
60
    /** Autowired assembly validator */
61
    @Autowired
62
    private AssemblyValidator assemblyValidator;
63

    
64
    /** Name of thymeleaf parameter, which will contain text in assembly form. */
65
    private String assemblyTitleName = "title";
66

    
67
    /** Title text for assembly edit form.  */
68
    private String assemblyEditTitleText = "Editace - ";
69

    
70
    /** Title text for new assembly form. */
71
    private String assemblyNewTitleText = "Vytvoření nové sestavy";
72

    
73
    /** Name of thymeleaf parameter, which will contain error text from validator. */
74
    private String assemblyErrorName = "errorText";
75

    
76
    /**
77
     * Bind assembly validator.
78
     * @param binder Binder.
79
     */
80
    @InitBinder("assembly")
81
    protected void userBinder(WebDataBinder binder) {
82
        binder.addValidators(this.assemblyValidator);
83
    }
84

    
85
    @GetMapping("/assembly")
86
    public ModelAndView assemblyIndex(@Valid @ModelAttribute("assemblyID") String id) {
87
        ModelAndView modelAndView = new ModelAndView("assembly");
88

    
89
        ModelMap modelMap = modelAndView.getModelMap();
90

    
91
        Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
92

    
93
        modelMap.addAttribute("assemblies", this.assemblyManager.getAssemblies());
94
        modelMap.addAttribute("assembly", assembly);
95

    
96
        return modelAndView;
97
    }
98

    
99

    
100
    @PostMapping("/assembly")
101
    public ModelAndView indexPost(@Valid Assembly assembly, BindingResult bindingResult, RedirectAttributes atts) {
102
        ModelAndView modelAndView = new ModelAndView();
103

    
104
        ModelMap modelMap = modelAndView.getModelMap();
105

    
106
        long assemblyID = assembly.getId();
107

    
108
        Assembly assembly2 = this.assemblyManager.getAssemblyById(assemblyID);
109

    
110
        modelMap.addAttribute("assemblies", this.assemblyManager.getAssemblies());
111
        modelMap.addAttribute("assembly", assembly2);
112
        modelMap.addAttribute("contingencyTableRows", this.sqlQueryManager.getContingencyTableRow(assembly2.getSQLQuery()));
113

    
114
        return modelAndView;
115
    }
116

    
117
    /**
118
     * Get method for assembly edit.
119
     * @param id - Id of assembly.
120
     * @return ModelAndView for assembly edit.
121
     */
122
    @GetMapping("/assembly_edit")
123
    public ModelAndView assemblyEditIndex(@Valid @ModelAttribute("assemblyID") String id) {
124
        ModelAndView modelAndView = new ModelAndView("assembly_manage");
125

    
126
        ModelMap modelMap = modelAndView.getModelMap();
127

    
128
        Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
129

    
130
        modelMap.addAttribute("assembly", assembly);
131

    
132
        modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
133

    
134
        modelMap.addAttribute("allRoles", this.roleManager.getRoles());
135
        modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
136
        modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
137
        modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
138
        modelMap.addAttribute("allLocations", this.locationManager.getLocations());
139

    
140
        return modelAndView;
141
    }
142

    
143
    /**
144
     * Post method for assembly edit.
145
     * @param assembly - Edited assembly values.
146
     * @param result - Validator results.
147
     * @param assemblyIsPublic - Attribute indicating if assembly is public or not.
148
     * @return ModelAndView for assembly edit.
149
     */
150
    @PostMapping("/assembly_edit")
151
    public ModelAndView assemblyEditPostIndex(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result,
152
                                              @Valid @ModelAttribute("checkboxPublic") String assemblyIsPublic) {
153

    
154
        ModelAndView modelAndView = new ModelAndView("assembly_manage");
155
        ModelMap modelMap = modelAndView.getModelMap();
156

    
157
        if (result.hasErrors()) {
158
            modelMap.addAttribute("assembly", assembly);
159

    
160
            modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
161
            modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
162
        }
163
        else {
164
            if (assemblyIsPublic.equals("on")) assembly.setIsPublic(1);
165
            else assembly.setIsPublic(0);
166

    
167
            // TODO: chybí defaultValue.
168
            Long assemblyID = this.assemblyManager.updateAssembly(assembly);
169
            this.parameterManager.updateParameters(assemblyID, assembly.getParameters());
170
            Assembly updatedAssembly = this.assemblyManager.getAssemblyById(assembly.getId());
171

    
172
            modelMap.addAttribute("assembly", updatedAssembly);
173
            modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + updatedAssembly.getName());
174
        }
175

    
176
        modelMap.addAttribute("allRoles", this.roleManager.getRoles());
177
        modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
178
        modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
179
        modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
180
        modelMap.addAttribute("allLocations", this.locationManager.getLocations());
181

    
182
        return modelAndView;
183
    }
184

    
185
    /**
186
     * Get method for new assembly form.
187
     * @return ModelAndView for new assembly.
188
     */
189
    @GetMapping("/assembly_new")
190
    public ModelAndView assemblyNewIndex() {
191
        ModelAndView modelAndView = new ModelAndView("assembly_manage");
192

    
193
        ModelMap modelMap = modelAndView.getModelMap();
194

    
195
        modelMap.addAttribute("assembly", new Assembly());
196

    
197
        modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
198

    
199
        modelMap.addAttribute("allRoles", this.roleManager.getRoles());
200
        modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
201
        modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
202
        modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
203
        modelMap.addAttribute("allLocations", this.locationManager.getLocations());
204

    
205
        return modelAndView;
206
    }
207

    
208
    /**
209
     * Post method for new assembly form.
210
     * @param assembly - Assembly values.
211
     * @param result - Validator results.
212
     * @return
213
     */
214
    @PostMapping("/assembly_new")
215
    public ModelAndView assemblyNewIndexPost(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result) {
216
        ModelAndView modelAndView = new ModelAndView();
217

    
218
        if (result.hasErrors()) {
219
            ModelMap modelMap = modelAndView.getModelMap();
220
            modelAndView.setViewName("assembly_manage");
221

    
222
            modelMap.addAttribute("assembly", assembly);
223

    
224
            modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
225
            modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
226

    
227
            modelMap.addAttribute("allRoles", this.roleManager.getRoles());
228
            modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
229
            modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
230
            modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
231
            modelMap.addAttribute("allLocations", this.locationManager.getLocations());
232
        }
233

    
234
        ModelMap modelMap = modelAndView.getModelMap();
235

    
236
        // TODO: chybí získání default value.
237
        Long assemblyId = this.assemblyManager.createAssembly(assembly);
238
        this.parameterManager.addParameters(assemblyId, assembly.getParameters());
239

    
240
        modelAndView.setViewName("redirect:/assembly_edit?assemblyID=" + assemblyId);
241

    
242
        return modelAndView;
243
    }
244
}
(1-1/4)