From 5764dbea2e4e5e7999ac18c577bf8abfe3bda1eb Mon Sep 17 00:00:00 2001 From: Baikov Dmitrii <44803026+D-Baykov@users.noreply.github.com> Date: Tue, 28 May 2024 15:52:05 +0300 Subject: [PATCH] FIN-33: Add ContractAdjustment using (#19) --- pom.xml | 2 +- .../converter/ShopTermSetConverter.java | 44 +++----------- .../converter/TerminalTermSetConverter.java | 7 ++- .../converter/WalletTermSetConverter.java | 44 +++----------- .../ShopTermSetDataObjectRowMapper.java | 4 +- .../WalletTermSetDataObjectRowMapper.java | 4 +- .../party/iface/ContractAdjustmentDao.java | 2 + .../dao/party/iface/TermSetHierarchyDao.java | 6 +- .../party/impl/ContractAdjustmentDaoImpl.java | 14 ++++- .../dao/party/impl/TermSetDaoImpl.java | 6 +- .../party/impl/TermSetHierarchyDaoImpl.java | 24 +++++++- .../dominator/data/ShopTermSetDataObject.java | 4 +- .../data/WalletTermSetDataObject.java | 4 +- .../handler/get/GetShopTermSetsHandler.java | 12 ++-- .../get/GetTerminalTermSetsHandler.java | 13 ++--- .../handler/get/GetWalletTermSetsHandler.java | 13 ++--- .../service/TermSetGettingService.java | 58 +++++++++++++++++++ .../com/empayre/dominator/dao/DaoTests.java | 3 + .../service/DominatorServiceTest.java | 13 ----- 19 files changed, 149 insertions(+), 128 deletions(-) create mode 100644 src/main/java/com/empayre/dominator/service/TermSetGettingService.java diff --git a/pom.xml b/pom.xml index 09b23a6..13042c9 100644 --- a/pom.xml +++ b/pom.xml @@ -155,7 +155,7 @@ dev.vality dominator-proto - 1.4-68a8678 + 1.5-b9788c2 dev.vality diff --git a/src/main/java/com/empayre/dominator/converter/ShopTermSetConverter.java b/src/main/java/com/empayre/dominator/converter/ShopTermSetConverter.java index 3c5c5dc..85c9ea2 100644 --- a/src/main/java/com/empayre/dominator/converter/ShopTermSetConverter.java +++ b/src/main/java/com/empayre/dominator/converter/ShopTermSetConverter.java @@ -1,22 +1,14 @@ package com.empayre.dominator.converter; -import com.empayre.dominator.dao.party.iface.TermSetHierarchyDao; import com.empayre.dominator.data.ShopTermSetDataObject; import com.empayre.dominator.domain.tables.pojos.TermSetHierarchy; -import dev.vality.damsel.domain.TermSetHierarchyObject; +import com.empayre.dominator.service.TermSetGettingService; import dev.vality.dominator.ShopTermSet; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.apache.thrift.TDeserializer; -import org.apache.thrift.TException; import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Component; -import org.springframework.util.CollectionUtils; -import java.util.ArrayList; -import java.util.List; - -import static com.empayre.dominator.util.TermSetConverterUtils.createEmptyTermSetHierarchyObject; import static com.empayre.dominator.util.TermSetConverterUtils.replaceNull; @Slf4j @@ -24,40 +16,22 @@ import static com.empayre.dominator.util.TermSetConverterUtils.replaceNull; @RequiredArgsConstructor public class ShopTermSetConverter implements Converter { - private final TDeserializer deserializer; - private final TermSetHierarchyDao termSetHierarchyDao; + private final TermSetGettingService termSetGettingService; @Override public ShopTermSet convert(ShopTermSetDataObject source) { + Long contractRecordId = source.getContractRecordId(); + TermSetHierarchy currentTermSet = + termSetGettingService.getCurrentTermSet(contractRecordId, source.getTermSetId()); return new ShopTermSet() .setOwnerId(replaceNull(source.getPartyId())) .setShopId(replaceNull(source.getShopId())) .setContractId(replaceNull(source.getContractId())) .setShopName(replaceNull(source.getShopName())) - .setTermSetName(replaceNull(source.getTermSetName())) + .setTermSetName(replaceNull(currentTermSet.getName())) .setCurrency(replaceNull(source.getCurrency())) - .setCurrentTermSet(deserializeTermSet(source.getCurrentTermSetHierarchyObject())) - .setTermSetHistory(deserializeTermSets( - termSetHierarchyDao.getTermSetHierarchyHistory(source.getTermSetId()))); - } - - private List deserializeTermSets(List termSetHierarchies) { - return CollectionUtils.isEmpty(termSetHierarchies) ? new ArrayList<>() : termSetHierarchies.stream() - .map(termSet -> deserializeTermSet(termSet.getTermSetHierarchyObject())) - .toList(); - } - - private TermSetHierarchyObject deserializeTermSet(byte[] object) { - try { - if (object == null || object.length == 0) { - return createEmptyTermSetHierarchyObject(); - } - TermSetHierarchyObject termSetHierarchyObject = new TermSetHierarchyObject(); - deserializer.deserialize(termSetHierarchyObject, object); - return termSetHierarchyObject; - } catch (TException e) { - log.error("TermSetHierarchyObject deserialization exception", e); - return createEmptyTermSetHierarchyObject(); - } + .setCurrentTermSet( + termSetGettingService.getTermSetFromObject(currentTermSet.getTermSetHierarchyObject())) + .setTermSetHistory(termSetGettingService.getTermSetHistory(contractRecordId)); } } diff --git a/src/main/java/com/empayre/dominator/converter/TerminalTermSetConverter.java b/src/main/java/com/empayre/dominator/converter/TerminalTermSetConverter.java index 6370606..adbde20 100644 --- a/src/main/java/com/empayre/dominator/converter/TerminalTermSetConverter.java +++ b/src/main/java/com/empayre/dominator/converter/TerminalTermSetConverter.java @@ -3,12 +3,12 @@ package com.empayre.dominator.converter; import com.empayre.dominator.dao.dominant.iface.TerminalDao; import com.empayre.dominator.data.TerminalTermSetDataObject; import com.empayre.dominator.domain.tables.pojos.Terminal; -import com.empayre.dominator.exception.SerializationException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import dev.vality.damsel.domain.ProviderRef; import dev.vality.damsel.domain.ProvisionTermSet; import dev.vality.damsel.domain.TerminalRef; +import dev.vality.dominator.ProvisionTermSetHistory; import dev.vality.dominator.TerminalTermSet; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -48,9 +48,10 @@ public class TerminalTermSetConverter implements Converter deserializeTermSets(List termSetHierarchies) { + private List deserializeTermSets(List termSetHierarchies) { return CollectionUtils.isEmpty(termSetHierarchies) ? new ArrayList<>() : termSetHierarchies.stream() - .map(terminal -> deserializeTermSet(terminal.getTermsObject())) + .map(terminal -> new ProvisionTermSetHistory() + .setTermSet(deserializeTermSet(terminal.getTermsObject()))) .toList(); } diff --git a/src/main/java/com/empayre/dominator/converter/WalletTermSetConverter.java b/src/main/java/com/empayre/dominator/converter/WalletTermSetConverter.java index 8e9b6de..b8e5eec 100644 --- a/src/main/java/com/empayre/dominator/converter/WalletTermSetConverter.java +++ b/src/main/java/com/empayre/dominator/converter/WalletTermSetConverter.java @@ -1,23 +1,15 @@ package com.empayre.dominator.converter; -import com.empayre.dominator.dao.party.iface.TermSetHierarchyDao; import com.empayre.dominator.data.WalletTermSetDataObject; import com.empayre.dominator.domain.tables.pojos.TermSetHierarchy; +import com.empayre.dominator.service.TermSetGettingService; import dev.vality.damsel.domain.IdentityProviderRef; -import dev.vality.damsel.domain.TermSetHierarchyObject; import dev.vality.dominator.WalletTermSet; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.apache.thrift.TDeserializer; -import org.apache.thrift.TException; import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Component; -import org.springframework.util.CollectionUtils; -import java.util.ArrayList; -import java.util.List; - -import static com.empayre.dominator.util.TermSetConverterUtils.createEmptyTermSetHierarchyObject; import static com.empayre.dominator.util.TermSetConverterUtils.replaceNull; @Slf4j @@ -25,41 +17,23 @@ import static com.empayre.dominator.util.TermSetConverterUtils.replaceNull; @RequiredArgsConstructor public class WalletTermSetConverter implements Converter { - private final TDeserializer deserializer; - private final TermSetHierarchyDao termSetHierarchyDao; + private final TermSetGettingService termSetGettingService; @Override public WalletTermSet convert(WalletTermSetDataObject source) { + Long contractRecordId = source.getContractRecordId(); + TermSetHierarchy currentTermSet = + termSetGettingService.getCurrentTermSet(contractRecordId, source.getTermSetId()); return new WalletTermSet() .setOwnerId(source.getPartyId()) .setIdentityId(new IdentityProviderRef().setId(source.getIdentityId())) .setContractId(replaceNull(source.getContractId())) .setWalletId(replaceNull(source.getWalletId())) .setWalletName(replaceNull(source.getWalletName())) - .setTermSetName(replaceNull(source.getTermSetName())) + .setTermSetName(replaceNull(currentTermSet.getName())) .setCurrency(replaceNull(source.getCurrency())) - .setCurrentTermSet(deserializeTermSet(source.getCurrentTermSetHierarchyObject())) - .setTermSetHistory(deserializeTermSets( - termSetHierarchyDao.getTermSetHierarchyHistory(source.getTermSetId()))); - } - - private List deserializeTermSets(List termSetHierarchies) { - return CollectionUtils.isEmpty(termSetHierarchies) ? new ArrayList<>() : termSetHierarchies.stream() - .map(termSet -> deserializeTermSet(termSet.getTermSetHierarchyObject())) - .toList(); - } - - private TermSetHierarchyObject deserializeTermSet(byte[] object) { - try { - if (object == null || object.length == 0) { - return createEmptyTermSetHierarchyObject(); - } - TermSetHierarchyObject termSetHierarchyObject = new TermSetHierarchyObject(); - deserializer.deserialize(termSetHierarchyObject, object); - return termSetHierarchyObject; - } catch (TException e) { - log.error("TermSetHierarchyObject deserialization exception", e); - return createEmptyTermSetHierarchyObject(); - } + .setCurrentTermSet( + termSetGettingService.getTermSetFromObject(currentTermSet.getTermSetHierarchyObject())) + .setTermSetHistory(termSetGettingService.getTermSetHistory(contractRecordId)); } } diff --git a/src/main/java/com/empayre/dominator/dao/mapper/ShopTermSetDataObjectRowMapper.java b/src/main/java/com/empayre/dominator/dao/mapper/ShopTermSetDataObjectRowMapper.java index b716e63..b1b93dd 100644 --- a/src/main/java/com/empayre/dominator/dao/mapper/ShopTermSetDataObjectRowMapper.java +++ b/src/main/java/com/empayre/dominator/dao/mapper/ShopTermSetDataObjectRowMapper.java @@ -18,9 +18,7 @@ public class ShopTermSetDataObjectRowMapper implements RecordMapper contractAdjustmentList) throws DaoException; List getByContractId(Long contractId) throws DaoException; + + ContractAdjustment getLastByContractId(Long contractId) throws DaoException; } diff --git a/src/main/java/com/empayre/dominator/dao/party/iface/TermSetHierarchyDao.java b/src/main/java/com/empayre/dominator/dao/party/iface/TermSetHierarchyDao.java index f5d4b64..fb5bce5 100644 --- a/src/main/java/com/empayre/dominator/dao/party/iface/TermSetHierarchyDao.java +++ b/src/main/java/com/empayre/dominator/dao/party/iface/TermSetHierarchyDao.java @@ -12,5 +12,9 @@ public interface TermSetHierarchyDao extends DomainObjectDao getTermSetHierarchyHistory(Integer refId); + TermSetHierarchy getCurrentTermSet(Integer refId); + + List getTermSetHierarchyHistory(Integer refId, boolean current); + + byte[] getTermSetHierarchyObject(Integer refId, boolean current); } diff --git a/src/main/java/com/empayre/dominator/dao/party/impl/ContractAdjustmentDaoImpl.java b/src/main/java/com/empayre/dominator/dao/party/impl/ContractAdjustmentDaoImpl.java index 4c3b129..e364822 100644 --- a/src/main/java/com/empayre/dominator/dao/party/impl/ContractAdjustmentDaoImpl.java +++ b/src/main/java/com/empayre/dominator/dao/party/impl/ContractAdjustmentDaoImpl.java @@ -29,7 +29,9 @@ public class ContractAdjustmentDaoImpl extends AbstractDao implements ContractAd .insertInto(CONTRACT_ADJUSTMENT) .set(contractAdjustmentRecord)) .collect(Collectors.toList()); - getDslContext().batch(queries).execute(); + getDslContext() + .batch(queries) + .execute(); } @Override @@ -41,4 +43,14 @@ public class ContractAdjustmentDaoImpl extends AbstractDao implements ContractAd .fetch() .into(ContractAdjustment.class); } + + @Override + public ContractAdjustment getLastByContractId(Long contractId) throws DaoException { + return getDslContext() + .selectFrom(CONTRACT_ADJUSTMENT) + .where(CONTRACT_ADJUSTMENT.CONTRACT_ID.eq(contractId)) + .orderBy(CONTRACT_ADJUSTMENT.ID.desc()) + .limit(1) + .fetchOneInto(ContractAdjustment.class); + } } diff --git a/src/main/java/com/empayre/dominator/dao/party/impl/TermSetDaoImpl.java b/src/main/java/com/empayre/dominator/dao/party/impl/TermSetDaoImpl.java index f0cce08..85da880 100644 --- a/src/main/java/com/empayre/dominator/dao/party/impl/TermSetDaoImpl.java +++ b/src/main/java/com/empayre/dominator/dao/party/impl/TermSetDaoImpl.java @@ -39,8 +39,7 @@ public class TermSetDaoImpl extends AbstractDao implements TermSetDao { public List getShopTermSets(Condition condition, int limit) { var fetch = getDslContext() .select(SHOP.ID, SHOP.PARTY_ID, SHOP.SHOP_ID, SHOP.CONTRACT_ID, SHOP.ACCOUNT_CURRENCY_CODE, - SHOP.DETAILS_NAME, CONTRACT.TERMS_ID, TERM_SET_HIERARCHY.NAME, - TERM_SET_HIERARCHY.TERM_SETS_JSON, TERM_SET_HIERARCHY.TERM_SET_HIERARCHY_OBJECT) + SHOP.DETAILS_NAME, CONTRACT.TERMS_ID, CONTRACT.ID) .from(SHOP) .join(CONTRACT) .on(CONTRACT.CONTRACT_ID.eq(SHOP.CONTRACT_ID).and(CONTRACT.CURRENT)) @@ -58,8 +57,7 @@ public class TermSetDaoImpl extends AbstractDao implements TermSetDao { public List getWalletTermSets(Condition condition, int limit) { var fetch = getDslContext() .select(WALLET.PARTY_ID, WALLET.IDENTITY_ID, IDENTITY.PARTY_CONTRACT_ID, WALLET.CURRENCY_CODE, - WALLET.WALLET_ID, WALLET.WALLET_NAME, CONTRACT.TERMS_ID, TERM_SET_HIERARCHY.NAME, - TERM_SET_HIERARCHY.TERM_SETS_JSON, WALLET.ID, TERM_SET_HIERARCHY.TERM_SET_HIERARCHY_OBJECT) + WALLET.WALLET_ID, WALLET.WALLET_NAME, CONTRACT.TERMS_ID, CONTRACT.ID, WALLET.ID) .from(WALLET) .join(IDENTITY) .on(WALLET.IDENTITY_ID.eq(IDENTITY.IDENTITY_ID).and(IDENTITY.CURRENT)) diff --git a/src/main/java/com/empayre/dominator/dao/party/impl/TermSetHierarchyDaoImpl.java b/src/main/java/com/empayre/dominator/dao/party/impl/TermSetHierarchyDaoImpl.java index 337ac5e..093b16c 100644 --- a/src/main/java/com/empayre/dominator/dao/party/impl/TermSetHierarchyDaoImpl.java +++ b/src/main/java/com/empayre/dominator/dao/party/impl/TermSetHierarchyDaoImpl.java @@ -43,12 +43,32 @@ public class TermSetHierarchyDaoImpl extends AbstractDao implements TermSetHiera } @Override - public List getTermSetHierarchyHistory(Integer refId) { + public TermSetHierarchy getCurrentTermSet(Integer refId) { return getDslContext() .selectFrom(TERM_SET_HIERARCHY) .where(TERM_SET_HIERARCHY.TERM_SET_HIERARCHY_REF_ID.eq(refId)) - .and(TERM_SET_HIERARCHY.CURRENT.isFalse()) + .and(TERM_SET_HIERARCHY.CURRENT.isTrue()) + .orderBy(TERM_SET_HIERARCHY.ID.desc()) + .fetchOneInto(TermSetHierarchy.class); + } + + @Override + public List getTermSetHierarchyHistory(Integer refId, boolean current) { + return getDslContext() + .selectFrom(TERM_SET_HIERARCHY) + .where(TERM_SET_HIERARCHY.TERM_SET_HIERARCHY_REF_ID.eq(refId)) + .and(TERM_SET_HIERARCHY.CURRENT.eq(current)) .orderBy(TERM_SET_HIERARCHY.ID.desc()) .fetchInto(TermSetHierarchy.class); } + + public byte[] getTermSetHierarchyObject(Integer refId, boolean current) { + return getDslContext() + .select(TERM_SET_HIERARCHY.TERM_SET_HIERARCHY_OBJECT) + .from(TERM_SET_HIERARCHY) + .where(TERM_SET_HIERARCHY.TERM_SET_HIERARCHY_REF_ID.eq(refId)) + .and(TERM_SET_HIERARCHY.CURRENT.eq(current)) + .fetchOne() + .value1(); + } } diff --git a/src/main/java/com/empayre/dominator/data/ShopTermSetDataObject.java b/src/main/java/com/empayre/dominator/data/ShopTermSetDataObject.java index 44d4788..872cdd8 100644 --- a/src/main/java/com/empayre/dominator/data/ShopTermSetDataObject.java +++ b/src/main/java/com/empayre/dominator/data/ShopTermSetDataObject.java @@ -14,7 +14,5 @@ public class ShopTermSetDataObject { private String currency; private String shopName; private Integer termSetId; - private String termSetName; - private String currentTermSetJson; - private byte[] currentTermSetHierarchyObject; + private Long contractRecordId; } diff --git a/src/main/java/com/empayre/dominator/data/WalletTermSetDataObject.java b/src/main/java/com/empayre/dominator/data/WalletTermSetDataObject.java index 3fe691a..599f2f0 100644 --- a/src/main/java/com/empayre/dominator/data/WalletTermSetDataObject.java +++ b/src/main/java/com/empayre/dominator/data/WalletTermSetDataObject.java @@ -15,7 +15,5 @@ public class WalletTermSetDataObject { private String walletId; private String walletName; private Integer termSetId; - private String termSetName; - private String currentTermSetJson; - private byte[] currentTermSetHierarchyObject; + private Long contractRecordId; } diff --git a/src/main/java/com/empayre/dominator/handler/get/GetShopTermSetsHandler.java b/src/main/java/com/empayre/dominator/handler/get/GetShopTermSetsHandler.java index 8d17110..b9b987a 100644 --- a/src/main/java/com/empayre/dominator/handler/get/GetShopTermSetsHandler.java +++ b/src/main/java/com/empayre/dominator/handler/get/GetShopTermSetsHandler.java @@ -26,22 +26,20 @@ public class GetShopTermSetsHandler implements GetTermSetsHandler shopTermSets = termSetDao.getShopTermSets( - createShopCondition(query), - query.getCommonSearchQueryParams().getLimit() - ); + int limit = query.getCommonSearchQueryParams().getLimit(); + List shopTermSets = termSetDao.getShopTermSets(createShopCondition(query), limit); ShopTermSetsResponse response = new ShopTermSetsResponse() .setTerms(shopTermSets.stream().map(termSet -> shopTermSetConverter.convert(termSet)).toList()) - .setContinuationToken(createContinuationToken(shopTermSets)); + .setContinuationToken(createContinuationToken(shopTermSets, limit)); log.info("Finish ShopTermSets getting (query: {}, terms.size: {}, token: {})", query, response.getTerms().size(), response.getContinuationToken()); log.debug("Result ShopTermSetsResponse: {}", response); return response; } - private String createContinuationToken(List shopTermSets) { - return CollectionUtils.isEmpty(shopTermSets) + private String createContinuationToken(List shopTermSets, int limit) { + return CollectionUtils.isEmpty(shopTermSets) || shopTermSets.size() < limit ? null : String.valueOf(shopTermSets.get(shopTermSets.size() - 1).getId()); } } diff --git a/src/main/java/com/empayre/dominator/handler/get/GetTerminalTermSetsHandler.java b/src/main/java/com/empayre/dominator/handler/get/GetTerminalTermSetsHandler.java index c0e380f..6536834 100644 --- a/src/main/java/com/empayre/dominator/handler/get/GetTerminalTermSetsHandler.java +++ b/src/main/java/com/empayre/dominator/handler/get/GetTerminalTermSetsHandler.java @@ -26,22 +26,21 @@ public class GetTerminalTermSetsHandler implements GetTermSetsHandler terminalTermSets = termSetDao.getTerminalTermSets( - createTerminalCondition(query), - query.getCommonSearchQueryParams().getLimit() - ); + int limit = query.getCommonSearchQueryParams().getLimit(); + List terminalTermSets = + termSetDao.getTerminalTermSets(createTerminalCondition(query), limit); TerminalTermSetsResponse response = new TerminalTermSetsResponse() .setTerms(terminalTermSets.stream().map(set -> terminalTermSetConverter.convert(set)).toList()) - .setContinuationToken(createContinuationToken(terminalTermSets)); + .setContinuationToken(createContinuationToken(terminalTermSets, limit)); log.info("Finish TerminalTermSets getting (query: {}, terms.size: {}, token: {})", query, response.getTerms().size(), response.getContinuationToken()); log.debug("Result TerminalTermSetsResponse: {}", response); return response; } - private String createContinuationToken(List terminalTermSets) { - return CollectionUtils.isEmpty(terminalTermSets) + private String createContinuationToken(List terminalTermSets, int limit) { + return CollectionUtils.isEmpty(terminalTermSets) || terminalTermSets.size() < limit ? null : String.valueOf(terminalTermSets.get(terminalTermSets.size() - 1).getId()); } } diff --git a/src/main/java/com/empayre/dominator/handler/get/GetWalletTermSetsHandler.java b/src/main/java/com/empayre/dominator/handler/get/GetWalletTermSetsHandler.java index f256914..638f72c 100644 --- a/src/main/java/com/empayre/dominator/handler/get/GetWalletTermSetsHandler.java +++ b/src/main/java/com/empayre/dominator/handler/get/GetWalletTermSetsHandler.java @@ -26,22 +26,21 @@ public class GetWalletTermSetsHandler implements GetTermSetsHandler walletTermSets = termSetDao.getWalletTermSets( - createWalletCondition(query), - query.getCommonSearchQueryParams().getLimit() - ); + int limit = query.getCommonSearchQueryParams().getLimit(); + List walletTermSets = + termSetDao.getWalletTermSets(createWalletCondition(query), limit); WalletTermSetsResponse response = new WalletTermSetsResponse() .setTerms(walletTermSets.stream().map(set -> walletTermSetConverter.convert(set)).toList()) - .setContinuationToken(createContinuationToken(walletTermSets)); + .setContinuationToken(createContinuationToken(walletTermSets, limit)); log.info("Finish WalletTermSets getting (query: {}, terms.size: {}, token: {})", query, response.getTerms().size(), response.getContinuationToken()); log.debug("Result WalletTermSetsResponse: {}", response); return response; } - private String createContinuationToken(List walletTermSets) { - return CollectionUtils.isEmpty(walletTermSets) + private String createContinuationToken(List walletTermSets, int limit) { + return CollectionUtils.isEmpty(walletTermSets) || walletTermSets.size() < limit ? null : String.valueOf(walletTermSets.get(walletTermSets.size() - 1).getId()); } } diff --git a/src/main/java/com/empayre/dominator/service/TermSetGettingService.java b/src/main/java/com/empayre/dominator/service/TermSetGettingService.java new file mode 100644 index 0000000..adde23f --- /dev/null +++ b/src/main/java/com/empayre/dominator/service/TermSetGettingService.java @@ -0,0 +1,58 @@ +package com.empayre.dominator.service; + +import com.empayre.dominator.dao.party.iface.ContractAdjustmentDao; +import com.empayre.dominator.dao.party.iface.TermSetHierarchyDao; +import com.empayre.dominator.domain.tables.pojos.ContractAdjustment; +import com.empayre.dominator.domain.tables.pojos.TermSetHierarchy; +import dev.vality.damsel.domain.TermSetHierarchyObject; +import dev.vality.dominator.TermSetHistory; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.thrift.TDeserializer; +import org.apache.thrift.TException; +import org.springframework.stereotype.Service; + +import java.util.List; + +import static com.empayre.dominator.util.TermSetConverterUtils.createEmptyTermSetHierarchyObject; + +@Slf4j +@Service +@RequiredArgsConstructor +public class TermSetGettingService { + + private final TDeserializer deserializer; + private final TermSetHierarchyDao termSetHierarchyDao; + private final ContractAdjustmentDao contractAdjustmentDao; + + public TermSetHierarchy getCurrentTermSet(Long contractRecordId, Integer defaultTermSetId) { + ContractAdjustment lastAdjustment = contractAdjustmentDao.getLastByContractId(contractRecordId); + Integer currentTermSetId = lastAdjustment == null ? defaultTermSetId : lastAdjustment.getTermsId(); + return termSetHierarchyDao.getCurrentTermSet(currentTermSetId); + } + + public List getTermSetHistory(long contractRecordId) { + return contractAdjustmentDao.getByContractId(contractRecordId).stream() + .map( + adj -> new TermSetHistory() + .setTermSet(getTermSetFromObject( + termSetHierarchyDao.getTermSetHierarchyObject(adj.getTermsId(), true))) + .setAppliedAt(adj.getCreatedAt().toString()) + ) + .toList(); + } + + public TermSetHierarchyObject getTermSetFromObject(byte[] object) { + try { + if (object == null || object.length == 0) { + return createEmptyTermSetHierarchyObject(); + } + TermSetHierarchyObject termSetHierarchyObject = new TermSetHierarchyObject(); + deserializer.deserialize(termSetHierarchyObject, object); + return termSetHierarchyObject; + } catch (TException e) { + log.error("TermSetHierarchyObject deserialization exception", e); + return createEmptyTermSetHierarchyObject(); + } + } +} diff --git a/src/test/java/com/empayre/dominator/dao/DaoTests.java b/src/test/java/com/empayre/dominator/dao/DaoTests.java index b420a66..efe133c 100644 --- a/src/test/java/com/empayre/dominator/dao/DaoTests.java +++ b/src/test/java/com/empayre/dominator/dao/DaoTests.java @@ -59,6 +59,9 @@ public class DaoTests { @Test public void dominantDaoTest() { Provider provider = RandomBeans.random(Provider.class); + provider.setTerminalObject(new byte[0]); + provider.setWalletTermsObject(new byte[0]); + provider.setPaymentTermsObject(new byte[0]); provider.setCurrent(true); providerDao.save(provider); providerDao.updateNotCurrent(provider.getProviderRefId()); diff --git a/src/test/java/com/empayre/dominator/service/DominatorServiceTest.java b/src/test/java/com/empayre/dominator/service/DominatorServiceTest.java index 7c597da..171d469 100644 --- a/src/test/java/com/empayre/dominator/service/DominatorServiceTest.java +++ b/src/test/java/com/empayre/dominator/service/DominatorServiceTest.java @@ -81,7 +81,6 @@ public class DominatorServiceTest { .setPartyId("party-1") ); assertNotNull(partyResponse); - assertNotNull(partyResponse.getContinuationToken()); assertNotNull(partyResponse.getTerms()); assertEquals(1, partyResponse.getTerms().size()); @@ -91,7 +90,6 @@ public class DominatorServiceTest { .setShopIds(List.of("party-1-shop-1", "party-2-shop-2", "party-2-shop-3", "party-3-shop-3")) ); assertNotNull(shopResponse); - assertNotNull(shopResponse.getContinuationToken()); assertEquals(3, shopResponse.getTerms().size()); ShopTermSetsResponse termSetsIdsResponse = dominatorService.searchShopTermSets( @@ -103,7 +101,6 @@ public class DominatorServiceTest { new TermSetHierarchyRef(5))) ); assertNotNull(termSetsIdsResponse); - assertNotNull(termSetsIdsResponse.getContinuationToken()); assertEquals(3, termSetsIdsResponse.getTerms().size()); ShopTermSetsResponse termSetsNamesResponse = dominatorService.searchShopTermSets( @@ -112,7 +109,6 @@ public class DominatorServiceTest { .setTermSetsNames(List.of("Name-1", "Name-6")) ); assertNotNull(termSetsNamesResponse); - assertNotNull(termSetsNamesResponse.getContinuationToken()); assertEquals(2, termSetsNamesResponse.getTerms().size()); } @@ -141,7 +137,6 @@ public class DominatorServiceTest { .setCommonSearchQueryParams(new CommonSearchQueryParams()) ); assertNotNull(emptyRequestResponse); - assertNotNull(emptyRequestResponse.getContinuationToken()); assertEquals(10, emptyRequestResponse.getTerms().size()); WalletTermSetsResponse partyResponse = dominatorService.searchWalletTermSets( @@ -150,7 +145,6 @@ public class DominatorServiceTest { .setPartyId("WParty-1") ); assertNotNull(partyResponse); - assertNotNull(partyResponse.getContinuationToken()); assertEquals(1, partyResponse.getTerms().size()); WalletTermSetsResponse walletResponse = dominatorService.searchWalletTermSets( @@ -159,7 +153,6 @@ public class DominatorServiceTest { .setWalletIds(List.of("Wallet-9", "Wallet-8", "Wallet-81")) ); assertNotNull(walletResponse); - assertNotNull(walletResponse.getContinuationToken()); assertEquals(2, walletResponse.getTerms().size()); WalletTermSetsResponse identityResponse = dominatorService.searchWalletTermSets( @@ -172,7 +165,6 @@ public class DominatorServiceTest { )) ); assertNotNull(identityResponse); - assertNotNull(identityResponse.getContinuationToken()); assertEquals(3, identityResponse.getTerms().size()); WalletTermSetsResponse termIdsResponse = dominatorService.searchWalletTermSets( @@ -186,7 +178,6 @@ public class DominatorServiceTest { )) ); assertNotNull(termIdsResponse); - assertNotNull(termIdsResponse.getContinuationToken()); assertEquals(4, termIdsResponse.getTerms().size()); WalletTermSetsResponse termNamesResponse = dominatorService.searchWalletTermSets( @@ -195,7 +186,6 @@ public class DominatorServiceTest { .setTermSetsNames(List.of("Name-1", "Name-6")) ); assertNotNull(termNamesResponse); - assertNotNull(termNamesResponse.getContinuationToken()); assertEquals(2, termNamesResponse.getTerms().size()); } @@ -216,7 +206,6 @@ public class DominatorServiceTest { TerminalTermSetsResponse simpleResponse = dominatorService.searchTerminalTermSets( new TerminalSearchQuery().setCommonSearchQueryParams(new CommonSearchQueryParams())); assertNotNull(simpleResponse); - assertNotNull(simpleResponse.getContinuationToken()); assertEquals(10, simpleResponse.getTerms().size()); @@ -230,7 +219,6 @@ public class DominatorServiceTest { )) ); assertNotNull(terminalIdsResponse); - assertNotNull(terminalIdsResponse.getContinuationToken()); assertEquals(3, terminalIdsResponse.getTerms().size()); TerminalTermSetsResponse providerIdsResponse = dominatorService.searchTerminalTermSets( @@ -245,7 +233,6 @@ public class DominatorServiceTest { )) ); assertNotNull(providerIdsResponse); - assertNotNull(providerIdsResponse.getContinuationToken()); assertEquals(5, providerIdsResponse.getTerms().size()); } }