/* * ============LICENSE_START======================================================= * Copyright (C) 2021-2023 Nordix Foundation. * Modifications Copyright (C) 2020-2021 Bell Canada. * Modifications Copyright (C) 2020-2021 Pantheon.tech. * Modifications Copyright (C) 2023 TechMahindra Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ package org.onap.cps.spi.repository; import java.util.Collection; import java.util.List; import java.util.Optional; import org.onap.cps.spi.entities.AnchorEntity; import org.onap.cps.spi.entities.DataspaceEntity; import org.onap.cps.spi.entities.FragmentEntity; import org.onap.cps.spi.exceptions.DataNodeNotFoundException; import org.onap.cps.spi.utils.EscapeUtils; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @Repository public interface FragmentRepository extends JpaRepository, FragmentRepositoryCpsPathQuery, FragmentPrefetchRepository { Optional findByAnchorAndXpath(AnchorEntity anchorEntity, String xpath); default FragmentEntity getByAnchorAndXpath(final AnchorEntity anchorEntity, final String xpath) { return findByAnchorAndXpath(anchorEntity, xpath).orElseThrow(() -> new DataNodeNotFoundException(anchorEntity.getDataspace().getName(), anchorEntity.getName(), xpath)); } @Query(value = "SELECT * FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)", nativeQuery = true) List findByAnchorIdAndXpathIn(@Param("anchorId") long anchorId, @Param("xpaths") String[] xpaths); default List findByAnchorAndXpathIn(final AnchorEntity anchorEntity, final Collection xpaths) { return findByAnchorIdAndXpathIn(anchorEntity.getId(), xpaths.toArray(new String[0])); } @Query(value = "SELECT * FROM fragment WHERE anchor_id = :anchorId \n" + "AND xpath LIKE :escapedXpath||'[@%]' AND xpath NOT LIKE :escapedXpath||'[@%]/%[@%]'", nativeQuery = true) List findListByAnchorIdAndEscapedXpath(@Param("anchorId") long anchorId, @Param("escapedXpath") String escapedXpath); default List findListByAnchorAndXpath(final AnchorEntity anchorEntity, final String xpath) { final String escapedXpath = EscapeUtils.escapeForSqlLike(xpath); return findListByAnchorIdAndEscapedXpath(anchorEntity.getId(), escapedXpath); } @Query(value = "SELECT fragment.* FROM fragment JOIN anchor ON anchor.id = fragment.anchor_id " + "WHERE dataspace_id = :dataspaceId AND xpath = ANY (:xpaths)", nativeQuery = true) List findByDataspaceIdAndXpathIn(@Param("dataspaceId") int dataspaceId, @Param("xpaths") String[] xpaths); default List findByDataspaceAndXpathIn(final DataspaceEntity dataspaceEntity, final Collection xpaths) { return findByDataspaceIdAndXpathIn(dataspaceEntity.getId(), xpaths.toArray(new String[0])); } @Query(value = "SELECT * FROM fragment WHERE anchor_id IN (:anchorIds)" + " AND xpath = ANY (:xpaths)", nativeQuery = true) List findByAnchorIdsAndXpathIn(@Param("anchorIds") Long[] anchorIds, @Param("xpaths") String[] xpaths); @Query(value = "SELECT * FROM fragment WHERE anchor_id = :anchorId LIMIT 1", nativeQuery = true) Optional findOneByAnchorId(@Param("anchorId") long anchorId); @Modifying @Query(value = "DELETE FROM fragment WHERE anchor_id = ANY (:anchorIds)", nativeQuery = true) void deleteByAnchorIdIn(@Param("anchorIds") long[] anchorIds); default void deleteByAnchorIn(final Collection anchorEntities) { deleteByAnchorIdIn(anchorEntities.stream().map(AnchorEntity::getId).mapToLong(id -> id).toArray()); } @Modifying @Query(value = "DELETE FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)", nativeQuery = true) void deleteByAnchorIdAndXpaths(@Param("anchorId") long anchorId, @Param("xpaths") String[] xpaths); default void deleteByAnchorIdAndXpaths(final long anchorId, final Collection xpaths) { deleteByAnchorIdAndXpaths(anchorId, xpaths.toArray(new String[0])); } @Modifying @Query(value = "DELETE FROM fragment f WHERE anchor_id = :anchorId AND xpath LIKE ANY (:xpathPatterns)", nativeQuery = true) void deleteByAnchorIdAndXpathLikeAny(@Param("anchorId") long anchorId, @Param("xpathPatterns") String[] xpathPatterns); default void deleteListsByAnchorIdAndXpaths(long anchorId, Collection xpaths) { deleteByAnchorIdAndXpathLikeAny(anchorId, xpaths.stream().map(xpath -> EscapeUtils.escapeForSqlLike(xpath) + "[@%").toArray(String[]::new)); } @Query(value = "SELECT xpath FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)", nativeQuery = true) List findAllXpathByAnchorIdAndXpathIn(@Param("anchorId") long anchorId, @Param("xpaths") String[] xpaths); default List findAllXpathByAnchorAndXpathIn(final AnchorEntity anchorEntity, final Collection xpaths) { return findAllXpathByAnchorIdAndXpathIn(anchorEntity.getId(), xpaths.toArray(new String[0])); } boolean existsByAnchorAndXpathStartsWith(AnchorEntity anchorEntity, String xpath); @Query(value = "SELECT * FROM fragment WHERE anchor_id = :anchorId AND parent_id IS NULL", nativeQuery = true) List findRootsByAnchorId(@Param("anchorId") long anchorId); }