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;
|
https://kivprogrammers.atlassian.net/browse/TSP2-30 kontrolo validace dat