diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index d7bffb5e4754ff0d756c780645ead2407eddd9c7..1737eff41c0046d1c8542817ff789ed006b50c6a 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -10,11 +10,15 @@ import java.time.Instant; import java.time.LocalDate; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; +import javax.annotation.PostConstruct; + import org.apache.commons.lang3.SerializationUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,10 +26,12 @@ import org.springframework.data.cassandra.core.CassandraBatchOperations; import org.springframework.data.cassandra.core.CassandraTemplate; import org.springframework.data.cassandra.core.UpdateOptions; import org.springframework.data.cassandra.core.WriteResult; +import org.springframework.data.cassandra.core.cql.CqlOperations; import org.springframework.stereotype.Service; import com.datastax.driver.core.BatchStatement; import com.datastax.driver.core.BoundStatement; +import com.datastax.driver.core.PreparedStatement; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Session; @@ -56,9 +62,9 @@ import biz.nynja.account.services.decomposition.IncorrectAccountCountException; import biz.nynja.account.services.erlang.ErlangAccountBridge; import io.grpc.StatusRuntimeException; -// TODO: 11/19/2018 refactor this class and adding rolback cassandra data if erlang return fail state @Service public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditional { + private PreparedStatement preparedCql; private static final Logger logger = LoggerFactory.getLogger(AccountRepositoryAdditionalImpl.class); private final PendingAccountConfiguration pendingAccountConfiguration; @@ -106,6 +112,15 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio this.accountDataConfiguration = accountDataConfiguration; } + @PostConstruct + public void initStatements() { + + preparedCql = session.prepare( + "INSERT INTO pendingAccount (accountId, profileId, authenticationProvider, authenticationProviderType, creationTimestamp)" + + " VALUES (?, ?, ? ,?, ?) " + "USING TTL " + pendingAccountConfiguration.getTtl() * 60 + ";"); + + } + public Account completePendingAccountCreation(CompletePendingAccountCreationRequest request) { Transaction sagaTransaction = new SagaTransaction(cassandraTemplate); PendingAccount pendingAccount = pendingAccountRepository @@ -198,8 +213,8 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio public Account updateAccount(UpdateAccountRequest request) { Transaction sagaTransaction = new SagaTransaction(cassandraTemplate); - Account existingAccount = accountRepository.findByAccountId(UUID.fromString(request.getAccountId())); + Account existingAccount = accountRepository.findByAccountId(UUID.fromString(request.getAccountId())); if (existingAccount == null) { logger.error("Existing account with the provided id {} was not found.", request.getAccountId()); logger.debug("Existing account with the provided id {} was not found.", request.getAccountId()); @@ -211,7 +226,6 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio Set roles = existingAccount.getRoles().stream().map(Role::valueOf).collect(Collectors.toSet()); request = UpdateAccountRequest.newBuilder(request).clearRoles().addAllRoles(roles).build(); } - Long timeUpdated = Instant.now().toEpochMilli(); WriteResult wr = null; try { @@ -998,20 +1012,18 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio return false; } - public PendingAccount savePendingAccount(PendingAccount pendingAccount) { + public PendingAccount savePendingAccount(PendingAccount pendingAccount) throws RuntimeException { // for saving back compatibility. need refactoring in the future - if (!createSavePendingAccount(pendingAccount)) { - throw new RuntimeException("Exception for save save pending account id=" + pendingAccount.getAccountId()); - } - return pendingAccount; - } - private boolean createSavePendingAccount(PendingAccount pendingAccount) { - String cql = "INSERT INTO pendingAccount (accountId, profileId, authenticationProvider, authenticationProviderType, creationTimestamp)" - + " VALUES (?, ?, ? ,?, ?) " + "USING TTL " + pendingAccountConfiguration.getTtl() * 60 + ";"; - return cassandraTemplate.getCqlOperations().execute(cql, pendingAccount.getAccountId(), + BoundStatement boundStatement = new BoundStatement(preparedCql).bind(pendingAccount.getAccountId(), pendingAccount.getProfileId(), pendingAccount.getAuthenticationProvider(), pendingAccount.getAuthenticationProviderType(), pendingAccount.getCreationTimestamp()); + try { + ResultSet result = session.execute(boundStatement); + } catch (Exception ex) { + throw new RuntimeException("Exception during save pending account id=" + pendingAccount.getAccountId()); + } + return pendingAccount; } public void removeNullsForSearchableOption() {