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
@Transactional
public class AssetService {
public class AssetService implements IdToDtoResolver<AssetDTO> {
private final Logger log = LoggerFactory.getLogger(AssetService.class);

View File

@ -1,6 +1,7 @@
package org.hostsharing.hsadminng.service.dto;
import org.hostsharing.hsadminng.domain.enumeration.AssetAction;
import org.hostsharing.hsadminng.service.AssetService;
import org.hostsharing.hsadminng.service.CustomerService;
import org.hostsharing.hsadminng.service.accessfilter.*;
import org.springframework.boot.jackson.JsonComponent;
@ -18,7 +19,7 @@ import java.util.Objects;
*/
public class AssetDTO implements Serializable, AccessMappings {
@SelfId(resolver = CustomerService.class)
@SelfId(resolver = AssetService.class)
@AccessFor(read = Role.ANY_CUSTOMER_USER)
private Long id;
@ -46,7 +47,7 @@ public class AssetDTO implements Serializable, AccessMappings {
@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})
@AccessFor(init = Role.ANYBODY, update = Role.ANYBODY, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
private String membershipDisplayReference;
public Long getId() {

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})
@ -26,18 +25,18 @@ public class MembershipDTO extends FluentBuilder<MembershipDTO> implements Seria
@AccessFor(init = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
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;
@NotNull
@AccessFor(init = Role.ADMIN, read = {Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT})
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;
@Size(max = 160)
@AccessFor(init = Role.ADMIN, read = Role.SUPPORTER)
@AccessFor(init = Role.ADMIN, update = Role.ADMIN, read = Role.SUPPORTER)
private String remark;
@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})
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() + "] "
+ Objects.toString(entity.getAdmissionDocumentDate()) + " - "
+ 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
) {}
}

View File

@ -27,6 +27,7 @@ import org.springframework.validation.Validator;
import javax.persistence.EntityManager;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;
@ -168,16 +169,16 @@ public class AssetResourceIntTest {
assertThat(testAsset.getDocumentDate()).isEqualTo(DEFAULT_DOCUMENT_DATE);
assertThat(testAsset.getValueDate()).isEqualTo(DEFAULT_VALUE_DATE);
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);
}
@Test
@Transactional
public void createAssetWithExistingId() throws Exception {
public void createAssetWithIdForNonExistingEntity() throws Exception {
int databaseSizeBeforeCreate = assetRepository.findAll().size();
// Create the Asset with an existing ID
// Create the Asset with an ID
asset.setId(1L);
AssetDTO assetDTO = assetMapper.toDto(asset);
@ -192,6 +193,27 @@ public class AssetResourceIntTest {
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
@Transactional
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
em.detach(updatedMembership);
updatedMembership
.admissionDocumentDate(UPDATED_ADMISSION_DOCUMENT_DATE)
.cancellationDocumentDate(UPDATED_CANCELLATION_DOCUMENT_DATE)
.memberFromDate(UPDATED_MEMBER_FROM_DATE)
.memberUntilDate(UPDATED_MEMBER_UNTIL_DATE)
.remark(UPDATED_REMARK);
MembershipDTO membershipDTO = membershipMapper.toDto(updatedMembership);
@ -698,9 +696,9 @@ public class MembershipResourceIntTest {
List<Membership> membershipList = membershipRepository.findAll();
assertThat(membershipList).hasSize(databaseSizeBeforeUpdate);
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.getMemberFromDate()).isEqualTo(UPDATED_MEMBER_FROM_DATE);
assertThat(testMembership.getMemberFromDate()).isEqualTo(DEFAULT_MEMBER_FROM_DATE);
assertThat(testMembership.getMemberUntilDate()).isEqualTo(UPDATED_MEMBER_UNTIL_DATE);
assertThat(testMembership.getRemark()).isEqualTo(UPDATED_REMARK);
}