Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RequestMapping(value = "/get-grouped-report/{id}", method = RequestMethod.GET)
- @ApiOperation(value = "получение сгруппированного отчета", response = ResponseEntity.class)
- public ResponseEntity<PaginationResponse<GroupChunk<Map<String, Comparable>>>> getGroupedReport(
- @PathVariable Long id,
- @RequestParam(name = "groupingColumns") String[] groupingColumns,
- @RequestParam(value = "page") Integer pageNumber,
- @ApiParam(value = "размер страницы PaginationResponse")
- @RequestParam(value = "size") Integer size,
- @ApiParam(value = "порядок сортировки")
- @RequestParam(value = "direction", required = false) Sort.Direction direction,
- @ApiParam(value = "параметры, по которым производится сортировка")
- @RequestParam(value = "properties", required = false) String... properties) {
- ReportSettings reportSettings = service.get(id);
- List<Map<String, Comparable>> res = new ArrayList<>();
- ReportSettingsConfig config = reportSettings.getConfig();
- for (int i = 0; i < 100; i++) {
- Map<String, Comparable> row = new HashMap<>();
- for (ReportColumn column: config.getColumns()) {
- if (column.getType().equals(ColumnType.STRING)) {
- row.put(column.getId(), String.valueOf(i % 3));
- } else if (column.getType().equals(ColumnType.BOOLEAN)) {
- row.put(column.getId(), String.valueOf((i % 2) == 0));
- } else {
- row.put(column.getId(), (Math.random() * 10000));
- }
- }
- res.add(row);
- }
- if (direction != null) {
- String property = properties[0];
- res.sort((o1, o2) -> direction.equals(Sort.Direction.ASC) ?
- o1.get(property) != null ? o1.get(property).compareTo(o2.get(property)) : -1
- :
- o2.get(property) != null ? o2.get(property).compareTo(o1.get(property)) : -1
- );
- }
- int start = pageNumber * size;
- int end = pageNumber * size + size > 100 ? 100 : pageNumber * size + size;
- List<Map<String, Comparable>> subList = res.subList(start, end);
- GroupChunk groupChunk = new GroupChunk();
- groupChunk.setContent(subList);
- generateChunksRecursevely(groupChunk, Lists.newArrayList(groupingColumns),
- Lists.newArrayList(groupingColumns), res);
- return ResponseEntity.ok(new PaginationResponse<GroupChunk<Map<String, Comparable>>>(Long.valueOf(size), groupChunk.getContent()));
- }
- private void generateChunksRecursevely(GroupChunk groupChunk, List<String> properties,
- List<String> allProperties, List<Map<String, Comparable>> allEntities) {
- if (properties.size() > 0) {
- String property = properties.get(0);
- properties = properties.stream().filter(prop -> !prop.equals(property)).collect(Collectors.toList());
- List oldContent = groupChunk.getContent();
- List newContent = (List) ((Map)oldContent.stream().collect(Collectors.groupingBy((map -> ((Map)map).get(property) ))))
- .entrySet().stream().map(set -> {
- GroupChunk subChunk = new GroupChunk();
- subChunk.setGroupColumn(property);
- subChunk.setColumnValue(((Map.Entry)set).getKey().toString());
- subChunk.setContent(((Map.Entry<String, List<Map>>)set).getValue());
- subChunk.getAggregates().put("column3", 100);
- int finalInThisChunk = 0;
- for (int i = 0; i < (subChunk.getContent()).size(); i++) {
- if (allEntities.indexOf(subChunk.getContent().get(i)) > allEntities.indexOf(subChunk.getContent().get(finalInThisChunk))) {
- finalInThisChunk = i;
- }
- }
- Object fin = subChunk.getContent().get(finalInThisChunk);
- if (finalInThisChunk < 100) {
- Map<String, Comparable> nextEntity = allEntities.get(finalInThisChunk + 1);
- subChunk.setIsLastChunk(!nextEntity.get(property).equals(((Map<String, Comparable>)fin).get(property))
- );
- }
- return subChunk;
- }).collect(Collectors.toList());
- for (Object o: newContent) {
- generateChunksRecursevely((GroupChunk) o, properties, allProperties, allEntities);
- }
- groupChunk.setContent(newContent);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement