Merge "Use native query to delete data nodes"
[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.sql.PreparedStatement;
24 import javax.persistence.EntityManager;
25 import javax.persistence.PersistenceContext;
26 import org.hibernate.Session;
27 import org.springframework.stereotype.Repository;
28
29 @Repository
30 public class FragmentNativeRepositoryImpl implements FragmentNativeRepository {
31
32     private static final String DROP_FRAGMENT_CONSTRAINT
33             = "ALTER TABLE fragment DROP CONSTRAINT fragment_parent_id_fkey;";
34     private static final String ADD_FRAGMENT_CONSTRAINT_WITH_CASCADE
35             = "ALTER TABLE fragment ADD CONSTRAINT fragment_parent_id_fkey FOREIGN KEY (parent_id) "
36             + "REFERENCES fragment (id) ON DELETE CASCADE;";
37     private static final String DELETE_FRAGMENT = "DELETE FROM fragment WHERE id =?;";
38     private static final String ADD_ORIGINAL_FRAGMENT_CONSTRAINT
39             = "ALTER TABLE fragment ADD CONSTRAINT fragment_parent_id_fkey FOREIGN KEY (parent_id) "
40             + "REFERENCES fragment (id) ON DELETE NO ACTION;";
41
42     @PersistenceContext
43     private EntityManager entityManager;
44
45     @Override
46     public void deleteFragmentEntity(final long fragmentEntityId) {
47         final Session session = entityManager.unwrap(Session.class);
48         session.doWork(connection -> {
49             try (PreparedStatement preparedStatement = connection.prepareStatement(
50                     DROP_FRAGMENT_CONSTRAINT
51                             + ADD_FRAGMENT_CONSTRAINT_WITH_CASCADE
52                             + DELETE_FRAGMENT
53                             + DROP_FRAGMENT_CONSTRAINT
54                             + ADD_ORIGINAL_FRAGMENT_CONSTRAINT)) {
55                 preparedStatement.setLong(1, fragmentEntityId);
56                 preparedStatement.executeUpdate();
57             }
58         });
59     }
60 }
61