diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/DashboardRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/DashboardRepository.java index df5b2657cfa..da736f983a3 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/DashboardRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/DashboardRepository.java @@ -35,7 +35,7 @@ import org.openmetadata.catalog.util.EntityUtil; import org.openmetadata.catalog.util.EntityUtil.Fields; import org.openmetadata.catalog.util.JsonUtils; import org.openmetadata.catalog.util.RestUtil.PutResponse; -import org.openmetadata.common.utils.CipherText; +import org.openmetadata.catalog.util.ResultList; import org.skife.jdbi.v2.sqlobject.Bind; import org.skife.jdbi.v2.sqlobject.CreateSqlObject; import org.skife.jdbi.v2.sqlobject.SqlQuery; @@ -46,7 +46,9 @@ import javax.json.JsonPatch; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; +import java.text.ParseException; import java.util.ArrayList; import java.util.List; @@ -86,47 +88,48 @@ public abstract class DashboardRepository { @CreateSqlObject abstract TagRepository.TagDAO tagDAO(); + EntityRepository entityRepository = new EntityRepository() { + @Override + public List listAfter(String fqnPrefix, int limitParam, String after) { + return dashboardDAO().listAfter(fqnPrefix, limitParam, after); + } + + @Override + public List listBefore(String fqnPrefix, int limitParam, String before) { + return dashboardDAO().listBefore(fqnPrefix, limitParam, before); + } + + @Override + public int listCount(String fqnPrefix) { + return dashboardDAO().listCount(fqnPrefix); + } + + @Override + public String getFullyQualifiedName(Dashboard entity) { + return entity.getFullyQualifiedName(); + } + + @Override + public Dashboard setFields(Dashboard entity, Fields fields) throws IOException, ParseException { + return DashboardRepository.this.setFields(entity, fields); + } + + @Override + public ResultList getResultList(List entities, String beforeCursor, String afterCursor, int total) throws GeneralSecurityException, UnsupportedEncodingException { + return new DashboardList(entities, beforeCursor, afterCursor, total); + } + }; @Transaction - public DashboardList listAfter(Fields fields, String serviceName, int limitParam, String after) throws IOException, - GeneralSecurityException { - // forward scrolling, if after == null then first page is being asked being asked - List jsons = dashboardDAO().listAfter(serviceName, limitParam + 1, after == null ? "" : - CipherText.instance().decrypt(after)); - - List dashboards = new ArrayList<>(); - for (String json : jsons) { - dashboards.add(setFields(JsonUtils.readValue(json, Dashboard.class), fields)); - } - int total = dashboardDAO().listCount(serviceName); - - String beforeCursor, afterCursor = null; - beforeCursor = after == null ? null : dashboards.get(0).getFullyQualifiedName(); - if (dashboards.size() > limitParam) { // If extra result exists, then next page exists - return after cursor - dashboards.remove(limitParam); - afterCursor = dashboards.get(limitParam - 1).getFullyQualifiedName(); - } - return new DashboardList(dashboards, beforeCursor, afterCursor, total); + public ResultList listAfter(Fields fields, String serviceName, int limitParam, String after) throws IOException, + GeneralSecurityException, ParseException { + return EntityUtil.listAfter(entityRepository, Dashboard.class, fields, serviceName, limitParam, after); } @Transaction - public DashboardList listBefore(Fields fields, String serviceName, int limitParam, String before) - throws IOException, GeneralSecurityException { - // Reverse scrolling - Get one extra result used for computing before cursor - List jsons = dashboardDAO().listBefore(serviceName, limitParam + 1, CipherText.instance().decrypt(before)); - List dashboards = new ArrayList<>(); - for (String json : jsons) { - dashboards.add(setFields(JsonUtils.readValue(json, Dashboard.class), fields)); - } - int total = dashboardDAO().listCount(serviceName); - - String beforeCursor = null, afterCursor; - if (dashboards.size() > limitParam) { // If extra result exists, then previous page exists - return before cursor - dashboards.remove(0); - beforeCursor = dashboards.get(0).getFullyQualifiedName(); - } - afterCursor = dashboards.get(dashboards.size() - 1).getFullyQualifiedName(); - return new DashboardList(dashboards, beforeCursor, afterCursor, total); + public ResultList listBefore(Fields fields, String serviceName, int limitParam, String before) + throws IOException, GeneralSecurityException, ParseException { + return EntityUtil.listBefore(entityRepository, Dashboard.class, fields, serviceName, limitParam, before); } @Transaction diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/resources/dashboards/DashboardResource.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/resources/dashboards/DashboardResource.java index b7c74fdb395..9790e8158f3 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/resources/dashboards/DashboardResource.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/resources/dashboards/DashboardResource.java @@ -63,6 +63,7 @@ import javax.ws.rs.core.UriInfo; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; +import java.text.ParseException; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -134,7 +135,7 @@ public class DashboardResource { content = @Content(mediaType = "application/json", schema = @Schema(implementation = DashboardList.class))) }) - public DashboardList list(@Context UriInfo uriInfo, + public ResultList list(@Context UriInfo uriInfo, @Context SecurityContext securityContext, @Parameter(description = "Fields requested in the returned resource", schema = @Schema(type = "string", example = FIELDS)) @@ -154,11 +155,11 @@ public class DashboardResource { @Parameter(description = "Returns list of dashboards after this cursor", schema = @Schema(type = "string")) @QueryParam("after") String after - ) throws IOException, GeneralSecurityException { + ) throws IOException, GeneralSecurityException, ParseException { RestUtil.validateCursors(before, after); Fields fields = new Fields(FIELD_LIST, fieldsParam); - DashboardList dashboards; + ResultList dashboards; if (before != null) { // Reverse paging dashboards = dao.listBefore(fields, serviceParam, limitParam, before); // Ask for one extra entry } else { // Forward paging or first page diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/util/EntityUtil.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/util/EntityUtil.java index 374fdae187e..a2988a6892e 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/util/EntityUtil.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/util/EntityUtil.java @@ -658,7 +658,7 @@ public final class EntityUtil { public static ResultList listAfter(EntityRepository dao, Class clz, Fields fields, String prefixFqn, int limitParam, String after) throws IOException, ParseException, GeneralSecurityException { - // forward scrolling, if after == null then first page is being asked being asked + // forward scrolling, if after == null then first page is being asked List jsons = dao.listAfter(prefixFqn, limitParam + 1, after == null ? "" : CipherText.instance().decrypt(after));