1 | package org.cardanofoundation.explorer.api.service.impl; | |
2 | ||
3 | import java.util.List; | |
4 | import java.util.Map; | |
5 | import java.util.Set; | |
6 | import java.util.function.Function; | |
7 | import java.util.stream.Collectors; | |
8 | ||
9 | import lombok.RequiredArgsConstructor; | |
10 | ||
11 | import org.springframework.data.domain.Page; | |
12 | import org.springframework.data.domain.Pageable; | |
13 | import org.springframework.stereotype.Service; | |
14 | ||
15 | import com.bloxbean.cardano.client.util.HexUtil; | |
16 | import com.fasterxml.jackson.core.JsonProcessingException; | |
17 | import com.fasterxml.jackson.databind.ObjectMapper; | |
18 | import org.apache.commons.lang3.StringUtils; | |
19 | ||
20 | import org.cardanofoundation.explorer.api.exception.BusinessCode; | |
21 | import org.cardanofoundation.explorer.api.mapper.AssetMetadataMapper; | |
22 | import org.cardanofoundation.explorer.api.mapper.TokenMapper; | |
23 | import org.cardanofoundation.explorer.api.model.response.BaseFilterResponse; | |
24 | import org.cardanofoundation.explorer.api.model.response.token.PolicyResponse; | |
25 | import org.cardanofoundation.explorer.api.model.response.token.PolicyScriptResponse; | |
26 | import org.cardanofoundation.explorer.api.model.response.token.TokenFilterResponse; | |
27 | import org.cardanofoundation.explorer.api.repository.ledgersync.AssetMetadataRepository; | |
28 | import org.cardanofoundation.explorer.api.repository.ledgersync.MultiAssetRepository; | |
29 | import org.cardanofoundation.explorer.api.repository.ledgersync.ScriptRepository; | |
30 | import org.cardanofoundation.explorer.api.service.PolicyService; | |
31 | import org.cardanofoundation.explorer.common.entity.enumeration.ScriptType; | |
32 | import org.cardanofoundation.explorer.common.entity.ledgersync.AssetMetadata; | |
33 | import org.cardanofoundation.explorer.common.entity.ledgersync.MultiAsset; | |
34 | import org.cardanofoundation.explorer.common.exception.BusinessException; | |
35 | ||
36 | @Service | |
37 | @RequiredArgsConstructor | |
38 | public class PolicyServiceImpl implements PolicyService { | |
39 | ||
40 | private final MultiAssetRepository multiAssetRepository; | |
41 | private final AssetMetadataRepository assetMetadataRepository; | |
42 | private final AssetMetadataMapper assetMetadataMapper; | |
43 | private final TokenMapper tokenMapper; | |
44 | private final ScriptRepository scriptRepository; | |
45 | private final ObjectMapper objectMapper; | |
46 | ||
47 | @Override | |
48 | public PolicyResponse getPolicyDetail(String policyId) { | |
49 | Integer tokenCount = multiAssetRepository.countByPolicy(policyId); | |
50 |
1
1. getPolicyDetail : negated conditional → KILLED |
if (Integer.valueOf(0).equals(tokenCount)) { |
51 | throw new BusinessException(BusinessCode.POLICY_NOT_FOUND); | |
52 | } | |
53 | var policyResponse = PolicyResponse.builder().policyId(policyId).totalToken(tokenCount); | |
54 | scriptRepository | |
55 | .findByHash(policyId) | |
56 |
1
1. getPolicyDetail : removed call to java/util/Optional::ifPresent → KILLED |
.ifPresent( |
57 | script -> { | |
58 |
1
1. lambda$getPolicyDetail$0 : negated conditional → KILLED |
if (StringUtils.isEmpty(script.getJson())) { |
59 | try { | |
60 | String policyScript = | |
61 | objectMapper.writeValueAsString( | |
62 | PolicyScriptResponse.builder() | |
63 | .script("Plutus") | |
64 | .byteCode(HexUtil.encodeHexString(script.getBytes())) | |
65 | .build()); | |
66 | policyResponse.policyScript(policyScript); | |
67 | } catch (JsonProcessingException e) { | |
68 | policyResponse.policyScript(null); | |
69 | } | |
70 | } else { | |
71 | policyResponse.policyScript(script.getJson()); | |
72 | } | |
73 |
1
1. lambda$getPolicyDetail$0 : negated conditional → SURVIVED |
if (ScriptType.TIMELOCK.equals(script.getType())) { |
74 | policyResponse.isNativeScript(true); | |
75 | policyResponse.isSmartContract(false); | |
76 | } else { | |
77 | policyResponse.isSmartContract(true); | |
78 | policyResponse.isNativeScript(false); | |
79 | } | |
80 | }); | |
81 |
1
1. getPolicyDetail : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/PolicyServiceImpl::getPolicyDetail → KILLED |
return policyResponse.build(); |
82 | } | |
83 | ||
84 | @Override | |
85 | public BaseFilterResponse<TokenFilterResponse> getTokens(String policyId, Pageable pageable) { | |
86 | Page<MultiAsset> multiAssetPage = multiAssetRepository.findAllByPolicy(policyId, pageable); | |
87 | Set<String> subjects = | |
88 | multiAssetPage.stream() | |
89 |
1
1. lambda$getTokens$1 : replaced return value with "" for org/cardanofoundation/explorer/api/service/impl/PolicyServiceImpl::lambda$getTokens$1 → SURVIVED |
.map(ma -> ma.getPolicy() + ma.getName()) |
90 | .collect(Collectors.toSet()); | |
91 | List<AssetMetadata> assetMetadataList = assetMetadataRepository.findBySubjectIn(subjects); | |
92 | Map<String, AssetMetadata> assetMetadataMap = | |
93 | assetMetadataList.stream() | |
94 | .collect(Collectors.toMap(AssetMetadata::getSubject, Function.identity())); | |
95 | var multiAssetResponsesList = multiAssetPage.map(tokenMapper::fromMultiAssetToFilterResponse); | |
96 |
1
1. getTokens : removed call to org/springframework/data/domain/Page::forEach → KILLED |
multiAssetResponsesList.forEach( |
97 | ma -> | |
98 |
1
1. lambda$getTokens$2 : removed call to org/cardanofoundation/explorer/api/model/response/token/TokenFilterResponse::setMetadata → KILLED |
ma.setMetadata( |
99 | assetMetadataMapper.fromAssetMetadata( | |
100 | assetMetadataMap.get(ma.getPolicy() + ma.getName())))); | |
101 |
1
1. getTokens : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/PolicyServiceImpl::getTokens → KILLED |
return new BaseFilterResponse<>(multiAssetResponsesList); |
102 | } | |
103 | } | |
Mutations | ||
50 |
1.1 |
|
56 |
1.1 |
|
58 |
1.1 |
|
73 |
1.1 |
|
81 |
1.1 |
|
89 |
1.1 |
|
96 |
1.1 |
|
98 |
1.1 |
|
101 |
1.1 |