Remove dataspace_id column from Fragment table
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / entities / FragmentEntityArranger.java
1 /*
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.spi.entities;
23
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Map;
28 import lombok.AccessLevel;
29 import lombok.NoArgsConstructor;
30
31 @NoArgsConstructor(access = AccessLevel.PRIVATE)
32 public class FragmentEntityArranger {
33
34     /**
35      * Convert a collection of (related) FragmentExtracts into  FragmentEntities (trees) with descendants.
36      *
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.
40      */
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);
48             }
49         }
50         return reuniteChildrenWithTheirParents(fragmentEntityPerId);
51     }
52
53     /**
54      * Convert a collection of (related) FragmentExtracts into  FragmentEntities (trees) with descendants.
55      *
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.
59      */
60     public static Collection<FragmentEntity> toFragmentEntityTreesAcrossAnchors(
61             final Map<Integer, 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);
67         }
68         return reuniteChildrenWithTheirParents(fragmentEntityPerId);
69     }
70
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;
81     }
82
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);
90             } else {
91                 parentFragmentEntity.getChildFragments().add(fragmentEntity);
92             }
93         }
94         return fragmentEntitiesWithoutParentInResultSet;
95     }
96
97 }