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
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.repository;
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;
31 @RequiredArgsConstructor
32 public class FragmentNativeRepositoryImpl implements FragmentNativeRepository {
34 private static final String DROP_FRAGMENT_CONSTRAINT
35 = "ALTER TABLE fragment DROP CONSTRAINT fragment_parent_id_fkey;";
36 private static final String ADD_FRAGMENT_CONSTRAINT_WITH_CASCADE
37 = "ALTER TABLE fragment ADD CONSTRAINT fragment_parent_id_fkey FOREIGN KEY (parent_id) "
38 + "REFERENCES fragment (id) ON DELETE CASCADE;";
39 private static final String ADD_ORIGINAL_FRAGMENT_CONSTRAINT
40 = "ALTER TABLE fragment ADD CONSTRAINT fragment_parent_id_fkey FOREIGN KEY (parent_id) "
41 + "REFERENCES fragment (id) ON DELETE NO ACTION;";
44 private final EntityManager entityManager;
47 public void deleteFragmentEntity(final long fragmentEntityId) {
48 entityManager.createNativeQuery(
49 addFragmentConstraintWithDeleteCascade("DELETE FROM fragment WHERE id = ?"))
50 .setParameter(1, fragmentEntityId)
55 public void deleteByAnchorIdAndXpaths(final int anchorId, final Collection<String> xpaths) {
56 final String queryString = addFragmentConstraintWithDeleteCascade(
57 "DELETE FROM fragment f WHERE f.anchor_id = ? AND (f.xpath IN (:parameterPlaceholders))");
58 executeUpdateWithAnchorIdAndCollection(queryString, anchorId, xpaths);
62 public void deleteListsByAnchorIdAndXpaths(final int anchorId, final Collection<String> listXpaths) {
63 final Collection<String> listXpathPatterns =
64 listXpaths.stream().map(listXpath -> listXpath + "[%").collect(Collectors.toSet());
65 final String queryString = addFragmentConstraintWithDeleteCascade(
66 "DELETE FROM fragment f WHERE f.anchor_id = ? AND (f.xpath LIKE ANY (array[:parameterPlaceholders]))");
67 executeUpdateWithAnchorIdAndCollection(queryString, anchorId, listXpathPatterns);
70 // Accept security hotspot as placeholders in SQL query are created internally, not from user input.
71 @SuppressWarnings("squid:S2077")
72 private void executeUpdateWithAnchorIdAndCollection(final String sqlTemplate, final int anchorId,
73 final Collection<String> collection) {
74 if (!collection.isEmpty()) {
75 final String parameterPlaceholders = String.join(",", Collections.nCopies(collection.size(), "?"));
76 final String queryStringWithParameterPlaceholders =
77 sqlTemplate.replaceFirst(":parameterPlaceholders\\b", parameterPlaceholders);
79 final Query query = entityManager.createNativeQuery(queryStringWithParameterPlaceholders);
80 query.setParameter(1, anchorId);
81 int parameterIndex = 2;
82 for (final String parameterValue : collection) {
83 query.setParameter(parameterIndex++, parameterValue);
85 query.executeUpdate();
89 private static String addFragmentConstraintWithDeleteCascade(final String queryString) {
90 return DROP_FRAGMENT_CONSTRAINT
91 + ADD_FRAGMENT_CONSTRAINT_WITH_CASCADE
93 + DROP_FRAGMENT_CONSTRAINT
94 + ADD_ORIGINAL_FRAGMENT_CONSTRAINT;