ISSUE-805: Remove boilerplate code for listAfter and listBefore methods in DashboardRepository (#825)

This commit is contained in:
Vivek Ratnavel Subramanian 2021-10-17 15:51:00 -07:00 committed by GitHub
parent bdd4fa8ce7
commit 9e6b1cb2ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 41 deletions

View File

@ -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<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
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<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);
public ResultList<Dashboard> 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<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);
public ResultList<Dashboard> listBefore(Fields fields, String serviceName, int limitParam, String before)
throws IOException, GeneralSecurityException, ParseException {
return EntityUtil.listBefore(entityRepository, Dashboard.class, fields, serviceName, limitParam, before);
}
@Transaction

View File

@ -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<Dashboard> 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<Dashboard> dashboards;
if (before != null) { // Reverse paging
dashboards = dao.listBefore(fields, serviceParam, limitParam, before); // Ask for one extra entry
} else { // Forward paging or first page

View File

@ -658,7 +658,7 @@ public final class EntityUtil {
public static <T> ResultList<T> listAfter(EntityRepository<T> dao, Class<T> 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<String> jsons = dao.listAfter(prefixFqn, limitParam + 1, after == null ? "" :
CipherText.instance().decrypt(after));