2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.spi.entities;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.HashSet;
27 import lombok.AccessLevel;
28 import lombok.NoArgsConstructor;
30 @NoArgsConstructor(access = AccessLevel.PRIVATE)
31 public class FragmentEntityArranger {
34 * Convert a collection of (related) FragmentExtracts into FragmentEntities (trees) with descendants.
36 * @param anchorEntity the anchor(entity) all the fragments belong to
37 * @param fragmentExtracts FragmentExtracts to convert
38 * @return a collection of FragmentEntities (trees) with descendants.
40 public static Collection<FragmentEntity> toFragmentEntityTrees(final AnchorEntity anchorEntity,
41 final Collection<FragmentExtract> fragmentExtracts) {
42 final Map<Long, FragmentEntity> fragmentEntityPerId = new HashMap<>();
43 for (final FragmentExtract fragmentExtract : fragmentExtracts) {
44 final FragmentEntity fragmentEntity = toFragmentEntity(anchorEntity, fragmentExtract);
45 fragmentEntityPerId.put(fragmentEntity.getId(), fragmentEntity);
47 return reuniteChildrenWithTheirParents(fragmentEntityPerId);
51 * Convert a collection of (related) FragmentExtracts into FragmentEntities (trees) with descendants.
53 * @param anchorEntityPerId the anchor(entities) the fragments belong to
54 * @param fragmentExtracts FragmentExtracts to convert
55 * @return a collection of FragmentEntities (trees) with descendants.
57 public static Collection<FragmentEntity> toFragmentEntityTreesAcrossAnchors(
58 final Map<Integer, AnchorEntity> anchorEntityPerId, final Collection<FragmentExtract> fragmentExtracts) {
59 final Map<Long, FragmentEntity> fragmentEntityPerId = new HashMap<>();
60 for (final FragmentExtract fragmentExtract : fragmentExtracts) {
61 final AnchorEntity anchorEntity = anchorEntityPerId.get(fragmentExtract.getAnchorId());
62 final FragmentEntity fragmentEntity = toFragmentEntity(anchorEntity, fragmentExtract);
63 fragmentEntityPerId.put(fragmentEntity.getId(), fragmentEntity);
65 return reuniteChildrenWithTheirParents(fragmentEntityPerId);
68 private static FragmentEntity toFragmentEntity(final AnchorEntity anchorEntity,
69 final FragmentExtract fragmentExtract) {
70 final FragmentEntity fragmentEntity = new FragmentEntity();
71 if (anchorEntity != null) {
72 fragmentEntity.setAnchor(anchorEntity);
73 fragmentEntity.setDataspace(anchorEntity.getDataspace());
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;