Use cascade delete in fragments table
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / FragmentNativeRepositoryImpl.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.repository;
22
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.stream.Collectors;
26 import javax.persistence.EntityManager;
27 import javax.persistence.PersistenceContext;
28 import javax.persistence.Query;
29 import lombok.RequiredArgsConstructor;
30
31 @RequiredArgsConstructor
32 public class FragmentNativeRepositoryImpl implements FragmentNativeRepository {
33
34     @PersistenceContext
35     private final EntityManager entityManager;
36
37     @Override
38     public void deleteByAnchorIdAndXpaths(final int anchorId, final Collection<String> xpaths) {
39         final String queryString =
40             "DELETE FROM fragment f WHERE f.anchor_id = ? AND (f.xpath IN (:parameterPlaceholders))";
41         executeUpdateWithAnchorIdAndCollection(queryString, anchorId, xpaths);
42     }
43
44     @Override
45     public void deleteListsByAnchorIdAndXpaths(final int anchorId, final Collection<String> listXpaths) {
46         final Collection<String> listXpathPatterns =
47             listXpaths.stream().map(listXpath -> listXpath + "[%").collect(Collectors.toSet());
48         final String queryString =
49             "DELETE FROM fragment f WHERE f.anchor_id = ? AND (f.xpath LIKE ANY (array[:parameterPlaceholders]))";
50         executeUpdateWithAnchorIdAndCollection(queryString, anchorId, listXpathPatterns);
51     }
52
53     // Accept security hotspot as placeholders in SQL query are created internally, not from user input.
54     @SuppressWarnings("squid:S2077")
55     private void executeUpdateWithAnchorIdAndCollection(final String sqlTemplate, final int anchorId,
56                                                         final Collection<String> collection) {
57         if (!collection.isEmpty()) {
58             final String parameterPlaceholders = String.join(",", Collections.nCopies(collection.size(), "?"));
59             final String queryStringWithParameterPlaceholders =
60                 sqlTemplate.replaceFirst(":parameterPlaceholders\\b", parameterPlaceholders);
61
62             final Query query = entityManager.createNativeQuery(queryStringWithParameterPlaceholders);
63             query.setParameter(1, anchorId);
64             int parameterIndex = 2;
65             for (final String parameterValue : collection) {
66                 query.setParameter(parameterIndex++, parameterValue);
67             }
68             query.executeUpdate();
69         }
70     }
71
72 }