Create plural version of deleteDataNode
[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 javax.persistence.EntityManager;
25 import javax.persistence.PersistenceContext;
26 import lombok.RequiredArgsConstructor;
27
28 @RequiredArgsConstructor
29 public class FragmentNativeRepositoryImpl implements FragmentNativeRepository {
30
31     private static final String DROP_FRAGMENT_CONSTRAINT
32             = "ALTER TABLE fragment DROP CONSTRAINT fragment_parent_id_fkey;";
33     private static final String ADD_FRAGMENT_CONSTRAINT_WITH_CASCADE
34             = "ALTER TABLE fragment ADD CONSTRAINT fragment_parent_id_fkey FOREIGN KEY (parent_id) "
35             + "REFERENCES fragment (id) ON DELETE CASCADE;";
36     private static final String ADD_ORIGINAL_FRAGMENT_CONSTRAINT
37             = "ALTER TABLE fragment ADD CONSTRAINT fragment_parent_id_fkey FOREIGN KEY (parent_id) "
38             + "REFERENCES fragment (id) ON DELETE NO ACTION;";
39
40     @PersistenceContext
41     private final EntityManager entityManager;
42
43     private final TempTableCreator tempTableCreator;
44
45     @Override
46     public void deleteFragmentEntity(final long fragmentEntityId) {
47         entityManager.createNativeQuery(
48                 DROP_FRAGMENT_CONSTRAINT
49                     + ADD_FRAGMENT_CONSTRAINT_WITH_CASCADE
50                     + "DELETE FROM fragment WHERE id = ?;"
51                     + DROP_FRAGMENT_CONSTRAINT
52                     + ADD_ORIGINAL_FRAGMENT_CONSTRAINT)
53             .setParameter(1, fragmentEntityId)
54             .executeUpdate();
55     }
56
57     @Override
58     // Accept security hotspot as temporary table name in SQL query is created internally, not from user input.
59     @SuppressWarnings("squid:S2077")
60     public void deleteByAnchorIdAndXpaths(final int anchorId, final Collection<String> xpaths) {
61         if (!xpaths.isEmpty()) {
62             final String tempTableName = tempTableCreator.createTemporaryTable("xpathsToDelete", xpaths, "xpath");
63             entityManager.createNativeQuery(
64                     DROP_FRAGMENT_CONSTRAINT
65                         + ADD_FRAGMENT_CONSTRAINT_WITH_CASCADE
66                         + "DELETE FROM fragment f USING " + tempTableName + " t"
67                         + " WHERE f.anchor_id = :anchorId AND f.xpath = t.xpath;"
68                         + DROP_FRAGMENT_CONSTRAINT
69                         + ADD_ORIGINAL_FRAGMENT_CONSTRAINT)
70                 .setParameter("anchorId", anchorId)
71                 .executeUpdate();
72         }
73     }
74
75     @Override
76     // Accept security hotspot as temporary table name in SQL query is created internally, not from user input.
77     @SuppressWarnings("squid:S2077")
78     public void deleteListsByAnchorIdAndXpaths(final int anchorId, final Collection<String> xpaths) {
79         if (!xpaths.isEmpty()) {
80             final String tempTableName = tempTableCreator.createTemporaryTable("xpathsToDelete", xpaths, "xpath");
81             entityManager.createNativeQuery(
82                 DROP_FRAGMENT_CONSTRAINT
83                     + ADD_FRAGMENT_CONSTRAINT_WITH_CASCADE
84                     + "DELETE FROM fragment f USING " + tempTableName + " t"
85                     + " WHERE f.anchor_id = :anchorId AND f.xpath LIKE CONCAT(t.xpath, :xpathListPattern);"
86                     + DROP_FRAGMENT_CONSTRAINT
87                     + ADD_ORIGINAL_FRAGMENT_CONSTRAINT)
88                 .setParameter("anchorId", anchorId)
89                 .setParameter("xpathListPattern", "[%%")
90                 .executeUpdate();
91         }
92     }
93
94 }