Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify QueryService #26913

Open
1 task done
amatosg opened this issue Aug 6, 2024 · 1 comment
Open
1 task done

Simplify QueryService #26913

amatosg opened this issue Aug 6, 2024 · 1 comment

Comments

@amatosg
Copy link
Contributor

amatosg commented Aug 6, 2024

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
  • Checking this box is mandatory (this is just to show you read everything)
@amatosg
Copy link
Contributor Author

amatosg commented Sep 10, 2024

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 😉

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants