Modified pagination functionality

This commit is contained in:
Paul Trudel 2023-01-18 21:22:10 +00:00
parent b995f6a33d
commit f7fe6b9b5d
7 changed files with 104 additions and 51 deletions

View File

@ -547,26 +547,29 @@ public class MeetingService implements MessageListener {
return recordingService.isRecordingExist(recordId);
}
public String getRecordings2x(List<String> idList, List<String> states, Map<String, String> metadataFilters, String page, String size) {
int p;
int s;
public String getRecordings2x(List<String> idList, List<String> states, Map<String, String> metadataFilters, String offset, String limit) {
Pageable pageable = null;
int o = -1;
int l = -1;
try {
p = Integer.parseInt(page);
o = Integer.parseInt(offset);
if(o < 0) o = 0;
} catch(NumberFormatException e) {
p = 0;
log.info("Invalid offset parameter {}", offset);
o = 0;
}
try {
s = Integer.parseInt(size);
l = Integer.parseInt(limit);
if(l < 1) l = 1;
else if(l > 100) l = 100;
} catch(NumberFormatException e) {
s = 25;
log.info("Invalid limit parameter {}", limit);
}
log.info("{} {}", p, s);
Pageable pageable = PageRequest.of(p, s);
return recordingService.getRecordings2x(idList, states, metadataFilters, pageable);
if(l != -1) pageable = PageRequest.ofSize(l);
return recordingService.getRecordings2x(idList, states, metadataFilters, o, pageable);
}
public boolean existsAnyRecording(List<String> idList) {

View File

@ -37,7 +37,7 @@ public interface RecordingService {
String getCaptionTrackInboxDir();
String getCaptionsDir();
boolean isRecordingExist(String recordId);
String getRecordings2x(List<String> idList, List<String> states, Map<String, String> metadataFilters, Pageable pageable);
String getRecordings2x(List<String> idList, List<String> states, Map<String, String> metadataFilters, int offset, Pageable pageable);
boolean existAnyRecording(List<String> idList);
boolean changeState(String recordingId, String state);
void updateMetaParams(List<String> recordIDs, Map<String,String> metaParams);
@ -47,9 +47,9 @@ public interface RecordingService {
void processMakePresentationDownloadableMsg(MakePresentationDownloadableMsg msg);
File getDownloadablePresentationFile(String meetingId, String presId, String presFilename);
default <T> Page<T> listToPage(List<T> list, Pageable pageable) {
int start = (int) pageable.getOffset();
int end = (int) (Math.min((start + pageable.getPageSize()), list.size()));
return new PageImpl<>(list.subList(start, end), pageable, list.size());
// Construct page using offset and limit parameters
default <T> Page<T> listToPage(List<T> list, int offset, Pageable pageable) {
int end = (int) (Math.min((offset + pageable.getPageSize()), list.size()));
return new PageImpl<>(list.subList(offset, end), pageable, list.size());
}
}

View File

@ -16,4 +16,5 @@ public interface XmlService {
String constructResponseFromRecordingsXml(String xml);
String constructPaginatedResponse(Page<?> page, String response);
Recording xmlToRecording(String recordId, String xml);
String noRecordings();
}

View File

@ -72,7 +72,10 @@ public class RecordingServiceDbImpl implements RecordingService {
}
@Override
public String getRecordings2x(List<String> idList, List<String> states, Map<String, String> metadataFilters, Pageable pageable) {
public String getRecordings2x(List<String> idList, List<String> states, Map<String, String> metadataFilters, int offset, Pageable pageable) {
// If no IDs or limit were provided return no recordings instead of every recording
if(idList.isEmpty() && pageable == null) return xmlService.noRecordings();
logger.info("Retrieving all recordings");
Set<Recording> recordings = new HashSet<>();
recordings.addAll(dataStore.findAll(Recording.class));
@ -127,9 +130,16 @@ public class RecordingServiceDbImpl implements RecordingService {
}
logger.info("{} recordings remaining", recordings.size());
Page<Recording> recordingsPage = listToPage(new ArrayList<>(recordings), pageable);
// If no/invalid pagination parameters were given do not paginate the response
if(pageable == null) {
String recordingsXml = xmlService.recordingsToXml(recordings);
return xmlService.constructResponseFromRecordingsXml(recordingsXml);
}
Page<Recording> recordingsPage = listToPage(new ArrayList<>(recordings), offset, pageable);
String recordingsXml = xmlService.recordingsToXml(recordingsPage.getContent());
String response = xmlService.constructResponseFromRecordingsXml(recordingsXml);
return xmlService.constructPaginatedResponse(recordingsPage, response);
}

View File

@ -201,11 +201,18 @@ public class RecordingServiceFileImpl implements RecordingService {
return recordingServiceHelper.putRecordingTextTrack(track);
}
public String getRecordings2x(List<String> idList, List<String> states, Map<String, String> metadataFilters, Pageable pageable) {
public String getRecordings2x(List<String> idList, List<String> states, Map<String, String> metadataFilters, int offset, Pageable pageable) {
// If no IDs or limit were provided return no recordings instead of every recording
if(idList.isEmpty() && pageable == null) return xmlService.noRecordings();
List<RecordingMetadata> recsList = getRecordingsMetadata(idList, states);
ArrayList<RecordingMetadata> recs = filterRecordingsByMetadata(recsList, metadataFilters);
Page<RecordingMetadata> recordingsPage = listToPage(recs, pageable);
String response = recordingServiceHelper.getRecordings2x(recs);
// If no/invalid pagination parameters were given do not paginate the response
if(pageable == null) return recordingServiceHelper.getRecordings2x(recs);
Page<RecordingMetadata> recordingsPage = listToPage(recs, offset, pageable);
String response = recordingServiceHelper.getRecordings2x(new ArrayList<RecordingMetadata>(recordingsPage.getContent()));
return xmlService.constructPaginatedResponse(recordingsPage, response);
}

View File

@ -263,6 +263,38 @@ public class XmlServiceImpl implements XmlService {
return null;
}
@Override
public String noRecordings() {
logger.info("Constructing no recordings response");
try {
setup();
Document document = builder.newDocument();
Element rootElement = createElement(document, "response", null);
document.appendChild(rootElement);
Element returnCode = createElement(document, "returncode", "SUCCESS");
rootElement.appendChild(returnCode);
Element messageKey = createElement(document, "messageKey", "noRecordings");
rootElement.appendChild(messageKey);
Element message = createElement(document, "message", "No recordings found. This may occur if you attempt to retrieve all recordings.");
rootElement.appendChild(message);
String result = documentToString(document);
// logger.info("========== Result ==========");
// logger.info("{}", result);
// logger.info("============================");
return result;
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public String constructPaginatedResponse(Page<?> page, String response) {
logger.info("Constructing paginated response");
@ -291,14 +323,14 @@ public class XmlServiceImpl implements XmlService {
Element totalElements = createElement(document, "totalElements", String.valueOf(page.getTotalElements()));
pagination.appendChild(totalElements);
Element last = createElement(document, "last", String.valueOf(page.isLast()));
pagination.appendChild(last);
// Element last = createElement(document, "last", String.valueOf(page.isLast()));
// pagination.appendChild(last);
Element totalPages = createElement(document, "totalPages", String.valueOf(page.getTotalPages()));
pagination.appendChild(totalPages);
Element first = createElement(document, "first", String.valueOf(page.isFirst()));
pagination.appendChild(first);
// Element first = createElement(document, "first", String.valueOf(page.isFirst()));
// pagination.appendChild(first);
Element empty = createElement(document, "empty", String.valueOf(!page.hasContent()));
pagination.appendChild(empty);
@ -327,28 +359,28 @@ public class XmlServiceImpl implements XmlService {
Element rootElement = createElement(document, "pageable", null);
document.appendChild(rootElement);
Sort sort = pageable.getSort();
Element sortElement = createElement(document, "sort", null);
Element unsorted = createElement(document, "unsorted", String.valueOf(sort.isUnsorted()));
sortElement.appendChild(unsorted);
Element sorted = createElement(document, "sorted", String.valueOf(sort.isSorted()));
sortElement.appendChild(sorted);
Element empty = createElement(document, "empty", String.valueOf(sort.isEmpty()));
sortElement.appendChild(empty);
rootElement.appendChild(sortElement);
// Sort sort = pageable.getSort();
// Element sortElement = createElement(document, "sort", null);
//
// Element unsorted = createElement(document, "unsorted", String.valueOf(sort.isUnsorted()));
// sortElement.appendChild(unsorted);
//
// Element sorted = createElement(document, "sorted", String.valueOf(sort.isSorted()));
// sortElement.appendChild(sorted);
//
// Element empty = createElement(document, "empty", String.valueOf(sort.isEmpty()));
// sortElement.appendChild(empty);
//
// rootElement.appendChild(sortElement);
Element offset = createElement(document, "offset", String.valueOf(pageable.getOffset()));
rootElement.appendChild(offset);
Element pageSize = createElement(document, "pageSize", String.valueOf(pageable.getPageSize()));
rootElement.appendChild(pageSize);
Element limit = createElement(document, "limit", String.valueOf(pageable.getPageSize()));
rootElement.appendChild(limit);
Element pageNumber = createElement(document, "pageNumber", String.valueOf(pageable.getPageNumber()));
rootElement.appendChild(pageNumber);
// Element pageNumber = createElement(document, "pageNumber", String.valueOf(pageable.getPageNumber()));
// rootElement.appendChild(pageNumber);
Element paged = createElement(document, "paged", String.valueOf(pageable.isPaged()));
rootElement.appendChild(paged);

View File

@ -104,19 +104,19 @@ class RecordingController {
Map<String, String> metadataFilters = ParamsProcessorUtil.processMetaParam(params)
String page
if(!StringUtils.isEmpty(params.page)) {
page = params.page
log.info("Requested page [${page}]")
String offset
if(!StringUtils.isEmpty(params.offset)) {
offset = params.offset
log.info("Requested offset [${offset}]")
}
String size
if(!StringUtils.isEmpty(params.size)) {
size = params.size
log.info("Requested page size [${size}]")
String limit
if(!StringUtils.isEmpty(params.limit)) {
limit = params.limit
log.info("Requested item limit [${limit}]")
}
def getRecordingsResult = meetingService.getRecordings2x(internalRecordIds, states, metadataFilters, page, size)
def getRecordingsResult = meetingService.getRecordings2x(internalRecordIds, states, metadataFilters, offset, limit)
withFormat {
xml {