jhipster / generator-jhipster

JHipster is a development platform to quickly generate, develop, & deploy modern web applications & microservice architectures.
https://www.jhipster.tech
Apache License 2.0
21.55k stars 4.02k forks source link

Simplify QueryService #26913

Open amatosg opened 3 months ago

amatosg commented 3 months ago
Overview of the feature request

Remove many, many lines of code and move them to the abstract class QueryService.

Motivation for or Use Case

This feature is important because the code will be less complex, therefore cleaner and more redeable and maintainable.

Instead of having many, many if statements,

if (criteria.getContact() != null) {
    specification = specification.and(buildStringSpecification(criteria.getContact(), Address_.contact));
}

there would be only one line:

specification = getStringSpecification(criteria.getContact(), specification, Address_.contact);

The QueryService abstract class would have this method (or something similar)

protected Specification<T> getStringSpecification(
        StringFilter stringFilter,
        Specification<T> specification,
        SingularAttribute<T, String> attribute
    ) {
        if (stringFilter != null) {
            specification = specification.and(buildStringSpecification(stringFilter, attribute));
        }
        return specification;
    }

This can be applied to other specifications (booleans, ranged, etc)

Related issues or PR
amatosg commented 1 month ago

this are the proposed methods that I'm currently using for my needs. Complexity is reduced from 356% to 6% in the most extreme cases. I guess it could be improved :wink:

Missing: relational and enum specifications. I tried but it doesn't work

protected Specification<ENTITY> getStringSpecification(
    Specification<ENTITY> specification,
    StringFilter filter,
    SingularAttribute<ENTITY, String> attribute
) {
    if (stringFilter != null) {
        specification = specification.and(buildStringSpecification(filter, attribute));
    }
    return specification;
}

protected Specification<ENTITY> getBooleanSpecification(
    Specification<ENTITY> specification,
    BooleanFilter filter,
    SingularAttribute<ENTITY, Boolean> attribute
) {
    if (booleanFilter != null) {
        specification = specification.and(buildSpecification(filter, attribute));
    }
    return specification;
}

protected Specification<ENTITY> getUuidSpecification(
    Specification<ENTITY> specification,
    UUIDFilter filter,
    SingularAttribute<ENTITY, UUID> attribute
) {
    if (stringFilter != null) {
        specification = specification.and(buildSpecification(filter, attribute));
    }
    return specification;
}

protected Specification<ENTITY> getDistinctSpecification(Specification<ENTITY> specification, Boolean distinct) {
    if (distinct != null) {
        specification = specification.and(distinct(distinct));
    }
    return specification;
}

protected Specification<ENTITY> getLongSpecification(
    Specification<ENTITY> specification,
    LongFilter filter,
    SingularAttribute<ENTITY, Long> attribute
) {
    if (longFilter != null) {
        specification = specification.and(buildRangeSpecification(filter, attribute));
    }
    return specification;
}

protected Specification<ENTITY> getIntegerSpecification(
    Specification<ENTITY> specification,
    IntegerFilter filter,
    SingularAttribute<ENTITY, Integer> attribute) {
    if (integerFilter != null) {
        specification = specification.and(buildRangeSpecification(filter, attribute));
    }
    return specification;
}

protected Specification<ENTITY> getZonedDateTimeSpecification(
    Specification<ENTITY> specification,
    ZonedDateTimeFilter filter,
    SingularAttribute<ENTITY, ZonedDateTime> attribute) {
    if (zonedDateTimeFilter != null) {
        specification = specification.and(buildRangeSpecification(filter, attribute));
    }
    return specification;
}
protected Specification<ENTITY> getLocalDateSpecification(
    Specification<ENTITY> specification,
    LocalDateFilter filter,
    SingularAttribute<ENTITY, LocalDate> attribute) {
    if (zonedDateTimeFilter != null) {
        specification = specification.and(buildRangeSpecification(filter, attribute));
    }
    return specification;
}

protected Specification<ENTITY> getBigDecimalSpecification(
    Specification<ENTITY> specification,
    BigDecimalFilter filter,
    SingularAttribute<ENTITY, BigDecimal> attribute) {
    if (bigDecimalFilter != null) {
        specification = specification.and(buildRangeSpecification(filter, attribute));
    }
    return specification;
}