|
| 1 | +/* |
| 2 | + * Copyright © 2015 The Gravitee team (http://gravitee.io) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.gravitee.rest.api.management.v2.rest.mapper; |
| 17 | + |
| 18 | +import io.gravitee.apim.core.exception.TechnicalDomainException; |
| 19 | +import io.gravitee.apim.core.portal_page.model.PortalNavigationItemId; |
| 20 | +import io.gravitee.rest.api.management.v2.rest.model.BaseCreatePortalNavigationItem; |
| 21 | +import io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationFolder; |
| 22 | +import io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationLink; |
| 23 | +import io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationPage; |
| 24 | +import io.gravitee.rest.api.management.v2.rest.model.PortalNavigationItem; |
| 25 | +import java.util.List; |
| 26 | +import java.util.UUID; |
| 27 | +import org.mapstruct.Mapper; |
| 28 | +import org.mapstruct.Mapping; |
| 29 | +import org.mapstruct.factory.Mappers; |
| 30 | + |
| 31 | +@Mapper |
| 32 | +public interface PortalNavigationItemsMapper { |
| 33 | + PortalNavigationItemsMapper INSTANCE = Mappers.getMapper(PortalNavigationItemsMapper.class); |
| 34 | + |
| 35 | + @Mapping(target = "type", constant = "PAGE") |
| 36 | + @Mapping( |
| 37 | + target = "configuration", |
| 38 | + expression = "java(new io.gravitee.rest.api.management.v2.rest.model.PortalNavigationPageAllOfConfiguration().portalPageContentId(page.getPortalPageContentId().id()))" |
| 39 | + ) |
| 40 | + io.gravitee.rest.api.management.v2.rest.model.PortalNavigationPage map( |
| 41 | + io.gravitee.apim.core.portal_page.model.PortalNavigationPage page |
| 42 | + ); |
| 43 | + |
| 44 | + @Mapping(target = "type", constant = "FOLDER") |
| 45 | + io.gravitee.rest.api.management.v2.rest.model.PortalNavigationFolder map( |
| 46 | + io.gravitee.apim.core.portal_page.model.PortalNavigationFolder folder |
| 47 | + ); |
| 48 | + |
| 49 | + @Mapping(target = "type", constant = "LINK") |
| 50 | + @Mapping( |
| 51 | + target = "configuration", |
| 52 | + expression = "java(new io.gravitee.rest.api.management.v2.rest.model.PortalNavigationLinkAllOfConfiguration().url(link.getUrl()))" |
| 53 | + ) |
| 54 | + io.gravitee.rest.api.management.v2.rest.model.PortalNavigationLink map( |
| 55 | + io.gravitee.apim.core.portal_page.model.PortalNavigationLink link |
| 56 | + ); |
| 57 | + |
| 58 | + default List<PortalNavigationItem> map(List<io.gravitee.apim.core.portal_page.model.PortalNavigationItem> items) { |
| 59 | + return items.stream().map(this::map).toList(); |
| 60 | + } |
| 61 | + |
| 62 | + default PortalNavigationItem map(io.gravitee.apim.core.portal_page.model.PortalNavigationItem portalNavigationItem) { |
| 63 | + return switch (portalNavigationItem) { |
| 64 | + case io.gravitee.apim.core.portal_page.model.PortalNavigationFolder folder -> new PortalNavigationItem(map(folder)); |
| 65 | + case io.gravitee.apim.core.portal_page.model.PortalNavigationPage page -> new PortalNavigationItem(map(page)); |
| 66 | + case io.gravitee.apim.core.portal_page.model.PortalNavigationLink link -> new PortalNavigationItem(map(link)); |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + @Mapping( |
| 71 | + target = "contentId", |
| 72 | + expression = "java(page.getContentId() == null ? null : io.gravitee.apim.core.portal_page.model.PortalPageContentId.of(page.getContentId().toString()))" |
| 73 | + ) |
| 74 | + io.gravitee.apim.core.portal_page.model.CreatePortalNavigationItem map( |
| 75 | + io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationPage page |
| 76 | + ); |
| 77 | + |
| 78 | + io.gravitee.apim.core.portal_page.model.CreatePortalNavigationItem map( |
| 79 | + io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationFolder folder |
| 80 | + ); |
| 81 | + |
| 82 | + @Mapping(target = "url", expression = "java(link.getUrl().toString())") |
| 83 | + io.gravitee.apim.core.portal_page.model.CreatePortalNavigationItem map( |
| 84 | + io.gravitee.rest.api.management.v2.rest.model.CreatePortalNavigationLink link |
| 85 | + ); |
| 86 | + |
| 87 | + default io.gravitee.apim.core.portal_page.model.CreatePortalNavigationItem map( |
| 88 | + BaseCreatePortalNavigationItem createPortalNavigationItem |
| 89 | + ) { |
| 90 | + return switch (createPortalNavigationItem) { |
| 91 | + case CreatePortalNavigationFolder folder -> map(folder); |
| 92 | + case CreatePortalNavigationPage page -> map(page); |
| 93 | + case CreatePortalNavigationLink link -> map(link); |
| 94 | + default -> throw new TechnicalDomainException( |
| 95 | + String.format("Unknown PortalNavigationItem class %s", createPortalNavigationItem.getClass().getSimpleName()) |
| 96 | + ); |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + default PortalNavigationItemId map(UUID id) { |
| 101 | + return id == null ? null : PortalNavigationItemId.of(id.toString()); |
| 102 | + } |
| 103 | + |
| 104 | + default String map(io.gravitee.apim.core.portal_page.model.PortalNavigationItemId id) { |
| 105 | + return id != null ? id.json() : null; |
| 106 | + } |
| 107 | +} |
0 commit comments