Projekt

Obecné

Profil

Stáhnout (2.82 KB) Statistiky
| Větev: | Revize:
1
package vldc.aswi.validators;
2

    
3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.stereotype.Component;
5
import org.springframework.validation.Errors;
6
import org.springframework.validation.Validator;
7
import vldc.aswi.domain.Assembly;
8
import vldc.aswi.domain.parameter.Parameter;
9
import vldc.aswi.service.SqlQueryManager;
10

    
11
import java.util.List;
12

    
13
/**
14
 * Validator for assembly.
15
 */
16
@Component
17
public class AssemblyValidator implements Validator {
18

    
19
    /** Autowired sql query manager. */
20
    @Autowired
21
    private SqlQueryManager sqlQueryManager;
22

    
23
    /**
24
     * Validator for supporting this class.
25
     * @param aClass - Class.
26
     * @return true if sent class match Assembly class.
27
     */
28
    @Override
29
    public boolean supports(Class<?> aClass) {
30
        return Assembly.class.isAssignableFrom(aClass);
31
    }
32

    
33
    /**
34
     * Validate assembly values.
35
     * @param obj - Validated object.
36
     * @param errors - Errors sended to output.
37
     */
38
    @Override
39
    public void validate(Object obj, Errors errors) {
40

    
41
        // Get assembly values.
42
        Assembly assembly = (Assembly) obj;
43

    
44
        // Replace ";" with blank char -> semicolon is automatically added to statement before execution,
45
        // so if SQL query already contains semicolon, it will raise exception.
46
        assembly.setSQLQuery(assembly.getSQLQuery().replace(";", ""));
47

    
48
        // Validate SQL Query.
49
        List<String> columnNames = this.sqlQueryManager.getNameOfColumnsFromQuery(assembly.getSQLQuery());
50

    
51
        if (columnNames == null) {
52
            errors.rejectValue("SQLQuery", "SQL Query není validní!");
53
            return;
54
        }
55

    
56
        if (assembly.getParameters() != null && assembly.getParameters().size() > 0) {
57
            for (int i = 0; i < assembly.getParameters().size(); i++) {
58
                Parameter parameter = assembly.getParameters().get(i);
59

    
60
                // If parameter does not have name then it means, that it is deleted parameter.
61
                if (parameter.getName() == null) {
62
                    continue;
63
                }
64

    
65
                // Check if parameter's nameOfSelect is contained in column names of SQL query.
66
                if (parameter.getNameOfSelect() == null || !columnNames.stream().anyMatch(n-> n.equalsIgnoreCase(parameter.getNameOfSelect()))) {
67
                    errors.reject("parameters[" + i + "].nameOfSelect", "Název parametru v SQL není validní u parametru '" + parameter.getName() + "'!");
68
                }
69

    
70
                // Check if current parameter have at least 1 location.
71
                if (parameter.getLocations() == null || parameter.getLocations().size() == 0) {
72
                    errors.rejectValue("parameters[" + i + "].locations", "", "Není vybrána žádná lokace u parametru '" + parameter.getName() + "'!");
73
                }
74
            }
75
        }
76
    }
77
}
    (1-1/1)