Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. @RequestMapping(value = "/get-grouped-report/{id}", method = RequestMethod.GET)
  2. @ApiOperation(value = "получение сгруппированного отчета", response = ResponseEntity.class)
  3. public ResponseEntity<PaginationResponse<GroupChunk<Map<String, Comparable>>>> getGroupedReport(
  4. @PathVariable Long id,
  5. @RequestParam(name = "groupingColumns") String[] groupingColumns,
  6. @RequestParam(value = "page") Integer pageNumber,
  7. @ApiParam(value = "размер страницы PaginationResponse")
  8. @RequestParam(value = "size") Integer size,
  9. @ApiParam(value = "порядок сортировки")
  10. @RequestParam(value = "direction", required = false) Sort.Direction direction,
  11. @ApiParam(value = "параметры, по которым производится сортировка")
  12. @RequestParam(value = "properties", required = false) String... properties) {
  13. ReportSettings reportSettings = service.get(id);
  14. List<Map<String, Comparable>> res = new ArrayList<>();
  15. ReportSettingsConfig config = reportSettings.getConfig();
  16.  
  17. for (int i = 0; i < 100; i++) {
  18. Map<String, Comparable> row = new HashMap<>();
  19. for (ReportColumn column: config.getColumns()) {
  20. if (column.getType().equals(ColumnType.STRING)) {
  21. row.put(column.getId(), String.valueOf(i % 3));
  22. } else if (column.getType().equals(ColumnType.BOOLEAN)) {
  23. row.put(column.getId(), String.valueOf((i % 2) == 0));
  24. } else {
  25. row.put(column.getId(), (Math.random() * 10000));
  26. }
  27. }
  28. res.add(row);
  29. }
  30. if (direction != null) {
  31. String property = properties[0];
  32. res.sort((o1, o2) -> direction.equals(Sort.Direction.ASC) ?
  33. o1.get(property) != null ? o1.get(property).compareTo(o2.get(property)) : -1
  34. :
  35. o2.get(property) != null ? o2.get(property).compareTo(o1.get(property)) : -1
  36. );
  37. }
  38. int start = pageNumber * size;
  39. int end = pageNumber * size + size > 100 ? 100 : pageNumber * size + size;
  40. List<Map<String, Comparable>> subList = res.subList(start, end);
  41. GroupChunk groupChunk = new GroupChunk();
  42. groupChunk.setContent(subList);
  43. generateChunksRecursevely(groupChunk, Lists.newArrayList(groupingColumns),
  44. Lists.newArrayList(groupingColumns), res);
  45.  
  46. return ResponseEntity.ok(new PaginationResponse<GroupChunk<Map<String, Comparable>>>(Long.valueOf(size), groupChunk.getContent()));
  47. }
  48.  
  49. private void generateChunksRecursevely(GroupChunk groupChunk, List<String> properties,
  50. List<String> allProperties, List<Map<String, Comparable>> allEntities) {
  51. if (properties.size() > 0) {
  52. String property = properties.get(0);
  53. properties = properties.stream().filter(prop -> !prop.equals(property)).collect(Collectors.toList());
  54. List oldContent = groupChunk.getContent();
  55. List newContent = (List) ((Map)oldContent.stream().collect(Collectors.groupingBy((map -> ((Map)map).get(property) ))))
  56. .entrySet().stream().map(set -> {
  57. GroupChunk subChunk = new GroupChunk();
  58. subChunk.setGroupColumn(property);
  59. subChunk.setColumnValue(((Map.Entry)set).getKey().toString());
  60. subChunk.setContent(((Map.Entry<String, List<Map>>)set).getValue());
  61. subChunk.getAggregates().put("column3", 100);
  62.  
  63. int finalInThisChunk = 0;
  64. for (int i = 0; i < (subChunk.getContent()).size(); i++) {
  65. if (allEntities.indexOf(subChunk.getContent().get(i)) > allEntities.indexOf(subChunk.getContent().get(finalInThisChunk))) {
  66. finalInThisChunk = i;
  67. }
  68. }
  69. Object fin = subChunk.getContent().get(finalInThisChunk);
  70. if (finalInThisChunk < 100) {
  71. Map<String, Comparable> nextEntity = allEntities.get(finalInThisChunk + 1);
  72. subChunk.setIsLastChunk(!nextEntity.get(property).equals(((Map<String, Comparable>)fin).get(property))
  73. );
  74. }
  75.  
  76.  
  77. return subChunk;
  78. }).collect(Collectors.toList());
  79. for (Object o: newContent) {
  80. generateChunksRecursevely((GroupChunk) o, properties, allProperties, allEntities);
  81. }
  82. groupChunk.setContent(newContent);
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement