From ddfe8b68cf030e7d67555ff5e930c342bbfc0326 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Fri, 11 Oct 2024 09:43:59 +0200 Subject: [PATCH] refactoring --- .../relation/HsOfficeRelationController.java | 17 ++--- .../HsOfficeRelationRbacRepository.java | 66 ++++++++++--------- .../HsOfficeDebitorEntityUnitTest.java | 1 - .../HsOfficeMembershipEntityUnitTest.java | 1 - .../person/HsOfficePersonEntityUnitTest.java | 1 - ...ficeRelationRepositoryIntegrationTest.java | 2 +- .../HsOfficeSepaMandateEntityUnitTest.java | 1 - 7 files changed, 42 insertions(+), 47 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationController.java index bb5c4914..635bf923 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationController.java @@ -53,14 +53,11 @@ public class HsOfficeRelationController implements HsOfficeRelationsApi { String contactData) { context.define(currentSubject, assumedRoles); - final var entities = - ( personData == null && contactData == null ) - ? relationRbacRepo.findRelationRelatedToPersonUuidAndRelationType(personUuid, - relationType == null ? null : HsOfficeRelationType.valueOf(relationType.name())) - : relationRbacRepo.findRelationRelatedToPersonUuidRelationTypePersonAndContactData( - personUuid, - relationType == null ? null : HsOfficeRelationType.valueOf(relationType.name()), - forLike(personData), forLike(contactData)); + final List entities = + relationRbacRepo.findRelationRelatedToPersonUuidRelationTypePersonAndContactData( + personUuid, + relationType == null ? null : HsOfficeRelationType.valueOf(relationType.name()), + personData, contactData); final var resources = mapper.mapList(entities, HsOfficeRelationResource.class, RELATION_ENTITY_TO_RESOURCE_POSTMAPPER); @@ -152,10 +149,6 @@ public class HsOfficeRelationController implements HsOfficeRelationsApi { return ResponseEntity.ok(mapped); } - private String forLike(final String text) { - return text == null ? null : ("%" + text.toLowerCase() + "%"); - } - final BiConsumer RELATION_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> { resource.setAnchor(mapper.map(entity.getAnchor(), HsOfficePersonResource.class)); resource.setHolder(mapper.map(entity.getHolder(), HsOfficePersonResource.class)); diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationRbacRepository.java b/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationRbacRepository.java index 4b83de46..231a3318 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationRbacRepository.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationRbacRepository.java @@ -12,51 +12,49 @@ public interface HsOfficeRelationRbacRepository extends Repository findByUuid(UUID id); - default List findRelationRelatedToPersonUuidAndRelationType(@NotNull UUID personUuid, HsOfficeRelationType relationType) { - return findRelationRelatedToPersonUuidAndRelationTypeString(personUuid, relationType == null ? null : relationType.toString()); - } - @Query(value = """ SELECT p.* FROM hs_office.relation_rv AS p WHERE p.anchorUuid = :personUuid OR p.holderUuid = :personUuid """, nativeQuery = true) List findRelationRelatedToPersonUuid(@NotNull UUID personUuid); - @Query(value = """ - SELECT p.* FROM hs_office.relation_rv AS p - WHERE (:relationType IS NULL OR p.type = cast(:relationType AS hs_office.RelationType)) - AND ( p.anchorUuid = :personUuid OR p.holderUuid = :personUuid) - """, nativeQuery = true) - List findRelationRelatedToPersonUuidAndRelationTypeString(@NotNull UUID personUuid, String relationType); - /** * Finds relations by a conjunction of optional criteria, including anchorPerson, holderPerson and contact data. - * + * * * @param personUuid the optional UUID of the anchorPerson or holderPerson * @param relationType the type of the relation - * @param personData a lower-case string to match the persons tradeName, familyName or givenName (use '%' for wildcard) - * @param contactData a lower-case string to match the contacts caption, postalAddress, emailAddresses or phoneNumbers (use '%' for wildcard) + * @param personData a string to match the persons tradeName, familyName or givenName (use '%' for wildcard), case ignored + * @param contactData a string to match the contacts caption, postalAddress, emailAddresses or phoneNumbers (use '%' for wildcard), case ignored * @return a list of (accessible) relations which match all given criteria */ - @Query(value = """ - SELECT rel FROM HsOfficeRelationRbacEntity AS rel - WHERE (:relationType IS NULL OR CAST(rel.type AS String) = CAST(:relationType AS String)) - AND ( :personUuid IS NULL OR - rel.anchor.uuid = :personUuid OR rel.holder.uuid = :personUuid ) - AND ( :personData IS NULL OR - lower(rel.anchor.tradeName) LIKE :personData OR lower(rel.holder.tradeName) LIKE :personData OR - lower(rel.anchor.familyName) LIKE :personData OR lower(rel.holder.familyName) LIKE :personData OR - lower(rel.anchor.givenName) LIKE :personData OR lower(rel.holder.givenName) LIKE :personData ) - AND ( :contactData IS NULL OR - lower(rel.contact.caption) LIKE :contactData OR - lower(rel.contact.postalAddress) LIKE :contactData OR - lower(CAST(rel.contact.emailAddresses AS String)) LIKE :contactData OR - lower(CAST(rel.contact.phoneNumbers AS String)) LIKE :contactData ) - """) - List findRelationRelatedToPersonUuidRelationTypePersonAndContactData( + default List findRelationRelatedToPersonUuidRelationTypePersonAndContactData( UUID personUuid, HsOfficeRelationType relationType, String personData, + String contactData) { + return findRelationRelatedToPersonUuidRelationTypePersonAndContactDataImpl( + personUuid, toStringOrNull(relationType), forLike(personData), forLike(contactData)); + } + + @Query(value = """ + SELECT rel FROM HsOfficeRelationRbacEntity AS rel + WHERE (:relationType IS NULL OR CAST(rel.type AS String) = :relationType) + AND ( :personUuid IS NULL + OR rel.anchor.uuid = :personUuid OR rel.holder.uuid = :personUuid ) + AND ( :personData IS NULL + OR lower(rel.anchor.tradeName) LIKE :personData OR lower(rel.holder.tradeName) LIKE :personData + OR lower(rel.anchor.familyName) LIKE :personData OR lower(rel.holder.familyName) LIKE :personData + OR lower(rel.anchor.givenName) LIKE :personData OR lower(rel.holder.givenName) LIKE :personData ) + AND ( :contactData IS NULL + OR lower(rel.contact.caption) LIKE :contactData + OR lower(rel.contact.postalAddress) LIKE :contactData + OR lower(CAST(rel.contact.emailAddresses AS String)) LIKE :contactData + OR lower(CAST(rel.contact.phoneNumbers AS String)) LIKE :contactData ) + """) + List findRelationRelatedToPersonUuidRelationTypePersonAndContactDataImpl( + UUID personUuid, + String relationType, + String personData, String contactData); HsOfficeRelationRbacEntity save(final HsOfficeRelationRbacEntity entity); @@ -64,4 +62,12 @@ public interface HsOfficeRelationRbacRepository extends Repository