@AccessFrom in AssetDTO and AssetMapper with improved membershipDisplayReference
This commit is contained in:
		@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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<AssetDTO> {
 | 
			
		||||
 | 
			
		||||
        public AssetJsonSerializer(final ApplicationContext ctx) {
 | 
			
		||||
            super(ctx);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @JsonComponent
 | 
			
		||||
    public static class AssetJsonDeserializer extends JsonDeserializerWithAccessFilter<AssetDTO> {
 | 
			
		||||
 | 
			
		||||
        public AssetJsonDeserializer(final ApplicationContext ctx) {
 | 
			
		||||
            super(ctx);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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<AssetDTO, Asset> {
 | 
			
		||||
 | 
			
		||||
    @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);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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:
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user