MembershipDTO JSonSerializerWithAccessFilter proper membershipDisplayReference

This commit is contained in:
Michael Hoennig 2019-04-26 17:42:30 +02:00
parent 4ad0d5d954
commit da5b667607
4 changed files with 55 additions and 9 deletions

View File

@ -2,10 +2,9 @@ package org.hostsharing.hsadminng.service.dto;
import org.hostsharing.hsadminng.service.CustomerService;
import org.hostsharing.hsadminng.service.MembershipService;
import org.hostsharing.hsadminng.service.accessfilter.AccessFor;
import org.hostsharing.hsadminng.service.accessfilter.ParentId;
import org.hostsharing.hsadminng.service.accessfilter.Role;
import org.hostsharing.hsadminng.service.accessfilter.SelfId;
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;
@ -16,7 +15,7 @@ import java.util.Objects;
/**
* 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)
@AccessFor(read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
@ -47,6 +46,9 @@ public class MembershipDTO extends FluentBuilder<MembershipDTO> implements Seria
@AccessFor(init = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
private String customerPrefix;
@AccessFor(read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
private String membershipDisplayReference;
public Long getId() {
return id;
}
@ -111,6 +113,14 @@ public class MembershipDTO extends FluentBuilder<MembershipDTO> implements Seria
this.customerPrefix = customerPrefix;
}
private String getMembershipDisplayReference() {
return membershipDisplayReference;
}
public void setMembershipDisplayReference(final String membershipDisplayReference) {
this.membershipDisplayReference = membershipDisplayReference;
}
@Override
public boolean equals(Object o) {
if (this == o) {
@ -145,4 +155,20 @@ public class MembershipDTO extends FluentBuilder<MembershipDTO> implements Seria
", 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;
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.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.
@ -13,8 +18,21 @@ public interface MembershipMapper extends EntityMapper<MembershipDTO, Membership
@Mapping(source = "customer.id", target = "customerId")
@Mapping(source = "customer.prefix", target = "customerPrefix")
@Mapping(target = "membershipDisplayReference", ignore = true)
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() + "] "
+ entity.getAdmissionDocumentDate().toString() + " - "
+ Objects.toString(entity.getCancellationDocumentDate(), ""));
}
@Mapping(target = "shares", ignore = true)
@Mapping(target = "assets", ignore = true)
@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>
<select class="form-control" id="field_membership" name="membership" [(ngModel)]="asset.membershipId" required>
<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>
</div>
<div [hidden]="!(editForm.controls.membership?.dirty && editForm.controls.membership?.invalid)">

View File

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