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);
50 private static FragmentEntity toFragmentEntity(final AnchorEntity anchorEntity,
51 final FragmentExtract fragmentExtract) {
52 final FragmentEntity fragmentEntity = new FragmentEntity();
53 fragmentEntity.setAnchor(anchorEntity);
54 fragmentEntity.setId(fragmentExtract.getId());
55 fragmentEntity.setXpath(fragmentExtract.getXpath());
56 fragmentEntity.setAttributes(fragmentExtract.getAttributes());
57 fragmentEntity.setParentId(fragmentExtract.getParentId());
58 fragmentEntity.setChildFragments(new HashSet<>());
59 fragmentEntity.setDataspace(anchorEntity.getDataspace());
60 return fragmentEntity;
63 private static Collection<FragmentEntity> reuniteChildrenWithTheirParents(
64 final Map<Long, FragmentEntity> fragmentEntityPerId) {
65 final Collection<FragmentEntity> fragmentEntitiesWithoutParentInResultSet = new HashSet<>();
66 for (final FragmentEntity fragmentEntity : fragmentEntityPerId.values()) {
67 final FragmentEntity parentFragmentEntity = fragmentEntityPerId.get(fragmentEntity.getParentId());
68 if (parentFragmentEntity == null) {
69 fragmentEntitiesWithoutParentInResultSet.add(fragmentEntity);
71 parentFragmentEntity.getChildFragments().add(fragmentEntity);
74 return fragmentEntitiesWithoutParentInResultSet;