|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2025 Contributors to the openHAB project |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Eclipse Public License 2.0 which is available at |
| 9 | + * http://www.eclipse.org/legal/epl-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: EPL-2.0 |
| 12 | + */ |
| 13 | +package org.openhab.core.io.rest.core.fileformat; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Collection; |
| 17 | +import java.util.LinkedHashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Objects; |
| 21 | + |
| 22 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 23 | +import org.eclipse.jdt.annotation.Nullable; |
| 24 | +import org.openhab.core.items.GroupItem; |
| 25 | +import org.openhab.core.items.Item; |
| 26 | +import org.openhab.core.items.ItemBuilderFactory; |
| 27 | +import org.openhab.core.items.Metadata; |
| 28 | +import org.openhab.core.items.MetadataKey; |
| 29 | +import org.openhab.core.items.dto.GroupItemDTO; |
| 30 | +import org.openhab.core.items.dto.ItemDTO; |
| 31 | +import org.openhab.core.items.dto.ItemDTOMapper; |
| 32 | +import org.openhab.core.items.dto.MetadataDTO; |
| 33 | + |
| 34 | +/** |
| 35 | + * The {@link FileFormatItemDTOMapper} is a utility class to map items into file format item data transfer objects |
| 36 | + * (DTOs). |
| 37 | + * |
| 38 | + * @author Laurent Garnier - Initial contribution |
| 39 | + */ |
| 40 | +@NonNullByDefault |
| 41 | +public class FileFormatItemDTOMapper { |
| 42 | + |
| 43 | + /** |
| 44 | + * Maps item into file format item DTO object. |
| 45 | + * |
| 46 | + * @param item the item |
| 47 | + * @param metadata some metadata (including channel links) |
| 48 | + * @return file format item DTO object |
| 49 | + */ |
| 50 | + public static FileFormatItemDTO map(Item item, List<Metadata> metadata) { |
| 51 | + ItemDTO itemDto = ItemDTOMapper.map(item); |
| 52 | + FileFormatItemDTO dto = new FileFormatItemDTO(itemDto, itemDto instanceof GroupItemDTO); |
| 53 | + |
| 54 | + List<FileFormatChannelLinkDTO> hannelLinks = new ArrayList<>(); |
| 55 | + Map<String, MetadataDTO> metadataDTO = new LinkedHashMap<>(); |
| 56 | + metadata.forEach(md -> { |
| 57 | + if (item.getName().equals(md.getUID().getItemName())) { |
| 58 | + if ("channel".equals(md.getUID().getNamespace())) { |
| 59 | + hannelLinks.add(new FileFormatChannelLinkDTO(md.getValue(), |
| 60 | + md.getConfiguration().isEmpty() ? null : md.getConfiguration())); |
| 61 | + } else { |
| 62 | + MetadataDTO mdDTO = new MetadataDTO(); |
| 63 | + mdDTO.value = md.getValue(); |
| 64 | + mdDTO.config = md.getConfiguration().isEmpty() ? null : md.getConfiguration(); |
| 65 | + metadataDTO.put(md.getUID().getNamespace(), mdDTO); |
| 66 | + } |
| 67 | + } |
| 68 | + }); |
| 69 | + if (!hannelLinks.isEmpty()) { |
| 70 | + dto.channelLinks = hannelLinks; |
| 71 | + } |
| 72 | + if (!metadataDTO.isEmpty()) { |
| 73 | + dto.metadata = metadataDTO; |
| 74 | + } |
| 75 | + |
| 76 | + return dto; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Maps file format item DTO object into item. |
| 81 | + * |
| 82 | + * @param dto the file format item DTO object |
| 83 | + * @param itemBuilderFactory the item builder factory |
| 84 | + * @return item |
| 85 | + */ |
| 86 | + public static @Nullable Item map(FileFormatItemDTO dto, ItemBuilderFactory itemBuilderFactory) { |
| 87 | + if (GroupItem.TYPE.equals(dto.type)) { |
| 88 | + GroupItemDTO groupDto = new GroupItemDTO(); |
| 89 | + groupDto.type = dto.type; |
| 90 | + groupDto.name = dto.name; |
| 91 | + groupDto.label = dto.label; |
| 92 | + groupDto.category = dto.category; |
| 93 | + groupDto.tags = dto.tags; |
| 94 | + groupDto.groupNames = dto.groupNames; |
| 95 | + groupDto.groupType = dto.groupType; |
| 96 | + groupDto.function = dto.function; |
| 97 | + return ItemDTOMapper.map(groupDto, itemBuilderFactory); |
| 98 | + } |
| 99 | + return ItemDTOMapper.map(dto, itemBuilderFactory); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Maps file format item DTO object into a collection of metadata including channels links |
| 104 | + * provided through the "channel" namespace. |
| 105 | + * |
| 106 | + * @param dto the file format item DTO object |
| 107 | + * @return the collection of metadata |
| 108 | + */ |
| 109 | + public static Collection<Metadata> mapMetadata(FileFormatItemDTO dto) { |
| 110 | + String name = dto.name; |
| 111 | + Collection<Metadata> metadata = new ArrayList<>(); |
| 112 | + if (dto.channelLinks != null) { |
| 113 | + for (FileFormatChannelLinkDTO link : dto.channelLinks) { |
| 114 | + MetadataKey key = new MetadataKey("channel", name); |
| 115 | + metadata.add(new Metadata(key, link.channelUID, link.configuration)); |
| 116 | + } |
| 117 | + } |
| 118 | + if (dto.metadata != null) { |
| 119 | + for (Map.Entry<String, MetadataDTO> md : dto.metadata.entrySet()) { |
| 120 | + MetadataKey key = new MetadataKey(md.getKey(), name); |
| 121 | + metadata.add(new Metadata(key, Objects.requireNonNull(md.getValue().value), md.getValue().config)); |
| 122 | + } |
| 123 | + } |
| 124 | + return metadata; |
| 125 | + } |
| 126 | +} |
0 commit comments