use of org.springframework.data.domain.Pageable in project CzechIdMng by bcvsolutions.
the class IdentityReportExecutor method generateData.
@Override
protected IdmAttachmentDto generateData(RptReportDto report) {
// prepare temp file for json stream
File temp = getAttachmentManager().createTempFile();
//
try (FileOutputStream outputStream = new FileOutputStream(temp)) {
// write into json stream
JsonGenerator jGenerator = getMapper().getFactory().createGenerator(outputStream, JsonEncoding.UTF8);
try {
// json will be array of identities
jGenerator.writeStartArray();
// form instance has useful methods to transform form values
IdmFormInstanceDto formInstance = new IdmFormInstanceDto(report, getFormDefinition(), report.getFilter());
// initialize filter by given form - transform to multi value map
// => form attribute defined above will be automaticaly mapped to identity filter
IdmIdentityFilter filter = new IdmIdentityFilter(formInstance.toMultiValueMap());
// report extends long running task - show progress by count and counter lrt attributes
counter = 0L;
// find a first page of identities
Pageable pageable = PageRequest.of(0, 100, new Sort(Direction.ASC, IdmIdentity_.username.getName()));
do {
Page<IdmIdentityDto> identities = identityService.find(filter, pageable, IdmBasePermission.READ);
if (count == null) {
// report extends long running task - show progress by count and counter lrt attributes
count = identities.getTotalElements();
}
boolean canContinue = true;
for (Iterator<IdmIdentityDto> i = identities.iterator(); i.hasNext() && canContinue; ) {
// write single identity into json
getMapper().writeValue(jGenerator, i.next());
//
// supports cancel report generating (report extends long running task)
++counter;
canContinue = updateState();
}
// iterate while next page of identities is available
pageable = identities.hasNext() && canContinue ? identities.nextPageable() : null;
} while (pageable != null);
//
// close array of identities
jGenerator.writeEndArray();
} finally {
// close json stream
jGenerator.close();
}
// save create temp file with array of identities in json as attachment
return createAttachment(report, new FileInputStream(temp));
} catch (IOException ex) {
throw new ReportGenerateException(report.getName(), ex);
} finally {
FileUtils.deleteQuietly(temp);
}
}
use of org.springframework.data.domain.Pageable in project hub-alert by blackducksoftware.
the class DefaultRestApiAuditAccessorTest method getPageOfAuditEntriesTest.
@Test
public void getPageOfAuditEntriesTest() {
Integer pageNumber = 0;
int pageSize = 2;
String searchTerm = null;
String sortField = "lastSent";
String sortOrder = "ASC";
Boolean onlyShowSentNotifications = Boolean.TRUE;
String overallStatus = "overallStatusString";
String lastSent = DateUtils.createCurrentDateString(DateUtils.AUDIT_DATE_FORMAT);
AuditEntryRepository auditEntryRepository = Mockito.mock(AuditEntryRepository.class);
DefaultNotificationAccessor notificationManager = Mockito.mock(DefaultNotificationAccessor.class);
AuditNotificationRepository auditNotificationRepository = Mockito.mock(AuditNotificationRepository.class);
PageRequest pageRequest = PageRequest.of(0, 10);
Mockito.when(auditEntryRepository.findMatchingAudit(Mockito.anyLong(), Mockito.any(UUID.class))).thenReturn(Optional.empty());
Mockito.when(notificationManager.getPageRequestForNotifications(pageNumber, pageSize, sortField, sortOrder)).thenReturn(pageRequest);
// At least two AlertNotificationModel are required for the comparator
AlertNotificationModel alertNotificationModel = new AlertNotificationModel(1L, 1L, "provider-test", "providerConfigName-test", "notificationType-test", "{content: \"content is here...\"}", DateUtils.createCurrentDateTimestamp(), DateUtils.createCurrentDateTimestamp(), false);
AlertNotificationModel alertNotificationModel2 = new AlertNotificationModel(2L, 2L, "provider-test2", "providerConfigName-test2", "notificationType-test2", "{content: \"content is here2..\"}", DateUtils.createCurrentDateTimestamp().minusSeconds(15), DateUtils.createCurrentDateTimestamp().minusSeconds(10), false);
Pageable auditPageable = Mockito.mock(Pageable.class);
Mockito.when(auditPageable.getOffset()).thenReturn(pageNumber.longValue());
Mockito.when(auditPageable.getPageSize()).thenReturn(pageSize);
Page<AlertNotificationModel> auditPage = new PageImpl<>(List.of(alertNotificationModel, alertNotificationModel2), auditPageable, 1);
Mockito.when(notificationManager.findAll(pageRequest, onlyShowSentNotifications)).thenReturn(auditPage);
NotificationConfig notificationConfig = new NotificationConfig("3", "createdAtString", "providerString", 2L, "providerConfigNameString", "providerCreationTimeString", "notificationTypeString", "content-test");
AuditEntryModel auditEntryModel = new AuditEntryModel("2", notificationConfig, List.of(), overallStatus, lastSent);
Function<AlertNotificationModel, AuditEntryModel> notificationToAuditEntryConverter = (AlertNotificationModel notificationModel) -> auditEntryModel;
DefaultRestApiAuditAccessor auditUtility = new DefaultRestApiAuditAccessor(auditEntryRepository, auditNotificationRepository, null, null, notificationManager, null);
AuditEntryPageModel alertPagedModel = auditUtility.getPageOfAuditEntries(pageNumber, pageSize, searchTerm, sortField, sortOrder, onlyShowSentNotifications, notificationToAuditEntryConverter);
assertEquals(1, alertPagedModel.getTotalPages());
assertEquals(pageNumber.intValue(), alertPagedModel.getCurrentPage());
assertEquals(pageSize, alertPagedModel.getPageSize());
assertEquals(2, alertPagedModel.getContent().size());
assertEquals(lastSent, alertPagedModel.getContent().get(0).getLastSent());
assertEquals(lastSent, alertPagedModel.getContent().get(1).getLastSent());
AuditEntryModel auditContentTest = alertPagedModel.getContent().stream().findFirst().orElse(null);
assertEquals(auditEntryModel.getId(), auditContentTest.getId());
assertEquals(notificationConfig, auditContentTest.getNotification());
assertEquals(0, auditContentTest.getJobs().size());
assertEquals(overallStatus, auditContentTest.getOverallStatus());
assertEquals(lastSent, auditContentTest.getLastSent());
}
use of org.springframework.data.domain.Pageable in project spring-data-mongodb by spring-projects.
the class AggregationUtils method appendLimitAndOffsetIfPresent.
/**
* Append {@code $skip} and {@code $limit} aggregation stage if {@link ConvertingParameterAccessor#getSort()} is
* present.
*
* @param aggregationPipeline
* @param accessor
* @param offsetOperator
* @param limitOperator
* @since 3.3
*/
static void appendLimitAndOffsetIfPresent(List<AggregationOperation> aggregationPipeline, ConvertingParameterAccessor accessor, LongUnaryOperator offsetOperator, IntUnaryOperator limitOperator) {
Pageable pageable = accessor.getPageable();
if (pageable.isUnpaged()) {
return;
}
if (pageable.getOffset() > 0) {
aggregationPipeline.add(Aggregation.skip(offsetOperator.applyAsLong(pageable.getOffset())));
}
aggregationPipeline.add(Aggregation.limit(limitOperator.applyAsInt(pageable.getPageSize())));
}
use of org.springframework.data.domain.Pageable in project spring-data-mongodb by spring-projects.
the class NearQueryUnitTests method shouldTakeSkipAndLimitSettingsFromPageableEvenIfItWasSpecifiedOnQuery.
// DATAMONGO-445, DATAMONGO-2264
@Test
public void shouldTakeSkipAndLimitSettingsFromPageableEvenIfItWasSpecifiedOnQuery() {
int limit = 10;
int skip = 5;
Pageable pageable = PageRequest.of(3, 5);
NearQuery query = NearQuery.near(new Point(1, 1)).query(Query.query(Criteria.where("foo").is("bar")).limit(limit).skip(skip)).with(pageable);
assertThat(query.getSkip()).isEqualTo((long) pageable.getPageNumber() * pageable.getPageSize());
assertThat(query.toDocument().get("num")).isEqualTo((long) pageable.getPageSize());
}
use of org.springframework.data.domain.Pageable in project spring-data-mongodb by spring-projects.
the class AbstractMongoQueryUnitTests method slicedExecutionShouldIncrementLimitByOne.
// DATAMONGO-1057
@Test
void slicedExecutionShouldIncrementLimitByOne() {
MongoQueryFake query = createQueryForMethod("findByLastname", String.class, Pageable.class);
Pageable page1 = PageRequest.of(0, 10);
Pageable page2 = page1.next();
query.execute(new Object[] { "fake", page1 });
query.execute(new Object[] { "fake", page2 });
ArgumentCaptor<Query> captor = ArgumentCaptor.forClass(Query.class);
verify(executableFind, times(2)).as(Person.class);
verify(withQueryMock, times(2)).matching(captor.capture());
assertThat(captor.getAllValues().get(0).getLimit()).isEqualTo(11);
assertThat(captor.getAllValues().get(1).getLimit()).isEqualTo(11);
}
Aggregations