1 |
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.configuration;
|
2 |
2 |
|
|
3 |
import com.google.gson.Gson;
|
3 |
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.ConfigurationControllerStatusCodes;
|
4 |
5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.*;
|
5 |
6 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.ConfigRepository;
|
... | ... | |
41 |
42 |
*/
|
42 |
43 |
@Override
|
43 |
44 |
public ConfigurationControllerStatusCodes addConfiguration(UserConfiguration cfg) {
|
|
45 |
Configuration configuration = parseUserConfiguration(cfg);
|
|
46 |
//if the request is missing the configuration definition then we kill it
|
|
47 |
if (configuration == null) {
|
|
48 |
return ConfigurationControllerStatusCodes.EMPTY_CONFIGURATION_DEFINITION;
|
|
49 |
}
|
44 |
50 |
User user = cfg.getUser();
|
45 |
|
Configuration configuration = cfg.getConfiguration();
|
46 |
|
|
47 |
51 |
String userName = user.getName();
|
48 |
52 |
//fetch the user from db because user in UserConfiguration does not contain id
|
49 |
53 |
user = this.userService.getUserByName(userName);
|
50 |
|
String configurationDefinition = configuration.getConfig();
|
51 |
|
//if the request is missing the configuration definition then we kill it
|
52 |
|
if(configurationDefinition == null) {
|
53 |
|
return ConfigurationControllerStatusCodes.EMPTY_CONFIGURATION_DEFINITION;
|
54 |
|
}
|
|
54 |
|
55 |
55 |
//create the hash of the configuration (w/o salting)
|
56 |
|
String configHash = Crypto.hashString(configurationDefinition);
|
|
56 |
String configHash = Crypto.hashString(configuration.getConfig());
|
57 |
57 |
Configuration existingConfiguration = this.configurationRepository.findConfigurationByConfigHash(configHash);
|
58 |
58 |
//configuration definition does not exist => upload the configuration into database
|
59 |
|
if(existingConfiguration == null){
|
|
59 |
if (existingConfiguration == null) {
|
60 |
60 |
configuration.setHash(configHash);
|
61 |
61 |
//save the configuration itself
|
62 |
62 |
Configuration tmp = this.configurationRepository.save(configuration);
|
63 |
63 |
//can only happen if db server fails or a constraint is breached
|
64 |
|
if(tmp == null){
|
|
64 |
if (configuration == null) {
|
65 |
65 |
return ConfigurationControllerStatusCodes.INSERT_FAILED;
|
66 |
66 |
}
|
67 |
67 |
}
|
... | ... | |
87 |
87 |
//the configuration pairing already exists, we do not have to do anything
|
88 |
88 |
//request like this should not happen from client, something fishy might be going on
|
89 |
89 |
//or the request is a duplicate
|
90 |
|
if(exists) {
|
|
90 |
if (exists) {
|
91 |
91 |
return ConfigurationControllerStatusCodes.CONFIGURATION_PAIRING_EXISTS;
|
92 |
92 |
}
|
93 |
93 |
//save the relation between user and configuration
|
... | ... | |
105 |
105 |
@Override
|
106 |
106 |
public List<Configuration> getUserConfigurations(User user) {
|
107 |
107 |
final String userName = user.getName();
|
108 |
|
if(userName == null){
|
|
108 |
if (userName == null) {
|
109 |
109 |
return null;
|
110 |
110 |
}
|
111 |
111 |
//client can only send his name - he obviously does not know his id in db, we have to query that
|
https://kivprogrammers.atlassian.net/browse/TSP2-30 připrava backendu stránky configuration