2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 Nordix Foundation
4 * Modifications Copyright (C) 2023 TechMahindra Ltd.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.spi.entities;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.HashSet;
28 import lombok.AccessLevel;
29 import lombok.NoArgsConstructor;
31 @NoArgsConstructor(access = AccessLevel.PRIVATE)
32 public class FragmentEntityArranger {
35 * Convert a collection of (related) FragmentExtracts into FragmentEntities (trees) with descendants.
37 * @param anchorEntity the anchor(entity) all the fragments belong to
38 * @param fragmentExtracts FragmentExtracts to convert
39 * @return a collection of FragmentEntities (trees) with descendants.
41 public static Collection<FragmentEntity> toFragmentEntityTrees(final AnchorEntity anchorEntity,
42 final Collection<FragmentExtract> fragmentExtracts) {
43 final Map<Long, FragmentEntity> fragmentEntityPerId = new HashMap<>();
44 if (fragmentExtracts != null) {
45 for (final FragmentExtract fragmentExtract : fragmentExtracts) {
46 final FragmentEntity fragmentEntity = toFragmentEntity(anchorEntity, fragmentExtract);
47 fragmentEntityPerId.put(fragmentEntity.getId(), fragmentEntity);
50 return reuniteChildrenWithTheirParents(fragmentEntityPerId);
54 * Convert a collection of (related) FragmentExtracts into FragmentEntities (trees) with descendants.
56 * @param anchorEntityPerId the anchor(entities) the fragments belong to
57 * @param fragmentExtracts FragmentExtracts to convert
58 * @return a collection of FragmentEntities (trees) with descendants.
60 public static Collection<FragmentEntity> toFragmentEntityTreesAcrossAnchors(
61 final Map<Long, AnchorEntity> anchorEntityPerId, final Collection<FragmentExtract> fragmentExtracts) {
62 final Map<Long, FragmentEntity> fragmentEntityPerId = new HashMap<>();
63 for (final FragmentExtract fragmentExtract : fragmentExtracts) {
64 final AnchorEntity anchorEntity = anchorEntityPerId.get(fragmentExtract.getAnchorId());
65 final FragmentEntity fragmentEntity = toFragmentEntity(anchorEntity, fragmentExtract);
66 fragmentEntityPerId.put(fragmentEntity.getId(), fragmentEntity);
68 return reuniteChildrenWithTheirParents(fragmentEntityPerId);
71 private static FragmentEntity toFragmentEntity(final AnchorEntity anchorEntity,
72 final FragmentExtract fragmentExtract) {
73 final FragmentEntity fragmentEntity = new FragmentEntity();
74 fragmentEntity.setAnchor(anchorEntity);
75 fragmentEntity.setId(fragmentExtract.getId());
76 fragmentEntity.setXpath(fragmentExtract.getXpath());
77 fragmentEntity.setAttributes(fragmentExtract.getAttributes());
78 fragmentEntity.setParentId(fragmentExtract.getParentId());
79 fragmentEntity.setChildFragments(new HashSet<>());
80 return fragmentEntity;
83 private static Collection<FragmentEntity> reuniteChildrenWithTheirParents(
84 final Map<Long, FragmentEntity> fragmentEntityPerId) {
85 final Collection<FragmentEntity> fragmentEntitiesWithoutParentInResultSet = new HashSet<>();
86 for (final FragmentEntity fragmentEntity : fragmentEntityPerId.values()) {
87 final FragmentEntity parentFragmentEntity = fragmentEntityPerId.get(fragmentEntity.getParentId());
88 if (parentFragmentEntity == null) {
89 fragmentEntitiesWithoutParentInResultSet.add(fragmentEntity);
91 parentFragmentEntity.getChildFragments().add(fragmentEntity);
94 return fragmentEntitiesWithoutParentInResultSet;