|
2 | 2 |
|
3 | 3 | import it.pagopa.selfcare.external_api.model.product.ProductResource;
|
4 | 4 | import it.pagopa.selfcare.onboarding.common.PartyRole;
|
5 |
| -import it.pagopa.selfcare.product.entity.ContractTemplate; |
6 | 5 | import it.pagopa.selfcare.product.entity.Product;
|
7 | 6 | import it.pagopa.selfcare.product.entity.ProductRoleInfo;
|
8 |
| -import org.mapstruct.Mapper; |
9 |
| -import org.mapstruct.Mapping; |
10 |
| -import org.mapstruct.Named; |
11 |
| - |
12 | 7 | import java.util.EnumMap;
|
13 | 8 | import java.util.Map;
|
14 |
| -import java.util.Objects; |
15 | 9 | import java.util.Optional;
|
| 10 | +import org.mapstruct.Mapper; |
| 11 | +import org.mapstruct.Mapping; |
| 12 | +import org.mapstruct.Named; |
16 | 13 |
|
17 | 14 | @Mapper(componentModel = "spring")
|
18 | 15 | public interface ProductsMapper {
|
19 | 16 |
|
20 |
| - @Mapping(target = "roleMappings", expression = "java(toRoleMappings(model.getRoleMappings(institutionType)))") |
21 |
| - @Mapping(target = "contractTemplatePath", expression = "java(toContractTemplatePath(model,institutionType))") |
22 |
| - @Mapping(target = "contractTemplateVersion", expression = "java(toContractTemplateVersion(model,institutionType))") |
23 |
| - ProductResource toResource(Product model, String institutionType); |
24 |
| - |
25 |
| - @Named("toRoleMappings") |
26 |
| - default EnumMap<PartyRole, ProductRoleInfo> toRoleMappings(Map<PartyRole, ProductRoleInfo> roleMappings){ |
27 |
| - EnumMap<PartyRole, ProductRoleInfo> result; |
28 |
| - if(roleMappings != null){ |
29 |
| - result = new EnumMap<>(PartyRole.class); |
30 |
| - |
31 |
| - roleMappings.forEach((key, value) -> { |
32 |
| - ProductRoleInfo productRoleInfo = new ProductRoleInfo(); |
33 |
| - productRoleInfo.setRoles(roleMappings.get(key).getRoles()); |
34 |
| - productRoleInfo.setMultiroleAllowed(roleMappings.get(key).isMultiroleAllowed()); |
35 |
| - result.put(key, productRoleInfo); |
36 |
| - }); |
37 |
| - } |
38 |
| - else{ |
39 |
| - result = null; |
40 |
| - } |
41 |
| - return result; |
42 |
| - } |
43 |
| - |
44 |
| - @Named("toContractTemplatePath") |
45 |
| - default String toContractTemplatePath(Product model, String institutionType){ |
46 |
| - if(Objects.isNull(institutionType)) return null; |
47 |
| - return Optional.ofNullable(model.getInstitutionContractTemplate(institutionType)) |
48 |
| - .map(ContractTemplate::getContractTemplatePath) |
49 |
| - .orElse(null); |
50 |
| - } |
51 |
| - |
52 |
| - @Named("toContractTemplateVersion") |
53 |
| - default String toContractTemplateVersion(Product model, String institutionType){ |
54 |
| - if(Objects.isNull(institutionType)) return null; |
55 |
| - return Optional.ofNullable(model.getInstitutionContractTemplate(institutionType)) |
56 |
| - .map(ContractTemplate::getContractTemplateVersion) |
57 |
| - .orElse(null); |
| 17 | + @Mapping( |
| 18 | + target = "roleMappings", |
| 19 | + expression = "java(toRoleMappings(model.getRoleMappings(institutionType)))") |
| 20 | + @Mapping( |
| 21 | + target = "contractTemplatePath", |
| 22 | + expression = "java(toContractTemplatePath(model,institutionType))") |
| 23 | + @Mapping( |
| 24 | + target = "contractTemplateVersion", |
| 25 | + expression = "java(toContractTemplateVersion(model,institutionType))") |
| 26 | + ProductResource toResource(Product model, String institutionType); |
| 27 | + |
| 28 | + @Named("toRoleMappings") |
| 29 | + default EnumMap<PartyRole, ProductRoleInfo> toRoleMappings( |
| 30 | + Map<PartyRole, ProductRoleInfo> roleMappings) { |
| 31 | + EnumMap<PartyRole, ProductRoleInfo> result; |
| 32 | + if (roleMappings != null) { |
| 33 | + result = new EnumMap<>(PartyRole.class); |
| 34 | + |
| 35 | + roleMappings.forEach( |
| 36 | + (key, value) -> { |
| 37 | + ProductRoleInfo productRoleInfo = new ProductRoleInfo(); |
| 38 | + productRoleInfo.setRoles(roleMappings.get(key).getRoles()); |
| 39 | + productRoleInfo.setMultiroleAllowed(roleMappings.get(key).isMultiroleAllowed()); |
| 40 | + result.put(key, productRoleInfo); |
| 41 | + }); |
| 42 | + } else { |
| 43 | + result = null; |
58 | 44 | }
|
| 45 | + return result; |
| 46 | + } |
| 47 | + |
| 48 | + @Named("toContractTemplatePath") |
| 49 | + default String toContractTemplatePath(Product model, String institutionType) { |
| 50 | + return Optional.ofNullable( |
| 51 | + model.getInstitutionContractTemplate(institutionType).getContractTemplatePath()) |
| 52 | + .orElse(null); |
| 53 | + } |
| 54 | + |
| 55 | + @Named("toContractTemplateVersion") |
| 56 | + default String toContractTemplateVersion(Product model, String institutionType) { |
| 57 | + return Optional.ofNullable( |
| 58 | + model.getInstitutionContractTemplate(institutionType).getContractTemplateVersion()) |
| 59 | + .orElse(null); |
| 60 | + } |
59 | 61 | }
|
0 commit comments