|
15 | 15 | */ |
16 | 16 | package io.gravitee.rest.api.management.v2.rest.mapper; |
17 | 17 |
|
| 18 | +import io.gravitee.apim.core.exception.TechnicalDomainException; |
18 | 19 | import io.gravitee.apim.core.portal_page.model.PortalArea; |
19 | | -import io.gravitee.apim.core.portal_page.model.PortalNavigationFolder; |
20 | 20 | import io.gravitee.apim.core.portal_page.model.PortalNavigationItemId; |
21 | | -import io.gravitee.apim.core.portal_page.model.PortalNavigationLink; |
22 | | -import io.gravitee.apim.core.portal_page.model.PortalNavigationPage; |
23 | 21 | import io.gravitee.apim.core.portal_page.model.PortalPageContentId; |
24 | | -import io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationItem; |
25 | | -import io.gravitee.rest.api.management.v2.rest.model.PortalNavigationItem; |
| 22 | +import io.gravitee.rest.api.management.v2.rest.model.BaseCreatePortalNavigationItem; |
| 23 | +import io.gravitee.rest.api.management.v2.rest.model.BasePortalNavigationItem; |
| 24 | +import io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationFolder; |
| 25 | +import io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationLink; |
| 26 | +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; |
26 | 30 | import org.mapstruct.Mapper; |
27 | | -import org.mapstruct.Mapping; |
28 | | -import org.mapstruct.Named; |
29 | 31 | import org.mapstruct.factory.Mappers; |
30 | 32 |
|
31 | 33 | @Mapper |
32 | 34 | public interface PortalNavigationItemsMapper { |
33 | 35 | PortalNavigationItemsMapper INSTANCE = Mappers.getMapper(PortalNavigationItemsMapper.class); |
34 | 36 |
|
35 | | - @Mapping(target = "id", qualifiedByName = "mapId") |
36 | | - @Mapping(target = "parentId", qualifiedByName = "mapParentId") |
37 | | - @Mapping(target = "type", expression = "java(mapType(portalNavigationItem))") |
38 | | - @Mapping(target = "contentId", expression = "java(mapContentId(portalNavigationItem))") |
39 | | - @Mapping(target = "uri", expression = "java(mapUri(portalNavigationItem))") |
40 | | - PortalNavigationItem map(io.gravitee.apim.core.portal_page.model.PortalNavigationItem portalNavigationItem); |
| 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 parentId = mapParentId(portalNavigationItem.getParentId()); |
41 | 42 |
|
42 | | - @Named("mapId") |
43 | | - default String mapId(PortalNavigationItemId id) { |
44 | | - return id == null ? null : id.toString(); |
45 | | - } |
46 | | - |
47 | | - @Named("mapParentId") |
48 | | - default String mapParentId(PortalNavigationItemId id) { |
49 | | - return id == null ? null : id.toString(); |
50 | | - } |
51 | | - |
52 | | - default PortalNavigationItem.TypeEnum mapType(io.gravitee.apim.core.portal_page.model.PortalNavigationItem portalNavigationItem) { |
53 | 43 | return switch (portalNavigationItem) { |
54 | | - case PortalNavigationFolder ignored -> PortalNavigationItem.TypeEnum.FOLDER; |
55 | | - case PortalNavigationPage ignored -> PortalNavigationItem.TypeEnum.PAGE; |
56 | | - case PortalNavigationLink ignored -> PortalNavigationItem.TypeEnum.LINK; |
| 44 | + case io.gravitee.apim.core.portal_page.model.PortalNavigationFolder ignored -> new PortalNavigationFolder() |
| 45 | + .id(id) |
| 46 | + .title(title) |
| 47 | + .area(area) |
| 48 | + .parentId(parentId); |
| 49 | + case io.gravitee.apim.core.portal_page.model.PortalNavigationPage page -> new PortalNavigationPage() |
| 50 | + .contentId(page.getContentId().toString()) |
| 51 | + .id(id) |
| 52 | + .title(title) |
| 53 | + .area(area) |
| 54 | + .parentId(parentId); |
| 55 | + case io.gravitee.apim.core.portal_page.model.PortalNavigationLink link -> new PortalNavigationLink() |
| 56 | + .url(link.getHref()) |
| 57 | + .id(id) |
| 58 | + .title(title) |
| 59 | + .area(area) |
| 60 | + .parentId(parentId); |
57 | 61 | }; |
58 | 62 | } |
59 | 63 |
|
60 | | - default String mapContentId(io.gravitee.apim.core.portal_page.model.PortalNavigationItem portalNavigationItem) { |
61 | | - if (portalNavigationItem instanceof PortalNavigationPage portalNavigationPage) { |
62 | | - return portalNavigationPage.getContentId().toString(); |
63 | | - } |
64 | | - return null; |
65 | | - } |
66 | | - |
67 | | - default String mapUri(io.gravitee.apim.core.portal_page.model.PortalNavigationItem portalNavigationItem) { |
68 | | - if (portalNavigationItem instanceof PortalNavigationLink portalNavigationLink) { |
69 | | - return portalNavigationLink.getHref(); |
70 | | - } |
71 | | - return null; |
72 | | - } |
73 | | - |
74 | 64 | default io.gravitee.apim.core.portal_page.model.PortalNavigationItem map( |
75 | 65 | String organizationId, |
76 | 66 | String environmentId, |
77 | | - CreatePortalNavigationItem createPortalNavigationItem |
| 67 | + BaseCreatePortalNavigationItem createPortalNavigationItem |
78 | 68 | ) { |
79 | 69 | final var id = PortalNavigationItemId.random(); |
80 | 70 | final var title = createPortalNavigationItem.getTitle(); |
81 | | - final var area = createPortalNavigationItem.getArea(); |
82 | | - final var parentIdStr = createPortalNavigationItem.getParentId(); |
83 | | - final var parentId = parentIdStr == null ? null : PortalNavigationItemId.of(parentIdStr); |
84 | | - final var contentIdStr = createPortalNavigationItem.getContentId(); |
85 | | - final var contentId = contentIdStr == null ? null : PortalPageContentId.of(contentIdStr); |
86 | | - final var uri = createPortalNavigationItem.getUri(); |
| 71 | + final var area = PortalArea.valueOf(createPortalNavigationItem.getArea().getValue()); |
| 72 | + final var parentId = mapParentId(createPortalNavigationItem.getParentId()); |
87 | 73 |
|
88 | | - return switch (createPortalNavigationItem.getType()) { |
89 | | - case FOLDER -> new PortalNavigationFolder( |
| 74 | + return switch (createPortalNavigationItem) { |
| 75 | + case CreatePortalNavigationFolder ignored -> new io.gravitee.apim.core.portal_page.model.PortalNavigationFolder( |
90 | 76 | id, |
91 | 77 | organizationId, |
92 | 78 | environmentId, |
93 | 79 | title, |
94 | | - PortalArea.valueOf(area.getValue()), |
| 80 | + area, |
95 | 81 | parentId |
96 | 82 | ); |
97 | | - case PAGE -> new PortalNavigationPage( |
| 83 | + case CreatePortalNavigationPage page -> new io.gravitee.apim.core.portal_page.model.PortalNavigationPage( |
98 | 84 | id, |
99 | 85 | organizationId, |
100 | 86 | environmentId, |
101 | 87 | title, |
102 | | - PortalArea.valueOf(area.getValue()), |
| 88 | + area, |
103 | 89 | parentId, |
104 | | - contentId |
| 90 | + PortalPageContentId.of(page.getContentId()) |
105 | 91 | ); |
106 | | - case LINK -> new PortalNavigationLink( |
| 92 | + case CreatePortalNavigationLink link -> new io.gravitee.apim.core.portal_page.model.PortalNavigationLink( |
107 | 93 | id, |
108 | 94 | organizationId, |
109 | 95 | environmentId, |
110 | 96 | title, |
111 | | - PortalArea.valueOf(area.getValue()), |
| 97 | + area, |
112 | 98 | parentId, |
113 | | - uri |
| 99 | + link.getUrl() |
| 100 | + ); |
| 101 | + default -> throw new TechnicalDomainException( |
| 102 | + String.format("Unknown PortalNavigationItem class %s", createPortalNavigationItem.getClass().getSimpleName()) |
114 | 103 | ); |
115 | 104 | }; |
116 | 105 | } |
| 106 | + |
| 107 | + private PortalNavigationItemId mapParentId(String parentId) { |
| 108 | + return parentId == null ? null : PortalNavigationItemId.of(parentId); |
| 109 | + } |
| 110 | + |
| 111 | + private String mapParentId(PortalNavigationItemId parentId) { |
| 112 | + return parentId == null ? null : parentId.toString(); |
| 113 | + } |
117 | 114 | } |
0 commit comments