MembershipDTO JSonSerializerWithAccessFilter proper membershipDisplayReference

This commit is contained in:
Michael Hoennig 2019-04-26 17:42:30 +02:00
parent bed81ee7f8
commit 53382beb88
8 changed files with 89 additions and 22 deletions

View File

@ -19,7 +19,7 @@ import java.util.Optional;
*/ */
@Service @Service
@Transactional @Transactional
public class AssetService { public class AssetService implements IdToDtoResolver<AssetDTO> {
private final Logger log = LoggerFactory.getLogger(AssetService.class); private final Logger log = LoggerFactory.getLogger(AssetService.class);

View File

@ -1,6 +1,7 @@
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.AssetService;
import org.hostsharing.hsadminng.service.CustomerService; import org.hostsharing.hsadminng.service.CustomerService;
import org.hostsharing.hsadminng.service.accessfilter.*; import org.hostsharing.hsadminng.service.accessfilter.*;
import org.springframework.boot.jackson.JsonComponent; import org.springframework.boot.jackson.JsonComponent;
@ -18,7 +19,7 @@ import java.util.Objects;
*/ */
public class AssetDTO implements Serializable, AccessMappings { public class AssetDTO implements Serializable, AccessMappings {
@SelfId(resolver = CustomerService.class) @SelfId(resolver = AssetService.class)
@AccessFor(read = Role.ANY_CUSTOMER_USER) @AccessFor(read = Role.ANY_CUSTOMER_USER)
private Long id; private Long id;

View File

@ -2,10 +2,9 @@ package org.hostsharing.hsadminng.service.dto;
import org.hostsharing.hsadminng.service.CustomerService; import org.hostsharing.hsadminng.service.CustomerService;
import org.hostsharing.hsadminng.service.MembershipService; import org.hostsharing.hsadminng.service.MembershipService;
import org.hostsharing.hsadminng.service.accessfilter.AccessFor; import org.hostsharing.hsadminng.service.accessfilter.*;
import org.hostsharing.hsadminng.service.accessfilter.ParentId; import org.springframework.boot.jackson.JsonComponent;
import org.hostsharing.hsadminng.service.accessfilter.Role; import org.springframework.context.ApplicationContext;
import org.hostsharing.hsadminng.service.accessfilter.SelfId;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
@ -16,7 +15,7 @@ import java.util.Objects;
/** /**
* A DTO for the Membership entity. * A DTO for the Membership entity.
*/ */
public class MembershipDTO extends FluentBuilder<MembershipDTO> implements Serializable { public class MembershipDTO extends FluentBuilder<MembershipDTO> implements Serializable, AccessMappings {
@SelfId(resolver = MembershipService.class) @SelfId(resolver = MembershipService.class)
@AccessFor(read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT}) @AccessFor(read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
@ -26,18 +25,18 @@ public class MembershipDTO extends FluentBuilder<MembershipDTO> implements Seria
@AccessFor(init = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT}) @AccessFor(init = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
private LocalDate admissionDocumentDate; private LocalDate admissionDocumentDate;
@AccessFor(init = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT}) @AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
private LocalDate cancellationDocumentDate; private LocalDate cancellationDocumentDate;
@NotNull @NotNull
@AccessFor(init = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT}) @AccessFor(init = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
private LocalDate memberFromDate; private LocalDate memberFromDate;
@AccessFor(init = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT}) @AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
private LocalDate memberUntilDate; private LocalDate memberUntilDate;
@Size(max = 160) @Size(max = 160)
@AccessFor(init = Role.ADMIN, read = Role.SUPPORTER) @AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = Role.SUPPORTER)
private String remark; private String remark;
@ParentId(resolver = CustomerService.class) @ParentId(resolver = CustomerService.class)
@ -47,6 +46,9 @@ public class MembershipDTO extends FluentBuilder<MembershipDTO> implements Seria
@AccessFor(init = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT}) @AccessFor(init = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
private String customerPrefix; private String customerPrefix;
@AccessFor(read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
private String membershipDisplayReference;
public Long getId() { public Long getId() {
return id; return id;
} }
@ -111,6 +113,14 @@ public class MembershipDTO extends FluentBuilder<MembershipDTO> implements Seria
this.customerPrefix = customerPrefix; this.customerPrefix = customerPrefix;
} }
private String getMembershipDisplayReference() {
return membershipDisplayReference;
}
public void setMembershipDisplayReference(final String membershipDisplayReference) {
this.membershipDisplayReference = membershipDisplayReference;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) { if (this == o) {
@ -145,4 +155,20 @@ public class MembershipDTO extends FluentBuilder<MembershipDTO> implements Seria
", customer='" + getCustomerPrefix() + "'" + ", customer='" + getCustomerPrefix() + "'" +
"}"; "}";
} }
@JsonComponent
public static class MembershipJsonSerializer extends JsonSerializerWithAccessFilter<MembershipDTO> {
public MembershipJsonSerializer(final ApplicationContext ctx) {
super(ctx);
}
}
@JsonComponent
public static class MembershipJsonDeserializer extends JsonDeserializerWithAccessFilter<MembershipDTO> {
public MembershipJsonDeserializer(final ApplicationContext ctx) {
super(ctx);
}
}
} }

View File

@ -1,9 +1,14 @@
package org.hostsharing.hsadminng.service.mapper; package org.hostsharing.hsadminng.service.mapper;
import org.hostsharing.hsadminng.domain.*; import org.hostsharing.hsadminng.domain.Customer;
import org.hostsharing.hsadminng.domain.Membership;
import org.hostsharing.hsadminng.service.dto.MembershipDTO; import org.hostsharing.hsadminng.service.dto.MembershipDTO;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.*; import java.util.Objects;
/** /**
* Mapper for the entity Membership and its DTO MembershipDTO. * Mapper for the entity Membership and its DTO MembershipDTO.
@ -13,8 +18,21 @@ public interface MembershipMapper extends EntityMapper<MembershipDTO, Membership
@Mapping(source = "customer.id", target = "customerId") @Mapping(source = "customer.id", target = "customerId")
@Mapping(source = "customer.prefix", target = "customerPrefix") @Mapping(source = "customer.prefix", target = "customerPrefix")
@Mapping(target = "membershipDisplayReference", ignore = true)
MembershipDTO toDto(Membership membership); MembershipDTO toDto(Membership membership);
// TODO BLOG HOWTO: multi-field display reference for selection lists
// also change the filed in the option list in *-update.html
@AfterMapping
default void setMembershipDisplayReference(final @MappingTarget MembershipDTO dto, final Membership entity) {
final Customer customer = entity.getCustomer();
dto.setMembershipDisplayReference(customer.getReference()
+ ":" + customer.getPrefix()
+ " [" + customer.getName() + "] "
+ Objects.toString(entity.getAdmissionDocumentDate()) + " - "
+ Objects.toString(entity.getCancellationDocumentDate(), ""));
}
@Mapping(target = "shares", ignore = true) @Mapping(target = "shares", ignore = true)
@Mapping(target = "assets", ignore = true) @Mapping(target = "assets", ignore = true)
@Mapping(source = "customerId", target = "customer") @Mapping(source = "customerId", target = "customer")

View File

@ -89,7 +89,7 @@
<label class="form-control-label" jhiTranslate="hsadminNgApp.asset.membership" for="field_membership">Membership</label> <label class="form-control-label" jhiTranslate="hsadminNgApp.asset.membership" for="field_membership">Membership</label>
<select class="form-control" id="field_membership" name="membership" [(ngModel)]="asset.membershipId" required> <select class="form-control" id="field_membership" name="membership" [(ngModel)]="asset.membershipId" required>
<option *ngIf="!editForm.value.membership" [ngValue]="null" selected></option> <option *ngIf="!editForm.value.membership" [ngValue]="null" selected></option>
<option [ngValue]="membershipOption.id" *ngFor="let membershipOption of memberships; trackBy: trackMembershipById">{{membershipOption.admissionDocumentDate}}</option> <option [ngValue]="membershipOption.id" *ngFor="let membershipOption of memberships; trackBy: trackMembershipById">{{membershipOption.membershipDisplayReference}}</option>
</select> </select>
</div> </div>
<div [hidden]="!(editForm.controls.membership?.dirty && editForm.controls.membership?.invalid)"> <div [hidden]="!(editForm.controls.membership?.dirty && editForm.controls.membership?.invalid)">

View File

@ -13,6 +13,7 @@ export interface IMembership {
assets?: IAsset[]; assets?: IAsset[];
customerPrefix?: string; customerPrefix?: string;
customerId?: number; customerId?: number;
membershipDisplayReference?: string;
} }
export class Membership implements IMembership { export class Membership implements IMembership {
@ -26,6 +27,7 @@ export class Membership implements IMembership {
public shares?: IShare[], public shares?: IShare[],
public assets?: IAsset[], public assets?: IAsset[],
public customerPrefix?: string, public customerPrefix?: string,
public customerId?: number public customerId?: number,
public membershipDisplayReference?: string
) {} ) {}
} }

View File

@ -27,6 +27,7 @@ import org.springframework.validation.Validator;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.List; import java.util.List;
@ -168,16 +169,16 @@ public class AssetResourceIntTest {
assertThat(testAsset.getDocumentDate()).isEqualTo(DEFAULT_DOCUMENT_DATE); assertThat(testAsset.getDocumentDate()).isEqualTo(DEFAULT_DOCUMENT_DATE);
assertThat(testAsset.getValueDate()).isEqualTo(DEFAULT_VALUE_DATE); assertThat(testAsset.getValueDate()).isEqualTo(DEFAULT_VALUE_DATE);
assertThat(testAsset.getAction()).isEqualTo(DEFAULT_ACTION); assertThat(testAsset.getAction()).isEqualTo(DEFAULT_ACTION);
assertThat(testAsset.getAmount()).isEqualTo(DEFAULT_AMOUNT.setScale(2)); assertThat(testAsset.getAmount()).isEqualTo(DEFAULT_AMOUNT.setScale(2, RoundingMode.HALF_DOWN));
assertThat(testAsset.getRemark()).isEqualTo(DEFAULT_REMARK); assertThat(testAsset.getRemark()).isEqualTo(DEFAULT_REMARK);
} }
@Test @Test
@Transactional @Transactional
public void createAssetWithExistingId() throws Exception { public void createAssetWithIdForNonExistingEntity() throws Exception {
int databaseSizeBeforeCreate = assetRepository.findAll().size(); int databaseSizeBeforeCreate = assetRepository.findAll().size();
// Create the Asset with an existing ID // Create the Asset with an ID
asset.setId(1L); asset.setId(1L);
AssetDTO assetDTO = assetMapper.toDto(asset); AssetDTO assetDTO = assetMapper.toDto(asset);
@ -192,6 +193,27 @@ public class AssetResourceIntTest {
assertThat(assetList).hasSize(databaseSizeBeforeCreate); assertThat(assetList).hasSize(databaseSizeBeforeCreate);
} }
@Test
@Transactional
public void createAssetWithExistingExistingEntity() throws Exception {
// Initialize the database
assetRepository.saveAndFlush(asset);
int databaseSizeBeforeCreate = assetRepository.findAll().size();
// Create the Asset with the ID of an existing ID
AssetDTO assetDTO = assetMapper.toDto(asset);
// An entity with an existing ID cannot be created, so this API call must fail
restAssetMockMvc.perform(post("/api/assets")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(assetDTO)))
.andExpect(status().isBadRequest());
// Validate the Asset in the database
List<Asset> assetList = assetRepository.findAll();
assertThat(assetList).hasSize(databaseSizeBeforeCreate);
}
@Test @Test
@Transactional @Transactional
public void checkDocumentDateIsRequired() throws Exception { public void checkDocumentDateIsRequired() throws Exception {

View File

@ -682,9 +682,7 @@ public class MembershipResourceIntTest {
// Disconnect from session so that the updates on updatedMembership are not directly saved in db // Disconnect from session so that the updates on updatedMembership are not directly saved in db
em.detach(updatedMembership); em.detach(updatedMembership);
updatedMembership updatedMembership
.admissionDocumentDate(UPDATED_ADMISSION_DOCUMENT_DATE)
.cancellationDocumentDate(UPDATED_CANCELLATION_DOCUMENT_DATE) .cancellationDocumentDate(UPDATED_CANCELLATION_DOCUMENT_DATE)
.memberFromDate(UPDATED_MEMBER_FROM_DATE)
.memberUntilDate(UPDATED_MEMBER_UNTIL_DATE) .memberUntilDate(UPDATED_MEMBER_UNTIL_DATE)
.remark(UPDATED_REMARK); .remark(UPDATED_REMARK);
MembershipDTO membershipDTO = membershipMapper.toDto(updatedMembership); MembershipDTO membershipDTO = membershipMapper.toDto(updatedMembership);
@ -698,9 +696,9 @@ public class MembershipResourceIntTest {
List<Membership> membershipList = membershipRepository.findAll(); List<Membership> membershipList = membershipRepository.findAll();
assertThat(membershipList).hasSize(databaseSizeBeforeUpdate); assertThat(membershipList).hasSize(databaseSizeBeforeUpdate);
Membership testMembership = membershipList.get(membershipList.size() - 1); Membership testMembership = membershipList.get(membershipList.size() - 1);
assertThat(testMembership.getAdmissionDocumentDate()).isEqualTo(UPDATED_ADMISSION_DOCUMENT_DATE); assertThat(testMembership.getAdmissionDocumentDate()).isEqualTo(DEFAULT_ADMISSION_DOCUMENT_DATE);
assertThat(testMembership.getCancellationDocumentDate()).isEqualTo(UPDATED_CANCELLATION_DOCUMENT_DATE); assertThat(testMembership.getCancellationDocumentDate()).isEqualTo(UPDATED_CANCELLATION_DOCUMENT_DATE);
assertThat(testMembership.getMemberFromDate()).isEqualTo(UPDATED_MEMBER_FROM_DATE); assertThat(testMembership.getMemberFromDate()).isEqualTo(DEFAULT_MEMBER_FROM_DATE);
assertThat(testMembership.getMemberUntilDate()).isEqualTo(UPDATED_MEMBER_UNTIL_DATE); assertThat(testMembership.getMemberUntilDate()).isEqualTo(UPDATED_MEMBER_UNTIL_DATE);
assertThat(testMembership.getRemark()).isEqualTo(UPDATED_REMARK); assertThat(testMembership.getRemark()).isEqualTo(UPDATED_REMARK);
} }