Projekt

Obecné

Profil

Stáhnout (16.9 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 configuration manager */
50
    @Autowired
51
    private ConfigurationManager configurationManager;
52

    
53
    /** Autowired role manager. */
54
    @Autowired
55
    private RoleManager roleManager;
56

    
57
    /** Autowired parameterType manager. */
58
    @Autowired
59
    private ParameterTypeManager parameterTypeManager;
60

    
61
    /** Autowired operator manager. */
62
    @Autowired
63
    private OperatorManager operatorManager;
64

    
65
    /** Autowired function manager. */
66
    @Autowired
67
    private FunctionManager functionManager;
68

    
69
    /** Autowired location manager. */
70
    @Autowired
71
    private LocationManager locationManager;
72

    
73
    /** Autowired parameter manager. */
74
    @Autowired
75
    private ParameterManager parameterManager;
76

    
77
    /** Autowired assembly validator */
78
    @Autowired
79
    private AssemblyValidator assemblyValidator;
80

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

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

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

    
90
    /** Name of thymeleaf parameter, which will contain error text from validator. */
91
    private String assemblyErrorName = "errorText";
92

    
93
    /**
94
     * Bind assembly validator.
95
     * @param binder Binder.
96
     */
97
    @InitBinder("assembly")
98
    protected void userBinder(WebDataBinder binder) {
99
        binder.addValidators(this.assemblyValidator);
100
    }
101

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

    
111
        ModelMap modelMap = modelAndView.getModelMap();
112

    
113
        Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
114

    
115
        // TODO: 04.05.2020 error page when id doesn't exist
116

    
117
        String roleName = AuthControl.getRoleName();
118

    
119
        if (roleName == null) {
120
            // TODO: 04.05.2020 error message, user not authenticated
121
        }
122

    
123
        Role role = roleManager.getRole(roleName);
124

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

    
130
        Configuration configuration = new Configuration();
131

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

    
145
        Comparator<ParameterInConfiguration> comparator = Comparator.comparingInt(o -> o.getParameter().getParameterOrder());
146

    
147
        modelMap.addAttribute("configuration", configuration);
148
        modelMap.addAttribute("comparator", comparator);
149
        modelMap.addAttribute("formAction", "/assembly?assemblyID=" + assembly.getId());
150

    
151
        return modelAndView;
152
    }
153

    
154

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

    
173
        ModelAndView modelAndView = new ModelAndView();
174

    
175
        if (bindingResult.hasErrors()) {
176
            // TODO: 04.05.2020 Error message
177
            modelAndView.setViewName("redirect:/");
178

    
179
            return modelAndView;
180
        }
181

    
182
        if (generateTable != null)
183
        {
184
            System.out.println("Generuj tabulku");
185
            prepareForTable(newConfiguration);
186
            System.out.println("Generuj tabulku");
187
            ModelMap modelMap = modelAndView.getModelMap();
188
            modelMap.addAttribute("configurationID", id);
189
            modelMap.addAttribute("contingencyTableRows", this.sqlQueryManager.getContingencyTableRow(newConfiguration));
190
            Comparator<ParameterInConfiguration> comparator = Comparator.comparingInt(o -> o.getParameter().getParameterOrder());
191
            initializeFields(newConfiguration);
192
            modelMap.addAttribute("configuration", newConfiguration);
193
            modelMap.addAttribute("comparator", comparator);
194
            modelMap.addAttribute("formAction", "/assembly?assemblyID=" + newConfiguration.getAssembly().getId());
195
            modelAndView.setViewName("assembly");
196
        }
197
        else if (exportXls != null)
198
        {
199
            System.out.println("Generuj XLS");
200
        }
201
        else if (exportPdf != null)
202
        {
203
            System.out.println("Generuj PDF");
204
        }
205
        else if (saveConfiguration != null)
206
        {
207
            System.out.println("ulož konfiguraci");
208

    
209
            Configuration configuration = configurationManager.saveConfiguration(newConfiguration, "");
210

    
211
            initializeFields(configuration);
212

    
213
            modelAndView.setViewName("redirect:/configuration?configurationID=" + configuration.getId());
214
        }
215

    
216
        return modelAndView;
217
/*
218
        ModelMap modelMap = modelAndView.getModelMap();
219

    
220
        long assemblyID = assembly.getId();
221
        System.out.println(assemblyID);
222

    
223
        Assembly assembly2 = this.assemblyManager.getAssemblyById(assemblyID);
224

    
225
        modelMap.addAttribute("assemblies", this.assemblyManager.getAssemblies());
226
        modelMap.addAttribute("assembly", assembly2);
227
        modelMap.addAttribute("contingencyTableRows", this.sqlQueryManager.getContingencyTableRow(assembly2.getSQLQuery()));
228

    
229
        return modelAndView;
230

    
231
 */
232
    }
233

    
234
    /**
235
     * Get method for assembly delete.
236
     * @param id - ID of assembly.
237
     * @return ModelAndView for index.
238
     */
239
    @GetMapping("/assembly_delete")
240
    public ModelAndView assemblyDeleteGet(@RequestParam("assemblyID") String id) {
241
        ModelAndView modelAndView = new ModelAndView("redirect:/");
242

    
243
        Long assemblyId = Utils.tryParseLong(id);
244

    
245
        if (assemblyId == null) {
246
            // TODO: print error in index.
247
        }
248
        boolean success = this.assemblyManager.deleteAssembly(assemblyId);
249
        // TODO: check success.
250

    
251
        return modelAndView;
252
    }
253

    
254
    /**
255
     * Get method for assembly edit.
256
     * @param id - Id of assembly.
257
     * @return ModelAndView for assembly edit.
258
     */
259
    @GetMapping("/assembly_edit")
260
    public ModelAndView assemblyEditGet(@RequestParam("assemblyID") String id) {
261
        long startTime = System.currentTimeMillis();
262
        ModelAndView modelAndView = new ModelAndView("assembly_manage");
263

    
264
        ModelMap modelMap = modelAndView.getModelMap();
265

    
266
        Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
267

    
268
        modelMap.addAttribute("assembly", assembly);
269

    
270
        modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
271

    
272
        modelMap.addAttribute("allRoles", this.roleManager.getRoles());
273
        modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
274
        modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
275
        modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
276
        modelMap.addAttribute("allLocations", this.locationManager.getLocations());
277
        long estimatedTime = System.currentTimeMillis() - startTime;
278
        System.out.println("EDIT GET estimated - " + ((double)estimatedTime / 1000) + " sekund ");
279

    
280
        return modelAndView;
281
    }
282

    
283
    /**
284
     * Post method for assembly edit.
285
     * @param assembly - Edited assembly values.
286
     * @param result - Validator results.
287
     * @param assemblyIsPublic - Attribute indicating if assembly is public or not.
288
     * @return ModelAndView for assembly edit.
289
     */
290
    @PostMapping("/assembly_edit")
291
    public ModelAndView assemblyEditPost(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result,
292
                                              @Valid @ModelAttribute("checkboxPublic") String assemblyIsPublic) {
293
        long startTime = System.currentTimeMillis();
294
        ModelAndView modelAndView = new ModelAndView("assembly_manage");
295
        ModelMap modelMap = modelAndView.getModelMap();
296

    
297
        if (result.hasErrors()) {
298
            modelMap.addAttribute("assembly", assembly);
299

    
300
            modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
301
            modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
302
        }
303
        else {
304
            if (assemblyIsPublic.equals("on")) assembly.setIsPublic(1);
305
            else assembly.setIsPublic(0);
306

    
307
            // TODO: chybí defaultValue.
308
            Long assemblyID = this.assemblyManager.updateAssembly(assembly);
309
            this.parameterManager.updateParameters(assemblyID, assembly.getParameters());
310
            Assembly updatedAssembly = this.assemblyManager.getAssemblyById(assemblyID);
311

    
312
            modelMap.addAttribute("assembly", updatedAssembly);
313
            modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + updatedAssembly.getName());
314
        }
315

    
316
        modelMap.addAttribute("allRoles", this.roleManager.getRoles());
317
        modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
318
        modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
319
        modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
320
        modelMap.addAttribute("allLocations", this.locationManager.getLocations());
321
        long estimatedTime = System.currentTimeMillis() - startTime;
322
        System.out.println("EDIT POST estimated - " + ((double)estimatedTime / 1000) + " sekund ");
323

    
324
        return modelAndView;
325
    }
326

    
327
    /**
328
     * Get method for new assembly form.
329
     * @return ModelAndView for new assembly.
330
     */
331
    @GetMapping("/assembly_new")
332
    public ModelAndView assemblyNewGet() {
333
        ModelAndView modelAndView = new ModelAndView("assembly_manage");
334

    
335
        ModelMap modelMap = modelAndView.getModelMap();
336

    
337
        modelMap.addAttribute("assembly", new Assembly());
338

    
339
        modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
340

    
341
        modelMap.addAttribute("allRoles", this.roleManager.getRoles());
342
        modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
343
        modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
344
        modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
345
        modelMap.addAttribute("allLocations", this.locationManager.getLocations());
346

    
347
        return modelAndView;
348
    }
349

    
350
    /**
351
     * Post method for new assembly form.
352
     * @param assembly - Assembly values.
353
     * @param result - Validator results.
354
     * @return
355
     */
356
    @PostMapping("/assembly_new")
357
    public ModelAndView assemblyNewPost(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result) {
358
        ModelAndView modelAndView = new ModelAndView();
359
        ModelMap modelMap = modelAndView.getModelMap();
360

    
361
        if (result.hasErrors()) {
362
            modelAndView.setViewName("assembly_manage");
363
            modelMap.addAttribute("assembly", assembly);
364

    
365
            modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
366
            modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
367
        }
368
        else {
369
            // TODO: chybí získání default value.
370
            Long assemblyId = this.assemblyManager.createAssembly(assembly);
371
            this.parameterManager.addParameters(assemblyId, assembly.getParameters());
372

    
373
            modelAndView.setViewName("redirect:/assembly_edit?assemblyID=" + assemblyId);
374
        }
375

    
376
        modelMap.addAttribute("allRoles", this.roleManager.getRoles());
377
        modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
378
        modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
379
        modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
380
        modelMap.addAttribute("allLocations", this.locationManager.getLocations());
381

    
382
        return modelAndView;
383
    }
384

    
385
    /**
386
     * Initializes fields of a given configuration
387
     * @param configuration configuration which fields should be initialized
388
     */
389
    private void initializeFields(Configuration configuration) {
390
        for(ParameterInConfiguration parameterInConfiguration : configuration.getParametersInConfiguration()) {
391
            if(parameterInConfiguration.getLocation() == null) {
392
                parameterInConfiguration.setLocation(new Location());
393
            }
394
            if(parameterInConfiguration.getOperator() == null) {
395
                parameterInConfiguration.setOperator(new Operator());
396
            }
397
            if(parameterInConfiguration.getFunctions() == null) {
398
                parameterInConfiguration.setFunctions(new ArrayList<>());
399
            }
400
        }
401
    }
402

    
403
    /**
404
     * Prepares the configuration for contingency table generation. Initializes assembly and parameters of parameters in
405
     * configuration.
406
     * @param configuration configuration to be prepared.
407
     */
408
    private void prepareForTable(Configuration configuration) {
409
        Assembly assembly = assemblyManager.getAssemblyById(configuration.getAssembly().getId());
410
        configuration.setAssembly(assembly);
411

    
412
        for(int i = 0; i < configuration.getParametersInConfiguration().size(); i++) {
413
            ParameterInConfiguration parameterInConfiguration = configuration.getParametersInConfiguration().get(i);
414
            parameterInConfiguration.setParameter(assembly.getParameters().get(i));
415
            if (parameterInConfiguration.getLocation() != null) {
416
                parameterInConfiguration.setLocation(locationManager.getLocationById(parameterInConfiguration.getLocation().getId()));
417
            }
418
        }
419
    }
420
}
(1-1/5)