io.github.jhipster.service.QueryService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jhipster-framework Show documentation
Show all versions of jhipster-framework Show documentation
Server-side library used by applications created with the JHipster generator, see https://www.jhipster.tech/ for more information on JHipster
/*
* Copyright 2016-2018 the original author or authors.
*
* 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
*
* http://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 io.github.jhipster.service;
import io.github.jhipster.service.filter.Filter;
import io.github.jhipster.service.filter.RangeFilter;
import io.github.jhipster.service.filter.StringFilter;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaBuilder.In;
import javax.persistence.metamodel.SetAttribute;
import javax.persistence.metamodel.SingularAttribute;
import java.util.Collection;
/**
* Base service for constructing and executing complex queries.
*
* @param the type of the entity which is queried.
*/
@Transactional(readOnly = true)
public abstract class QueryService {
/**
* Helper function to return a specification for filtering on a single field, where equality, and null/non-null
* conditions are supported.
*
* @param filter the individual attribute filter coming from the frontend.
* @param field the JPA static metamodel representing the field.
* @param The type of the attribute which is filtered.
* @return a Specification
*/
protected Specification buildSpecification(Filter filter, SingularAttribute super ENTITY, X>
field) {
if (filter.getEquals() != null) {
return equalsSpecification(field, filter.getEquals());
} else if (filter.getIn() != null) {
return valueIn(field, filter.getIn());
} else if (filter.getSpecified() != null) {
return byFieldSpecified(field, filter.getSpecified());
}
return null;
}
/**
* Helper function to return a specification for filtering on a {@link String} field, where equality, containment,
* and null/non-null conditions are supported.
*
* @param filter the individual attribute filter coming from the frontend.
* @param field the JPA static metamodel representing the field.
* @return a Specification
*/
protected Specification buildStringSpecification(StringFilter filter, SingularAttribute super ENTITY,
String> field) {
if (filter.getEquals() != null) {
return equalsSpecification(field, filter.getEquals());
} else if (filter.getIn() != null) {
return valueIn(field, filter.getIn());
} else if (filter.getContains() != null) {
return likeUpperSpecification(field, filter.getContains());
} else if (filter.getSpecified() != null) {
return byFieldSpecified(field, filter.getSpecified());
}
return null;
}
/**
* Helper function to return a specification for filtering on a single {@link Comparable}, where equality, less
* than, greater than and less-than-or-equal-to and greater-than-or-equal-to and null/non-null conditions are
* supported.
*
* @param filter the individual attribute filter coming from the frontend.
* @param field the JPA static metamodel representing the field.
* @param The type of the attribute which is filtered.
* @return a Specification
*/
protected > Specification buildRangeSpecification(RangeFilter filter,
SingularAttribute super ENTITY, X> field) {
if (filter.getEquals() != null) {
return equalsSpecification(field, filter.getEquals());
} else if (filter.getIn() != null) {
return valueIn(field, filter.getIn());
}
Specification result = Specification.where(null);
if (filter.getSpecified() != null) {
result = result.and(byFieldSpecified(field, filter.getSpecified()));
}
if (filter.getGreaterThan() != null) {
result = result.and(greaterThan(field, filter.getGreaterThan()));
}
if (filter.getGreaterOrEqualThan() != null) {
result = result.and(greaterThanOrEqualTo(field, filter.getGreaterOrEqualThan()));
}
if (filter.getLessThan() != null) {
result = result.and(lessThan(field, filter.getLessThan()));
}
if (filter.getLessOrEqualThan() != null) {
result = result.and(lessThanOrEqualTo(field, filter.getLessOrEqualThan()));
}
return result;
}
/**
* Helper function to return a specification for filtering on one-to-one or many-to-one reference. Usage:
*
* Specification<Employee> specByProjectId = buildReferringEntitySpecification(criteria.getProjectId(),
* Employee_.project, Project_.id);
* Specification<Employee> specByProjectName = buildReferringEntitySpecification(criteria.getProjectName(),
* Employee_.project, Project_.name);
*
*
* @param filter the filter object which contains a value, which needs to match or a flag if nullness is
* checked.
* @param reference the attribute of the static metamodel for the referring entity.
* @param valueField the attribute of the static metamodel of the referred entity, where the equality should be
* checked.
* @param The type of the referenced entity.
* @param The type of the attribute which is filtered.
* @return a Specification
*/
protected Specification buildReferringEntitySpecification(Filter filter,
SingularAttribute super ENTITY, OTHER> reference,
SingularAttribute valueField) {
if (filter.getEquals() != null) {
return equalsSpecification(reference, valueField, filter.getEquals());
} else if (filter.getIn() != null) {
return valueIn(reference, valueField, filter.getIn());
} else if (filter.getSpecified() != null) {
return byFieldSpecified(reference, filter.getSpecified());
}
return null;
}
/**
* Helper function to return a specification for filtering on one-to-one or many-to-one reference. Where equality, less
* than, greater than and less-than-or-equal-to and greater-than-or-equal-to and null/non-null conditions are
* supported. Usage:
*
* Specification<Employee> specByProjectId = buildReferringEntitySpecification(criteria.getProjectId(),
* Employee_.project, Project_.id);
* Specification<Employee> specByProjectName = buildReferringEntitySpecification(criteria.getProjectName(),
* Employee_.project, Project_.name);
*
*
* @param filter the filter object which contains a value, which needs to match or a flag if nullness is
* checked.
* @param reference the attribute of the static metamodel for the referring entity.
* @param valueField the attribute of the static metamodel of the referred entity, where the equality should be
* checked.
* @param The type of the referenced entity.
* @param The type of the attribute which is filtered.
* @return a Specification
*/
protected > Specification buildReferringEntitySpecification(final RangeFilter filter,
final SingularAttribute super ENTITY, OTHER> reference,
final SingularAttribute valueField) {
if (filter.getEquals() != null) {
return equalsSpecification(reference, valueField, filter.getEquals());
} else if (filter.getIn() != null) {
return valueIn(reference, valueField, filter.getIn());
}
Specification result = Specification.where(null);
if (filter.getSpecified() != null) {
result = result.and(byFieldSpecified(reference, filter.getSpecified()));
}
if (filter.getGreaterThan() != null) {
result = result.and(greaterThan(reference, valueField, filter.getGreaterThan()));
}
if (filter.getGreaterOrEqualThan() != null) {
result = result.and(greaterThanOrEqualTo(reference, valueField, filter.getGreaterOrEqualThan()));
}
if (filter.getLessThan() != null) {
result = result.and(lessThan(reference, valueField, filter.getLessThan()));
}
if (filter.getLessOrEqualThan() != null) {
result = result.and(lessThanOrEqualTo(reference, valueField, filter.getLessOrEqualThan()));
}
return result;
}
/**
* Helper function to return a specification for filtering on one-to-many or many-to-many reference. Usage:
*
* Specification<Employee> specByEmployeeId = buildReferringEntitySpecification(criteria.getEmployeId(),
* Project_.employees, Employee_.id);
* Specification<Employee> specByEmployeeName = buildReferringEntitySpecification(criteria.getEmployeName(),
* Project_.project, Project_.name);
*
*
* @param filter the filter object which contains a value, which needs to match or a flag if emptiness is
* checked.
* @param reference the attribute of the static metamodel for the referring entity.
* @param valueField the attribute of the static metamodel of the referred entity, where the equality should be
* checked.
* @param The type of the referenced entity.
* @param The type of the attribute which is filtered.
* @return a Specification
*/
protected Specification buildReferringEntitySpecification(Filter filter,
SetAttribute reference,
SingularAttribute valueField) {
if (filter.getEquals() != null) {
return equalsSetSpecification(reference, valueField, filter.getEquals());
} else if (filter.getSpecified() != null) {
return byFieldSpecified(reference, filter.getSpecified());
}
return null;
}
/**
* Helper function to return a specification for filtering on one-to-many or many-to-many reference. Where equality, less
* than, greater than and less-than-or-equal-to and greater-than-or-equal-to and null/non-null conditions are
* supported. Usage:
*
* Specification<Employee> specByEmployeeId = buildReferringEntitySpecification(criteria.getEmployeId(),
* Project_.employees, Employee_.id);
* Specification<Employee> specByEmployeeName = buildReferringEntitySpecification(criteria.getEmployeName(),
* Project_.project, Project_.name);
*
*
* @param filter the filter object which contains a value, which needs to match or a flag if emptiness is
* checked.
* @param reference the attribute of the static metamodel for the referring entity.
* @param valueField the attribute of the static metamodel of the referred entity, where the equality should be
* checked.
* @param The type of the referenced entity.
* @param The type of the attribute which is filtered.
* @return a Specification
*/
protected > Specification buildReferringEntitySpecification(final RangeFilter filter,
final SetAttribute reference,
final SingularAttribute valueField) {
if (filter.getEquals() != null) {
return equalsSetSpecification(reference, valueField, filter.getEquals());
} else if (filter.getIn() != null) {
return valueIn(reference, valueField, filter.getIn());
}
Specification result = Specification.where(null);
if (filter.getSpecified() != null) {
result = result.and(byFieldSpecified(reference, filter.getSpecified()));
}
if (filter.getGreaterThan() != null) {
result = result.and(greaterThan(reference, valueField, filter.getGreaterThan()));
}
if (filter.getGreaterOrEqualThan() != null) {
result = result.and(greaterThanOrEqualTo(reference, valueField, filter.getGreaterOrEqualThan()));
}
if (filter.getLessThan() != null) {
result = result.and(lessThan(reference, valueField, filter.getLessThan()));
}
if (filter.getLessOrEqualThan() != null) {
result = result.and(lessThanOrEqualTo(reference, valueField, filter.getLessOrEqualThan()));
}
return result;
}
protected Specification equalsSpecification(SingularAttribute super ENTITY, X> field, final X value) {
return (root, query, builder) -> builder.equal(root.get(field), value);
}
protected Specification equalsSpecification(SingularAttribute super ENTITY, OTHER>
reference, SingularAttribute idField,
X value) {
return (root, query, builder) -> builder.equal(root.get(reference).get(idField), value);
}
protected Specification equalsSetSpecification(SetAttribute super ENTITY, OTHER> reference,
SingularAttribute idField,
X value) {
return (root, query, builder) -> builder.equal(root.join(reference).get(idField), value);
}
protected Specification likeUpperSpecification(SingularAttribute super ENTITY, String> field, final
String value) {
return (root, query, builder) -> builder.like(builder.upper(root.get(field)), wrapLikeQuery(value));
}
protected Specification byFieldSpecified(SingularAttribute super ENTITY, X> field, final boolean
specified) {
return specified ? (root, query, builder) -> builder.isNotNull(root.get(field)) : (root, query, builder) ->
builder.isNull(root.get(field));
}
protected Specification byFieldSpecified(SetAttribute field, final boolean specified) {
return specified ? (root, query, builder) -> builder.isNotEmpty(root.get(field)) : (root, query, builder) ->
builder.isEmpty(root.get(field));
}
protected Specification valueIn(SingularAttribute super ENTITY, X> field, final Collection
values) {
return (root, query, builder) -> {
In in = builder.in(root.get(field));
for (X value : values) {
in = in.value(value);
}
return in;
};
}
protected Specification valueIn(SingularAttribute super ENTITY, OTHER> reference,
SingularAttribute valueField, final Collection values) {
return (root, query, builder) -> {
In in = builder.in(root.get(reference).get(valueField));
for (X value : values) {
in = in.value(value);
}
return in;
};
}
protected > Specification greaterThanOrEqualTo(SingularAttribute super
ENTITY, X> field, final X value) {
return (root, query, builder) -> builder.greaterThanOrEqualTo(root.get(field), value);
}
protected > Specification greaterThan(SingularAttribute super ENTITY,
X> field, final X value) {
return (root, query, builder) -> builder.greaterThan(root.get(field), value);
}
protected > Specification lessThanOrEqualTo(SingularAttribute super
ENTITY, X> field, final X value) {
return (root, query, builder) -> builder.lessThanOrEqualTo(root.get(field), value);
}
protected > Specification lessThan(SingularAttribute super ENTITY, X>
field, final X value) {
return (root, query, builder) -> builder.lessThan(root.get(field), value);
}
protected String wrapLikeQuery(String txt) {
return "%" + txt.toUpperCase() + '%';
}
protected Specification valueIn(final SetAttribute super ENTITY, OTHER> reference,
final SingularAttribute valueField, final Collection values) {
return (root, query, builder) -> {
CriteriaBuilder.In in = builder.in(root.join(reference).get(valueField));
for (X value : values) {
in = in.value(value);
}
return in;
};
}
protected > Specification greaterThan(final SingularAttribute super ENTITY, OTHER> reference, final SingularAttribute valueField, final X value) {
return (root, query, builder) -> builder.greaterThan(root.get(reference).get(valueField), value);
}
protected > Specification greaterThan(final SetAttribute super ENTITY, OTHER> reference, final SingularAttribute valueField, final X value) {
return (root, query, builder) -> builder.greaterThan(root.join(reference).get(valueField), value);
}
protected > Specification greaterThanOrEqualTo(final SingularAttribute super ENTITY, OTHER> reference, final SingularAttribute valueField, final X value) {
return (root, query, builder) -> builder.greaterThanOrEqualTo(root.get(reference).get(valueField), value);
}
protected > Specification greaterThanOrEqualTo(final SetAttribute super ENTITY, OTHER> reference, final SingularAttribute valueField, final X value) {
return (root, query, builder) -> builder.greaterThanOrEqualTo(root.join(reference).get(valueField), value);
}
protected > Specification lessThan(final SingularAttribute super ENTITY, OTHER> reference, final SingularAttribute valueField, final X value) {
return (root, query, builder) -> builder.lessThan(root.get(reference).get(valueField), value);
}
protected > Specification lessThan(final SetAttribute super ENTITY, OTHER> reference, final SingularAttribute valueField, final X value) {
return (root, query, builder) -> builder.lessThan(root.join(reference).get(valueField), value);
}
protected > Specification lessThanOrEqualTo(final SingularAttribute super ENTITY, OTHER> reference, final SingularAttribute valueField, final X value) {
return (root, query, builder) -> builder.lessThanOrEqualTo(root.get(reference).get(valueField), value);
}
protected > Specification lessThanOrEqualTo(final SetAttribute super ENTITY, OTHER> reference, final SingularAttribute valueField, final X value) {
return (root, query, builder) -> builder.lessThanOrEqualTo(root.join(reference).get(valueField), value);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy