CPS-314: Delete Dataspace
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / AnchorRepository.java
old mode 100644 (file)
new mode 100755 (executable)
index df665f9..471f175
@@ -1,6 +1,7 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2021 Pantheon.tech
+ *  Modifications Copyright (C) 2021 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -23,11 +24,36 @@ import java.util.Collection;
 import java.util.Optional;
 import javax.validation.constraints.NotNull;
 import org.onap.cps.spi.entities.AnchorEntity;
-import org.onap.cps.spi.entities.Dataspace;
+import org.onap.cps.spi.entities.DataspaceEntity;
+import org.onap.cps.spi.entities.SchemaSetEntity;
+import org.onap.cps.spi.exceptions.AnchorNotFoundException;
 import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
 
 public interface AnchorRepository extends JpaRepository<AnchorEntity, Integer> {
-    Optional<AnchorEntity> findByDataspaceAndName(@NotNull Dataspace dataspace, @NotNull String name);
 
-    Collection<AnchorEntity> findAllByDataspace(@NotNull Dataspace dataspace);
-}
+    Optional<AnchorEntity> findByDataspaceAndName(@NotNull DataspaceEntity dataspaceEntity, @NotNull String name);
+
+    default AnchorEntity getByDataspaceAndName(@NotNull DataspaceEntity dataspace,
+        @NotNull String anchorName) {
+        return findByDataspaceAndName(dataspace, anchorName)
+            .orElseThrow(() -> new AnchorNotFoundException(anchorName, dataspace.getName()));
+    }
+
+    Collection<AnchorEntity> findAllByDataspace(@NotNull DataspaceEntity dataspaceEntity);
+
+    Collection<AnchorEntity> findAllBySchemaSet(@NotNull SchemaSetEntity schemaSetEntity);
+
+    Integer countByDataspace(@NotNull DataspaceEntity dataspaceEntity);
+
+    @Query(value = "SELECT anchor.* FROM yang_resource\n"
+        + "JOIN schema_set_yang_resources ON schema_set_yang_resources.yang_resource_id = yang_resource.id\n"
+        + "JOIN schema_set ON schema_set.id = schema_set_yang_resources.schema_set_id\n"
+        + "JOIN anchor ON anchor.schema_set_id = schema_set.id\n"
+        + "WHERE schema_set.dataspace_id = :dataspaceId AND module_name IN (:moduleNames)\n"
+        + "GROUP BY anchor.id, anchor.name, anchor.dataspace_id, anchor.schema_set_id\n"
+        + "HAVING COUNT(DISTINCT module_name) = :sizeOfModuleNames", nativeQuery = true)
+    Collection<AnchorEntity> getAnchorsByDataspaceIdAndModuleNames(@Param("dataspaceId") int dataspaceId,
+        @Param("moduleNames") Collection<String> moduleNames, @Param("sizeOfModuleNames") int sizeOfModuleNames);
+}
\ No newline at end of file