X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;ds=inline;f=cps-ri%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fcps%2Fspi%2Frepository%2FFragmentRepository.java;h=c48c79ef65988f325bccceae4a93dc6c1f90bf78;hb=refs%2Fchanges%2F41%2F127541%2F32;hp=a40168a9d65e0e80387c88f1a413f4cc75e3c82f;hpb=f6afc1727aa7e06754cb617b6e4964391eb72664;p=cps.git diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java index a40168a9d..c48c79ef6 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java @@ -1,7 +1,8 @@ -/*- +/* * ============LICENSE_START======================================================= - * Copyright (C) 2020 Nordix Foundation. All rights reserved. - * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. + * Copyright (C) 2020-2021 Nordix Foundation. + * Modifications Copyright (C) 2020-2021 Bell Canada. + * Modifications Copyright (C) 2020-2021 Pantheon.tech. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,27 +38,35 @@ import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @Repository -public interface FragmentRepository extends JpaRepository { +public interface FragmentRepository extends JpaRepository, FragmentRepositoryCpsPathQuery { Optional findByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity, - @NonNull AnchorEntity anchorEntity, @NonNull String xpath); + @NonNull AnchorEntity anchorEntity, + @NonNull String xpath); default FragmentEntity getByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity, - @NonNull AnchorEntity anchorEntity, @NonNull String xpath) { + @NonNull AnchorEntity anchorEntity, + @NonNull String xpath) { return findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath) .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName(), xpath)); } + @Query( + value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND dataspace_id = :dataspace AND parent_id is NULL", + nativeQuery = true) + List findRootsByDataspaceAndAnchor(@Param("dataspace") int dataspaceId, + @Param("anchor") int anchorId); + + default FragmentEntity findFirstRootByDataspaceAndAnchor(@NonNull DataspaceEntity dataspaceEntity, + @NonNull AnchorEntity anchorEntity) { + return findRootsByDataspaceAndAnchor(dataspaceEntity.getId(), anchorEntity.getId()).stream().findFirst() + .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName())); + } + + List findAllByAnchorAndXpathIn(@NonNull AnchorEntity anchorEntity, + @NonNull Collection xpath); + @Modifying @Query("DELETE FROM FragmentEntity fe WHERE fe.anchor IN (:anchors)") void deleteByAnchorIn(@NotNull @Param("anchors") Collection anchorEntities); - - @Query(value = - "SELECT * FROM FRAGMENT WHERE (anchor_id = :anchor) AND (xpath = (:xpath) OR xpath LIKE " - + "CONCAT(:xpath,'\\[@%]')) AND attributes @> jsonb_build_object(:leafName , :leafValue)", - nativeQuery = true) - // Above query will match an xpath with or without the index for a list [@key=value] - // and match anchor id, leaf name and leaf value - List getByAnchorAndXpathAndLeafAttributes(@Param("anchor") int anchorId, @Param("xpath") - String xpathPrefix, @Param("leafName") String leafName, @Param("leafValue") Object leafValue); -} \ No newline at end of file +}