diff --git a/src/main/java/biz/nynja/account/models/Profile.java b/src/main/java/biz/nynja/account/models/Profile.java index f4e284455b7cc37e764f4d43f1c7dd02af3f6434..a8044eba96c049f1b608979dda63b2cce2939338 100644 --- a/src/main/java/biz/nynja/account/models/Profile.java +++ b/src/main/java/biz/nynja/account/models/Profile.java @@ -3,6 +3,7 @@ */ package biz.nynja.account.models; +import java.io.Serializable; import java.util.Set; import java.util.UUID; @@ -12,7 +13,7 @@ import biz.nynja.account.grpc.ProfileDetails; import biz.nynja.account.grpc.ProfileDetails.Builder; @Table -public class Profile { +public class Profile implements Serializable { @PrimaryKey private UUID profileId; diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index da4d68398489b25f26a50c15ce89e844396fa510..649b92f38a723cb65996fb2cc9e94f050ec0fb7d 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -18,6 +18,7 @@ import java.util.stream.Collectors; import biz.nynja.account.repositories.batch.SagaTransaction; import biz.nynja.account.repositories.batch.Transaction; import biz.nynja.account.services.erlang.ErlangAccountBridge; +import org.apache.commons.lang3.SerializationUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.cassandra.core.CassandraBatchOperations; @@ -158,14 +159,14 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio return null; } - private Account newAccountInsert(CassandraBatchOperations batchOps, CompletePendingAccountCreationRequest request, + private Account newAccountInsert(Transaction batchOps, CompletePendingAccountCreationRequest request, PendingAccount pendingAccount, Long creationTimestamp) { Account newAccount = AccountBuilder.buildNewAccount(request, pendingAccount, creationTimestamp); batchOps.insert(newAccount); return newAccount; } - private Profile newProfileInsert(CassandraBatchOperations batchOps, CompletePendingAccountCreationRequest request, + private Profile newProfileInsert(Transaction batchOps, CompletePendingAccountCreationRequest request, PendingAccount pendingAccount, Long creationTimestamp) { Profile newProfile = new Profile(); newProfile.setProfileId(pendingAccount.getProfileId()); @@ -180,8 +181,7 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio return newProfile; } - private void newProfileByAuthenticationProviderInsert(CassandraBatchOperations batchOps, - PendingAccount pendingAccount) { + private void newProfileByAuthenticationProviderInsert(Transaction batchOps, PendingAccount pendingAccount) { ProfileByAuthenticationProvider newProfileByAuthenticationProvider = new ProfileByAuthenticationProvider(); newProfileByAuthenticationProvider.setAuthenticationProvider(pendingAccount.getAuthenticationProvider()); newProfileByAuthenticationProvider @@ -295,7 +295,7 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio * @param existingProfile * @return true or false according to the result of removing the account's creation provider */ - private boolean removeCreationProvider(CassandraBatchOperations batchOperations, Account existingAccount, + private boolean removeCreationProvider(Transaction batchOperations, Account existingAccount, Profile existingProfile) { // update authentication providers of the profile by removing the one of the deleted account (if not // already manually removed by the user) @@ -341,38 +341,39 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio return false; } - private void updateAccountData(CassandraBatchOperations batchOps, UpdateAccountRequest request, - Account existingAccount, Long lastUpdateTimestamp) { - Account updatedAccount = existingAccount; - updatedAccount.setAvatar(request.getAvatar()); - updatedAccount.setAccountMark(request.getAccountMark()); - updatedAccount.setAccountName(request.getAccountName()); - updatedAccount.setFirstName(request.getFirstName()); - updatedAccount.setLastName(request.getLastName()); - updatedAccount.setUsername(request.getUsername()); - updatedAccount.setLastUpdateTimestamp(lastUpdateTimestamp); - updatedAccount.setAccessStatus(request.getAccessStatus().toString()); + private void updateAccountData(Transaction transaction, UpdateAccountRequest request, Account currentAccountState, + Long lastUpdateTimestamp) { + Account newAccountState = (Account) SerializationUtils.clone(currentAccountState); + newAccountState.setAvatar(request.getAvatar()); + newAccountState.setAccountMark(request.getAccountMark()); + newAccountState.setAccountName(request.getAccountName()); + newAccountState.setFirstName(request.getFirstName()); + newAccountState.setLastName(request.getLastName()); + newAccountState.setUsername(request.getUsername()); + newAccountState.setLastUpdateTimestamp(lastUpdateTimestamp); + newAccountState.setAccessStatus(request.getAccessStatus().toString()); if (request.getRolesList() == null || request.getRolesList().isEmpty()) { - updatedAccount.setRoles(Set.of(Role.USER.toString())); + newAccountState.setRoles(Set.of(Role.USER.toString())); } else { - updatedAccount.setRoles(request.getRolesList().stream().map(Enum::toString).collect(Collectors.toSet())); + newAccountState + .setRoles(request.getRolesList().stream().map(Enum::toString).collect(Collectors.toSet())); } if (util.validateBirthdayIsSet(request.getBirthday())) { - updatedAccount.setBirthday(LocalDate.of(request.getBirthday().getYear(), request.getBirthday().getMonth(), - request.getBirthday().getDay())); + newAccountState.setBirthday(LocalDate.of(request.getBirthday().getYear(), + request.getBirthday().getMonth(), request.getBirthday().getDay())); } else { - updatedAccount.setBirthday(null); + newAccountState.setBirthday(null); } - batchOps.update(updatedAccount); + transaction.update(newAccountState, currentAccountState); } - private void updateAuthProvidersInProfileWhenDeletingAccount(CassandraBatchOperations batchOps, - Profile existingProfile, Set authProvidersToUpdate, Long lastUpdateTimestamp) { - Profile updatedProfile = existingProfile; + private void updateAuthProvidersInProfileWhenDeletingAccount(Transaction transaction, Profile currentProfileState, + Set authProvidersToUpdate, Long lastUpdateTimestamp) { + Profile newProfileState = (Profile) SerializationUtils.clone(currentProfileState); if (authProvidersToUpdate != null) { - updatedProfile.setAuthenticationProviders(authProvidersToUpdate); - updatedProfile.setLastUpdateTimestamp(lastUpdateTimestamp); - batchOps.update(updatedProfile); + currentProfileState.setAuthenticationProviders(authProvidersToUpdate); + currentProfileState.setLastUpdateTimestamp(lastUpdateTimestamp); + transaction.update(newProfileState, currentProfileState); } } diff --git a/src/main/java/biz/nynja/account/repositories/batch/Transaction.java b/src/main/java/biz/nynja/account/repositories/batch/Transaction.java index 97f227a5fdd6e9e0e2ba2a27df53c34e7b59b7bb..ff745cb4e84857d13e31f8e9c529e5401a94ea01 100644 --- a/src/main/java/biz/nynja/account/repositories/batch/Transaction.java +++ b/src/main/java/biz/nynja/account/repositories/batch/Transaction.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2018 Nynja Inc. All rights reserved. + * Copyright (C) 2018 Nynja Inc. All rights reserved. */ package biz.nynja.account.repositories.batch; @@ -8,6 +8,14 @@ import org.springframework.data.cassandra.core.WriteResult; import org.springframework.data.cassandra.core.cql.WriteOptions; public interface Transaction extends CassandraBatchOperations { + @Deprecated + CassandraBatchOperations update(Object... objects); + + @Deprecated + CassandraBatchOperations update(Iterable iterable); + + @Deprecated + CassandraBatchOperations update(Iterable iterable, WriteOptions writeOptions); WriteResult rollBack();