X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cps-ri%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fcps%2Fspi%2Frepository%2FFragmentRepository.java;h=a40168a9d65e0e80387c88f1a413f4cc75e3c82f;hb=760e597acc7854a736363a0136343b5a77462cfb;hp=ba83f15881bee6beca11a6fd876a65f023590b50;hpb=11fd57a2a13c7c34e219c6be24793871bee58ca3;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 ba83f1588..a40168a9d 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,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2020 Nordix Foundation. All rights reserved. + * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,10 +21,43 @@ package org.onap.cps.spi.repository; -import org.onap.cps.spi.entities.Fragment; +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import javax.validation.constraints.NotNull; +import org.checkerframework.checker.nullness.qual.NonNull; +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.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 { +public interface FragmentRepository extends JpaRepository { + + Optional findByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity, + @NonNull AnchorEntity anchorEntity, @NonNull String xpath); + + default FragmentEntity getByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity, + @NonNull AnchorEntity anchorEntity, @NonNull String xpath) { + return findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath) + .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName(), 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