<%#
 Copyright 2013-2021 the original author or authors from the JHipster 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.
-%>
package <%= entityAbsolutePackage %>.service;

<%_
const serviceClassName = entityClass + 'QueryService';
const instanceType = restClass;
const instanceName = restInstance;
const mapper = entityInstance  + 'Mapper';
const dtoToEntity = mapper + '.'+ 'toEntity';
const entityToDto = mapper + '.'+ 'toDto';
const entityListToDto = mapper + '.' + 'toDto';
const entityToDtoReference = mapper + '::'+ 'toDto';
const repository = entityInstance  + 'Repository';
const criteria = entityClass + 'Criteria';
_%>
import java.util.List;

import javax.persistence.criteria.JoinType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tech.jhipster.service.QueryService;

import <%= entityAbsolutePackage %>.domain.<%= persistClass %>;
import <%= entityAbsolutePackage %>.domain.*; // for static metamodels
import <%= entityAbsolutePackage %>.repository.<%= entityClass %>Repository;
<%_ if (searchEngineElasticsearch) { _%>
import <%= entityAbsolutePackage %>.repository.search.<%= entityClass %>SearchRepository;
<%_ } _%>
import <%= entityAbsolutePackage %>.service.criteria.<%= entityClass %>Criteria;
<%_ if (dtoMapstruct) { _%>
import <%= entityAbsolutePackage %>.service.dto.<%= dtoClass %>;
import <%= entityAbsolutePackage %>.service.mapper.<%= entityClass %>Mapper;
<%_ } _%>

/**
 * Service for executing complex queries for {@link <%= persistClass %>} entities in the database.
 * The main input is a {@link <%= entityClass %>Criteria} which gets converted to {@link Specification},
 * in a way that all the filters must apply.
 * It returns a {@link List} of {@link <%= instanceType %>} or a {@link Page} of {@link <%= instanceType %>} which fulfills the criteria.
 */
@Service
<%_ if (databaseTypeSql) { _%>
@Transactional(readOnly = true)
<%_ } _%>
public class <%= serviceClassName %> extends QueryService<<%= persistClass %>> {

    private final Logger log = LoggerFactory.getLogger(<%= serviceClassName %>.class);
<%- include('../common/inject_template', {viaService: false, constructorName: serviceClassName, queryService: false, isUsingMapsId: false, mapsIdAssoc: null, isController: false}); -%>

    /**
     * Return a {@link List} of {@link <%= instanceType %>} which matches the criteria from the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public List<<%= instanceType %>> findByCriteria(<%= criteria %> criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<<%= persistClass %>> specification = createSpecification(criteria);
<%_ if (dtoMapstruct) { _%>
        return <%= entityListToDto %>(<%= repository %>.findAll(specification));
<%_ } else { _%>
        return <%= repository %>.findAll(specification);
<%_ } _%>
    }

    /**
     * Return a {@link Page} of {@link <%= instanceType %>} which matches the criteria from the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @param page The page, which should be returned.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public Page<<%= instanceType %>> findByCriteria(<%= criteria %> criteria, Pageable page) {
        log.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<<%= persistClass %>> specification = createSpecification(criteria);
<%_ if (dtoMapstruct) { _%>
        return <%= repository %>.findAll(specification, page)
            .map(<%= entityToDtoReference %>);
<%_ } else { _%>
        return <%= repository %>.findAll(specification, page);
<%_ } _%>
    }

    /**
     * Return the number of matching entities in the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the number of matching entities.
     */
    @Transactional(readOnly = true)
    public long countByCriteria(<%= criteria %> criteria) {
        log.debug("count by criteria : {}", criteria);
        final Specification<<%= persistClass %>> specification = createSpecification(criteria);
        return <%= repository %>.count(specification);
    }

    /**
     * Function to convert {@link <%= criteria %>} to a {@link Specification}
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching {@link Specification} of the entity.
     */
    protected Specification<<%= persistClass %>> createSpecification(<%= criteria %> criteria) {
        Specification<<%= persistClass %>> specification = Specification.where(null);
        if (criteria != null) {
            // This has to be called first, because the distinct method returns null
            if (criteria.getDistinct() != null) {
                specification = specification.and(distinct(criteria.getDistinct()));
            }
            if (criteria.get<%= primaryKey.nameCapitalized %>() != null) {
                specification = specification.and(<%= getSpecificationBuilder(primaryKey.type) %>(criteria.get<%= primaryKey.nameCapitalized %>(), <%= persistClass %>_.<%= primaryKey.name %>));
            }
<%_
fields.forEach((field) => {
  if (field.id) return;
  if (isFilterableType(field.fieldType)) { _%>
            if (criteria.get<%= field.fieldInJavaBeanMethod %>() != null) {
                specification = specification.and(<%= getSpecificationBuilder(field.fieldType) %>(criteria.get<%= field.fieldInJavaBeanMethod %>(), <%= persistClass %>_.<%= field.fieldName %>));
            }
  <%_ }
});

relationships.forEach((relationship) => {
  const metamodelFieldName = (relationship.relationshipManyToMany || relationship.relationshipOneToMany) ? relationship.relationshipFieldNamePlural : relationship.relationshipFieldName; _%>
if (criteria.get<%= relationship.relationshipNameCapitalized %>Id() != null) {
    specification = specification.and(buildSpecification(criteria.get<%= relationship.relationshipNameCapitalized %>Id(),
        root -> root.join(<%= persistClass %>_.<%= metamodelFieldName %>, JoinType.LEFT).get(<%= asEntity(relationship.otherEntityNameCapitalized) %>_.<%= relationship.otherEntity.primaryKey.name %>)));
}
<%_ }); /* forEach */ _%>
        }
        return specification;
    }
}
