@Override
public ResponseCode checkConfiguration(String username, String password, String apiKey, String matchSourceNumber,
String platform, int typeId) {
RemoteProviderType databaseRemoteProviderType = load(typeId);
boolean matchSourceNumberBoolean = "true".equals(matchSourceNumber);
if (databaseRemoteProviderType == null) {
return ResponseCode.BAD_ID;
}
databaseRemoteProviderType = decryptCredentials(databaseRemoteProviderType);
// TODO test this
// If the username hasn't changed but the password has, update the apps instead of deleting them.
if (databaseRemoteProviderType.getHasUserNamePassword() &&
username != null && password != null &&
username.equals(databaseRemoteProviderType.getUsername()) &&
!password.equals(USE_OLD_PASSWORD) &&
!password.equals(databaseRemoteProviderType.getPassword())) {
log.warn("Provider password has changed, updating applications.");
databaseRemoteProviderType.setPassword(password);
databaseRemoteProviderType.setPlatform(platform);
databaseRemoteProviderType.setMatchSourceNumbers(matchSourceNumberBoolean);
remoteProviderApplicationService.updateApplications(databaseRemoteProviderType);
store(databaseRemoteProviderType);
return ResponseCode.SUCCESS;
} else if (databaseRemoteProviderType.getHasApiKey() &&
apiKey != null && !apiKey.startsWith(USE_OLD_PASSWORD) &&
!apiKey.equals(databaseRemoteProviderType.getApiKey())
||
databaseRemoteProviderType.getHasUserNamePassword() &&
username != null &&
!username.equals(databaseRemoteProviderType.getUsername())) {
databaseRemoteProviderType.setApiKey(apiKey);
databaseRemoteProviderType.setUsername(username);
databaseRemoteProviderType.setPassword(password);
databaseRemoteProviderType.setPlatform(platform);
databaseRemoteProviderType.setMatchSourceNumbers(matchSourceNumberBoolean);
List<RemoteProviderApplication> apps = remoteProviderApplicationService
.getApplications(databaseRemoteProviderType);
if (apps == null) {
return ResponseCode.NO_APPS;
} else {
log.warn("Provider username has changed, deleting old apps.");
remoteProviderApplicationService.deleteApps(databaseRemoteProviderType);
databaseRemoteProviderType.setRemoteProviderApplications(apps);
for (RemoteProviderApplication remoteProviderApplication :
databaseRemoteProviderType.getRemoteProviderApplications()) {
remoteProviderApplicationService.store(remoteProviderApplication);
}
store(encryptCredentials(databaseRemoteProviderType));
return ResponseCode.SUCCESS;
}
} else if (databaseRemoteProviderType.getMatchSourceNumbers() == null ||
databaseRemoteProviderType.getMatchSourceNumbers() != matchSourceNumberBoolean) {
databaseRemoteProviderType.setMatchSourceNumbers(matchSourceNumberBoolean);
store(databaseRemoteProviderType);
return ResponseCode.SUCCESS;
} else {
log.info("No change was made to the credentials.");