Projekt

Obecné

Profil

« Předchozí | Další » 

Revize dec281a6

Přidáno uživatelem Václav Hrabík před více než 1 rok

Zobrazit rozdíly:

src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/ConfigurationController.java
43 43
        return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.valueOf(returnCode.getStatusCode()));
44 44
    }
45 45

  
46
    /**
47
     * Method updates existing configuration
48
     * @param userConfiguration wrapper around client request for update - same as for inserting a new configuration
49
     * @return ResponseEntity with status code and message informing client about result of the update operation
50
     */
51
    @PutMapping(value="/update_configuration")
52
    public ResponseEntity<String> updateConfiguration(@RequestBody UserConfiguration userConfiguration) {
53
        ConfigurationControllerStatusCodes returnCode = configurationService.updateConfiguration(userConfiguration);
54
        String message = returnCode.getLabel();
55
        Map<String,Object> json = new HashMap<>();
56
        json.put("message",message);
57
        //ResponseEntity<String> response = configurationService.addConfiguration(userConfiguration);
58
        return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.valueOf(returnCode.getStatusCode()));
59
    }
60

  
46 61

  
47 62
    @PostMapping(value="/configuration")
48 63
    public ResponseEntity<String> getConfiguration(@RequestBody UserConfiguration userConfiguration) {
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/dials/ConfigurationControllerStatusCodes.java
9 9
    EMPTY_CONFIGURATION_DEFINITION("No configuration definition provided",400),
10 10
    EMPTY_CONFIGURATION_NAME("No configuration name provided",400),
11 11

  
12
    CONFIGURATION_IS_DEFAULT("Default configurations can not be changed", 400),
13

  
14
    USER_DONT_HAVE_RIGHTS_TO_CHANGE_CONFIGURATION("You don't have rights to change configuration", 403 ),
15

  
12 16
    INSERT_FAILED("Failed to save configuration",500),
13 17

  
14 18
    INSERT_SUCCESSFUL("Configuration saved",200),
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigService.java
12 12
    //upload configuration
13 13
    ConfigurationControllerStatusCodes addConfiguration(Configuration cfg);
14 14
    ConfigurationControllerStatusCodes addConfiguration(UserConfiguration cfg);
15
    ConfigurationControllerStatusCodes updateConfiguration(UserConfiguration cfg);
15 16

  
16 17
    ConfigurationControllerStatusCodes pairConfigurationWithUser(User user, Configuration configuration);
17 18

  
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigurationServiceImplementation.java
60 60
        if (configuration == null) {
61 61
            return ConfigurationControllerStatusCodes.EMPTY_CONFIGURATION_DEFINITION;
62 62
        }
63

  
64
        if (cfg.getConfigurationName() == null || cfg.getConfigurationName().equals("")) {
63
        if (isConfigurationNameInvalid(cfg)) {
65 64
            return ConfigurationControllerStatusCodes.EMPTY_CONFIGURATION_NAME;
66 65
        }
67

  
68
        User user = cfg.getUser();
69
        String userName = user.getName();
70 66
        //fetch the user from db because user in UserConfiguration does not contain id
71
        user = this.userService.getUserByName(userName);
67
        User user = this.getUser(cfg.getUser());
72 68

  
73 69
        //create the hash of the configuration (w/o salting)
74 70
        String configHash = Crypto.hashString(configuration.getConfig());
......
92 88
        return ConfigurationControllerStatusCodes.INSERT_SUCCESSFUL;
93 89
    }
94 90

  
91
    /**
92
     * Wrapper around user service for fetching user from database by username
93
     * @param user Userdto with username only
94
     * @return User from db or null if user does not exist
95
     */
96

  
97
    private User getUser(User user) {
98
        String userName = user.getName();
99
        //fetch the user from db because user in UserConfiguration does not contain id
100
        return this.userService.getUserByName(userName);
101
    }
102

  
103
    /**
104
     * Quick verification if configuration name is empty
105
     * @param cfg dto wrapper around user update / insert configuration request
106
     * @return true if configuration name is empty or empty string
107
     */
108
    private boolean isConfigurationNameInvalid(UserConfiguration cfg) {
109
        return cfg.getConfigurationName() == null || cfg.getConfigurationName().equals("");
110
    }
111

  
112
    /**
113
     * Util function for creating hash of configuration definition
114
     * @param configuration Configuration instance with configuration definition as string
115
     * @return String hash created from configuration definition
116
     *         empty string if configuration definition is null
117
     */
118
    private String createConfigurationHash(Configuration configuration) {
119
        if(configuration == null) {return "";}
120
        return Crypto.hashString(configuration.getConfig());
121
    }
122

  
123
    /**
124
     * Method updates existing non default user configuration
125
     * the user in request must have the rights to access the configuration, ie entry userId,configId must exist in join table
126
     * @param cfg UserConfiguration wrapper around update request
127
     * @return enum with http code and message informing about the result of the operation
128
     */
129
    @Override
130
    public ConfigurationControllerStatusCodes updateConfiguration(UserConfiguration cfg) {
131
        Configuration configuration = parseUserConfiguration(cfg);
132
        //if the request is missing the configuration definition then we kill it
133
        if (configuration == null) {
134
            return ConfigurationControllerStatusCodes.EMPTY_CONFIGURATION_DEFINITION;
135
        }
136

  
137
        if (isConfigurationNameInvalid(cfg)) {
138
            return ConfigurationControllerStatusCodes.EMPTY_CONFIGURATION_NAME;
139
        }
140
        int configurationId = Integer.parseInt(cfg.getConfigurationName());
141
        Configuration oldConfiguration = this.getConfiguration(cfg.getUser().getName(), configurationId);
142

  
143
        if (oldConfiguration == null) {
144
            return ConfigurationControllerStatusCodes.USER_DONT_HAVE_RIGHTS_TO_CHANGE_CONFIGURATION;
145
        }
146

  
147
        if (oldConfiguration.getIsDefault().equals("Y")) {
148
            return ConfigurationControllerStatusCodes.CONFIGURATION_IS_DEFAULT;
149
        }
150
        String updatedConfigHash = this.createConfigurationHash(configuration);
151
        int configuration1 = this.configurationRepository.updateHashAndConfigurationDefinition(configurationId,configuration.getConfig(),updatedConfigHash);
152

  
153
        if (configuration1 == 0) {
154
            return ConfigurationControllerStatusCodes.INSERT_FAILED;
155
        }
156

  
157
        return ConfigurationControllerStatusCodes.UPDATE_SUCCESSFUL;
158
    }
159

  
95 160
    /**
96 161
     * Parser for user request wrapped around configuration
97 162
     * @param cfg UserConfiguration instance - wrapper around user request
......
173 238

  
174 239
    public Configuration getConfiguration(String userName, int id) {
175 240
        User user = this.userService.getUserByName(userName);
241
        if (user == null) {
242
            return null;
243
        }
176 244
        UserConfigKey key = new UserConfigKey(user.getId(), id);
177 245
        Configuration configuration = this.configurationRepository.findConfigurationByUserNameAndID(key);
178 246
        return configuration;

Také k dispozici: Unified diff