From c8bb8cb2ca97386a913494f7d24fbd1d5247952a Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Fri, 26 Apr 2019 17:15:39 +0200 Subject: [PATCH] @AccessFrom in AssetDTO and AssetMapper with improved membershipDisplayReference --- .../hsadminng/service/AssetService.java | 7 +++- .../hsadminng/service/dto/AssetDTO.java | 33 +++++++++++++++++-- .../hsadminng/service/mapper/AssetMapper.java | 20 ++++++++++- src/main/resources/config/application-dev.yml | 3 ++ .../web/rest/AssetResourceIntTest.java | 8 ++--- 5 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/hostsharing/hsadminng/service/AssetService.java b/src/main/java/org/hostsharing/hsadminng/service/AssetService.java index bc15e0b3..a375241c 100644 --- a/src/main/java/org/hostsharing/hsadminng/service/AssetService.java +++ b/src/main/java/org/hostsharing/hsadminng/service/AssetService.java @@ -11,6 +11,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import javax.persistence.EntityManager; import java.util.Optional; /** @@ -26,8 +27,10 @@ public class AssetService { private final AssetMapper assetMapper; private final AssetValidator assetValidator; + private final EntityManager em; - public AssetService(AssetRepository assetRepository, AssetMapper assetMapper, AssetValidator assetValidator ) { + public AssetService(final EntityManager em, final AssetRepository assetRepository, final AssetMapper assetMapper, final AssetValidator assetValidator) { + this.em = em; this.assetRepository = assetRepository; this.assetMapper = assetMapper; this.assetValidator = assetValidator; @@ -44,6 +47,8 @@ public class AssetService { assetValidator.validate(assetDTO); Asset asset = assetMapper.toEntity(assetDTO); asset = assetRepository.save(asset); + em.flush(); + em.refresh(asset); return assetMapper.toDto(asset); } diff --git a/src/main/java/org/hostsharing/hsadminng/service/dto/AssetDTO.java b/src/main/java/org/hostsharing/hsadminng/service/dto/AssetDTO.java index d52a535a..d78c34b3 100644 --- a/src/main/java/org/hostsharing/hsadminng/service/dto/AssetDTO.java +++ b/src/main/java/org/hostsharing/hsadminng/service/dto/AssetDTO.java @@ -1,6 +1,10 @@ package org.hostsharing.hsadminng.service.dto; import org.hostsharing.hsadminng.domain.enumeration.AssetAction; +import org.hostsharing.hsadminng.service.CustomerService; +import org.hostsharing.hsadminng.service.accessfilter.*; +import org.springframework.boot.jackson.JsonComponent; +import org.springframework.context.ApplicationContext; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @@ -12,28 +16,37 @@ import java.util.Objects; /** * A DTO for the Asset entity. */ -public class AssetDTO implements Serializable { +public class AssetDTO implements Serializable, AccessMappings { + @SelfId(resolver = CustomerService.class) + @AccessFor(read = Role.ANY_CUSTOMER_USER) private Long id; @NotNull + @AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT}) private LocalDate documentDate; @NotNull + @AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT}) private LocalDate valueDate; @NotNull + @AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT}) private AssetAction action; @NotNull + @AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT}) private BigDecimal amount; @Size(max = 160) + @AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = Role.ADMIN) private String remark; - + @ParentId(resolver = CustomerService.class) + @AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT}) private Long membershipId; + @AccessFor(init=Role.ANYBODY, update=Role.ANYBODY, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT}) private String membershipDisplayReference; public Long getId() { @@ -134,4 +147,20 @@ public class AssetDTO implements Serializable { ", membership='" + getMembershipDisplayReference() + "'" + "}"; } + + @JsonComponent + public static class AssetJsonSerializer extends JsonSerializerWithAccessFilter { + + public AssetJsonSerializer(final ApplicationContext ctx) { + super(ctx); + } + } + + @JsonComponent + public static class AssetJsonDeserializer extends JsonDeserializerWithAccessFilter { + + public AssetJsonDeserializer(final ApplicationContext ctx) { + super(ctx); + } + } } diff --git a/src/main/java/org/hostsharing/hsadminng/service/mapper/AssetMapper.java b/src/main/java/org/hostsharing/hsadminng/service/mapper/AssetMapper.java index 40399960..e4e4a530 100644 --- a/src/main/java/org/hostsharing/hsadminng/service/mapper/AssetMapper.java +++ b/src/main/java/org/hostsharing/hsadminng/service/mapper/AssetMapper.java @@ -1,9 +1,15 @@ package org.hostsharing.hsadminng.service.mapper; import org.hostsharing.hsadminng.domain.Asset; +import org.hostsharing.hsadminng.domain.Customer; +import org.hostsharing.hsadminng.domain.Membership; import org.hostsharing.hsadminng.service.dto.AssetDTO; +import org.mapstruct.AfterMapping; import org.mapstruct.Mapper; import org.mapstruct.Mapping; +import org.mapstruct.MappingTarget; + +import java.util.Objects; /** * Mapper for the entity Asset and its DTO AssetDTO. @@ -12,9 +18,21 @@ import org.mapstruct.Mapping; public interface AssetMapper extends EntityMapper { @Mapping(source = "membership.id", target = "membershipId") - @Mapping(source = "membership.admissionDocumentDate", target = "membershipDisplayReference") + @Mapping(target = "membershipDisplayReference", ignore = true) AssetDTO toDto(Asset asset); + @AfterMapping + default void setMembershipDisplayReference(final @MappingTarget AssetDTO dto, final Asset entity) { + // TODO: rather use method extracted from MembershipMaper.setMembershipDisplayReference() to avoid duplicate code + final Membership membership = entity.getMembership(); + final Customer customer = membership.getCustomer(); + dto.setMembershipDisplayReference(customer.getReference() + + ":" + customer.getPrefix() + + " [" + customer.getName() + "] " + + membership.getAdmissionDocumentDate().toString() + " - " + + Objects.toString(membership.getCancellationDocumentDate(), "")); + } + @Mapping(source = "membershipId", target = "membership") Asset toEntity(AssetDTO assetDTO); diff --git a/src/main/resources/config/application-dev.yml b/src/main/resources/config/application-dev.yml index 502ea108..8e53af01 100644 --- a/src/main/resources/config/application-dev.yml +++ b/src/main/resources/config/application-dev.yml @@ -37,7 +37,10 @@ spring: indent-output: true datasource: type: com.zaxxer.hikari.HikariDataSource + # H2 in memory: url: jdbc:h2:mem:hsadminng;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE + # H2 in file: + # url: jdbc:h2:~/.hsadminng.h2db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE username: hsadminNg password: hikari: diff --git a/src/test/java/org/hostsharing/hsadminng/web/rest/AssetResourceIntTest.java b/src/test/java/org/hostsharing/hsadminng/web/rest/AssetResourceIntTest.java index ae7212be..83b0726c 100644 --- a/src/test/java/org/hostsharing/hsadminng/web/rest/AssetResourceIntTest.java +++ b/src/test/java/org/hostsharing/hsadminng/web/rest/AssetResourceIntTest.java @@ -54,8 +54,8 @@ public class AssetResourceIntTest { private static final AssetAction DEFAULT_ACTION = AssetAction.PAYMENT; private static final AssetAction UPDATED_ACTION = AssetAction.HANDOVER; - private static final BigDecimal DEFAULT_AMOUNT = new BigDecimal(1); - private static final BigDecimal UPDATED_AMOUNT = new BigDecimal(2); + private static final BigDecimal DEFAULT_AMOUNT = new BigDecimal("1.00"); + private static final BigDecimal UPDATED_AMOUNT = new BigDecimal("2.00"); private static final String DEFAULT_REMARK = "AAAAAAAAAA"; private static final String UPDATED_REMARK = "BBBBBBBBBB"; @@ -283,7 +283,7 @@ public class AssetResourceIntTest { .andExpect(jsonPath("$.[*].valueDate").value(hasItem(DEFAULT_VALUE_DATE.toString()))) .andExpect(jsonPath("$.[*].action").value(hasItem(DEFAULT_ACTION.toString()))) .andExpect(jsonPath("$.[*].amount").value(hasItem(DEFAULT_AMOUNT.intValue()))) - .andExpect(jsonPath("$.[*].remark").value(hasItem(DEFAULT_REMARK.toString()))); + .andExpect(jsonPath("$.[*].remark").value(hasItem(DEFAULT_REMARK))); } @Test @@ -301,7 +301,7 @@ public class AssetResourceIntTest { .andExpect(jsonPath("$.valueDate").value(DEFAULT_VALUE_DATE.toString())) .andExpect(jsonPath("$.action").value(DEFAULT_ACTION.toString())) .andExpect(jsonPath("$.amount").value(DEFAULT_AMOUNT.intValue())) - .andExpect(jsonPath("$.remark").value(DEFAULT_REMARK.toString())); + .andExpect(jsonPath("$.remark").value(DEFAULT_REMARK)); } @Test