27891c525e7a9a655848ebc002f4f1a77f4c1b05
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / entities / FragmentEntityArranger.java
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.spi.entities;
22
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import lombok.AccessLevel;
28 import lombok.NoArgsConstructor;
29
30 @NoArgsConstructor(access = AccessLevel.PRIVATE)
31 public class FragmentEntityArranger {
32
33     /**
34      * Convert a collection of (related) FragmentExtracts into a FragmentEntity (tree) with descendants.
35      * Multiple top level nodes not yet support. If found only the first top level element is returned
36      *
37      * @param anchorEntity the anchor(entity) all the fragments belong to
38      * @param fragmentExtracts FragmentExtracts to convert
39      * @return a FragmentEntity (tree) with descendants, null if none found.
40      */
41     public static FragmentEntity toFragmentEntityTree(final AnchorEntity anchorEntity,
42                                                       final Collection<FragmentExtract> fragmentExtracts) {
43         final Map<Long, FragmentEntity> fragmentEntityPerId = new HashMap<>();
44         for (final FragmentExtract fragmentExtract : fragmentExtracts) {
45             final FragmentEntity fragmentEntity = toFragmentEntity(anchorEntity, fragmentExtract);
46             fragmentEntityPerId.put(fragmentEntity.getId(), fragmentEntity);
47         }
48         return reuniteChildrenWithTheirParents(fragmentEntityPerId);
49     }
50
51     private static FragmentEntity toFragmentEntity(final AnchorEntity anchorEntity,
52                                                    final FragmentExtract fragmentExtract) {
53         final FragmentEntity fragmentEntity = new FragmentEntity();
54         fragmentEntity.setAnchor(anchorEntity);
55         fragmentEntity.setId(fragmentExtract.getId());
56         fragmentEntity.setXpath(fragmentExtract.getXpath());
57         fragmentEntity.setAttributes(fragmentExtract.getAttributes());
58         fragmentEntity.setParentId(fragmentExtract.getParentId());
59         fragmentEntity.setChildFragments(new HashSet<>());
60         fragmentEntity.setDataspace(anchorEntity.getDataspace());
61         return fragmentEntity;
62     }
63
64     private static FragmentEntity reuniteChildrenWithTheirParents(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);
70             } else {
71                 parentFragmentEntity.getChildFragments().add(fragmentEntity);
72             }
73         }
74         return fragmentEntitiesWithoutParentInResultSet.stream().findFirst().orElse(null);
75     }
76
77 }