<%#
 Copyright 2013-2021 the original author or authors from the BegCode project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
    <%_
    const instanceType = restClass;
    const instanceName = restInstance;
    const mapper = entityInstance + 'Mapper';
    const entityListToDtoListReference = mapper + '.' + 'toDto';
    const entityToDtoReference = mapper + '::'+ 'toDto';
    const pageName = reactive ? 'Flux' : (ormTool !== 'mybatis' ? 'Page' : 'IPage');
    const pageType = ormTool !== 'mybatis' ? 'Pageable' : 'Pageable';
    const pageInstance = ormTool !== 'mybatis' ? 'pageable' : 'PageableUtils.toPage(pageable)'
    const pageUtil = ormTool !== 'mybatis' ? 'PaginationUtil': 'IPageUtil';
    const pageGetContent = ormTool !== 'mybatis' ? 'getContent': 'getRecords';
    const pageGetTotal = ormTool !== 'mybatis' ? 'getTotalElements': 'getTotal';
    const reactiveEntityToDto = (dtoMapstruct && !viaService) ? `.map(${entityToDtoReference})` : '';
    _%>
    public <% if (reactive) { %>Mono<<% }%>ResponseEntity<PageRecord<<%= instanceType %>>><% if (reactive) { %>><% } %> getAll<%= entityClassPlural %>ofTree(<%= entityClass %>Criteria criteria, <% if (!paginationNo) { %><%- pageType%> pageable<% } %><% if (reactive) { %><% if (!paginationNo) { %>, <% } %>ServerHttpRequest request<% } %>) {
        log.debug("REST request to get a page of <%= entityClassPlural %>");
        <%_ if (!reactive) { _%>
        <%= pageName%><<%= instanceType %>> page = <%= entityInstance %><%= viaService ? 'QueryService' : 'Repository' %><% if (ormTool === 'hibernate') { %>.findAllTop(criteria, <%= pageInstance%>)<% } else { %><% if (viaService) { %>.findAllTop(criteria, <%= pageInstance%>)<% } else { %>.selectPage(<%= pageInstance%>, null)<% } %><% } %>;
        PageRecord<<%= instanceType %>> result = new PageRecord<>();
        result.records(page.<%= pageGetContent%>()).size((int) page.getSize()).total(page.<%= pageGetTotal %>()).page(<% if (ormTool === 'mybatis') { %>page.getCurrent()<% } else { _%>pageable.getPageNumber()<% } %>);
        HttpHeaders headers = <%= pageUtil%>.generatePaginationHttpHeaders(<% if (!reactive) { %>ServletUriComponentsBuilder.fromCurrentRequest()<% } else { %>UriComponentsBuilder.fromHttpRequest(request)<% } %>, page);
        return ResponseEntity.ok().headers(headers).body(result);
        <%_ } else { _%>
        return <%= entityInstance %><%= viaService ? 'Service.countAll' : 'Repository.count' %>().zipWith(<%= entityInstance %><%= viaService ? 'Service.findAllTop' : 'Repository.findAllTop' %>(pageable)<%= reactiveEntityToDto %>.collectList())
                .map(countWithEntities -> {
                    return ResponseEntity.ok()
                        .headers(
                            PaginationUtil.generatePaginationHttpHeaders(
                                UriComponentsBuilder.fromHttpRequest(request),
                                new PageImpl<>(countWithEntities.getT2(), pageable, countWithEntities.getT1())
                            )
                        ).body(countWithEntities.getT2());
                });
        <%_ } _%>
    }
