Revize e284c23a
Přidáno uživatelem Václav Hrabík před více než 1 rok
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/ConfigurationController.java | ||
---|---|---|
58 | 58 |
return new ResponseEntity<>(JSONBuilder.buildJSON(json),HttpStatus.valueOf(returnCode.getStatusCode())); |
59 | 59 |
} |
60 | 60 |
|
61 |
|
|
61 |
/** |
|
62 |
* Endpoint for accessing anti-patterns and configurations |
|
63 |
* @param userConfiguration Wrapper around client request body |
|
64 |
* @return ResponseEntity with anti-pattern array and configuration array if everything went well |
|
65 |
* ResponseEntity with error status code otherwise |
|
66 |
*/ |
|
62 | 67 |
@PostMapping(value="/configuration") |
63 | 68 |
public ResponseEntity<String> getConfiguration(@RequestBody UserConfiguration userConfiguration) { |
64 | 69 |
Map<String, Object> json = new HashMap<>(); |
65 |
// Configuration config = this.configurationService.getConfigurationById(Integer.parseInt(userConfiguration.getId())); |
|
66 | 70 |
|
67 | 71 |
Configuration config = this.configurationService.getConfiguration(userConfiguration.getUser().getName(), Integer.parseInt(userConfiguration.getId())); |
68 | 72 |
|
... | ... | |
83 | 87 |
|
84 | 88 |
return new ResponseEntity<>(new Gson().toJson(json),HttpStatus.OK); |
85 | 89 |
|
86 |
// List<Configuration> configuration = this.configurationService.getUserConfigurations(user); |
|
87 |
// //this can only happen if db server is offline |
|
88 |
// //or no default configuration exists in database |
|
89 |
// if(configuration == null) { |
|
90 |
// json.put("message", "internal sever error"); |
|
91 |
// return new ResponseEntity<>(JSONBuilder.buildJSON(json), HttpStatus.INTERNAL_SERVER_ERROR); |
|
92 |
// } |
|
93 |
// //list of configuration definitions |
|
94 |
// List<String> configurationDefinition = new ArrayList<>(); |
|
95 |
// //and their names |
|
96 |
// List<String> configurationNames = new ArrayList<>(); |
|
97 |
// //hipster syntax for Petr |
|
98 |
// configuration.forEach(cfg -> { |
|
99 |
// configurationDefinition.add(cfg.getConfig()); |
|
100 |
// configurationNames.add(cfg.getConfigurationName()); |
|
101 |
// }); |
|
102 |
// json.put("configuration",configurationDefinition); |
|
103 |
// json.put("configuration_names",configurationNames); |
|
104 |
// |
|
105 |
// return new ResponseEntity<>(new Gson().toJson(json),HttpStatus.OK); |
|
106 | 90 |
} |
107 | 91 |
|
108 | 92 |
/** |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/dials/ConfigurationControllerStatusCodes.java | ||
---|---|---|
15 | 15 |
|
16 | 16 |
INSERT_FAILED("Failed to save configuration",500), |
17 | 17 |
|
18 |
INSERT_SUCCESSFUL("Configuration saved",200), |
|
18 |
INSERT_SUCCESSFUL("Configuration saved",201), |
|
19 |
UPDATE_SUCCESSFUL("Configuration updated",200), |
|
19 | 20 |
|
20 | 21 |
CONFIGURATION_PAIRING_EXISTS("Configuration already exists in your collection",400), |
21 | 22 |
CONFIGURATION_PAIRING_CREATED("configuration added to collection",200); |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/repository/ConfigRepository.java | ||
---|---|---|
9 | 9 |
import org.springframework.data.jpa.repository.Modifying; |
10 | 10 |
import org.springframework.data.jpa.repository.Query; |
11 | 11 |
import org.springframework.stereotype.Repository; |
12 |
import org.springframework.transaction.annotation.Transactional; |
|
12 | 13 |
|
13 | 14 |
import java.util.List; |
14 | 15 |
@Repository |
... | ... | |
21 | 22 |
List<Configuration> getPublicConfigurations(); |
22 | 23 |
//retrieve all public configurations and configurations available to the user |
23 | 24 |
//assumption - default configurations are not assigned to any user explicitly |
24 |
@Query("select config, userconfig from Configuration config left join UserConfigurationJoin userconfig on config.id = userconfig.id.configId where userconfig.id.userId = ?1 or config.isDefault = 'Y'")
|
|
25 |
@Query("select config, userconfig from Configuration config inner join UserConfigurationJoin userconfig on config.id = userconfig.id.configId where userconfig.id.userId = ?1 or config.isDefault = 'Y'")
|
|
25 | 26 |
List<Object[]> getAllUserConfigurations(int userId); |
26 | 27 |
|
28 |
@Modifying |
|
29 |
@Transactional |
|
30 |
@Query("UPDATE Configuration cfg SET cfg.config = ?2, cfg.configHash= ?3 WHERE cfg.id = ?1") |
|
31 |
int updateHashAndConfigurationDefinition(int configurationId, String configurationDefinition, String configurationHash); |
|
32 |
|
|
33 |
|
|
34 |
|
|
27 | 35 |
@Query("select userconfig.configurationName,userconfig.id.configId from UserConfigurationJoin userconfig where userconfig.id.userId = ?1") |
28 | 36 |
List<Object[]> getAllUserConfigurationNames(int userId); |
29 | 37 |
|
... | ... | |
31 | 39 |
String findConfigurationByCompoundKey(UserConfigKey key); |
32 | 40 |
|
33 | 41 |
@Query("SELECT \n" + |
34 |
"cfg.config \n" +
|
|
42 |
"cfg \n" + |
|
35 | 43 |
"FROM \n" + |
36 | 44 |
"UserConfigurationJoin userconfig INNER JOIN Configuration cfg \n" + |
37 | 45 |
"ON userconfig.id.configId = cfg.id\n" + |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigService.java | ||
---|---|---|
10 | 10 |
|
11 | 11 |
public interface ConfigService { |
12 | 12 |
//upload configuration |
13 |
ConfigurationControllerStatusCodes addConfiguration(Configuration cfg); |
|
14 | 13 |
ConfigurationControllerStatusCodes addConfiguration(UserConfiguration cfg); |
15 | 14 |
ConfigurationControllerStatusCodes updateConfiguration(UserConfiguration cfg); |
16 | 15 |
|
... | ... | |
21 | 20 |
|
22 | 21 |
Configuration getConfigurationById(int id); |
23 | 22 |
|
24 |
public Configuration getConfiguration(String userName, int id);
|
|
23 |
Configuration getConfiguration(String userName, int id); |
|
25 | 24 |
String getConfigurationName(int userId, int configurationId); |
26 | 25 |
|
27 | 26 |
Map<String, AntiPattern> getAntiPatterns(); |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigurationServiceImplementation.java | ||
---|---|---|
37 | 37 |
//user service is also necessary for retrieving information about users (primarily database query for fetching id) |
38 | 38 |
@Autowired |
39 | 39 |
private UserService userService; |
40 |
/** |
|
41 |
* Saves configuration to database |
|
42 |
* the configuration is default to everyone |
|
43 |
* @param cfg Configuration - JSON format of configuration with antipatterns and thresholds |
|
44 |
* @return ResponseEntity with status code and message with more information about operation |
|
45 |
*/ |
|
46 |
@Override |
|
47 |
public ConfigurationControllerStatusCodes addConfiguration(Configuration cfg) { |
|
48 |
return null; |
|
49 |
} |
|
40 |
|
|
50 | 41 |
/** |
51 | 42 |
* Saves configuration to database |
52 | 43 |
* the configuration is available to the user who sent the request |
... | ... | |
67 | 58 |
User user = this.getUser(cfg.getUser()); |
68 | 59 |
|
69 | 60 |
//create the hash of the configuration (w/o salting) |
70 |
String configHash = Crypto.hashString(configuration.getConfig());
|
|
61 |
String configHash = createConfigurationHash(configuration);
|
|
71 | 62 |
Configuration existingConfiguration = this.configurationRepository.findConfigurationByConfigHash(configHash); |
72 | 63 |
//configuration definition does not exist => upload the configuration into database |
73 | 64 |
if (existingConfiguration == null) { |
... | ... | |
230 | 221 |
} |
231 | 222 |
return configurationList; |
232 | 223 |
} |
233 |
//Wrappers around db connection to configuration table |
|
224 |
|
|
225 |
/** |
|
226 |
* finds configuration by id |
|
227 |
* @param id integer id of configuration to be retrieved |
|
228 |
*/ |
|
234 | 229 |
@Override |
235 | 230 |
public Configuration getConfigurationById(int id) { |
236 | 231 |
return this.configurationRepository.findConfigurationById(id); |
237 | 232 |
} |
238 | 233 |
|
234 |
/** |
|
235 |
* Method fetches configuration with id @param id from database |
|
236 |
* @param userName String username |
|
237 |
* @param id int configuration id |
|
238 |
* @return Configuration if configuration with id @param id exists and user @param userName has access to it |
|
239 |
* null otherwise |
|
240 |
*/ |
|
241 |
@Override |
|
239 | 242 |
public Configuration getConfiguration(String userName, int id) { |
240 | 243 |
User user = this.userService.getUserByName(userName); |
241 | 244 |
if (user == null) { |
... | ... | |
251 | 254 |
return this.configurationRepository.findConfigurationByCompoundKey(new UserConfigKey(userId, configurationId)); |
252 | 255 |
} |
253 | 256 |
|
257 |
/** |
|
258 |
* Method loads antipatterns from json files on file system for metadata about anti patterns |
|
259 |
* @return Map of antipatterns |
|
260 |
*/ |
|
254 | 261 |
@Override |
255 | 262 |
public Map<String, AntiPattern> getAntiPatterns() { |
256 | 263 |
Map<String, AntiPattern> antiPatterns = new HashMap<>(); |
Také k dispozici: Unified diff
https://kivprogrammers.atlassian.net/browse/TSP2-29