List of usage examples for org.springframework.data.domain Page hasNext
boolean hasNext();
From source file:org.yukung.daguerreo.domain.repository.BasicJooqRepositoryTest.java
@Test public void findAllByPageable() throws Exception { // given// w ww.j a v a2s . c o m dbSetupTracker.skipNextLaunch(); // when PageRequest page1 = new PageRequest(0, 2); PageRequest page2 = new PageRequest(1, 2); Page<BookApi> bookApis1 = repository.findAll(page1); Page<BookApi> bookApis2 = repository.findAll(page2); // then assertThat(bookApis1).isNotNull().isNotEmpty().hasSize(2).extracting("id", "name", "url").containsExactly( tuple(1, "Amazon Product Advertising API", "https://ecs.amazonaws.jp/onca/xml"), tuple(2, "Google Books API", "https://www.googleapis.com/books/v1/volumes")); assertThat(bookApis1.getNumber()).isEqualTo(0); assertThat(bookApis1.getNumberOfElements()).isEqualTo(2); assertThat(bookApis1.getSize()).isEqualTo(2); assertThat(bookApis1.getTotalPages()).isEqualTo(2); assertThat(bookApis1.getTotalElements()).isEqualTo(3); assertThat(bookApis1.isFirst()).isTrue(); assertThat(bookApis1.isLast()).isFalse(); assertThat(bookApis1.hasNext()).isTrue(); assertThat(bookApis1.hasPrevious()).isFalse(); assertThat(bookApis2).isNotNull().isNotEmpty().hasSize(1).extracting("id", "name", "url") .containsExactly(tuple(3, "?API", "https://app.rakuten.co.jp/services/api/BooksBook/Search/20130522")); assertThat(bookApis2.getNumber()).isEqualTo(1); assertThat(bookApis2.getNumberOfElements()).isEqualTo(1); assertThat(bookApis2.getSize()).isEqualTo(2); assertThat(bookApis2.getTotalPages()).isEqualTo(2); assertThat(bookApis2.getTotalElements()).isEqualTo(3); assertThat(bookApis2.isFirst()).isFalse(); assertThat(bookApis2.isLast()).isTrue(); assertThat(bookApis2.hasNext()).isFalse(); assertThat(bookApis2.hasPrevious()).isTrue(); }
From source file:org.yukung.daguerreo.domain.repository.BasicJooqRepositoryTest.java
@Test public void findAllByPageableAndDescendingSort() throws Exception { // given// w w w . ja va2 s . com dbSetupTracker.skipNextLaunch(); // when PageRequest page1 = new PageRequest(0, 2, Sort.Direction.DESC, "id"); PageRequest page2 = new PageRequest(1, 2, Sort.Direction.DESC, "id"); Page<BookApi> bookApis1 = repository.findAll(page1); Page<BookApi> bookApis2 = repository.findAll(page2); // then assertThat(bookApis1).isNotNull().isNotEmpty().hasSize(2).extracting("id", "name", "url").containsExactly( tuple(3, "?API", "https://app.rakuten.co.jp/services/api/BooksBook/Search/20130522"), tuple(2, "Google Books API", "https://www.googleapis.com/books/v1/volumes")); assertThat(bookApis1.getNumber()).isEqualTo(0); assertThat(bookApis1.getNumberOfElements()).isEqualTo(2); assertThat(bookApis1.getSize()).isEqualTo(2); assertThat(bookApis1.getTotalPages()).isEqualTo(2); assertThat(bookApis1.getTotalElements()).isEqualTo(3); assertThat(bookApis1.isFirst()).isTrue(); assertThat(bookApis1.isLast()).isFalse(); assertThat(bookApis1.hasNext()).isTrue(); assertThat(bookApis1.hasPrevious()).isFalse(); assertThat(bookApis2).isNotNull().isNotEmpty().hasSize(1).extracting("id", "name", "url") .containsExactly(tuple(1, "Amazon Product Advertising API", "https://ecs.amazonaws.jp/onca/xml")); assertThat(bookApis2.getNumber()).isEqualTo(1); assertThat(bookApis2.getNumberOfElements()).isEqualTo(1); assertThat(bookApis2.getSize()).isEqualTo(2); assertThat(bookApis2.getTotalPages()).isEqualTo(2); assertThat(bookApis2.getTotalElements()).isEqualTo(3); assertThat(bookApis2.isFirst()).isFalse(); assertThat(bookApis2.isLast()).isTrue(); assertThat(bookApis2.hasNext()).isFalse(); assertThat(bookApis2.hasPrevious()).isTrue(); }
From source file:org.yukung.daguerreo.domain.repository.BasicJooqRepositoryTest.java
@Test public void findAllByPageableNothing() throws Exception { // given/*from ww w .jav a 2 s . c om*/ dbSetupTracker.skipNextLaunch(); Pageable nothing = null; // when Page<BookApi> bookApis = repository.findAll(nothing); // then assertThat(bookApis).isNotNull().isNotEmpty().hasSize(3).extracting("id", "name", "url").containsExactly( tuple(1, "Amazon Product Advertising API", "https://ecs.amazonaws.jp/onca/xml"), tuple(2, "Google Books API", "https://www.googleapis.com/books/v1/volumes"), tuple(3, "?API", "https://app.rakuten.co.jp/services/api/BooksBook/Search/20130522")); assertThat(bookApis.getNumber()).isEqualTo(0); assertThat(bookApis.getNumberOfElements()).isEqualTo(3); assertThat(bookApis.getSize()).isEqualTo(0); assertThat(bookApis.getTotalPages()).isEqualTo(1); assertThat(bookApis.getTotalElements()).isEqualTo(3); assertThat(bookApis.isFirst()).isTrue(); assertThat(bookApis.isLast()).isTrue(); assertThat(bookApis.hasNext()).isFalse(); assertThat(bookApis.hasPrevious()).isFalse(); }
From source file:com.daphne.es.showcase.excel.service.ExcelDataService.java
/** * excel 2003// w w w. ja v a 2 s. c o m * 1????XML * 2??????excel??????? * 3????testExportExcel2003_3 * <p/> * ? * <p/> * ?html??sheet * @param user * @param contextRootPath * @param searchable */ @Async public void exportExcel2003WithXml(final User user, final String contextRootPath, final Searchable searchable) { int perSheetRows = 60000; //?sheet 6w? int totalSheets = 0; int totalRows = 0; Long maxId = 0L; String templateEncoding = "utf-8"; String fileName = generateFilename(user, contextRootPath, "xls"); File file = new File(fileName); BufferedOutputStream out = null; try { long beginTime = System.currentTimeMillis(); String workBookHeader = IOUtils .toString(ExcelDataService.class.getResourceAsStream("excel_2003_xml_workbook_header.txt")); String workBookFooter = IOUtils .toString(ExcelDataService.class.getResourceAsStream("excel_2003_xml_workbook_footer.txt")); String sheetHeader = IOUtils .toString(ExcelDataService.class.getResourceAsStream("excel_2003_xml_sheet_header.txt")); String sheetFooter = IOUtils .toString(ExcelDataService.class.getResourceAsStream("excel_2003_xml_sheet_footer.txt")); String rowTemplate = IOUtils .toString(ExcelDataService.class.getResourceAsStream("excel_2003_xml_row.txt")); out = new BufferedOutputStream(new FileOutputStream(file)); out.write(workBookHeader.getBytes(templateEncoding)); while (true) { totalSheets++; out.write(sheetHeader.replace("{sheetName}", "Sheet" + totalSheets).getBytes(templateEncoding)); Page<ExcelData> page = null; totalRows = 1; do { searchable.setPage(0, pageSize); // if (!searchable.containsSearchKey("id_in")) { searchable.addSearchFilter("id", SearchOperator.gt, maxId); } page = findAll(searchable); for (ExcelData data : page.getContent()) { out.write(rowTemplate.replace("{id}", String.valueOf(data.getId())) .replace("{content}", data.getContent()).getBytes(templateEncoding)); maxId = Math.max(maxId, data.getId()); totalRows++; } //clear entity manager RepositoryHelper.clear(); } while (page.hasNext() && totalRows <= perSheetRows); out.write(sheetFooter.getBytes(templateEncoding)); if (!page.hasNext()) { break; } } out.write(workBookFooter.getBytes(templateEncoding)); IOUtils.closeQuietly(out); if (needCompress(file)) { fileName = compressAndDeleteOriginal(fileName); } long endTime = System.currentTimeMillis(); Map<String, Object> context = Maps.newHashMap(); context.put("seconds", (endTime - beginTime) / 1000); context.put("url", fileName.replace(contextRootPath, "")); notificationApi.notify(user.getId(), "excelExportSuccess", context); } catch (Exception e) { e.printStackTrace(); IOUtils.closeQuietly(out); log.error("excel export error", e); Map<String, Object> context = Maps.newHashMap(); context.put("error", e.getMessage()); notificationApi.notify(user.getId(), "excelExportError", context); } }
From source file:com.daphne.es.showcase.excel.service.ExcelDataService.java
@Async public void exportCvs(final User user, final String contextRootPath, final Searchable searchable) { String encoding = "gbk"; int perSheetRows = 60000; //?sheet 6w? int totalRows = 0; String separator = ","; Long maxId = 0L;/* w ww .j a v a 2 s.c o m*/ String fileName = generateFilename(user, contextRootPath, "csv"); File file = new File(fileName); BufferedOutputStream out = null; try { long beginTime = System.currentTimeMillis(); out = new BufferedOutputStream(new FileOutputStream(file)); out.write("?,\n".getBytes(encoding)); while (true) { totalRows = 0; Page<ExcelData> page = null; do { searchable.setPage(0, pageSize); // if (!searchable.containsSearchKey("id_in")) { searchable.addSearchFilter("id", SearchOperator.gt, maxId); } page = findAll(searchable); for (ExcelData data : page.getContent()) { out.write(String.valueOf(data.getId()).getBytes(encoding)); out.write(separator.getBytes(encoding)); out.write((data.getContent()).replace(separator, ";").getBytes(encoding)); out.write("\n".getBytes(encoding)); maxId = Math.max(maxId, data.getId()); totalRows++; } //clear entity manager RepositoryHelper.clear(); } while (page.hasNext() && totalRows <= perSheetRows); if (!page.hasNext()) { break; } } IOUtils.closeQuietly(out); if (needCompress(file)) { fileName = compressAndDeleteOriginal(fileName); } long endTime = System.currentTimeMillis(); Map<String, Object> context = Maps.newHashMap(); context.put("seconds", (endTime - beginTime) / 1000); context.put("url", fileName.replace(contextRootPath, "")); notificationApi.notify(user.getId(), "excelExportSuccess", context); } catch (Exception e) { IOUtils.closeQuietly(out); log.error("excel export error", e); Map<String, Object> context = Maps.newHashMap(); context.put("error", e.getMessage()); notificationApi.notify(user.getId(), "excelExportError", context); } }
From source file:com.daphne.es.showcase.excel.service.ExcelDataService.java
/** * excel 2003/*from w w w . ja va 2 s . co m*/ * ???? * ?sheet65536(usermodel? ?flush ????) * @param user * @param contextRootPath * @param searchable */ @Async public void exportExcel2003WithUsermodel(final User user, final String contextRootPath, final Searchable searchable) { int perSheetRows = 60000; //?sheet 6w? int totalRows = 0; Long maxId = 0L; String fileName = generateFilename(user, contextRootPath, "xls"); File file = new File(fileName); BufferedOutputStream out = null; try { long beginTime = System.currentTimeMillis(); HSSFWorkbook wb = new HSSFWorkbook(); while (true) { Sheet sheet = wb.createSheet(); Row headerRow = sheet.createRow(0); Cell idHeaderCell = headerRow.createCell(0); idHeaderCell.setCellValue("?"); Cell contentHeaderCell = headerRow.createCell(1); contentHeaderCell.setCellValue(""); totalRows = 1; Page<ExcelData> page = null; do { searchable.setPage(0, pageSize); // if (!searchable.containsSearchKey("id_in")) { searchable.addSearchFilter("id", SearchOperator.gt, maxId); } page = findAll(searchable); for (ExcelData data : page.getContent()) { Row row = sheet.createRow(totalRows); Cell idCell = row.createCell(0); idCell.setCellValue(data.getId()); Cell contentCell = row.createCell(1); contentCell.setCellValue(data.getContent()); maxId = Math.max(maxId, data.getId()); totalRows++; } //clear entity manager RepositoryHelper.clear(); } while (page.hasNext() && totalRows <= perSheetRows); if (!page.hasNext()) { break; } } out = new BufferedOutputStream(new FileOutputStream(file)); wb.write(out); IOUtils.closeQuietly(out); if (needCompress(file)) { fileName = compressAndDeleteOriginal(fileName); } long endTime = System.currentTimeMillis(); Map<String, Object> context = Maps.newHashMap(); context.put("seconds", (endTime - beginTime) / 1000); context.put("url", fileName.replace(contextRootPath, "")); notificationApi.notify(user.getId(), "excelExportSuccess", context); } catch (Exception e) { IOUtils.closeQuietly(out); log.error("excel export error", e); Map<String, Object> context = Maps.newHashMap(); context.put("error", e.getMessage()); notificationApi.notify(user.getId(), "excelExportError", context); } }
From source file:com.daphne.es.showcase.excel.service.ExcelDataService.java
/** * ???//from w w w . j a v a 2s . c om * excel 2007 ?sheet1048576 * @param user * @param contextRootPath * @param searchable */ @Async public void exportExcel2007(final User user, final String contextRootPath, final Searchable searchable) { int rowAccessWindowSize = 1000; //??? int perSheetRows = 100000; //?sheet 10w? int totalRows = 0; // Long maxId = 0L;//??id String fileName = generateFilename(user, contextRootPath, "xlsx"); File file = new File(fileName); BufferedOutputStream out = null; SXSSFWorkbook wb = null; try { long beginTime = System.currentTimeMillis(); wb = new SXSSFWorkbook(rowAccessWindowSize); wb.setCompressTempFiles(true);//?gzip while (true) { Sheet sheet = wb.createSheet(); Row headerRow = sheet.createRow(0); Cell idHeaderCell = headerRow.createCell(0); idHeaderCell.setCellValue("?"); Cell contentHeaderCell = headerRow.createCell(1); contentHeaderCell.setCellValue(""); totalRows = 1; Page<ExcelData> page = null; do { searchable.setPage(0, pageSize); // if (!searchable.containsSearchKey("id_in")) { searchable.addSearchFilter("id", SearchOperator.gt, maxId); } page = findAll(searchable); for (ExcelData data : page.getContent()) { Row row = sheet.createRow(totalRows); Cell idCell = row.createCell(0); idCell.setCellValue(data.getId()); Cell contentCell = row.createCell(1); contentCell.setCellValue(data.getContent()); maxId = Math.max(maxId, data.getId()); totalRows++; } //clear entity manager RepositoryHelper.clear(); } while (page.hasNext() && totalRows <= perSheetRows); if (!page.hasNext()) { break; } } out = new BufferedOutputStream(new FileOutputStream(file)); wb.write(out); IOUtils.closeQuietly(out); if (needCompress(file)) { fileName = compressAndDeleteOriginal(fileName); } long endTime = System.currentTimeMillis(); Map<String, Object> context = Maps.newHashMap(); context.put("seconds", (endTime - beginTime) / 1000); context.put("url", fileName.replace(contextRootPath, "")); notificationApi.notify(user.getId(), "excelExportSuccess", context); } catch (Exception e) { IOUtils.closeQuietly(out); log.error("excel export error", e); Map<String, Object> context = Maps.newHashMap(); context.put("error", e.getMessage()); notificationApi.notify(user.getId(), "excelExportError", context); } finally { // ? wb.dispose(); } }
From source file:com.daphne.es.showcase.excel.service.ExcelDataService.java
/** * workbook//w w w . j a v a 2s. c o m * 1?vbs ? * 2?c#?? * ? ????office 2007 ? * @param user * @param contextRootPath * @param searchable */ @Async public void exportExcel2003WithOneSheetPerWorkBook(final User user, final String contextRootPath, final Searchable searchable) { int workbookCount = 0; List<String> workbookFileNames = new ArrayList<String>(); int perSheetRows = 60000; //?sheet 6w? int totalRows = 0; String extension = "xls"; int pageSize = 1000; Long maxId = 0L; BufferedOutputStream out = null; try { long beginTime = System.currentTimeMillis(); while (true) { workbookCount++; String fileName = generateFilename(user, contextRootPath, workbookCount, extension); workbookFileNames.add(fileName); File file = new File(fileName); HSSFWorkbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet(); Row headerRow = sheet.createRow(0); Cell idHeaderCell = headerRow.createCell(0); idHeaderCell.setCellValue("?"); Cell contentHeaderCell = headerRow.createCell(1); contentHeaderCell.setCellValue(""); totalRows = 1; Page<ExcelData> page = null; do { searchable.setPage(0, pageSize); // if (!searchable.containsSearchKey("id_in")) { searchable.addSearchFilter("id", SearchOperator.gt, maxId); } page = findAll(searchable); for (ExcelData data : page.getContent()) { Row row = sheet.createRow(totalRows); Cell idCell = row.createCell(0); idCell.setCellValue(data.getId()); Cell contentCell = row.createCell(1); contentCell.setCellValue(data.getContent()); maxId = Math.max(maxId, data.getId()); totalRows++; } //clear entity manager RepositoryHelper.clear(); } while (page.hasNext() && totalRows <= perSheetRows); out = new BufferedOutputStream(new FileOutputStream(file)); wb.write(out); IOUtils.closeQuietly(out); if (!page.hasNext()) { break; } } String fileName = workbookFileNames.get(0); if (workbookCount > 1 || needCompress(new File(fileName))) { fileName = fileName.substring(0, fileName.lastIndexOf("_")) + ".zip"; // compressAndDeleteOriginal(fileName, workbookFileNames.toArray(new String[0])); } else { String newFileName = fileName.substring(0, fileName.lastIndexOf("_")) + "." + extension; FileUtils.moveFile(new File(fileName), new File(newFileName)); fileName = newFileName; } long endTime = System.currentTimeMillis(); Map<String, Object> context = Maps.newHashMap(); context.put("seconds", (endTime - beginTime) / 1000); context.put("url", fileName.replace(contextRootPath, "")); notificationApi.notify(user.getId(), "excelExportSuccess", context); } catch (Exception e) { e.printStackTrace(); // IOUtils.closeQuietly(out); log.error("excel export error", e); Map<String, Object> context = Maps.newHashMap(); context.put("error", e.getMessage()); notificationApi.notify(user.getId(), "excelExportError", context); } }