refactoring of Page model Integer --> int

This commit is contained in:
anhefti 2023-07-12 11:50:05 +02:00
parent 77646cd40e
commit 24416e7faa
13 changed files with 64 additions and 36 deletions

View file

@ -11,11 +11,14 @@ package ch.ethz.seb.sebserver.gbl.model;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import javax.validation.constraints.Size;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import ch.ethz.seb.sebserver.gbl.util.Utils; import ch.ethz.seb.sebserver.gbl.util.Utils;
import io.swagger.v3.oas.annotations.media.Schema;
/** Data class that defines a Page that corresponds with the SEB Server API page JSON object /** Data class that defines a Page that corresponds with the SEB Server API page JSON object
* *
@ -30,24 +33,36 @@ public final class Page<T> {
public static final String ATTR_COMPLETE = "complete"; public static final String ATTR_COMPLETE = "complete";
public static final String ATTR_CONTENT = "content"; public static final String ATTR_CONTENT = "content";
@Schema(description = "The number of available pages for the specified page size.")
@JsonProperty(ATTR_NUMBER_OF_PAGES) @JsonProperty(ATTR_NUMBER_OF_PAGES)
public final Integer numberOfPages; public final int numberOfPages;
@Schema(description = "The actual page number. Starting with 1.")
@Size(min = 1)
@JsonProperty(ATTR_PAGE_NUMBER) @JsonProperty(ATTR_PAGE_NUMBER)
public final Integer pageNumber; public final int pageNumber;
@Schema(description = "The the actual size of a page")
@Size(min = 1)
@JsonProperty(ATTR_PAGE_SIZE) @JsonProperty(ATTR_PAGE_SIZE)
public final Integer pageSize; public final int pageSize;
@Schema(description = "The page sort column name", nullable = true)
@JsonProperty(ATTR_SORT) @JsonProperty(ATTR_SORT)
public final String sort; public final String sort;
@JsonProperty(ATTR_COMPLETE)
public final boolean complete;
@Schema(description = "The actual content objects of the page. Might be empty.", nullable = false)
@JsonProperty(ATTR_CONTENT) @JsonProperty(ATTR_CONTENT)
public final List<T> content; public final List<T> content;
@JsonProperty(ATTR_COMPLETE)
public final boolean complete;
@JsonCreator @JsonCreator
public Page( public Page(
@JsonProperty(ATTR_NUMBER_OF_PAGES) final Integer numberOfPages, @JsonProperty(value = ATTR_NUMBER_OF_PAGES, required = true) final int numberOfPages,
@JsonProperty(ATTR_PAGE_NUMBER) final Integer pageNumber, @JsonProperty(value = ATTR_PAGE_NUMBER, required = true) final int pageNumber,
@JsonProperty(value = ATTR_PAGE_SIZE, required = true) final int pageSize,
@JsonProperty(ATTR_SORT) final String sort, @JsonProperty(ATTR_SORT) final String sort,
@JsonProperty(ATTR_CONTENT) final Collection<T> content, @JsonProperty(ATTR_CONTENT) final Collection<T> content,
@JsonProperty(ATTR_COMPLETE) final boolean complet) { @JsonProperty(ATTR_COMPLETE) final boolean complet) {
@ -55,35 +70,36 @@ public final class Page<T> {
this.numberOfPages = numberOfPages; this.numberOfPages = numberOfPages;
this.pageNumber = pageNumber; this.pageNumber = pageNumber;
this.content = Utils.immutableListOf(content); this.content = Utils.immutableListOf(content);
this.pageSize = content.size(); this.pageSize = pageSize;
this.sort = sort; this.sort = sort;
this.complete = complet; this.complete = complet;
} }
public Page( public Page(
final Integer numberOfPages, final int numberOfPages,
final Integer pageNumber, final int pageNumber,
final int pageSize,
final String sort, final String sort,
final Collection<T> content) { final Collection<T> content) {
this.numberOfPages = numberOfPages; this.numberOfPages = numberOfPages;
this.pageNumber = pageNumber; this.pageNumber = pageNumber;
this.content = Utils.immutableListOf(content); this.content = Utils.immutableListOf(content);
this.pageSize = content.size(); this.pageSize = pageSize;
this.sort = sort; this.sort = sort;
this.complete = true; this.complete = true;
} }
public int getNumberOfPages() { public int getNumberOfPages() {
return (this.numberOfPages != null) ? this.numberOfPages : 1; return this.numberOfPages;
} }
public int getPageNumber() { public int getPageNumber() {
return (this.pageNumber != null) ? this.pageNumber : 0; return this.pageNumber;
} }
public int getPageSize() { public int getPageSize() {
return (this.pageSize != null) ? this.pageSize : -1; return this.pageSize;
} }
public boolean isComplete() { public boolean isComplete() {

View file

@ -100,21 +100,21 @@ public class RemoteListPageSupplier<T> implements PageSupplier<T> {
: new ArrayList<>(collection); : new ArrayList<>(collection);
if (list.isEmpty()) { if (list.isEmpty()) {
return new Page<>(0, this.pageNumber, this.column, list); return new Page<>(0, this.pageNumber, this.pageSize, this.column, list);
} }
if (this.pageSize <= 0) { if (this.pageSize <= 0) {
return new Page<>(1, 1, this.column, list); return new Page<>(1, 1, list.size(), this.column, list);
} }
final int numOfPages = list.size() / this.pageSize; final int numOfPages = list.size() / this.pageSize;
if (numOfPages <= 0) { if (numOfPages <= 0) {
return new Page<>(1, 1, this.column, list); return new Page<>(1, 1, this.pageSize, this.column, list);
} }
final List<T> subList = list.subList(this.pageNumber * this.pageSize, final List<T> subList = list.subList(this.pageNumber * this.pageSize,
this.pageNumber * this.pageSize + this.pageSize); this.pageNumber * this.pageSize + this.pageSize);
return new Page<>(numOfPages, this.pageNumber, this.column, subList); return new Page<>(numOfPages, this.pageNumber, this.pageSize, this.column, subList);
}); });
} }
} }

View file

@ -94,16 +94,16 @@ public class StaticListPageSupplier<T> implements PageSupplier<T> {
public Result<Page<T>> getPage() { public Result<Page<T>> getPage() {
return Result.tryCatch(() -> { return Result.tryCatch(() -> {
if (this.list.isEmpty()) { if (this.list.isEmpty()) {
return new Page<>(0, this.pageNumber, this.column, this.list); return new Page<>(0, this.pageNumber, this.pageSize, this.column, this.list);
} }
if (this.pageSize <= 0) { if (this.pageSize <= 0) {
return new Page<>(1, 1, this.column, this.list); return new Page<>(1, 1, this.list.size(), this.column, this.list);
} }
int numOfPages = this.list.size() / this.pageSize; int numOfPages = this.list.size() / this.pageSize;
if (numOfPages <= 0) { if (numOfPages <= 0) {
return new Page<>(1, 1, this.column, this.list); return new Page<>(1, 1, this.pageSize, this.column, this.list);
} }
if (this.list.size() % this.pageSize > 0) { if (this.list.size() % this.pageSize > 0) {
numOfPages++; numOfPages++;
@ -119,7 +119,7 @@ public class StaticListPageSupplier<T> implements PageSupplier<T> {
} }
final List<T> subList = this.list.subList(from, to); final List<T> subList = this.list.subList(from, to);
return new Page<>(numOfPages, this.pageNumber, this.column, subList); return new Page<>(numOfPages, this.pageNumber, this.pageSize, this.column, subList);
}); });
} }
} }

View file

@ -139,6 +139,7 @@ public interface PaginationService {
return new Page<>( return new Page<>(
(numberOfPages > 0) ? numberOfPages : 1, (numberOfPages > 0) ? numberOfPages : 1,
_pageNumber, _pageNumber,
_pageSize,
sort, sort,
sorted.subList(start, end)); sorted.subList(start, end));
} }

View file

@ -143,6 +143,7 @@ public class PaginationServiceImpl implements PaginationService {
return new Page<>( return new Page<>(
page.getPages(), page.getPages(),
page.getPageNum(), page.getPageNum(),
getPageSize(pageSize),
sort, sort,
list); list);
}); });
@ -166,6 +167,7 @@ public class PaginationServiceImpl implements PaginationService {
return new Page<>( return new Page<>(
page.getPages(), page.getPages(),
page.getPageNum(), page.getPageNum(),
getPageSize(pageSize),
sort, sort,
list); list);
}); });

View file

@ -78,7 +78,7 @@ public interface QuizLookupService {
return lookupResult -> { return lookupResult -> {
final List<QuizData> quizzes = lookupResult.quizData; final List<QuizData> quizzes = lookupResult.quizData;
if (quizzes.isEmpty()) { if (quizzes.isEmpty()) {
return new Page<>(0, 1, sortAttribute, Collections.emptyList(), lookupResult.completed); return new Page<>(0, 1, pageSize, sortAttribute, Collections.emptyList(), lookupResult.completed);
} }
int start = (pageNumber - 1) * pageSize; int start = (pageNumber - 1) * pageSize;
@ -95,6 +95,7 @@ public interface QuizLookupService {
return new Page<>( return new Page<>(
(quizzes.size() <= pageSize) ? 1 : quizzes.size() / pageSize + 1, (quizzes.size() <= pageSize) ? 1 : quizzes.size() / pageSize + 1,
start / pageSize + 1, start / pageSize + 1,
pageSize,
sortAttribute, sortAttribute,
quizzes.subList(start, end)); quizzes.subList(start, end));
} }
@ -107,6 +108,7 @@ public interface QuizLookupService {
? quizzes.size() / pageSize + 1 ? quizzes.size() / pageSize + 1
: quizzes.size() / pageSize, : quizzes.size() / pageSize,
pageNumber, pageNumber,
pageSize,
sortAttribute, sortAttribute,
quizzes.subList(start, end), quizzes.subList(start, end),
lookupResult.completed); lookupResult.completed);

View file

@ -21,6 +21,7 @@ import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
@ -48,7 +49,7 @@ public class APIExceptionHandler extends ResponseEntityExceptionHandler {
@Override @Override
protected ResponseEntity<Object> handleExceptionInternal( protected ResponseEntity<Object> handleExceptionInternal(
final Exception ex, final Exception ex,
final Object body, @Nullable final Object body,
final HttpHeaders headers, final HttpHeaders headers,
final HttpStatus status, final HttpStatus status,
final WebRequest request) { final WebRequest request) {

View file

@ -434,11 +434,14 @@ public class ConfigurationNodeController extends EntityController<ConfigurationN
numOfPages++; numOfPages++;
} }
final List<TemplateAttribute> subList = attrs.subList(start, end);
return new Page<>( return new Page<>(
numOfPages, numOfPages,
this.paginationService.getPageNumber(pageNumber), this.paginationService.getPageNumber(pageNumber),
(pageSize != null) ? pageSize : subList.size(),
sort, sort,
attrs.subList(start, end)); subList);
} }
@RequestMapping( @RequestMapping(

View file

@ -242,7 +242,8 @@ public class ModelObjectJSONGenerator {
System.out.println(domainObject.getClass().getSimpleName() + ":"); System.out.println(domainObject.getClass().getSimpleName() + ":");
System.out.println(writerWithDefaultPrettyPrinter.writeValueAsString(domainObject)); System.out.println(writerWithDefaultPrettyPrinter.writeValueAsString(domainObject));
domainObject = new Page<>(3, 1, "columnName", Arrays.asList("Entry1", "Entry1", "Entry1", "Entry1", "Entry1")); domainObject =
new Page<>(3, 1, 5, "columnName", Arrays.asList("Entry1", "Entry1", "Entry1", "Entry1", "Entry1"));
System.out.println(domainObject.getClass().getSimpleName() + ":"); System.out.println(domainObject.getClass().getSimpleName() + ":");
System.out.println(writerWithDefaultPrettyPrinter.writeValueAsString(domainObject)); System.out.println(writerWithDefaultPrettyPrinter.writeValueAsString(domainObject));

View file

@ -40,7 +40,7 @@ public class InstitutionTest {
@Test @Test
public void pageOfInstituions() throws Exception { public void pageOfInstituions() throws Exception {
final Page<Institution> page = new Page<>(2, 1, "name", Arrays.asList( final Page<Institution> page = new Page<>(2, 1, 3, "name", Arrays.asList(
new Institution(1L, "InstOne", "one", "", "", true), new Institution(1L, "InstOne", "one", "", "", true),
new Institution(2L, "InstTwo", "two", "", "", true), new Institution(2L, "InstTwo", "two", "", "", true),
new Institution(3L, "InstThree", "three", "", "", true))); new Institution(3L, "InstThree", "three", "", "", true)));
@ -49,7 +49,7 @@ public class InstitutionTest {
//final ObjectWriter writerWithDefaultPrettyPrinter = jsonMapper.writerWithDefaultPrettyPrinter(); //final ObjectWriter writerWithDefaultPrettyPrinter = jsonMapper.writerWithDefaultPrettyPrinter();
String json = jsonMapper.writeValueAsString(page); String json = jsonMapper.writeValueAsString(page);
assertEquals( assertEquals(
"{\"number_of_pages\":2,\"page_number\":1,\"sort\":\"name\",\"content\":[{\"id\":1,\"name\":\"InstOne\",\"urlSuffix\":\"one\",\"logoImage\":\"\",\"themeName\":\"\",\"active\":true},{\"id\":2,\"name\":\"InstTwo\",\"urlSuffix\":\"two\",\"logoImage\":\"\",\"themeName\":\"\",\"active\":true},{\"id\":3,\"name\":\"InstThree\",\"urlSuffix\":\"three\",\"logoImage\":\"\",\"themeName\":\"\",\"active\":true}],\"complete\":true,\"page_size\":3}", "{\"number_of_pages\":2,\"page_number\":1,\"page_size\":3,\"sort\":\"name\",\"content\":[{\"id\":1,\"name\":\"InstOne\",\"urlSuffix\":\"one\",\"logoImage\":\"\",\"themeName\":\"\",\"active\":true},{\"id\":2,\"name\":\"InstTwo\",\"urlSuffix\":\"two\",\"logoImage\":\"\",\"themeName\":\"\",\"active\":true},{\"id\":3,\"name\":\"InstThree\",\"urlSuffix\":\"three\",\"logoImage\":\"\",\"themeName\":\"\",\"active\":true}],\"complete\":true}",
json); json);
final List<EntityName> namesList = page.content.stream() final List<EntityName> namesList = page.content.stream()

View file

@ -25,7 +25,7 @@ public class UserInfoTest {
@Test @Test
public void pageOfUserAccount() throws Exception { public void pageOfUserAccount() throws Exception {
final Page<UserInfo> page = new Page<>(2, 1, "name", Arrays.asList( final Page<UserInfo> page = new Page<>(2, 1, 3, "name", Arrays.asList(
new UserInfo("id1", 1L, new DateTime(0, DateTimeZone.UTC), "user1", "", "user1", "user1@inst2.none", new UserInfo("id1", 1L, new DateTime(0, DateTimeZone.UTC), "user1", "", "user1", "user1@inst2.none",
true, Locale.ENGLISH, true, Locale.ENGLISH,
DateTimeZone.UTC, DateTimeZone.UTC,
@ -43,7 +43,7 @@ public class UserInfoTest {
//final ObjectWriter writerWithDefaultPrettyPrinter = jsonMapper.writerWithDefaultPrettyPrinter(); //final ObjectWriter writerWithDefaultPrettyPrinter = jsonMapper.writerWithDefaultPrettyPrinter();
final String json = jsonMapper.writeValueAsString(page); final String json = jsonMapper.writeValueAsString(page);
assertEquals( assertEquals(
"{\"number_of_pages\":2,\"page_number\":1,\"sort\":\"name\",\"content\":[{\"uuid\":\"id1\",\"institutionId\":1,\"creationDate\":\"1970-01-01T00:00:00.000Z\",\"name\":\"user1\",\"surname\":\"\",\"username\":\"user1\",\"email\":\"user1@inst2.none\",\"active\":true,\"language\":\"en\",\"timezone\":\"UTC\",\"userRoles\":[\"EXAM_ADMIN\"]},{\"uuid\":\"id2\",\"institutionId\":3,\"creationDate\":\"1970-01-01T00:00:00.000Z\",\"name\":\"user2\",\"surname\":\"\",\"username\":\"user2\",\"email\":\"user2@inst2.none\",\"active\":true,\"language\":\"en\",\"timezone\":\"UTC\",\"userRoles\":[\"EXAM_ADMIN\"]},{\"uuid\":\"id3\",\"institutionId\":4,\"creationDate\":\"1970-01-01T00:00:00.000Z\",\"name\":\"user3\",\"surname\":\"\",\"username\":\"user3\",\"email\":\"user3@inst2.none\",\"active\":false,\"language\":\"de\",\"timezone\":\"UTC\",\"userRoles\":[\"EXAM_ADMIN\"]}],\"complete\":true,\"page_size\":3}", "{\"number_of_pages\":2,\"page_number\":1,\"page_size\":3,\"sort\":\"name\",\"content\":[{\"uuid\":\"id1\",\"institutionId\":1,\"creationDate\":\"1970-01-01T00:00:00.000Z\",\"name\":\"user1\",\"surname\":\"\",\"username\":\"user1\",\"email\":\"user1@inst2.none\",\"active\":true,\"language\":\"en\",\"timezone\":\"UTC\",\"userRoles\":[\"EXAM_ADMIN\"]},{\"uuid\":\"id2\",\"institutionId\":3,\"creationDate\":\"1970-01-01T00:00:00.000Z\",\"name\":\"user2\",\"surname\":\"\",\"username\":\"user2\",\"email\":\"user2@inst2.none\",\"active\":true,\"language\":\"en\",\"timezone\":\"UTC\",\"userRoles\":[\"EXAM_ADMIN\"]},{\"uuid\":\"id3\",\"institutionId\":4,\"creationDate\":\"1970-01-01T00:00:00.000Z\",\"name\":\"user3\",\"surname\":\"\",\"username\":\"user3\",\"email\":\"user3@inst2.none\",\"active\":false,\"language\":\"de\",\"timezone\":\"UTC\",\"userRoles\":[\"EXAM_ADMIN\"]}],\"complete\":true}",
json); json);
} }

View file

@ -400,7 +400,7 @@ public class InstitutionAPITest extends AdministrationAPIIntegrationTester {
}); });
assertNotNull(institutions); assertNotNull(institutions);
assertTrue(institutions.pageSize == 1); assertTrue(institutions.content.size() == 1);
assertEquals("[3]", getOrderedUUIDs(institutions.content)); assertEquals("[3]", getOrderedUUIDs(institutions.content));
} }

View file

@ -308,7 +308,7 @@ public class UserAPITest extends AdministrationAPIIntegrationTester {
assertNotNull(userInfos); assertNotNull(userInfos);
assertTrue(userInfos.numberOfPages == 2); assertTrue(userInfos.numberOfPages == 2);
assertNotNull(userInfos.content); assertNotNull(userInfos.content);
assertTrue(userInfos.pageSize == 1); assertTrue(userInfos.pageSize == 3);
assertTrue(userInfos.content.size() == 1); assertTrue(userInfos.content.size() == 1);
assertEquals("[user7]", getOrderedUUIDs(userInfos.content)); assertEquals("[user7]", getOrderedUUIDs(userInfos.content));
@ -329,6 +329,7 @@ public class UserAPITest extends AdministrationAPIIntegrationTester {
assertTrue(userInfos.pageNumber == 2); assertTrue(userInfos.pageNumber == 2);
assertNotNull(userInfos.content); assertNotNull(userInfos.content);
assertTrue(userInfos.content.size() == 1); assertTrue(userInfos.content.size() == 1);
assertTrue(userInfos.pageSize == 3);
assertEquals("[user7]", getOrderedUUIDs(userInfos.content)); assertEquals("[user7]", getOrderedUUIDs(userInfos.content));
// first page descending sort order // first page descending sort order
@ -1050,7 +1051,8 @@ public class UserAPITest extends AdministrationAPIIntegrationTester {
}); });
assertNotNull(usersPage); assertNotNull(usersPage);
assertTrue(usersPage.pageSize == 3); assertTrue(usersPage.content.size() == 3);
assertTrue(usersPage.pageSize == 10);
assertEquals("[user1, user2, user5]", getOrderedUUIDs(usersPage.content)); assertEquals("[user1, user2, user5]", getOrderedUUIDs(usersPage.content));
// all inactive of the own institution // all inactive of the own institution
@ -1064,7 +1066,7 @@ public class UserAPITest extends AdministrationAPIIntegrationTester {
}); });
assertNotNull(usersPage); assertNotNull(usersPage);
assertTrue(usersPage.pageSize == 0); assertTrue(usersPage.content.size() == 0);
assertEquals("[]", getOrderedUUIDs(usersPage.content)); assertEquals("[]", getOrderedUUIDs(usersPage.content));
// all active of institution 2 // all active of institution 2
@ -1080,7 +1082,7 @@ public class UserAPITest extends AdministrationAPIIntegrationTester {
}); });
assertNotNull(usersPage); assertNotNull(usersPage);
assertTrue(usersPage.pageSize == 3); assertTrue(usersPage.content.size() == 3);
assertEquals("[user3, user4, user7]", getOrderedUUIDs(usersPage.content)); assertEquals("[user3, user4, user7]", getOrderedUUIDs(usersPage.content));
// all inactive of institution 2 // all inactive of institution 2
@ -1096,7 +1098,7 @@ public class UserAPITest extends AdministrationAPIIntegrationTester {
}); });
assertNotNull(usersPage); assertNotNull(usersPage);
assertTrue(usersPage.pageSize == 1); assertTrue(usersPage.content.size() == 1);
assertEquals("[user6]", getOrderedUUIDs(usersPage.content)); assertEquals("[user6]", getOrderedUUIDs(usersPage.content));
} }