Projekt

Obecné

Profil

Stáhnout (13.4 KB) Statistiky
| Větev: | Revize:
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.GrantedAuthority;
7
import org.springframework.security.core.context.SecurityContextHolder;
8
import org.springframework.stereotype.Controller;
9
import org.springframework.ui.ModelMap;
10
import org.springframework.validation.BindingResult;
11
import org.springframework.web.bind.WebDataBinder;
12
import org.springframework.web.bind.annotation.GetMapping;
13
import org.springframework.web.bind.annotation.InitBinder;
14
import org.springframework.web.bind.annotation.ModelAttribute;
15
import org.springframework.web.bind.annotation.PostMapping;
16
import org.springframework.web.bind.annotation.RequestParam;
17
import org.springframework.web.servlet.ModelAndView;
18
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
19
import vldc.aswi.domain.*;
20
import vldc.aswi.domain.parameter.Parameter;
21
import vldc.aswi.domain.parameter.ParameterInConfiguration;
22
import vldc.aswi.service.*;
23
import vldc.aswi.service.parameter.ParameterManager;
24
import vldc.aswi.service.parameter.ParameterTypeManager;
25
import vldc.aswi.utils.AuthControl;
26
import vldc.aswi.validators.AssemblyValidator;
27
import vldc.aswi.utils.Utils;
28

    
29
import javax.validation.Valid;
30
import java.util.ArrayList;
31
import java.util.Comparator;
32
import java.util.Set;
33
import java.util.stream.Collectors;
34

    
35
/**
36
 * Controller for assemblies and configurations
37
 */
38
@Controller
39
public class AssemblyController extends BasicController {
40

    
41
    /** Autowired sql query manager. */
42
    @Autowired
43
    private SqlQueryManager sqlQueryManager;
44

    
45
    /** Autowired assembly manager. */
46
    @Autowired
47
    private AssemblyManager assemblyManager;
48

    
49
    /** Autowired role manager. */
50
    @Autowired
51
    private RoleManager roleManager;
52

    
53
    /** Autowired parameterType manager. */
54
    @Autowired
55
    private ParameterTypeManager parameterTypeManager;
56

    
57
    /** Autowired operator manager. */
58
    @Autowired
59
    private OperatorManager operatorManager;
60

    
61
    /** Autowired function manager. */
62
    @Autowired
63
    private FunctionManager functionManager;
64

    
65
    /** Autowired location manager. */
66
    @Autowired
67
    private LocationManager locationManager;
68

    
69
    /** Autowired parameter manager. */
70
    @Autowired
71
    private ParameterManager parameterManager;
72

    
73
    /** Autowired assembly validator */
74
    @Autowired
75
    private AssemblyValidator assemblyValidator;
76

    
77
    /** Name of thymeleaf parameter, which will contain text in assembly form. */
78
    private String assemblyTitleName = "title";
79

    
80
    /** Title text for assembly edit form.  */
81
    private String assemblyEditTitleText = "Editace - ";
82

    
83
    /** Title text for new assembly form. */
84
    private String assemblyNewTitleText = "Vytvoření nové sestavy";
85

    
86
    /** Name of thymeleaf parameter, which will contain error text from validator. */
87
    private String assemblyErrorName = "errorText";
88

    
89
    /**
90
     * Bind assembly validator.
91
     * @param binder Binder.
92
     */
93
    @InitBinder("assembly")
94
    protected void userBinder(WebDataBinder binder) {
95
        binder.addValidators(this.assemblyValidator);
96
    }
97

    
98
    /**
99
     * Get method for form, where configuration is created from assembly.
100
     * @param id - ID of assembly.
101
     * @return ModelAndView for assembly.
102
     */
103
    @GetMapping("/assembly")
104
    public ModelAndView assemblyGet(@RequestParam("assemblyID") String id) {
105
        ModelAndView modelAndView = new ModelAndView("assembly");
106

    
107
        ModelMap modelMap = modelAndView.getModelMap();
108

    
109
        Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
110

    
111
        // TODO: 04.05.2020 error page when id doesn't exist
112

    
113
        String roleName = AuthControl.getRoleName();
114

    
115
        if (roleName == null) {
116
            // TODO: 04.05.2020 error message, user not authenticated
117
        }
118

    
119
        Role role = roleManager.getRole(roleName);
120

    
121
        if (!assembly.getRoles().contains(role) && !role.getName().equals("Administrátor")) {
122
            // TODO: 04.05.2020 Error page, wrong role
123
            return new ModelAndView("redirect:/");
124
        }
125

    
126
        Configuration configuration = new Configuration();
127

    
128
        configuration.setAssembly(assembly);
129
        configuration.setTableName(assembly.getName());
130
        configuration.setParametersInConfiguration(new ArrayList<>());
131
        for(Parameter parameter : assembly.getParameters()) {
132
            ParameterInConfiguration parameterInConfiguration = new ParameterInConfiguration();
133
            parameterInConfiguration.setParameter(parameter);
134
            parameterInConfiguration.setConfiguration(configuration);
135
            parameterInConfiguration.setOperator(new Operator());
136
            parameterInConfiguration.setLocation(new Location());
137
            parameterInConfiguration.setParameterOrder(parameter.getParameterOrder());
138
            configuration.getParametersInConfiguration().add(parameterInConfiguration);
139
        }
140

    
141
        Comparator<ParameterInConfiguration> comparator = Comparator.comparingInt(o -> o.getParameter().getParameterOrder());
142

    
143
        modelMap.addAttribute("configuration", configuration);
144
        modelMap.addAttribute("comparator", comparator);
145
        modelMap.addAttribute("formAction", "/assembly?assemblyID=" + assembly.getId());
146

    
147
        return modelAndView;
148
    }
149

    
150

    
151
    /**
152
     * Post method for form, where configuration is created from assembly.
153
     * @param assembly - Assembly values.
154
     * @param bindingResult - Error results from assembly validators.
155
     * @param atts - Redirect attributes.
156
     * @return ModelAndView for assembly.
157
     */
158
    @PostMapping("/assembly")
159
    public ModelAndView assemblyPost(Assembly assembly,
160
                                     BindingResult bindingResult,
161
                                     RedirectAttributes atts,
162
                                     @RequestParam("assemblyID") String id,
163
                                     @RequestParam(required=false, value="generateTable") String generateTable,
164
                                     @RequestParam(required=false, value="exportXls") String exportXls,
165
                                     @RequestParam(required=false, value="exportPdf") String exportPdf,
166
                                     @RequestParam(required=false, value="saveConfiguration") String saveConfiguration)
167
    {
168
        if (generateTable != null)
169
        {
170
            System.out.println("Generuj tabulku");
171
        }
172
        else if (exportXls != null)
173
        {
174
            System.out.println("Generuj XLS");
175
        }
176
        else if (exportPdf != null)
177
        {
178
            System.out.println("Generuj PDF");
179
        }
180
        else if (saveConfiguration != null)
181
        {
182
            System.out.println("ulož konfiguraci");
183
        }
184

    
185

    
186

    
187
        ModelAndView modelAndView = new ModelAndView("redirect:/assembly?assemblyID=" + id);
188

    
189
        return modelAndView;
190
/*
191
        ModelMap modelMap = modelAndView.getModelMap();
192

    
193
        long assemblyID = assembly.getId();
194
        System.out.println(assemblyID);
195

    
196
        Assembly assembly2 = this.assemblyManager.getAssemblyById(assemblyID);
197

    
198
        modelMap.addAttribute("assemblies", this.assemblyManager.getAssemblies());
199
        modelMap.addAttribute("assembly", assembly2);
200
        modelMap.addAttribute("contingencyTableRows", this.sqlQueryManager.getContingencyTableRow(assembly2.getSQLQuery()));
201

    
202
        return modelAndView;
203

    
204
 */
205
    }
206

    
207
    /**
208
     * Get method for assembly delete.
209
     * @param id - ID of assembly.
210
     * @return ModelAndView for index.
211
     */
212
    @GetMapping("/assembly_delete")
213
    public ModelAndView assemblyDeleteGet(@RequestParam("assemblyID") String id) {
214
        ModelAndView modelAndView = new ModelAndView("redirect:/");
215

    
216
        Long assemblyId = Utils.tryParseLong(id);
217

    
218
        if (assemblyId == null) {
219
            // TODO: print error in index.
220
        }
221
        boolean success = this.assemblyManager.deleteAssembly(assemblyId);
222
        // TODO: check success.
223

    
224
        return modelAndView;
225
    }
226

    
227
    /**
228
     * Get method for assembly edit.
229
     * @param id - Id of assembly.
230
     * @return ModelAndView for assembly edit.
231
     */
232
    @GetMapping("/assembly_edit")
233
    public ModelAndView assemblyEditGet(@RequestParam("assemblyID") String id) {
234
        long startTime = System.currentTimeMillis();
235
        ModelAndView modelAndView = new ModelAndView("assembly_manage");
236

    
237
        ModelMap modelMap = modelAndView.getModelMap();
238

    
239
        Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
240

    
241
        modelMap.addAttribute("assembly", assembly);
242

    
243
        modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
244

    
245
        modelMap.addAttribute("allRoles", this.roleManager.getRoles());
246
        modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
247
        modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
248
        modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
249
        modelMap.addAttribute("allLocations", this.locationManager.getLocations());
250
        long estimatedTime = System.currentTimeMillis() - startTime;
251
        System.out.println("EDIT GET estimated - " + ((double)estimatedTime / 1000) + " sekund ");
252

    
253
        return modelAndView;
254
    }
255

    
256
    /**
257
     * Post method for assembly edit.
258
     * @param assembly - Edited assembly values.
259
     * @param result - Validator results.
260
     * @param assemblyIsPublic - Attribute indicating if assembly is public or not.
261
     * @return ModelAndView for assembly edit.
262
     */
263
    @PostMapping("/assembly_edit")
264
    public ModelAndView assemblyEditPost(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result,
265
                                              @Valid @ModelAttribute("checkboxPublic") String assemblyIsPublic) {
266
        long startTime = System.currentTimeMillis();
267
        ModelAndView modelAndView = new ModelAndView("assembly_manage");
268
        ModelMap modelMap = modelAndView.getModelMap();
269

    
270
        if (result.hasErrors()) {
271
            modelMap.addAttribute("assembly", assembly);
272

    
273
            modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
274
            modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
275
        }
276
        else {
277
            if (assemblyIsPublic.equals("on")) assembly.setIsPublic(1);
278
            else assembly.setIsPublic(0);
279

    
280
            // TODO: chybí defaultValue.
281
            Long assemblyID = this.assemblyManager.updateAssembly(assembly);
282
            this.parameterManager.updateParameters(assemblyID, assembly.getParameters());
283
            Assembly updatedAssembly = this.assemblyManager.getAssemblyById(assemblyID);
284

    
285
            modelMap.addAttribute("assembly", updatedAssembly);
286
            modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + updatedAssembly.getName());
287
        }
288

    
289
        modelMap.addAttribute("allRoles", this.roleManager.getRoles());
290
        modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
291
        modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
292
        modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
293
        modelMap.addAttribute("allLocations", this.locationManager.getLocations());
294
        long estimatedTime = System.currentTimeMillis() - startTime;
295
        System.out.println("EDIT POST estimated - " + ((double)estimatedTime / 1000) + " sekund ");
296

    
297
        return modelAndView;
298
    }
299

    
300
    /**
301
     * Get method for new assembly form.
302
     * @return ModelAndView for new assembly.
303
     */
304
    @GetMapping("/assembly_new")
305
    public ModelAndView assemblyNewGet() {
306
        ModelAndView modelAndView = new ModelAndView("assembly_manage");
307

    
308
        ModelMap modelMap = modelAndView.getModelMap();
309

    
310
        modelMap.addAttribute("assembly", new Assembly());
311

    
312
        modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
313

    
314
        modelMap.addAttribute("allRoles", this.roleManager.getRoles());
315
        modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
316
        modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
317
        modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
318
        modelMap.addAttribute("allLocations", this.locationManager.getLocations());
319

    
320
        return modelAndView;
321
    }
322

    
323
    /**
324
     * Post method for new assembly form.
325
     * @param assembly - Assembly values.
326
     * @param result - Validator results.
327
     * @return
328
     */
329
    @PostMapping("/assembly_new")
330
    public ModelAndView assemblyNewPost(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result) {
331
        ModelAndView modelAndView = new ModelAndView();
332
        ModelMap modelMap = modelAndView.getModelMap();
333

    
334
        if (result.hasErrors()) {
335
            modelAndView.setViewName("assembly_manage");
336
            modelMap.addAttribute("assembly", assembly);
337

    
338
            modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
339
            modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
340
        }
341
        else {
342
            // TODO: chybí získání default value.
343
            Long assemblyId = this.assemblyManager.createAssembly(assembly);
344
            this.parameterManager.addParameters(assemblyId, assembly.getParameters());
345

    
346
            modelAndView.setViewName("redirect:/assembly_edit?assemblyID=" + assemblyId);
347
        }
348

    
349
        return modelAndView;
350
    }
351
}
(1-1/5)