Projekt

Obecné

Profil

Stáhnout (16.3 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
        ModelAndView modelAndView = new ModelAndView();
173

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

    
201
            Configuration configuration = configurationManager.saveConfiguration(newConfiguration, "");
202

    
203
            initializeFields(configuration);
204

    
205
            modelAndView.setViewName("redirect:/configuration?configurationID=" + configuration.getId());
206
        }
207

    
208
        return modelAndView;
209
/*
210
        ModelMap modelMap = modelAndView.getModelMap();
211

    
212
        long assemblyID = assembly.getId();
213
        System.out.println(assemblyID);
214

    
215
        Assembly assembly2 = this.assemblyManager.getAssemblyById(assemblyID);
216

    
217
        modelMap.addAttribute("assemblies", this.assemblyManager.getAssemblies());
218
        modelMap.addAttribute("assembly", assembly2);
219
        modelMap.addAttribute("contingencyTableRows", this.sqlQueryManager.getContingencyTableRow(assembly2.getSQLQuery()));
220

    
221
        return modelAndView;
222

    
223
 */
224
    }
225

    
226
    /**
227
     * Get method for assembly delete.
228
     * @param id - ID of assembly.
229
     * @return ModelAndView for index.
230
     */
231
    @GetMapping("/assembly_delete")
232
    public ModelAndView assemblyDeleteGet(@RequestParam("assemblyID") String id) {
233
        ModelAndView modelAndView = new ModelAndView("redirect:/");
234

    
235
        Long assemblyId = Utils.tryParseLong(id);
236

    
237
        if (assemblyId == null) {
238
            // TODO: print error in index.
239
        }
240
        boolean success = this.assemblyManager.deleteAssembly(assemblyId);
241
        // TODO: check success.
242

    
243
        return modelAndView;
244
    }
245

    
246
    /**
247
     * Get method for assembly edit.
248
     * @param id - Id of assembly.
249
     * @return ModelAndView for assembly edit.
250
     */
251
    @GetMapping("/assembly_edit")
252
    public ModelAndView assemblyEditGet(@RequestParam("assemblyID") String id) {
253
        long startTime = System.currentTimeMillis();
254
        ModelAndView modelAndView = new ModelAndView("assembly_manage");
255

    
256
        ModelMap modelMap = modelAndView.getModelMap();
257

    
258
        Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
259

    
260
        modelMap.addAttribute("assembly", assembly);
261

    
262
        modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
263

    
264
        modelMap.addAttribute("allRoles", this.roleManager.getRoles());
265
        modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
266
        modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
267
        modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
268
        modelMap.addAttribute("allLocations", this.locationManager.getLocations());
269
        long estimatedTime = System.currentTimeMillis() - startTime;
270
        System.out.println("EDIT GET estimated - " + ((double)estimatedTime / 1000) + " sekund ");
271

    
272
        return modelAndView;
273
    }
274

    
275
    /**
276
     * Post method for assembly edit.
277
     * @param assembly - Edited assembly values.
278
     * @param result - Validator results.
279
     * @param assemblyIsPublic - Attribute indicating if assembly is public or not.
280
     * @return ModelAndView for assembly edit.
281
     */
282
    @PostMapping("/assembly_edit")
283
    public ModelAndView assemblyEditPost(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result,
284
                                              @Valid @ModelAttribute("checkboxPublic") String assemblyIsPublic) {
285
        long startTime = System.currentTimeMillis();
286
        ModelAndView modelAndView = new ModelAndView("assembly_manage");
287
        ModelMap modelMap = modelAndView.getModelMap();
288

    
289
        if (result.hasErrors()) {
290
            modelMap.addAttribute("assembly", assembly);
291

    
292
            modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
293
            modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + assembly.getName());
294
        }
295
        else {
296
            if (assemblyIsPublic.equals("on")) assembly.setIsPublic(1);
297
            else assembly.setIsPublic(0);
298

    
299
            // TODO: chybí defaultValue.
300
            Long assemblyID = this.assemblyManager.updateAssembly(assembly);
301
            this.parameterManager.updateParameters(assemblyID, assembly.getParameters());
302
            Assembly updatedAssembly = this.assemblyManager.getAssemblyById(assemblyID);
303

    
304
            modelMap.addAttribute("assembly", updatedAssembly);
305
            modelMap.addAttribute(assemblyTitleName, assemblyEditTitleText + " " + updatedAssembly.getName());
306
        }
307

    
308
        modelMap.addAttribute("allRoles", this.roleManager.getRoles());
309
        modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
310
        modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
311
        modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
312
        modelMap.addAttribute("allLocations", this.locationManager.getLocations());
313
        long estimatedTime = System.currentTimeMillis() - startTime;
314
        System.out.println("EDIT POST estimated - " + ((double)estimatedTime / 1000) + " sekund ");
315

    
316
        return modelAndView;
317
    }
318

    
319
    /**
320
     * Get method for new assembly form.
321
     * @return ModelAndView for new assembly.
322
     */
323
    @GetMapping("/assembly_new")
324
    public ModelAndView assemblyNewGet() {
325
        ModelAndView modelAndView = new ModelAndView("assembly_manage");
326

    
327
        ModelMap modelMap = modelAndView.getModelMap();
328

    
329
        modelMap.addAttribute("assembly", new Assembly());
330

    
331
        modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
332

    
333
        modelMap.addAttribute("allRoles", this.roleManager.getRoles());
334
        modelMap.addAttribute("allParameterTypes", this.parameterTypeManager.getParameterTypes());
335
        modelMap.addAttribute("allFunctions", this.functionManager.getFunctions());
336
        modelMap.addAttribute("allOperators", this.operatorManager.getOperators());
337
        modelMap.addAttribute("allLocations", this.locationManager.getLocations());
338

    
339
        return modelAndView;
340
    }
341

    
342
    /**
343
     * Post method for new assembly form.
344
     * @param assembly - Assembly values.
345
     * @param result - Validator results.
346
     * @return
347
     */
348
    @PostMapping("/assembly_new")
349
    public ModelAndView assemblyNewPost(@Valid @ModelAttribute("assembly") Assembly assembly, BindingResult result) {
350
        ModelAndView modelAndView = new ModelAndView();
351
        ModelMap modelMap = modelAndView.getModelMap();
352

    
353
        if (result.hasErrors()) {
354
            modelAndView.setViewName("assembly_manage");
355
            modelMap.addAttribute("assembly", assembly);
356

    
357
            modelMap.addAttribute(assemblyErrorName, getFullErrorMessage(result));
358
            modelMap.addAttribute(assemblyTitleName, assemblyNewTitleText);
359
        }
360
        else {
361
            // TODO: chybí získání default value.
362
            Long assemblyId = this.assemblyManager.createAssembly(assembly);
363
            this.parameterManager.addParameters(assemblyId, assembly.getParameters());
364

    
365
            modelAndView.setViewName("redirect:/assembly_edit?assemblyID=" + assemblyId);
366
        }
367

    
368
        return modelAndView;
369
    }
370

    
371
    /**
372
     * Initializes fields of a given configuration
373
     * @param configuration configuration which fields should be initialized
374
     */
375
    private void initializeFields(Configuration configuration) {
376
        for(ParameterInConfiguration parameterInConfiguration : configuration.getParametersInConfiguration()) {
377
            if(parameterInConfiguration.getLocation() == null) {
378
                parameterInConfiguration.setLocation(new Location());
379
            }
380
            if(parameterInConfiguration.getOperator() == null) {
381
                parameterInConfiguration.setOperator(new Operator());
382
            }
383
            if(parameterInConfiguration.getFunctions() == null) {
384
                parameterInConfiguration.setFunctions(new ArrayList<>());
385
            }
386
        }
387
    }
388

    
389
    /**
390
     * Prepares the configuration for contingency table generation. Initializes assembly and parameters of parameters in
391
     * configuration.
392
     * @param configuration configuration to be prepared.
393
     */
394
    private void prepareForTable(Configuration configuration) {
395
        Assembly assembly = assemblyManager.getAssemblyById(configuration.getAssembly().getId());
396
        configuration.setAssembly(assembly);
397

    
398
        for(int i = 0; i < configuration.getParametersInConfiguration().size(); i++) {
399
            ParameterInConfiguration parameterInConfiguration = configuration.getParametersInConfiguration().get(i);
400
            parameterInConfiguration.setParameter(assembly.getParameters().get(i));
401
            if (parameterInConfiguration.getLocation() != null) {
402
                parameterInConfiguration.setLocation(locationManager.getLocationById(parameterInConfiguration.getLocation().getId()));
403
            }
404
        }
405
    }
406
}
(1-1/5)