From 1841257597b963291a2c4b67a7b30b62367b0884 Mon Sep 17 00:00:00 2001 From: Josh Garde Date: Tue, 20 Aug 2024 14:31:20 -0700 Subject: [PATCH] Initial implementation of admin endpoints (#43) * Initial implementation of admin endpoints * Update changelog --- CHANGELOG.md | 1 + .../L2RasterProductController.java | 31 +++++++++++ .../l2rasterproduct/L2RasterProductQuery.java | 13 +++++ .../L2RasterProductQueryImpl.java | 51 ++++++++++++++++++- .../podaac/swodlr/user/UserController.java | 16 ++++++ src/main/resources/graphql/query.graphqls | 18 +++++++ 6 files changed, 128 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1eb792..7d62e92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added + - Implement two new endpoints: query/user and query/l2RasterProducts ### Deprecated ### Removed ### Fixed diff --git a/src/main/java/gov/nasa/podaac/swodlr/l2rasterproduct/L2RasterProductController.java b/src/main/java/gov/nasa/podaac/swodlr/l2rasterproduct/L2RasterProductController.java index 86ab8eb..dbd0d55 100644 --- a/src/main/java/gov/nasa/podaac/swodlr/l2rasterproduct/L2RasterProductController.java +++ b/src/main/java/gov/nasa/podaac/swodlr/l2rasterproduct/L2RasterProductController.java @@ -7,6 +7,7 @@ import gov.nasa.podaac.swodlr.status.StatusRepository; import gov.nasa.podaac.swodlr.user.User; import gov.nasa.podaac.swodlr.user.UserReference; +import gov.nasa.podaac.swodlr.user.UserRepository; import java.time.DateTimeException; import java.time.LocalDateTime; import java.time.format.DateTimeParseException; @@ -96,6 +97,36 @@ public L2RasterProduct invalidateProduct(@Argument UUID id) { return product; } + @PreAuthorize("hasRole(\"ROLE_Administrator\")") + @QueryMapping + public List l2RasterProducts( + @Argument Integer cycle, + @Argument Integer pass, + @Argument Integer scene, + @Argument Boolean outputGranuleExtentFlag, + @Argument GridType outputSamplingGridType, + @Argument Integer rasterResolution, + @Argument Integer utmZoneAdjust, + @Argument Integer mgrsBandAdjust, + @Argument UUID after, + @Argument int limit + ) { + List products = l2RasterProductRepository.findByParameters( + cycle, + pass, + scene, + outputGranuleExtentFlag, + outputSamplingGridType, + rasterResolution, + utmZoneAdjust, + mgrsBandAdjust, + after, + limit + ); + + return products; + } + @QueryMapping public L2RasterProduct l2RasterProduct(@Argument UUID id) { var result = l2RasterProductRepository.findById(id); diff --git a/src/main/java/gov/nasa/podaac/swodlr/l2rasterproduct/L2RasterProductQuery.java b/src/main/java/gov/nasa/podaac/swodlr/l2rasterproduct/L2RasterProductQuery.java index c2db059..e06452d 100644 --- a/src/main/java/gov/nasa/podaac/swodlr/l2rasterproduct/L2RasterProductQuery.java +++ b/src/main/java/gov/nasa/podaac/swodlr/l2rasterproduct/L2RasterProductQuery.java @@ -22,4 +22,17 @@ List findByUser( UUID after, int limit ); + + List findByParameters( + Integer cycle, + Integer pass, + Integer scene, + Boolean outputGranuleExtentFlag, + GridType outputSamplingGridType, + Integer rasterResolution, + Integer utmZoneAdjust, + Integer mgrsBandAdjust, + UUID after, + int limit + ); } diff --git a/src/main/java/gov/nasa/podaac/swodlr/l2rasterproduct/L2RasterProductQueryImpl.java b/src/main/java/gov/nasa/podaac/swodlr/l2rasterproduct/L2RasterProductQueryImpl.java index ba6d604..b00af39 100644 --- a/src/main/java/gov/nasa/podaac/swodlr/l2rasterproduct/L2RasterProductQueryImpl.java +++ b/src/main/java/gov/nasa/podaac/swodlr/l2rasterproduct/L2RasterProductQueryImpl.java @@ -75,8 +75,8 @@ public List findByUser( (:rasterResolution is NULL OR \"rasterResolution\" = :rasterResolution) AND (:utmZoneAdjust is NULL OR \"utmZoneAdjust\" = :utmZoneAdjust) AND (:mgrsBandAdjust is NULL OR \"mgrsBandAdjust\" = :mgrsBandAdjust) AND - (CAST(:beforeTimestamp as TIMESTAMP) is NULL OR \"L2RasterProducts\".timestamp <= :beforeTimestamp) AND - (CAST(:afterTimestamp as TIMESTAMP) is NULL OR \"L2RasterProducts\".timestamp >= :afterTimestamp) AND + (CAST(:beforeTimestamp as TIMESTAMP) is NULL OR \"ProductHistory\".timestamp <= :beforeTimestamp) AND + (CAST(:afterTimestamp as TIMESTAMP) is NULL OR \"ProductHistory\".timestamp >= :afterTimestamp) AND (\"ProductHistory\".\"requestedById\" = CAST(:userId as UUID)) AND ( (:after is NULL) @@ -105,4 +105,51 @@ public List findByUser( return query.getResultList(); } + + @Override + public List findByParameters( + Integer cycle, + Integer pass, + Integer scene, + Boolean outputGranuleExtentFlag, + GridType outputSamplingGridType, + Integer rasterResolution, + Integer utmZoneAdjust, + Integer mgrsBandAdjust, + UUID after, + int limit + ) { + @SuppressWarnings("LineLength") + String statement = + """ + SELECT \"L2RasterProducts\".* FROM \"L2RasterProducts\" + WHERE + (:cycle is NULL OR \"cycle\" = :cycle) AND + (:pass is NULL OR \"pass\" = :pass) AND + (:scene is NULL OR \"scene\" = :scene) AND + (:outputGranuleExtentFlag is NULL OR \"outputGranuleExtentFlag\" = :outputGranuleExtentFlag) AND + (:outputSamplingGridType is NULL OR \"outputSamplingGridType\" = :outputSamplingGridType) AND + (:rasterResolution is NULL OR \"rasterResolution\" = :rasterResolution) AND + (:utmZoneAdjust is NULL OR \"utmZoneAdjust\" = :utmZoneAdjust) AND + (:mgrsBandAdjust is NULL OR \"mgrsBandAdjust\" = :mgrsBandAdjust) AND + (:after is NULL OR \"id\" > CAST(:after as UUID)) + ORDER BY id DESC LIMIT :limit + """; + + Session session = entityManager.unwrap(Session.class); + Query query = session.createNativeQuery(statement, L2RasterProduct.class); + query.setParameter("cycle", cycle, IntegerType.INSTANCE); + query.setParameter("pass", pass, IntegerType.INSTANCE); + query.setParameter("scene", scene, IntegerType.INSTANCE); + query.setParameter("outputGranuleExtentFlag", outputGranuleExtentFlag, BooleanType.INSTANCE); + query.setParameter("outputSamplingGridType", outputSamplingGridType != null + ? outputSamplingGridType.toString() : null, StringType.INSTANCE); + query.setParameter("rasterResolution", rasterResolution, IntegerType.INSTANCE); + query.setParameter("utmZoneAdjust", utmZoneAdjust, IntegerType.INSTANCE); + query.setParameter("mgrsBandAdjust", mgrsBandAdjust, IntegerType.INSTANCE); + query.setParameter("after", after, UUIDCharType.INSTANCE); + query.setParameter("limit", limit, IntegerType.INSTANCE); + + return query.getResultList(); + } } diff --git a/src/main/java/gov/nasa/podaac/swodlr/user/UserController.java b/src/main/java/gov/nasa/podaac/swodlr/user/UserController.java index 9f217a2..b459458 100644 --- a/src/main/java/gov/nasa/podaac/swodlr/user/UserController.java +++ b/src/main/java/gov/nasa/podaac/swodlr/user/UserController.java @@ -1,10 +1,14 @@ package gov.nasa.podaac.swodlr.user; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.graphql.data.method.annotation.Argument; import org.springframework.graphql.data.method.annotation.ContextValue; import org.springframework.graphql.data.method.annotation.QueryMapping; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; +import gov.nasa.podaac.swodlr.exception.SwodlrException; + @Controller public class UserController { @Autowired @@ -14,4 +18,16 @@ public class UserController { public User currentUser(@ContextValue UserReference userRef) { return userRef.fetch(); } + + @PreAuthorize("hasRole(\"ROLE_Administrator\")") + @QueryMapping + public User user(@Argument String username) { + var result = userRepository.findByUsername(username); + if (result.isEmpty()) { + throw new SwodlrException("User not found"); + } + + User user = result.get(); + return user; + } } diff --git a/src/main/resources/graphql/query.graphqls b/src/main/resources/graphql/query.graphqls index 1aff073..ecf8098 100644 --- a/src/main/resources/graphql/query.graphqls +++ b/src/main/resources/graphql/query.graphqls @@ -4,4 +4,22 @@ type Query { statusByProduct(product: ID!, limit: Int = 10): [Status!] statusByPrevious(after: ID!, limit: Int = 10): [Status!] availableScene(cycle: Int!, pass: Int!, scene: Int!): Boolean! + + # -- Admin -- + user(username: String!): User + + l2RasterProducts( + cycle: Int, + pass: Int, + scene: Int, + outputGranuleExtentFlag: Boolean, + outputSamplingGridType: GridType, + rasterResolution: Int, + utmZoneAdjust: Int, + mgrsBandAdjust: Int, + + # Pagination + after: ID, + limit: Int = 10 + ): [L2RasterProduct!]! }