Projekt

Obecné

Profil

Stáhnout (17.8 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.bind.annotation.RequestParam;
13
import org.springframework.web.servlet.ModelAndView;
14
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
15
import vldc.aswi.domain.*;
16
import vldc.aswi.domain.parameter.Parameter;
17
import vldc.aswi.domain.parameter.ParameterInConfiguration;
18
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow;
19
import vldc.aswi.service.*;
20
import vldc.aswi.service.parameter.ParameterManager;
21
import vldc.aswi.service.parameter.ParameterTypeManager;
22
import vldc.aswi.utils.AuthControl;
23
import vldc.aswi.validators.AssemblyValidator;
24
import vldc.aswi.utils.Utils;
25

    
26
import javax.validation.Valid;
27
import java.util.ArrayList;
28
import java.util.Comparator;
29
import java.util.List;
30
import java.util.Set;
31
import java.util.stream.Collectors;
32

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

    
39
    /** Autowired sql query manager. */
40
    @Autowired
41
    private SqlQueryManager sqlQueryManager;
42

    
43
    /** Autowired assembly manager. */
44
    @Autowired
45
    private AssemblyManager assemblyManager;
46

    
47
    /** Autowired configuration manager */
48
    @Autowired
49
    private ConfigurationManager configurationManager;
50

    
51
    /** Autowired role manager. */
52
    @Autowired
53
    private RoleManager roleManager;
54

    
55
    /** Autowired parameterType manager. */
56
    @Autowired
57
    private ParameterTypeManager parameterTypeManager;
58

    
59
    /** Autowired operator manager. */
60
    @Autowired
61
    private OperatorManager operatorManager;
62

    
63
    /** Autowired function manager. */
64
    @Autowired
65
    private FunctionManager functionManager;
66

    
67
    /** Autowired location manager. */
68
    @Autowired
69
    private LocationManager locationManager;
70

    
71
    /** Autowired parameter manager. */
72
    @Autowired
73
    private ParameterManager parameterManager;
74

    
75
    /** Autowired assembly validator */
76
    @Autowired
77
    private AssemblyValidator assemblyValidator;
78

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

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

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

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

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

    
106
        ModelMap modelMap = modelAndView.getModelMap();
107

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

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

    
112
        String roleName = AuthControl.getRoleName();
113

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

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

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

    
125
        Configuration configuration = new Configuration();
126

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

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

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

    
146
        return modelAndView;
147
    }
148

    
149

    
150
    /**
151
     * Post method for form, where configuration is created from assembly.
152
     * @param newConfiguration - Configuration values.
153
     * @param bindingResult - Error results from assembly validators.
154
     * @param atts - Redirect attributes.
155
     * @return ModelAndView for assembly.
156
     */
157
    @PostMapping("/assembly")
158
    public ModelAndView assemblyPost(Configuration newConfiguration,
159
                                     BindingResult bindingResult,
160
                                     RedirectAttributes atts,
161
                                     @RequestParam("assemblyID") String id,
162
                                     @RequestParam(required=false, value="generateTable") String generateTable,
163
                                     @RequestParam(required=false, value="exportXls") String exportXls,
164
                                     @RequestParam(required=false, value="exportPdf") String exportPdf,
165
                                     @RequestParam(required=false, value="saveConfiguration") String saveConfiguration,
166
                                     RedirectAttributes redirectAttributes)
167
    {
168

    
169
        ModelAndView modelAndView = new ModelAndView();
170

    
171
        if (bindingResult.hasErrors()) {
172
            // TODO: 04.05.2020 Error message
173
            modelAndView.setViewName("redirect:/");
174

    
175
            return modelAndView;
176
        }
177

    
178
        if (generateTable != null)
179
        {
180
            Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
181
            newConfiguration.setAssembly(assembly);
182
            newConfiguration.setTableName(assembly.getName());
183

    
184
            // If configuration name is empty, then replace it by assembly name.
185
            if (newConfiguration.getName().equals("")) {
186
                newConfiguration.setName(assembly.getName());
187
            }
188

    
189
            System.out.println("Generuj tabulku");
190
            prepareForTable(newConfiguration);
191

    
192
            List<ContingencyTableRow> rows = this.sqlQueryManager.getContingencyTableRow(newConfiguration.getAssembly().getSQLQuery(),
193
                    newConfiguration.getParametersInConfiguration());
194

    
195
            System.out.println("Generuj tabulku");
196
            ModelMap modelMap = modelAndView.getModelMap();
197
            modelMap.addAttribute("configurationID", id);
198
            modelMap.addAttribute("contingencyTableRows", rows);
199
            Comparator<ParameterInConfiguration> comparator = Comparator.comparingInt(o -> o.getParameter().getParameterOrder());
200
            initializeFields(newConfiguration);
201

    
202
            modelMap.addAttribute("configuration", newConfiguration);
203
            modelMap.addAttribute("comparator", comparator);
204
            modelMap.addAttribute("formAction", "/assembly?assemblyID=" + newConfiguration.getAssembly().getId());
205
            modelAndView.setViewName("assembly");
206
        }
207
        else if (exportXls != null)
208
        {
209
            System.out.println("Generuj XLS");
210
        }
211
        else if (exportPdf != null)
212
        {
213
            System.out.println("Generuj PDF");
214
        }
215
        else if (saveConfiguration != null)
216
        {
217
            System.out.println("ulož konfiguraci");
218

    
219
            Configuration configuration = configurationManager.saveConfiguration(newConfiguration, "");
220

    
221
            initializeFields(configuration);
222

    
223
            redirectAttributes.addFlashAttribute(assemblySuccessName, "Šablona byla úspěšně uložena");
224

    
225
            modelAndView.setViewName("redirect:/configuration?configurationID=" + configuration.getId());
226
        }
227

    
228
        return modelAndView;
229
/*
230
        ModelMap modelMap = modelAndView.getModelMap();
231

    
232
        long assemblyID = assembly.getId();
233
        System.out.println(assemblyID);
234

    
235
        Assembly assembly2 = this.assemblyManager.getAssemblyById(assemblyID);
236

    
237
        modelMap.addAttribute("assemblies", this.assemblyManager.getAssemblies());
238
        modelMap.addAttribute("assembly", assembly2);
239
        modelMap.addAttribute("contingencyTableRows", this.sqlQueryManager.getContingencyTableRow(assembly2.getSQLQuery()));
240

    
241
        return modelAndView;
242

    
243
 */
244
    }
245

    
246
    /**
247
     * Get method for assembly delete.
248
     * @param id - ID of assembly.
249
     * @return ModelAndView for index.
250
     */
251
    @GetMapping("/assembly_delete")
252
    public ModelAndView assemblyDeleteGet(@RequestParam("assemblyID") String id, RedirectAttributes redirectAttributes) {
253
        ModelAndView modelAndView = new ModelAndView("redirect:/");
254

    
255
        Long assemblyId = Utils.tryParseLong(id);
256

    
257
        if (assemblyId == null) {
258
            redirectAttributes.addFlashAttribute(assemblyErrorName, "Sestavu se nepodařilo odstranit.");
259
        }
260
        boolean success = this.assemblyManager.deleteAssembly(assemblyId);
261

    
262
        if (success)
263
        {
264
            redirectAttributes.addFlashAttribute(assemblySuccessName, "Sestava byla úspěšně odstraněna");
265
        }
266
        else
267
        {
268
            redirectAttributes.addFlashAttribute(assemblyErrorName, "Sestavu se nepodařilo odstranit");
269
        }
270

    
271
        return modelAndView;
272
    }
273

    
274
    /**
275
     * Get method for assembly edit.
276
     * @param id - Id of assembly.
277
     * @return ModelAndView for assembly edit.
278
     */
279
    @GetMapping("/assembly_edit")
280
    public ModelAndView assemblyEditGet(@RequestParam("assemblyID") String id) {
281
        long startTime = System.currentTimeMillis();
282
        ModelAndView modelAndView = new ModelAndView("assembly_manage");
283

    
284
        ModelMap modelMap = modelAndView.getModelMap();
285

    
286
        Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
287

    
288
        modelMap.addAttribute("assembly", assembly);
289

    
290
        modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
291

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

    
300
        return modelAndView;
301
    }
302

    
303
    /**
304
     * Post method for assembly edit.
305
     * @param assembly - Edited assembly values.
306
     * @param result - Validator results.
307
     * @param assemblyIsPublic - Attribute indicating if assembly is public or not.
308
     * @return ModelAndView for assembly edit.
309
     */
310
    @PostMapping("/assembly_edit")
311
    public ModelAndView assemblyEditPost(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result,
312
                                              @Valid @ModelAttribute("checkboxPublic") String assemblyIsPublic) {
313
        long startTime = System.currentTimeMillis();
314
        ModelAndView modelAndView = new ModelAndView("assembly_manage");
315
        ModelMap modelMap = modelAndView.getModelMap();
316

    
317
        if (result.hasErrors()) {
318
            modelMap.addAttribute("assembly", assembly);
319

    
320
            modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
321
            modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
322
        }
323
        else {
324
            if (assemblyIsPublic.equals("on")) assembly.setIsPublic(1);
325
            else assembly.setIsPublic(0);
326

    
327
            // TODO: chybí defaultValue.
328
            Long assemblyID = this.assemblyManager.updateAssembly(assembly);
329

    
330
            this.parameterManager.updateParameters(assemblyID, assembly.getParameters());
331
            Assembly updatedAssembly = this.assemblyManager.getAssemblyById(assemblyID);
332

    
333
            modelMap.addAttribute("assembly", updatedAssembly);
334
            modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + updatedAssembly.getName());
335
            modelMap.addAttribute(assemblySuccessName, "Úspěšně uloženo");
336
        }
337

    
338
        modelMap.addAttribute("allRoles", this.roleManager.getRoles());
339
        modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
340
        modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
341
        modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
342
        modelMap.addAttribute("allLocations", this.locationManager.getLocations());
343
        long estimatedTime = System.currentTimeMillis() - startTime;
344
        System.out.println("EDIT POST estimated - " + ((double)estimatedTime / 1000) + " sekund ");
345

    
346
        return modelAndView;
347
    }
348

    
349
    /**
350
     * Get method for new assembly form.
351
     * @return ModelAndView for new assembly.
352
     */
353
    @GetMapping("/assembly_new")
354
    public ModelAndView assemblyNewGet() {
355
        ModelAndView modelAndView = new ModelAndView("assembly_manage");
356

    
357
        ModelMap modelMap = modelAndView.getModelMap();
358

    
359
        modelMap.addAttribute("assembly", new Assembly());
360

    
361
        modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
362

    
363
        modelMap.addAttribute("allRoles", this.roleManager.getRoles());
364
        modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
365
        modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
366
        modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
367
        modelMap.addAttribute("allLocations", this.locationManager.getLocations());
368

    
369
        return modelAndView;
370
    }
371

    
372
    /**
373
     * Post method for new assembly form.
374
     * @param assembly - Assembly values.
375
     * @param result - Validator results.
376
     * @return
377
     */
378
    @PostMapping("/assembly_new")
379
    public ModelAndView assemblyNewPost(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result) {
380
        ModelAndView modelAndView = new ModelAndView();
381
        ModelMap modelMap = modelAndView.getModelMap();
382

    
383
        if (result.hasErrors()) {
384
            modelAndView.setViewName("assembly_manage");
385
            modelMap.addAttribute("assembly", assembly);
386

    
387
            modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
388
            modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
389
        }
390
        else {
391
            // TODO: chybí získání default value.
392
            Long assemblyId = this.assemblyManager.createAssembly(assembly);
393
            this.parameterManager.addParameters(assemblyId, assembly.getParameters());
394

    
395
            modelAndView.setViewName("redirect:/assembly_edit?assemblyID=" + assemblyId);
396
            modelMap.addAttribute(assemblySuccessName, "Sestava byla úspěšně vytvořena");
397
        }
398

    
399
        modelMap.addAttribute("allRoles", this.roleManager.getRoles());
400
        modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
401
        modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
402
        modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
403
        modelMap.addAttribute("allLocations", this.locationManager.getLocations());
404

    
405
        return modelAndView;
406
    }
407

    
408
    /**
409
     * Initializes fields of a given configuration
410
     * @param configuration configuration which fields should be initialized
411
     */
412
    private void initializeFields(Configuration configuration) {
413
        for(ParameterInConfiguration parameterInConfiguration : configuration.getParametersInConfiguration()) {
414
            if(parameterInConfiguration.getLocation() == null) {
415
                parameterInConfiguration.setLocation(new Location());
416
            }
417
            if(parameterInConfiguration.getOperator() == null) {
418
                parameterInConfiguration.setOperator(new Operator());
419
            }
420
            if(parameterInConfiguration.getFunctions() == null) {
421
                parameterInConfiguration.setFunctions(new ArrayList<>());
422
            }
423
        }
424
    }
425

    
426
    /**
427
     * Prepares the configuration for contingency table generation. Initializes assembly and parameters of parameters in
428
     * configuration.
429
     * @param configuration configuration to be prepared.
430
     */
431
    private void prepareForTable(Configuration configuration) {
432
        Assembly assembly = assemblyManager.getAssemblyById(configuration.getAssembly().getId());
433
        configuration.setAssembly(assembly);
434

    
435
        for(int i = 0; i < configuration.getParametersInConfiguration().size(); i++) {
436
            ParameterInConfiguration parameterInConfiguration = configuration.getParametersInConfiguration().get(i);
437
            parameterInConfiguration.setParameter(assembly.getParameters().get(i));
438
            if (parameterInConfiguration.getLocation() != null) {
439
                parameterInConfiguration.setLocation(locationManager.getLocationById(parameterInConfiguration.getLocation().getId()));
440
            }
441
        }
442
    }
443
}
(1-1/5)