@AccessFrom in AssetDTO and AssetMapper with improved membershipDisplayReference

This commit is contained in:
Michael Hoennig 2019-04-26 17:15:39 +02:00
parent 6570981dc2
commit c8bb8cb2ca
5 changed files with 63 additions and 8 deletions

View File

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

View File

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

View File

@ -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);

View File

@ -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:

View File

@ -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