@AccessFrom in AssetDTO and AssetMapper with improved membershipDisplayReference
This commit is contained in:
parent
6092afe32b
commit
4ad0d5d954
@ -11,6 +11,7 @@ import org.springframework.data.domain.Pageable;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -26,8 +27,10 @@ public class AssetService {
|
|||||||
|
|
||||||
private final AssetMapper assetMapper;
|
private final AssetMapper assetMapper;
|
||||||
private final AssetValidator assetValidator;
|
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.assetRepository = assetRepository;
|
||||||
this.assetMapper = assetMapper;
|
this.assetMapper = assetMapper;
|
||||||
this.assetValidator = assetValidator;
|
this.assetValidator = assetValidator;
|
||||||
@ -44,6 +47,8 @@ public class AssetService {
|
|||||||
assetValidator.validate(assetDTO);
|
assetValidator.validate(assetDTO);
|
||||||
Asset asset = assetMapper.toEntity(assetDTO);
|
Asset asset = assetMapper.toEntity(assetDTO);
|
||||||
asset = assetRepository.save(asset);
|
asset = assetRepository.save(asset);
|
||||||
|
em.flush();
|
||||||
|
em.refresh(asset);
|
||||||
return assetMapper.toDto(asset);
|
return assetMapper.toDto(asset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package org.hostsharing.hsadminng.service.dto;
|
package org.hostsharing.hsadminng.service.dto;
|
||||||
|
|
||||||
import org.hostsharing.hsadminng.domain.enumeration.AssetAction;
|
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.NotNull;
|
||||||
import javax.validation.constraints.Size;
|
import javax.validation.constraints.Size;
|
||||||
@ -12,28 +16,37 @@ import java.util.Objects;
|
|||||||
/**
|
/**
|
||||||
* A DTO for the Asset entity.
|
* 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;
|
private Long id;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
|
||||||
private LocalDate documentDate;
|
private LocalDate documentDate;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
|
||||||
private LocalDate valueDate;
|
private LocalDate valueDate;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
|
||||||
private AssetAction action;
|
private AssetAction action;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
|
||||||
private BigDecimal amount;
|
private BigDecimal amount;
|
||||||
|
|
||||||
@Size(max = 160)
|
@Size(max = 160)
|
||||||
|
@AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = Role.ADMIN)
|
||||||
private String remark;
|
private String remark;
|
||||||
|
|
||||||
|
@ParentId(resolver = CustomerService.class)
|
||||||
|
@AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
|
||||||
private Long membershipId;
|
private Long membershipId;
|
||||||
|
|
||||||
|
@AccessFor(init=Role.ANYBODY, update=Role.ANYBODY, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
|
||||||
private String membershipDisplayReference;
|
private String membershipDisplayReference;
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
@ -134,4 +147,20 @@ public class AssetDTO implements Serializable {
|
|||||||
", membership='" + getMembershipDisplayReference() + "'" +
|
", 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;
|
package org.hostsharing.hsadminng.service.mapper;
|
||||||
|
|
||||||
import org.hostsharing.hsadminng.domain.Asset;
|
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.hostsharing.hsadminng.service.dto.AssetDTO;
|
||||||
|
import org.mapstruct.AfterMapping;
|
||||||
import org.mapstruct.Mapper;
|
import org.mapstruct.Mapper;
|
||||||
import org.mapstruct.Mapping;
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.MappingTarget;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mapper for the entity Asset and its DTO AssetDTO.
|
* Mapper for the entity Asset and its DTO AssetDTO.
|
||||||
@ -12,9 +18,21 @@ import org.mapstruct.Mapping;
|
|||||||
public interface AssetMapper extends EntityMapper<AssetDTO, Asset> {
|
public interface AssetMapper extends EntityMapper<AssetDTO, Asset> {
|
||||||
|
|
||||||
@Mapping(source = "membership.id", target = "membershipId")
|
@Mapping(source = "membership.id", target = "membershipId")
|
||||||
@Mapping(source = "membership.admissionDocumentDate", target = "membershipDisplayReference")
|
@Mapping(target = "membershipDisplayReference", ignore = true)
|
||||||
AssetDTO toDto(Asset asset);
|
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")
|
@Mapping(source = "membershipId", target = "membership")
|
||||||
Asset toEntity(AssetDTO assetDTO);
|
Asset toEntity(AssetDTO assetDTO);
|
||||||
|
|
||||||
|
@ -37,7 +37,10 @@ spring:
|
|||||||
indent-output: true
|
indent-output: true
|
||||||
datasource:
|
datasource:
|
||||||
type: com.zaxxer.hikari.HikariDataSource
|
type: com.zaxxer.hikari.HikariDataSource
|
||||||
|
# H2 in memory:
|
||||||
url: jdbc:h2:mem:hsadminng;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
|
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
|
username: hsadminNg
|
||||||
password:
|
password:
|
||||||
hikari:
|
hikari:
|
||||||
|
@ -54,8 +54,8 @@ public class AssetResourceIntTest {
|
|||||||
private static final AssetAction DEFAULT_ACTION = AssetAction.PAYMENT;
|
private static final AssetAction DEFAULT_ACTION = AssetAction.PAYMENT;
|
||||||
private static final AssetAction UPDATED_ACTION = AssetAction.HANDOVER;
|
private static final AssetAction UPDATED_ACTION = AssetAction.HANDOVER;
|
||||||
|
|
||||||
private static final BigDecimal DEFAULT_AMOUNT = new BigDecimal(1);
|
private static final BigDecimal DEFAULT_AMOUNT = new BigDecimal("1.00");
|
||||||
private static final BigDecimal UPDATED_AMOUNT = new BigDecimal(2);
|
private static final BigDecimal UPDATED_AMOUNT = new BigDecimal("2.00");
|
||||||
|
|
||||||
private static final String DEFAULT_REMARK = "AAAAAAAAAA";
|
private static final String DEFAULT_REMARK = "AAAAAAAAAA";
|
||||||
private static final String UPDATED_REMARK = "BBBBBBBBBB";
|
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("$.[*].valueDate").value(hasItem(DEFAULT_VALUE_DATE.toString())))
|
||||||
.andExpect(jsonPath("$.[*].action").value(hasItem(DEFAULT_ACTION.toString())))
|
.andExpect(jsonPath("$.[*].action").value(hasItem(DEFAULT_ACTION.toString())))
|
||||||
.andExpect(jsonPath("$.[*].amount").value(hasItem(DEFAULT_AMOUNT.intValue())))
|
.andExpect(jsonPath("$.[*].amount").value(hasItem(DEFAULT_AMOUNT.intValue())))
|
||||||
.andExpect(jsonPath("$.[*].remark").value(hasItem(DEFAULT_REMARK.toString())));
|
.andExpect(jsonPath("$.[*].remark").value(hasItem(DEFAULT_REMARK)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -301,7 +301,7 @@ public class AssetResourceIntTest {
|
|||||||
.andExpect(jsonPath("$.valueDate").value(DEFAULT_VALUE_DATE.toString()))
|
.andExpect(jsonPath("$.valueDate").value(DEFAULT_VALUE_DATE.toString()))
|
||||||
.andExpect(jsonPath("$.action").value(DEFAULT_ACTION.toString()))
|
.andExpect(jsonPath("$.action").value(DEFAULT_ACTION.toString()))
|
||||||
.andExpect(jsonPath("$.amount").value(DEFAULT_AMOUNT.intValue()))
|
.andExpect(jsonPath("$.amount").value(DEFAULT_AMOUNT.intValue()))
|
||||||
.andExpect(jsonPath("$.remark").value(DEFAULT_REMARK.toString()));
|
.andExpect(jsonPath("$.remark").value(DEFAULT_REMARK));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user