mirror of
https://github.com/open-metadata/OpenMetadata.git
synced 2025-11-11 16:31:57 +00:00
ISSUE-805: Remove boilerplate code for listAfter and listBefore methods in DashboardRepository (#825)
This commit is contained in:
parent
bdd4fa8ce7
commit
9e6b1cb2ae
@ -35,7 +35,7 @@ import org.openmetadata.catalog.util.EntityUtil;
|
|||||||
import org.openmetadata.catalog.util.EntityUtil.Fields;
|
import org.openmetadata.catalog.util.EntityUtil.Fields;
|
||||||
import org.openmetadata.catalog.util.JsonUtils;
|
import org.openmetadata.catalog.util.JsonUtils;
|
||||||
import org.openmetadata.catalog.util.RestUtil.PutResponse;
|
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.Bind;
|
||||||
import org.skife.jdbi.v2.sqlobject.CreateSqlObject;
|
import org.skife.jdbi.v2.sqlobject.CreateSqlObject;
|
||||||
import org.skife.jdbi.v2.sqlobject.SqlQuery;
|
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;
|
||||||
import javax.ws.rs.core.Response.Status;
|
import javax.ws.rs.core.Response.Status;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.security.GeneralSecurityException;
|
import java.security.GeneralSecurityException;
|
||||||
|
import java.text.ParseException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -86,47 +88,48 @@ public abstract class DashboardRepository {
|
|||||||
@CreateSqlObject
|
@CreateSqlObject
|
||||||
abstract TagRepository.TagDAO tagDAO();
|
abstract TagRepository.TagDAO tagDAO();
|
||||||
|
|
||||||
|
EntityRepository<Dashboard> entityRepository = new EntityRepository<Dashboard>() {
|
||||||
|
@Override
|
||||||
|
public List<String> listAfter(String fqnPrefix, int limitParam, String after) {
|
||||||
|
return dashboardDAO().listAfter(fqnPrefix, limitParam, after);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> 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<Dashboard> getResultList(List<Dashboard> entities, String beforeCursor, String afterCursor, int total) throws GeneralSecurityException, UnsupportedEncodingException {
|
||||||
|
return new DashboardList(entities, beforeCursor, afterCursor, total);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
@Transaction
|
@Transaction
|
||||||
public DashboardList listAfter(Fields fields, String serviceName, int limitParam, String after) throws IOException,
|
public ResultList<Dashboard> listAfter(Fields fields, String serviceName, int limitParam, String after) throws IOException,
|
||||||
GeneralSecurityException {
|
GeneralSecurityException, ParseException {
|
||||||
// forward scrolling, if after == null then first page is being asked being asked
|
return EntityUtil.listAfter(entityRepository, Dashboard.class, fields, serviceName, limitParam, after);
|
||||||
List<String> jsons = dashboardDAO().listAfter(serviceName, limitParam + 1, after == null ? "" :
|
|
||||||
CipherText.instance().decrypt(after));
|
|
||||||
|
|
||||||
List<Dashboard> 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transaction
|
@Transaction
|
||||||
public DashboardList listBefore(Fields fields, String serviceName, int limitParam, String before)
|
public ResultList<Dashboard> listBefore(Fields fields, String serviceName, int limitParam, String before)
|
||||||
throws IOException, GeneralSecurityException {
|
throws IOException, GeneralSecurityException, ParseException {
|
||||||
// Reverse scrolling - Get one extra result used for computing before cursor
|
return EntityUtil.listBefore(entityRepository, Dashboard.class, fields, serviceName, limitParam, before);
|
||||||
List<String> jsons = dashboardDAO().listBefore(serviceName, limitParam + 1, CipherText.instance().decrypt(before));
|
|
||||||
List<Dashboard> 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transaction
|
@Transaction
|
||||||
|
|||||||
@ -63,6 +63,7 @@ import javax.ws.rs.core.UriInfo;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.security.GeneralSecurityException;
|
import java.security.GeneralSecurityException;
|
||||||
|
import java.text.ParseException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -134,7 +135,7 @@ public class DashboardResource {
|
|||||||
content = @Content(mediaType = "application/json",
|
content = @Content(mediaType = "application/json",
|
||||||
schema = @Schema(implementation = DashboardList.class)))
|
schema = @Schema(implementation = DashboardList.class)))
|
||||||
})
|
})
|
||||||
public DashboardList list(@Context UriInfo uriInfo,
|
public ResultList<Dashboard> list(@Context UriInfo uriInfo,
|
||||||
@Context SecurityContext securityContext,
|
@Context SecurityContext securityContext,
|
||||||
@Parameter(description = "Fields requested in the returned resource",
|
@Parameter(description = "Fields requested in the returned resource",
|
||||||
schema = @Schema(type = "string", example = FIELDS))
|
schema = @Schema(type = "string", example = FIELDS))
|
||||||
@ -154,11 +155,11 @@ public class DashboardResource {
|
|||||||
@Parameter(description = "Returns list of dashboards after this cursor",
|
@Parameter(description = "Returns list of dashboards after this cursor",
|
||||||
schema = @Schema(type = "string"))
|
schema = @Schema(type = "string"))
|
||||||
@QueryParam("after") String after
|
@QueryParam("after") String after
|
||||||
) throws IOException, GeneralSecurityException {
|
) throws IOException, GeneralSecurityException, ParseException {
|
||||||
RestUtil.validateCursors(before, after);
|
RestUtil.validateCursors(before, after);
|
||||||
Fields fields = new Fields(FIELD_LIST, fieldsParam);
|
Fields fields = new Fields(FIELD_LIST, fieldsParam);
|
||||||
|
|
||||||
DashboardList dashboards;
|
ResultList<Dashboard> dashboards;
|
||||||
if (before != null) { // Reverse paging
|
if (before != null) { // Reverse paging
|
||||||
dashboards = dao.listBefore(fields, serviceParam, limitParam, before); // Ask for one extra entry
|
dashboards = dao.listBefore(fields, serviceParam, limitParam, before); // Ask for one extra entry
|
||||||
} else { // Forward paging or first page
|
} else { // Forward paging or first page
|
||||||
|
|||||||
@ -658,7 +658,7 @@ public final class EntityUtil {
|
|||||||
public static <T> ResultList<T> listAfter(EntityRepository<T> dao, Class<T> clz, Fields fields, String prefixFqn,
|
public static <T> ResultList<T> listAfter(EntityRepository<T> dao, Class<T> clz, Fields fields, String prefixFqn,
|
||||||
int limitParam, String after)
|
int limitParam, String after)
|
||||||
throws IOException, ParseException, GeneralSecurityException {
|
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<String> jsons = dao.listAfter(prefixFqn, limitParam + 1, after == null ? "" :
|
List<String> jsons = dao.listAfter(prefixFqn, limitParam + 1, after == null ? "" :
|
||||||
CipherText.instance().decrypt(after));
|
CipherText.instance().decrypt(after));
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user