2 * ============LICENSE_START=======================================================
\r
3 * Copyright (C) 2021-2023 Nordix Foundation.
\r
4 * Modifications Copyright (C) 2020-2021 Bell Canada.
\r
5 * Modifications Copyright (C) 2020-2021 Pantheon.tech.
\r
6 * Modifications Copyright (C) 2023 TechMahindra Ltd.
\r
7 * ================================================================================
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
20 * SPDX-License-Identifier: Apache-2.0
\r
21 * ============LICENSE_END=========================================================
\r
24 package org.onap.cps.spi.repository;
\r
26 import java.util.Collection;
\r
27 import java.util.List;
\r
28 import java.util.Optional;
\r
29 import org.onap.cps.spi.entities.AnchorEntity;
\r
30 import org.onap.cps.spi.entities.FragmentEntity;
\r
31 import org.onap.cps.spi.entities.FragmentExtract;
\r
32 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
\r
33 import org.springframework.data.jpa.repository.JpaRepository;
\r
34 import org.springframework.data.jpa.repository.Modifying;
\r
35 import org.springframework.data.jpa.repository.Query;
\r
36 import org.springframework.data.repository.query.Param;
\r
37 import org.springframework.stereotype.Repository;
\r
40 public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>, FragmentRepositoryCpsPathQuery,
\r
41 FragmentNativeRepository {
\r
43 Optional<FragmentEntity> findByAnchorAndXpath(AnchorEntity anchorEntity, String xpath);
\r
45 default FragmentEntity getByAnchorAndXpath(final AnchorEntity anchorEntity, final String xpath) {
\r
46 return findByAnchorAndXpath(anchorEntity, xpath).orElseThrow(() ->
\r
47 new DataNodeNotFoundException(anchorEntity.getDataspace().getName(), anchorEntity.getName(), xpath));
\r
50 boolean existsByAnchorId(int anchorId);
\r
52 @Query("SELECT f FROM FragmentEntity f WHERE anchor = :anchor")
\r
53 List<FragmentExtract> findAllExtractsByAnchor(@Param("anchor") AnchorEntity anchorEntity);
\r
55 List<FragmentEntity> findAllByAnchorAndXpathIn(AnchorEntity anchorEntity, Collection<String> xpath);
\r
57 List<FragmentEntity> findAllByXpathIn(Collection<String> xpath);
\r
60 @Query("DELETE FROM FragmentEntity WHERE anchor IN (:anchors)")
\r
61 void deleteByAnchorIn(@Param("anchors") Collection<AnchorEntity> anchorEntities);
\r
63 @Query("SELECT f FROM FragmentEntity f WHERE anchor = :anchor"
\r
64 + " AND (xpath = :parentXpath OR xpath LIKE CONCAT(:parentXpath,'/%'))")
\r
65 List<FragmentExtract> findByAnchorAndParentXpath(@Param("anchor") AnchorEntity anchorEntity,
\r
66 @Param("parentXpath") String parentXpath);
\r
68 @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId,"
\r
69 + " CAST(attributes AS TEXT) AS attributes"
\r
70 + " FROM FRAGMENT WHERE "
\r
71 + "( xpath = :parentXpath OR xpath LIKE CONCAT(:parentXpath,'/%') )",
\r
73 List<FragmentExtract> findByParentXpath(@Param("parentXpath") String parentXpath);
\r
75 @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId,"
\r
76 + " CAST(attributes AS TEXT) AS attributes"
\r
77 + " FROM FRAGMENT WHERE anchor_id = :anchorId"
\r
78 + " AND xpath ~ :xpathRegex",
\r
80 List<FragmentExtract> quickFindWithDescendants(@Param("anchorId") int anchorId,
\r
81 @Param("xpathRegex") String xpathRegex);
\r
83 @Query("SELECT xpath FROM FragmentEntity WHERE anchor = :anchor AND xpath IN :xpaths")
\r
84 List<String> findAllXpathByAnchorAndXpathIn(@Param("anchor") AnchorEntity anchorEntity,
\r
85 @Param("xpaths") Collection<String> xpaths);
\r
87 boolean existsByAnchorAndXpathStartsWith(AnchorEntity anchorEntity, String xpath);
\r
89 @Query("SELECT xpath FROM FragmentEntity WHERE anchor = :anchor AND parentId IS NULL")
\r
90 List<String> findAllXpathByAnchorAndParentIdIsNull(@Param("anchor") AnchorEntity anchorEntity);
\r
93 = "WITH RECURSIVE parent_search AS ("
\r
94 + " SELECT id, 0 AS depth "
\r
96 + " WHERE anchor_id = :anchorId AND xpath IN :xpaths "
\r
98 + " SELECT c.id, depth + 1 "
\r
99 + " FROM fragment c INNER JOIN parent_search p ON c.parent_id = p.id"
\r
100 + " WHERE depth <= (SELECT CASE WHEN :maxDepth = -1 THEN " + Integer.MAX_VALUE + " ELSE :maxDepth END) "
\r
102 + "SELECT f.id, anchor_id AS anchorId, xpath, f.parent_id AS parentId, CAST(attributes AS TEXT) AS attributes "
\r
103 + "FROM fragment f INNER JOIN parent_search p ON f.id = p.id",
\r
106 List<FragmentExtract> findExtractsWithDescendants(@Param("anchorId") int anchorId,
\r
107 @Param("xpaths") Collection<String> xpaths,
\r
108 @Param("maxDepth") int maxDepth);
\r
110 @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId,"
\r
111 + " CAST(attributes AS TEXT) AS attributes"
\r
112 + " FROM FRAGMENT WHERE xpath ~ :xpathRegex",
\r
113 nativeQuery = true)
\r
114 List<FragmentExtract> quickFindWithDescendantsAcrossAnchor(@Param("xpathRegex") String xpathRegex);
\r