3 |
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.*;
|
4 |
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.ConfigRepository;
|
5 |
5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.UserConfigurationJoinRepository;
|
6 |
|
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.UserRepository;
|
7 |
6 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.Crypto;
|
|
7 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder;
|
8 |
8 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
9 |
import org.springframework.http.HttpStatus;
|
9 |
10 |
import org.springframework.http.ResponseEntity;
|
10 |
11 |
import org.springframework.stereotype.Service;
|
11 |
12 |
|
|
13 |
import java.util.HashMap;
|
12 |
14 |
import java.util.List;
|
|
15 |
import java.util.Map;
|
13 |
16 |
|
14 |
17 |
@Service
|
15 |
18 |
public class ConfigurationServiceImplementation implements ConfigurationService{
|
16 |
|
|
|
19 |
//repository which represents connection to database and the configuration table in particular
|
17 |
20 |
@Autowired
|
18 |
21 |
private ConfigRepository configurationRepository;
|
|
22 |
//repository for Join table, necessary to add associations between users and configurations
|
19 |
23 |
@Autowired
|
20 |
24 |
private UserConfigurationJoinRepository userConfigurationJoinRepository;
|
21 |
|
|
|
25 |
//user service is also necessary for retrieving information about users (primarily database query for fetching id)
|
22 |
26 |
@Autowired
|
23 |
27 |
private UserService userService;
|
24 |
28 |
/**
|
... | ... | |
42 |
46 |
User user = cfg.getUser();
|
43 |
47 |
Configuration configuration = cfg.getConfiguration();
|
44 |
48 |
|
|
49 |
Map<String,Object> json = new HashMap<>();
|
45 |
50 |
String userName = user.getName();
|
|
51 |
//fetch the user from db because user in UserConfiguration does not contain id
|
46 |
52 |
user = this.userService.getUserByName(userName);
|
47 |
53 |
String configurationDefinition = configuration.getConfig();
|
|
54 |
//if the request is missing the configuration definition then we kill it
|
48 |
55 |
if(configurationDefinition == null){
|
49 |
|
//todo konfigurace neni poslana, chyba requestu
|
50 |
|
|
|
56 |
json.put("message","no configuration definition provided.");
|
|
57 |
String jsonString = JSONBuilder.buildJSON(json);
|
|
58 |
return new ResponseEntity<>(jsonString,HttpStatus.BAD_REQUEST);
|
51 |
59 |
}
|
|
60 |
//create the hash of the configuration (w/o salting)
|
52 |
61 |
String configHash = Crypto.hashString(configurationDefinition);
|
53 |
62 |
Configuration existingConfiguration = this.configurationRepository.findConfigurationByConfigHash(configHash);
|
54 |
63 |
//configuration definition does not exist => upload the configuration into database
|
... | ... | |
56 |
65 |
configuration.setHash(configHash);
|
57 |
66 |
//save the configuration itself
|
58 |
67 |
Configuration tmp = this.configurationRepository.save(configuration);
|
|
68 |
//can only happen if db server fails or a constraint is breached
|
59 |
69 |
if(tmp == null){
|
60 |
|
//todo selhal insert do databaze
|
|
70 |
json.put("message","fatal server failure");
|
|
71 |
return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.INTERNAL_SERVER_ERROR);
|
61 |
72 |
}
|
62 |
73 |
}
|
63 |
|
|
64 |
74 |
//pair the configuration to the user
|
65 |
|
return pairConfigurationWithUser(user,configuration);
|
|
75 |
pairConfigurationWithUser(user,configuration);
|
|
76 |
json.put("message","configuration uploaded successfully");
|
|
77 |
return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.OK);
|
66 |
78 |
|
67 |
79 |
|
68 |
80 |
}
|
69 |
81 |
|
|
82 |
/**
|
|
83 |
* This method saves user and configuration id into join table
|
|
84 |
* creates association between user and configuration in the sense of: "User @param user owns configuration @param configuration"
|
|
85 |
* (Multiple users can own the same configuration but the configuration is not public)
|
|
86 |
* @param user User - user who will be associated with configuration @param configuration.
|
|
87 |
* @param configuration Configuration - the configuration that will be associated with user (just the id is necessary)
|
|
88 |
* @return ResponseEntity<String> - Http response with status code and message about the operation
|
|
89 |
*/
|
70 |
90 |
@Override
|
71 |
91 |
public ResponseEntity<String> pairConfigurationWithUser(User user, Configuration configuration) {
|
|
92 |
Map<String,Object> json = new HashMap<>();
|
72 |
93 |
final UserConfigKey key = new UserConfigKey(user.getId(),configuration.getId());
|
73 |
94 |
boolean exists = this.userConfigurationJoinRepository.existsById(key);
|
|
95 |
//the configuration pairing already exists, we do not have to do anything
|
|
96 |
//request like this should not happen from client, something fishy might be going on
|
|
97 |
//or the request is a duplicate
|
74 |
98 |
if(exists){
|
75 |
|
//todo uzivatel se chce sparovat s konfiguraci se kterou je jiz sparovan, jenom poslat ok nebo error
|
|
99 |
json.put("message","configuration already exists in your collection!");
|
|
100 |
return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.BAD_REQUEST);
|
76 |
101 |
}
|
77 |
102 |
//save the relation between user and configuration
|
78 |
103 |
this.userConfigurationJoinRepository.save(new UserConfigurationJoin(key));
|
79 |
|
return null;
|
|
104 |
json.put("message","configuration added to collection.");
|
|
105 |
return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.OK);
|
80 |
106 |
}
|
81 |
107 |
|
82 |
108 |
@Override
|
... | ... | |
87 |
113 |
//fetch all configurations this particular user can see
|
88 |
114 |
//ie all public configs + configurations uploaded by this particular user
|
89 |
115 |
List<Configuration> configurations = this.configurationRepository.getAllUserConfigurations(userInfo.getId());
|
90 |
|
return null;
|
|
116 |
StringBuilder sb = new StringBuilder();
|
|
117 |
for(int i = 0; i < configurations.size(); i++){
|
|
118 |
sb.append(configurations.get(i).getConfig());
|
|
119 |
}
|
|
120 |
Map<String,Object> json = new HashMap<>();
|
|
121 |
json.put("message","configuration retrived");
|
|
122 |
json.put("configurations",sb.toString());
|
|
123 |
String jsonString = JSONBuilder.buildJSON(json);
|
|
124 |
return new ResponseEntity<>(jsonString,HttpStatus.OK);
|
91 |
125 |
}
|
92 |
126 |
}
|
#10517 repository a controller + service vytvořena.