generator-jhipster icon indicating copy to clipboard operation
generator-jhipster copied to clipboard

Simplify QueryService

Open amatosg opened this issue 1 year ago • 1 comments

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
  • [x] Checking this box is mandatory (this is just to show you read everything)

amatosg avatar Aug 06 '24 19:08 amatosg

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;
}

amatosg avatar Sep 10 '24 03:09 amatosg

This issue is stale because it has been open for too long without any activity. Due to the moving nature of jhipster generated application, bugs can become invalid. If this issue still applies please comment otherwise it will be closed in 7 days

github-actions[bot] avatar Mar 17 '25 00:03 github-actions[bot]