Skip to content

Commit a0ced4d

Browse files
committed
feat: use polymorphic entities for portal navigation items.
https://gravitee.atlassian.net/browse/APIM-11696
1 parent 5a9a97a commit a0ced4d

File tree

10 files changed

+304
-289
lines changed

10 files changed

+304
-289
lines changed

gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/main/java/io/gravitee/rest/api/management/v2/rest/mapper/PortalNavigationItemsMapper.java

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -15,103 +15,100 @@
1515
*/
1616
package io.gravitee.rest.api.management.v2.rest.mapper;
1717

18+
import io.gravitee.apim.core.exception.TechnicalDomainException;
1819
import io.gravitee.apim.core.portal_page.model.PortalArea;
19-
import io.gravitee.apim.core.portal_page.model.PortalNavigationFolder;
2020
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;
2321
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;
2630
import org.mapstruct.Mapper;
27-
import org.mapstruct.Mapping;
28-
import org.mapstruct.Named;
2931
import org.mapstruct.factory.Mappers;
3032

3133
@Mapper
3234
public interface PortalNavigationItemsMapper {
3335
PortalNavigationItemsMapper INSTANCE = Mappers.getMapper(PortalNavigationItemsMapper.class);
3436

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());
4142

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) {
5343
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);
5761
};
5862
}
5963

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-
7464
default io.gravitee.apim.core.portal_page.model.PortalNavigationItem map(
7565
String organizationId,
7666
String environmentId,
77-
CreatePortalNavigationItem createPortalNavigationItem
67+
BaseCreatePortalNavigationItem createPortalNavigationItem
7868
) {
7969
final var id = PortalNavigationItemId.random();
8070
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());
8773

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(
9076
id,
9177
organizationId,
9278
environmentId,
9379
title,
94-
PortalArea.valueOf(area.getValue()),
80+
area,
9581
parentId
9682
);
97-
case PAGE -> new PortalNavigationPage(
83+
case CreatePortalNavigationPage page -> new io.gravitee.apim.core.portal_page.model.PortalNavigationPage(
9884
id,
9985
organizationId,
10086
environmentId,
10187
title,
102-
PortalArea.valueOf(area.getValue()),
88+
area,
10389
parentId,
104-
contentId
90+
PortalPageContentId.of(page.getContentId())
10591
);
106-
case LINK -> new PortalNavigationLink(
92+
case CreatePortalNavigationLink link -> new io.gravitee.apim.core.portal_page.model.PortalNavigationLink(
10793
id,
10894
organizationId,
10995
environmentId,
11096
title,
111-
PortalArea.valueOf(area.getValue()),
97+
area,
11298
parentId,
113-
uri
99+
link.getUrl()
100+
);
101+
default -> throw new TechnicalDomainException(
102+
String.format("Unknown PortalNavigationItem class %s", createPortalNavigationItem.getClass().getSimpleName())
114103
);
115104
};
116105
}
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+
}
117114
}

gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/main/java/io/gravitee/rest/api/management/v2/rest/resource/ui/PortalNavigationItemsResource.java

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@
1818
import io.gravitee.apim.core.portal_page.use_case.CreatePortalNavigationItemUseCase;
1919
import io.gravitee.common.http.MediaType;
2020
import io.gravitee.rest.api.management.v2.rest.mapper.PortalNavigationItemsMapper;
21-
import io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationItem;
22-
import io.gravitee.rest.api.management.v2.rest.model.Error;
23-
import io.gravitee.rest.api.management.v2.rest.model.ErrorDetailsInner;
21+
import io.gravitee.rest.api.management.v2.rest.model.BaseCreatePortalNavigationItem;
2422
import io.gravitee.rest.api.management.v2.rest.resource.AbstractResource;
25-
import io.gravitee.rest.api.management.v2.rest.validation.PortalNavigationItemsValidator;
2623
import io.gravitee.rest.api.model.permissions.RolePermission;
2724
import io.gravitee.rest.api.model.permissions.RolePermissionAction;
2825
import io.gravitee.rest.api.rest.annotation.Permission;
@@ -33,9 +30,7 @@
3330
import jakarta.validation.constraints.NotNull;
3431
import jakarta.ws.rs.*;
3532
import jakarta.ws.rs.core.Response;
36-
import java.util.stream.Collectors;
3733
import lombok.extern.slf4j.Slf4j;
38-
import org.openapitools.jackson.nullable.JsonNullable;
3934

4035
@Slf4j
4136
public class PortalNavigationItemsResource extends AbstractResource {
@@ -49,30 +44,7 @@ public class PortalNavigationItemsResource extends AbstractResource {
4944
@Permissions({ @Permission(value = RolePermission.ENVIRONMENT_SETTINGS, acls = { RolePermissionAction.UPDATE }) })
5045
@Consumes(MediaType.APPLICATION_JSON)
5146
@Produces(MediaType.APPLICATION_JSON)
52-
public Response createPortalNavigationItem(@Valid @NotNull final CreatePortalNavigationItem createPortalNavigationItem) {
53-
final var errors = PortalNavigationItemsValidator.validate(createPortalNavigationItem);
54-
55-
if (!errors.isEmpty()) {
56-
return Response.status(Response.Status.BAD_REQUEST)
57-
.entity(
58-
new Error()
59-
.httpStatus(Response.Status.BAD_REQUEST.getStatusCode())
60-
.message("Validation error")
61-
.details(
62-
errors
63-
.stream()
64-
.map(error ->
65-
new ErrorDetailsInner()
66-
.message(error.getErrorMessage())
67-
.invalidValue(error.isPresent() ? JsonNullable.of(error.getLocation()) : JsonNullable.undefined())
68-
.location(error.getLocation())
69-
)
70-
.toList()
71-
)
72-
)
73-
.build();
74-
}
75-
47+
public Response createPortalNavigationItem(@Valid @NotNull final BaseCreatePortalNavigationItem createPortalNavigationItem) {
7648
final var executionContext = GraviteeContext.getExecutionContext();
7749

7850
final var output = createPortalNavigationItemUseCase.execute(

gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/main/java/io/gravitee/rest/api/management/v2/rest/validation/PortalNavigationItemsValidator.java

Lines changed: 0 additions & 150 deletions
This file was deleted.

0 commit comments

Comments
 (0)