|
18 | 18 | import io.gravitee.apim.core.exception.TechnicalDomainException; |
19 | 19 | import io.gravitee.apim.core.portal_page.model.PortalArea; |
20 | 20 | import io.gravitee.apim.core.portal_page.model.PortalNavigationItemId; |
21 | | -import io.gravitee.apim.core.portal_page.model.PortalPageContentId; |
22 | | -import io.gravitee.rest.api.management.v2.rest.model.BaseCreatePortalNavigationItem; |
23 | | -import io.gravitee.rest.api.management.v2.rest.model.BasePortalNavigationItem; |
24 | 21 | import io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationFolder; |
| 22 | +import io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationItem; |
25 | 23 | import io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationLink; |
26 | 24 | import io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationPage; |
27 | | -import io.gravitee.rest.api.management.v2.rest.model.PortalNavigationFolder; |
28 | | -import io.gravitee.rest.api.management.v2.rest.model.PortalNavigationLink; |
29 | | -import io.gravitee.rest.api.management.v2.rest.model.PortalNavigationPage; |
| 25 | +import io.gravitee.rest.api.management.v2.rest.model.PortalNavigationItem; |
| 26 | +import java.util.List; |
| 27 | +import java.util.UUID; |
30 | 28 | import org.mapstruct.Mapper; |
| 29 | +import org.mapstruct.Mapping; |
31 | 30 | import org.mapstruct.factory.Mappers; |
32 | 31 |
|
33 | 32 | @Mapper |
34 | 33 | public interface PortalNavigationItemsMapper { |
35 | 34 | PortalNavigationItemsMapper INSTANCE = Mappers.getMapper(PortalNavigationItemsMapper.class); |
36 | 35 |
|
37 | | - default BasePortalNavigationItem map(io.gravitee.apim.core.portal_page.model.PortalNavigationItem portalNavigationItem) { |
38 | | - final var id = portalNavigationItem.getId().toString(); |
39 | | - final var title = portalNavigationItem.getTitle(); |
40 | | - final var area = BasePortalNavigationItem.AreaEnum.fromValue(portalNavigationItem.getArea().name()); |
41 | | - final var order = portalNavigationItem.getOrder(); |
42 | | - final var parentId = mapParentId(portalNavigationItem.getParentId()); |
| 36 | + @Mapping(target = "type", constant = "PAGE") |
| 37 | + @Mapping( |
| 38 | + target = "configuration", |
| 39 | + expression = "java(new io.gravitee.rest.api.management.v2.rest.model.PortalNavigationPageAllOfConfiguration().portalPageContentId(page.getPortalPageContentId().id()))" |
| 40 | + ) |
| 41 | + io.gravitee.rest.api.management.v2.rest.model.PortalNavigationPage map( |
| 42 | + io.gravitee.apim.core.portal_page.model.PortalNavigationPage page |
| 43 | + ); |
43 | 44 |
|
| 45 | + @Mapping(target = "type", constant = "FOLDER") |
| 46 | + io.gravitee.rest.api.management.v2.rest.model.PortalNavigationFolder map( |
| 47 | + io.gravitee.apim.core.portal_page.model.PortalNavigationFolder folder |
| 48 | + ); |
| 49 | + |
| 50 | + @Mapping(target = "type", constant = "LINK") |
| 51 | + @Mapping( |
| 52 | + target = "configuration", |
| 53 | + expression = "java(new io.gravitee.rest.api.management.v2.rest.model.PortalNavigationLinkAllOfConfiguration().url(link.getUrl()))" |
| 54 | + ) |
| 55 | + io.gravitee.rest.api.management.v2.rest.model.PortalNavigationLink map( |
| 56 | + io.gravitee.apim.core.portal_page.model.PortalNavigationLink link |
| 57 | + ); |
| 58 | + |
| 59 | + @Mapping(target = "id", expression = "java(PortalNavigationItemId.random())") |
| 60 | + io.gravitee.apim.core.portal_page.model.PortalNavigationFolder map( |
| 61 | + String organizationId, |
| 62 | + String environmentId, |
| 63 | + io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationFolder folder |
| 64 | + ); |
| 65 | + |
| 66 | + @Mapping(target = "id", expression = "java(PortalNavigationItemId.random())") |
| 67 | + @Mapping(target = "portalPageContentId", expression = "java(PortalPageContentId.of(page.getContentId().toString()))") |
| 68 | + io.gravitee.apim.core.portal_page.model.PortalNavigationPage map( |
| 69 | + String organizationId, |
| 70 | + String environmentId, |
| 71 | + io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationPage page |
| 72 | + ); |
| 73 | + |
| 74 | + @Mapping(target = "id", expression = "java(PortalNavigationItemId.random())") |
| 75 | + io.gravitee.apim.core.portal_page.model.PortalNavigationLink map( |
| 76 | + String organizationId, |
| 77 | + String environmentId, |
| 78 | + io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationLink link |
| 79 | + ); |
| 80 | + |
| 81 | + default List<PortalNavigationItem> map(List<io.gravitee.apim.core.portal_page.model.PortalNavigationItem> items) { |
| 82 | + return items.stream().map(this::map).toList(); |
| 83 | + } |
| 84 | + |
| 85 | + default PortalNavigationItem map(io.gravitee.apim.core.portal_page.model.PortalNavigationItem portalNavigationItem) { |
44 | 86 | return switch (portalNavigationItem) { |
45 | | - case io.gravitee.apim.core.portal_page.model.PortalNavigationFolder ignored -> new PortalNavigationFolder() |
46 | | - .id(id) |
47 | | - .title(title) |
48 | | - .area(area) |
49 | | - .order(order) |
50 | | - .parentId(parentId); |
51 | | - case io.gravitee.apim.core.portal_page.model.PortalNavigationPage page -> new PortalNavigationPage() |
52 | | - .contentId(page.getPortalPageContentId().toString()) |
53 | | - .id(id) |
54 | | - .title(title) |
55 | | - .area(area) |
56 | | - .order(order) |
57 | | - .parentId(parentId); |
58 | | - case io.gravitee.apim.core.portal_page.model.PortalNavigationLink link -> new PortalNavigationLink() |
59 | | - .url(link.getHref()) |
60 | | - .id(id) |
61 | | - .title(title) |
62 | | - .area(area) |
63 | | - .order(order) |
64 | | - .parentId(parentId); |
| 87 | + case io.gravitee.apim.core.portal_page.model.PortalNavigationFolder folder -> new PortalNavigationItem(map(folder)); |
| 88 | + case io.gravitee.apim.core.portal_page.model.PortalNavigationPage page -> new PortalNavigationItem(map(page)); |
| 89 | + case io.gravitee.apim.core.portal_page.model.PortalNavigationLink link -> new PortalNavigationItem(map(link)); |
65 | 90 | }; |
66 | 91 | } |
67 | 92 |
|
68 | 93 | default io.gravitee.apim.core.portal_page.model.PortalNavigationItem map( |
69 | 94 | String organizationId, |
70 | 95 | String environmentId, |
71 | | - BaseCreatePortalNavigationItem createPortalNavigationItem |
| 96 | + CreatePortalNavigationItem createPortalNavigationItem |
72 | 97 | ) { |
73 | | - final var id = PortalNavigationItemId.random(); |
74 | | - final var title = createPortalNavigationItem.getTitle(); |
75 | | - final var area = PortalArea.valueOf(createPortalNavigationItem.getArea().getValue()); |
76 | | - final var order = createPortalNavigationItem.getOrder() == null ? Integer.MAX_VALUE : createPortalNavigationItem.getOrder(); |
77 | | - |
78 | | - final var item = switch (createPortalNavigationItem) { |
79 | | - case CreatePortalNavigationFolder ignored -> new io.gravitee.apim.core.portal_page.model.PortalNavigationFolder( |
80 | | - id, |
81 | | - organizationId, |
82 | | - environmentId, |
83 | | - title, |
84 | | - area, |
85 | | - order |
86 | | - ); |
87 | | - case CreatePortalNavigationPage page -> new io.gravitee.apim.core.portal_page.model.PortalNavigationPage( |
88 | | - id, |
89 | | - organizationId, |
90 | | - environmentId, |
91 | | - title, |
92 | | - area, |
93 | | - order, |
94 | | - PortalPageContentId.of(page.getContentId()) |
95 | | - ); |
96 | | - case CreatePortalNavigationLink link -> new io.gravitee.apim.core.portal_page.model.PortalNavigationLink( |
97 | | - id, |
98 | | - organizationId, |
99 | | - environmentId, |
100 | | - title, |
101 | | - area, |
102 | | - order, |
103 | | - link.getUrl() |
104 | | - ); |
| 98 | + return switch (createPortalNavigationItem.getActualInstance()) { |
| 99 | + case CreatePortalNavigationFolder folder -> map(organizationId, environmentId, folder); |
| 100 | + case CreatePortalNavigationPage page -> map(organizationId, environmentId, page); |
| 101 | + case CreatePortalNavigationLink link -> map(organizationId, environmentId, link); |
105 | 102 | default -> throw new TechnicalDomainException( |
106 | 103 | String.format("Unknown PortalNavigationItem class %s", createPortalNavigationItem.getClass().getSimpleName()) |
107 | 104 | ); |
108 | 105 | }; |
109 | | - item.setParentId(mapParentId(createPortalNavigationItem.getParentId())); |
| 106 | + } |
| 107 | + |
| 108 | + default Integer map(Integer order) { |
| 109 | + return order == null ? Integer.MAX_VALUE : order; |
| 110 | + } |
110 | 111 |
|
111 | | - return item; |
| 112 | + default PortalNavigationItemId map(UUID id) { |
| 113 | + return id == null ? null : PortalNavigationItemId.of(id.toString()); |
112 | 114 | } |
113 | 115 |
|
114 | | - private PortalNavigationItemId mapParentId(String parentId) { |
115 | | - return parentId == null ? null : PortalNavigationItemId.of(parentId); |
| 116 | + default String map(io.gravitee.apim.core.portal_page.model.PortalNavigationItemId id) { |
| 117 | + return id != null ? id.json() : null; |
116 | 118 | } |
117 | 119 |
|
118 | | - private String mapParentId(PortalNavigationItemId parentId) { |
119 | | - return parentId == null ? null : parentId.toString(); |
| 120 | + default PortalArea map(io.gravitee.rest.api.management.v2.rest.model.PortalArea area) { |
| 121 | + return PortalArea.valueOf(area.name()); |
120 | 122 | } |
121 | 123 | } |
0 commit comments