diff --git a/src/main/java/biz/nynja/account/StartupScriptsListener.java b/src/main/java/biz/nynja/account/StartupScriptsListener.java index 88f09f532aacec51828be670b3456cabd5e372c0..628bc54483619b4644742e7a6cb9f139af4ffe88 100644 --- a/src/main/java/biz/nynja/account/StartupScriptsListener.java +++ b/src/main/java/biz/nynja/account/StartupScriptsListener.java @@ -6,8 +6,6 @@ package biz.nynja.account; import java.util.Arrays; import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.event.ContextRefreshedEvent; @@ -15,9 +13,7 @@ import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; import com.datastax.driver.core.Session; -import com.datastax.driver.core.exceptions.InvalidQueryException; -import biz.nynja.account.repositories.AccountRepositoryAdditional; /** * This acts as {@link CassandraAccountConfig} startupScripts executor but activated after the spring has setup the needed * tables though JPA @@ -28,45 +24,18 @@ import biz.nynja.account.repositories.AccountRepositoryAdditional; @Component public class StartupScriptsListener { - private static final Logger logger = LoggerFactory.getLogger(StartupScriptsListener.class); private String keyspace; @Autowired private Session session; - @Autowired - private AccountRepositoryAdditional accountRepositoryAdditional; - @EventListener(ContextRefreshedEvent.class) public void contextRefreshedEvent() { keyspace = session.getLoggedKeyspace(); - boolean searchableColumnAlreadyExists = false, searchableFeildAlreadyExists = false; for (String script : getStartupScripts()) { session.execute(script); } - - try { - // add searchable column - session.execute(getScriptsForSearchableOption().get(0)); - } catch (InvalidQueryException e) { - logger.warn("Exception while executing script for adding searchable column: {}", e.getMessage()); - // In the current case InvalidQueryException is used to confirm that the searchable column already exists. - searchableColumnAlreadyExists = true; - } - - try { - // add searchable field - session.execute(getScriptsForSearchableOption().get(1)); - } catch (InvalidQueryException e) { - logger.warn("Exception while executing script for adding searchable field: {}", e.getMessage()); - // In the current case InvalidQueryException is used to confirm that the searchable column already exists. - searchableFeildAlreadyExists = true; - } - - if (searchableColumnAlreadyExists && searchableFeildAlreadyExists) { - accountRepositoryAdditional.removeNullsForSearchableOption(); - } } private List getStartupScripts() { @@ -120,13 +89,4 @@ public class StartupScriptsListener { scriptAccountViewByFirstName, scriptAccountViewByLastName, scriptAccountViewByAccessStatus, scriptAccountViewByCreationTimestamp, scriptAccountViewByLastUpdateTimestamp); } - - private List getScriptsForSearchableOption() { - String addSearchableColumnScript = "ALTER TABLE " + keyspace - + ".profilebyauthenticationprovider ADD searchable boolean;"; - - String addSearchableToAuthenticationProviderType = "ALTER TYPE authenticationprovider add searchable boolean;"; - - return Arrays.asList(addSearchableColumnScript, addSearchableToAuthenticationProviderType); - } } diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java index 800a1cde24c7dd695aa9f8077dbcc9d9c0a7239d..4149a68ed8b17049cc62cd4129977eb57b66f6d1 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java @@ -62,7 +62,5 @@ public interface AccountRepositoryAdditional { Optional searchAccountByLoginOption(AuthenticationProvider loginOption) throws IncorrectAccountCountException; - void removeNullsForSearchableOption(); - boolean updateSearchableOption(UUID profileId, String authProviderType, String authProvider, SearchableOption searchableOption); } diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index 1b11b636a7b39d1b1f16fc5affd7f0419a717448..3b19c2fa031408f90e74e99772952d1768928922 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -1090,61 +1090,6 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio return pendingAccount; } - public void removeNullsForSearchableOption() { - List profilesByAuthenticationProvider = profileByAuthenticationProviderRepository - .findAll(); - for (int i = 0; i < profilesByAuthenticationProvider.size(); i++) { - if (profilesByAuthenticationProvider.get(i).getSearchable() == null) { - logger.error("Found null for searchable option for {}:{} in profile: {}", - profilesByAuthenticationProvider.get(i).getAuthenticationProviderType(), - profilesByAuthenticationProvider.get(i).getAuthenticationProvider(), - profilesByAuthenticationProvider.get(i).getProfileId()); - Profile profileToUpdate = profileRepository - .findByProfileId(profilesByAuthenticationProvider.get(i).getProfileId()); - if (profileToUpdate == null) { - logger.error( - "Error replacing null with default searchable option for auth provider {}:{} in profile {}. Profile not found.", - profilesByAuthenticationProvider.get(i).getAuthenticationProviderType(), - profilesByAuthenticationProvider.get(i).getAuthenticationProvider(), - profilesByAuthenticationProvider.get(i).getProfileId()); - continue; - } - - logger.info("Replacing null with default searchable option for profile {}", - profilesByAuthenticationProvider.get(i).getProfileId()); - - CassandraBatchOperations batchOperations = cassandraTemplate.batchOps(); - WriteResult wr; - try { - updateSearchableInProfileByAuthenticationProvider(batchOperations, - profilesByAuthenticationProvider.get(i), SearchableOption.SEARCH_ENABLED); - if (!updateSearchableInProfile(batchOperations, profileToUpdate, - profilesByAuthenticationProvider.get(i).getAuthenticationProviderType(), - profilesByAuthenticationProvider.get(i).getAuthenticationProvider(), - SearchableOption.SEARCH_ENABLED)) { - logger.error( - "Error replacing null with default searchable option for profile {}: auth provider {}:{}.", - profilesByAuthenticationProvider.get(i).getProfileId(), - profilesByAuthenticationProvider.get(i).getAuthenticationProviderType(), - profilesByAuthenticationProvider.get(i).getAuthenticationProvider()); - } - wr = batchOperations.execute(); - } catch (IllegalArgumentException | IllegalStateException e) { - logger.debug( - "Exception while replacing null with default searchable option for auth provider {}:{} in profile{}: {}.", - profilesByAuthenticationProvider.get(i).getAuthenticationProviderType(), - profilesByAuthenticationProvider.get(i).getAuthenticationProvider(), - profilesByAuthenticationProvider.get(i).getProfileId(), e.getMessage()); - continue; - } - if (wr != null && wr.wasApplied()) { - logger.info("Successfully replaced null with default searchable option in profile {}.", - profilesByAuthenticationProvider.get(i).getProfileId()); - } - } - } - } - private void updateSearchableInProfileByAuthenticationProvider(CassandraBatchOperations batchOps, ProfileByAuthenticationProvider profileByAuthenticationProvider, SearchableOption searchableOption) { profileByAuthenticationProvider.setSearchable(AuthenticationProvider.isSearchDisabled(searchableOption));