avoid select before insert and map sub-entities in mapper
This commit is contained in:
@ -21,13 +21,13 @@ class CustomErrorResponse {
|
||||
|
||||
static String firstMessageLine(final Throwable exception) {
|
||||
if (exception.getMessage() != null) {
|
||||
return firstLine(exception.getMessage());
|
||||
return line(exception.getMessage(), 0);
|
||||
}
|
||||
return "ERROR: [500] " + exception.getClass().getName();
|
||||
}
|
||||
|
||||
static String firstLine(final String message) {
|
||||
return message.split("\\r|\\n|\\r\\n", 0)[0];
|
||||
static String line(final String message, final int lineNo) {
|
||||
return message.split("\\r|\\n|\\r\\n", 0)[lineNo];
|
||||
}
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")
|
||||
|
@ -31,21 +31,25 @@ public class RestResponseEntityExceptionHandler
|
||||
protected ResponseEntity<CustomErrorResponse> handleConflict(
|
||||
final RuntimeException exc, final WebRequest request) {
|
||||
|
||||
final var message = firstLine(NestedExceptionUtils.getMostSpecificCause(exc).getMessage());
|
||||
final var rawMessage = NestedExceptionUtils.getMostSpecificCause(exc).getMessage();
|
||||
var message = line(rawMessage, 0);
|
||||
if (message.contains("violates foreign key constraint")) {
|
||||
return errorResponse(request, HttpStatus.BAD_REQUEST, line(rawMessage, 1).replaceAll(" *Detail: *", ""));
|
||||
}
|
||||
return errorResponse(request, HttpStatus.CONFLICT, message);
|
||||
}
|
||||
|
||||
@ExceptionHandler(JpaSystemException.class)
|
||||
protected ResponseEntity<CustomErrorResponse> handleJpaExceptions(
|
||||
final RuntimeException exc, final WebRequest request) {
|
||||
final var message = firstLine(NestedExceptionUtils.getMostSpecificCause(exc).getMessage());
|
||||
final var message = line(NestedExceptionUtils.getMostSpecificCause(exc).getMessage(), 0);
|
||||
return errorResponse(request, httpStatus(message).orElse(HttpStatus.INTERNAL_SERVER_ERROR), message);
|
||||
}
|
||||
|
||||
@ExceptionHandler(NoSuchElementException.class)
|
||||
protected ResponseEntity<CustomErrorResponse> handleNoSuchElementException(
|
||||
final RuntimeException exc, final WebRequest request) {
|
||||
final var message = firstLine(NestedExceptionUtils.getMostSpecificCause(exc).getMessage());
|
||||
final var message = line(NestedExceptionUtils.getMostSpecificCause(exc).getMessage(), 0);
|
||||
return errorResponse(request, HttpStatus.NOT_FOUND, message);
|
||||
}
|
||||
|
||||
@ -54,14 +58,14 @@ public class RestResponseEntityExceptionHandler
|
||||
final RuntimeException exc, final WebRequest request) {
|
||||
final var message =
|
||||
userReadableEntityClassName(
|
||||
firstLine(NestedExceptionUtils.getMostSpecificCause(exc).getMessage()));
|
||||
line(NestedExceptionUtils.getMostSpecificCause(exc).getMessage(), 0));
|
||||
return errorResponse(request, HttpStatus.BAD_REQUEST, message);
|
||||
}
|
||||
|
||||
@ExceptionHandler({ Iban4jException.class, ValidationException.class })
|
||||
protected ResponseEntity<CustomErrorResponse> handleIbanAndBicExceptions(
|
||||
final Throwable exc, final WebRequest request) {
|
||||
final var message = firstLine(NestedExceptionUtils.getMostSpecificCause(exc).getMessage());
|
||||
final var message = line(NestedExceptionUtils.getMostSpecificCause(exc).getMessage(), 0);
|
||||
return errorResponse(request, HttpStatus.BAD_REQUEST, message);
|
||||
}
|
||||
|
||||
|
@ -56,14 +56,13 @@ public class HsOfficeBankAccountController implements HsOfficeBankAccountsApi {
|
||||
BicUtil.validate(body.getBic());
|
||||
|
||||
final var entityToSave = mapper.map(body, HsOfficeBankAccountEntity.class);
|
||||
entityToSave.setUuid(UUID.randomUUID());
|
||||
|
||||
final var saved = bankAccountRepo.save(entityToSave);
|
||||
|
||||
final var uri =
|
||||
MvcUriComponentsBuilder.fromController(getClass())
|
||||
.path("/api/hs/office/BankAccounts/{id}")
|
||||
.buildAndExpand(entityToSave.getUuid())
|
||||
.path("/api/hs/office/bankaccounts/{id}")
|
||||
.buildAndExpand(saved.getUuid())
|
||||
.toUri();
|
||||
final var mapped = mapper.map(saved, HsOfficeBankAccountResource.class);
|
||||
return ResponseEntity.created(uri).body(mapped);
|
||||
|
@ -2,11 +2,13 @@ package net.hostsharing.hsadminng.hs.office.bankaccount;
|
||||
|
||||
import lombok.*;
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
import net.hostsharing.hsadminng.errors.DisplayName;
|
||||
import net.hostsharing.hsadminng.stringify.Stringify;
|
||||
import net.hostsharing.hsadminng.stringify.Stringifyable;
|
||||
import net.hostsharing.hsadminng.errors.DisplayName;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import java.util.UUID;
|
||||
@ -29,7 +31,10 @@ public class HsOfficeBankAccountEntity implements Stringifyable {
|
||||
.withProp(Fields.iban, HsOfficeBankAccountEntity::getIban)
|
||||
.withProp(Fields.bic, HsOfficeBankAccountEntity::getBic);
|
||||
|
||||
private @Id UUID uuid;
|
||||
@Id
|
||||
@GeneratedValue(generator = "UUID")
|
||||
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
||||
private UUID uuid;
|
||||
private String holder;
|
||||
|
||||
private String iban;
|
||||
|
@ -52,14 +52,13 @@ public class HsOfficeContactController implements HsOfficeContactsApi {
|
||||
context.define(currentUser, assumedRoles);
|
||||
|
||||
final var entityToSave = mapper.map(body, HsOfficeContactEntity.class);
|
||||
entityToSave.setUuid(UUID.randomUUID());
|
||||
|
||||
final var saved = contactRepo.save(entityToSave);
|
||||
|
||||
final var uri =
|
||||
MvcUriComponentsBuilder.fromController(getClass())
|
||||
.path("/api/hs/office/contacts/{id}")
|
||||
.buildAndExpand(entityToSave.getUuid())
|
||||
.buildAndExpand(saved.getUuid())
|
||||
.toUri();
|
||||
final var mapped = mapper.map(saved, HsOfficeContactResource.class);
|
||||
return ResponseEntity.created(uri).body(mapped);
|
||||
|
@ -5,11 +5,9 @@ import lombok.experimental.FieldNameConstants;
|
||||
import net.hostsharing.hsadminng.errors.DisplayName;
|
||||
import net.hostsharing.hsadminng.stringify.Stringify;
|
||||
import net.hostsharing.hsadminng.stringify.Stringifyable;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.*;
|
||||
import java.util.UUID;
|
||||
|
||||
import static net.hostsharing.hsadminng.stringify.Stringify.stringify;
|
||||
@ -29,7 +27,11 @@ public class HsOfficeContactEntity implements Stringifyable {
|
||||
.withProp(Fields.label, HsOfficeContactEntity::getLabel)
|
||||
.withProp(Fields.emailAddresses, HsOfficeContactEntity::getEmailAddresses);
|
||||
|
||||
private @Id UUID uuid;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(generator = "UUID")
|
||||
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
||||
private UUID uuid;
|
||||
private String label;
|
||||
|
||||
@Column(name = "postaladdress")
|
||||
|
@ -66,14 +66,13 @@ public class HsOfficeCoopAssetsTransactionController implements HsOfficeCoopAsse
|
||||
validate(requestBody);
|
||||
|
||||
final var entityToSave = mapper.map(requestBody, HsOfficeCoopAssetsTransactionEntity.class);
|
||||
entityToSave.setUuid(UUID.randomUUID());
|
||||
|
||||
final var saved = coopAssetsTransactionRepo.save(entityToSave);
|
||||
|
||||
final var uri =
|
||||
MvcUriComponentsBuilder.fromController(getClass())
|
||||
.path("/api/hs/office/coopassetstransactions/{id}")
|
||||
.buildAndExpand(entityToSave.getUuid())
|
||||
.buildAndExpand(saved.getUuid())
|
||||
.toUri();
|
||||
final var mapped = mapper.map(saved, HsOfficeCoopAssetsTransactionResource.class);
|
||||
return ResponseEntity.created(uri).body(mapped);
|
||||
|
@ -6,6 +6,7 @@ import net.hostsharing.hsadminng.errors.DisplayName;
|
||||
import net.hostsharing.hsadminng.hs.office.membership.HsOfficeMembershipEntity;
|
||||
import net.hostsharing.hsadminng.stringify.Stringify;
|
||||
import net.hostsharing.hsadminng.stringify.Stringifyable;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
import org.hibernate.annotations.Type;
|
||||
import org.hibernate.annotations.TypeDef;
|
||||
|
||||
@ -40,7 +41,10 @@ public class HsOfficeCoopAssetsTransactionEntity implements Stringifyable {
|
||||
.withSeparator(", ")
|
||||
.quotedValues(false);
|
||||
|
||||
private @Id UUID uuid;
|
||||
@Id
|
||||
@GeneratedValue(generator = "UUID")
|
||||
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
||||
private UUID uuid;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "membershipuuid")
|
||||
|
@ -67,14 +67,13 @@ public class HsOfficeCoopSharesTransactionController implements HsOfficeCoopShar
|
||||
validate(requestBody);
|
||||
|
||||
final var entityToSave = mapper.map(requestBody, HsOfficeCoopSharesTransactionEntity.class);
|
||||
entityToSave.setUuid(UUID.randomUUID());
|
||||
|
||||
final var saved = coopSharesTransactionRepo.save(entityToSave);
|
||||
|
||||
final var uri =
|
||||
MvcUriComponentsBuilder.fromController(getClass())
|
||||
.path("/api/hs/office/coopsharestransactions/{id}")
|
||||
.buildAndExpand(entityToSave.getUuid())
|
||||
.buildAndExpand(saved.getUuid())
|
||||
.toUri();
|
||||
final var mapped = mapper.map(saved, HsOfficeCoopSharesTransactionResource.class);
|
||||
return ResponseEntity.created(uri).body(mapped);
|
||||
|
@ -6,6 +6,7 @@ import net.hostsharing.hsadminng.stringify.Stringify;
|
||||
import net.hostsharing.hsadminng.stringify.Stringifyable;
|
||||
import net.hostsharing.hsadminng.errors.DisplayName;
|
||||
import net.hostsharing.hsadminng.hs.office.membership.HsOfficeMembershipEntity;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
import org.hibernate.annotations.Type;
|
||||
import org.hibernate.annotations.TypeDef;
|
||||
|
||||
@ -38,7 +39,10 @@ public class HsOfficeCoopSharesTransactionEntity implements Stringifyable {
|
||||
.withSeparator(", ")
|
||||
.quotedValues(false);
|
||||
|
||||
private @Id UUID uuid;
|
||||
@Id
|
||||
@GeneratedValue(generator = "UUID")
|
||||
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
||||
private UUID uuid;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "membershipuuid")
|
||||
|
@ -59,14 +59,13 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi {
|
||||
context.define(currentUser, assumedRoles);
|
||||
|
||||
final var entityToSave = mapper.map(body, HsOfficeDebitorEntity.class);
|
||||
entityToSave.setUuid(UUID.randomUUID());
|
||||
|
||||
final var saved = debitorRepo.save(entityToSave);
|
||||
|
||||
final var uri =
|
||||
MvcUriComponentsBuilder.fromController(getClass())
|
||||
.path("/api/hs/office/debitors/{id}")
|
||||
.buildAndExpand(entityToSave.getUuid())
|
||||
.buildAndExpand(saved.getUuid())
|
||||
.toUri();
|
||||
final var mapped = mapper.map(saved, HsOfficeDebitorResource.class);
|
||||
return ResponseEntity.created(uri).body(mapped);
|
||||
|
@ -7,6 +7,7 @@ import net.hostsharing.hsadminng.stringify.Stringifyable;
|
||||
import net.hostsharing.hsadminng.hs.office.bankaccount.HsOfficeBankAccountEntity;
|
||||
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactEntity;
|
||||
import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerEntity;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.UUID;
|
||||
@ -30,28 +31,35 @@ public class HsOfficeDebitorEntity implements Stringifyable {
|
||||
.withSeparator(": ")
|
||||
.quotedValues(false);
|
||||
|
||||
private @Id UUID uuid;
|
||||
@Id
|
||||
@GeneratedValue(generator = "UUID")
|
||||
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
||||
private UUID uuid;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "partneruuid")
|
||||
private HsOfficePartnerEntity partner;
|
||||
|
||||
private @Column(name = "debitornumber") Integer debitorNumber;
|
||||
@Column(name = "debitornumber")
|
||||
private Integer debitorNumber;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "billingcontactuuid")
|
||||
private HsOfficeContactEntity billingContact;
|
||||
|
||||
private @Column(name = "vatid") String vatId;
|
||||
private @Column(name = "vatcountrycode") String vatCountryCode;
|
||||
private @Column(name = "vatbusiness") boolean vatBusiness;
|
||||
@Column(name = "vatid")
|
||||
private String vatId;
|
||||
|
||||
@Column(name = "vatcountrycode")
|
||||
private String vatCountryCode;
|
||||
|
||||
@Column(name = "vatbusiness")
|
||||
private boolean vatBusiness;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "refundbankaccountuuid")
|
||||
private HsOfficeBankAccountEntity refundBankAccount;
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return stringify.apply(this);
|
||||
|
@ -61,14 +61,13 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
|
||||
context.define(currentUser, assumedRoles);
|
||||
|
||||
final var entityToSave = mapper.map(body, HsOfficeMembershipEntity.class);
|
||||
entityToSave.setUuid(UUID.randomUUID());
|
||||
|
||||
final var saved = membershipRepo.save(entityToSave);
|
||||
|
||||
final var uri =
|
||||
MvcUriComponentsBuilder.fromController(getClass())
|
||||
.path("/api/hs/office/Memberships/{id}")
|
||||
.buildAndExpand(entityToSave.getUuid())
|
||||
.path("/api/hs/office/memberships/{id}")
|
||||
.buildAndExpand(saved.getUuid())
|
||||
.toUri();
|
||||
final var mapped = mapper.map(saved, HsOfficeMembershipResource.class,
|
||||
SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER);
|
||||
|
@ -9,12 +9,11 @@ import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
|
||||
import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerEntity;
|
||||
import net.hostsharing.hsadminng.stringify.Stringify;
|
||||
import net.hostsharing.hsadminng.stringify.Stringifyable;
|
||||
import org.hibernate.annotations.Fetch;
|
||||
import org.hibernate.annotations.FetchMode;
|
||||
import org.hibernate.annotations.Type;
|
||||
import org.hibernate.annotations.TypeDef;
|
||||
import org.hibernate.annotations.*;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
import java.time.LocalDate;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -48,7 +47,10 @@ public class HsOfficeMembershipEntity implements Stringifyable {
|
||||
.withSeparator(", ")
|
||||
.quotedValues(false);
|
||||
|
||||
private @Id UUID uuid;
|
||||
@Id
|
||||
@GeneratedValue(generator = "UUID")
|
||||
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
||||
private UUID uuid;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "partneruuid")
|
||||
@ -63,7 +65,7 @@ public class HsOfficeMembershipEntity implements Stringifyable {
|
||||
private int memberNumber;
|
||||
|
||||
@Column(name = "validity", columnDefinition = "daterange")
|
||||
private Range<LocalDate> validity = Range.infinite(LocalDate.class);
|
||||
private Range<LocalDate> validity;
|
||||
|
||||
@Column(name = "reasonfortermination")
|
||||
@Enumerated(EnumType.STRING)
|
||||
@ -78,6 +80,13 @@ public class HsOfficeMembershipEntity implements Stringifyable {
|
||||
validity = toPostgresDateRange(getValidity().lower(), validTo);
|
||||
}
|
||||
|
||||
public Range<LocalDate> getValidity() {
|
||||
if ( validity == null ) {
|
||||
validity = Range.infinite(LocalDate.class);
|
||||
};
|
||||
return validity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return stringify.apply(this);
|
||||
|
@ -1,6 +1,7 @@
|
||||
package net.hostsharing.hsadminng.hs.office.partner;
|
||||
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactEntity;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficePartnersApi;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePartnerInsertResource;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePartnerPatchResource;
|
||||
@ -56,16 +57,13 @@ public class HsOfficePartnerController implements HsOfficePartnersApi {
|
||||
context.define(currentUser, assumedRoles);
|
||||
|
||||
final var entityToSave = mapper.map(body, HsOfficePartnerEntity.class);
|
||||
entityToSave.setUuid(UUID.randomUUID());
|
||||
entityToSave.setDetails(mapper.map(body.getDetails(), HsOfficePartnerDetailsEntity.class));
|
||||
entityToSave.getDetails().setUuid(UUID.randomUUID());
|
||||
|
||||
final var saved = partnerRepo.save(entityToSave);
|
||||
|
||||
final var uri =
|
||||
MvcUriComponentsBuilder.fromController(getClass())
|
||||
.path("/api/hs/office/partners/{id}")
|
||||
.buildAndExpand(entityToSave.getUuid())
|
||||
.buildAndExpand(saved.getUuid())
|
||||
.toUri();
|
||||
final var mapped = mapper.map(saved, HsOfficePartnerResource.class);
|
||||
return ResponseEntity.created(uri).body(mapped);
|
||||
|
@ -4,11 +4,9 @@ import lombok.*;
|
||||
import net.hostsharing.hsadminng.stringify.Stringify;
|
||||
import net.hostsharing.hsadminng.stringify.Stringifyable;
|
||||
import net.hostsharing.hsadminng.errors.DisplayName;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDate;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -35,7 +33,10 @@ public class HsOfficePartnerDetailsEntity implements Stringifyable {
|
||||
.withSeparator(", ")
|
||||
.quotedValues(false);
|
||||
|
||||
private @Id UUID uuid;
|
||||
@Id
|
||||
@GeneratedValue(generator = "UUID")
|
||||
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
||||
private UUID uuid;
|
||||
|
||||
private @Column(name = "registrationoffice") String registrationOffice;
|
||||
private @Column(name = "registrationnumber") String registrationNumber;
|
||||
|
@ -6,6 +6,7 @@ import net.hostsharing.hsadminng.stringify.Stringify;
|
||||
import net.hostsharing.hsadminng.stringify.Stringifyable;
|
||||
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactEntity;
|
||||
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntity;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
import org.hibernate.annotations.NotFound;
|
||||
import org.hibernate.annotations.NotFoundAction;
|
||||
|
||||
@ -30,7 +31,10 @@ public class HsOfficePartnerEntity implements Stringifyable {
|
||||
.withSeparator(": ")
|
||||
.quotedValues(false);
|
||||
|
||||
private @Id UUID uuid;
|
||||
@Id
|
||||
@GeneratedValue(generator = "UUID")
|
||||
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
||||
private UUID uuid;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "personuuid", nullable = false)
|
||||
|
@ -52,14 +52,13 @@ public class HsOfficePersonController implements HsOfficePersonsApi {
|
||||
context.define(currentUser, assumedRoles);
|
||||
|
||||
final var entityToSave = mapper.map(body, HsOfficePersonEntity.class);
|
||||
entityToSave.setUuid(UUID.randomUUID());
|
||||
|
||||
final var saved = personRepo.save(entityToSave);
|
||||
|
||||
final var uri =
|
||||
MvcUriComponentsBuilder.fromController(getClass())
|
||||
.path("/api/hs/office/persons/{id}")
|
||||
.buildAndExpand(entityToSave.getUuid())
|
||||
.buildAndExpand(saved.getUuid())
|
||||
.toUri();
|
||||
final var mapped = mapper.map(saved, HsOfficePersonResource.class);
|
||||
return ResponseEntity.created(uri).body(mapped);
|
||||
|
@ -7,6 +7,7 @@ import net.hostsharing.hsadminng.errors.DisplayName;
|
||||
import net.hostsharing.hsadminng.stringify.Stringify;
|
||||
import net.hostsharing.hsadminng.stringify.Stringifyable;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
import org.hibernate.annotations.Type;
|
||||
import org.hibernate.annotations.TypeDef;
|
||||
|
||||
@ -36,7 +37,10 @@ public class HsOfficePersonEntity implements Stringifyable {
|
||||
.withProp(Fields.familyName, HsOfficePersonEntity::getFamilyName)
|
||||
.withProp(Fields.givenName, HsOfficePersonEntity::getGivenName);
|
||||
|
||||
private @Id UUID uuid;
|
||||
@Id
|
||||
@GeneratedValue(generator = "UUID")
|
||||
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
||||
private UUID uuid;
|
||||
|
||||
@Column(name = "persontype")
|
||||
@Enumerated(EnumType.STRING)
|
||||
|
@ -69,7 +69,6 @@ public class HsOfficeRelationshipController implements HsOfficeRelationshipsApi
|
||||
|
||||
final var entityToSave = new HsOfficeRelationshipEntity();
|
||||
entityToSave.setRelType(HsOfficeRelationshipType.valueOf(body.getRelType()));
|
||||
entityToSave.setUuid(UUID.randomUUID());
|
||||
entityToSave.setRelAnchor(relHolderRepo.findByUuid(body.getRelAnchorUuid()).orElseThrow(
|
||||
() -> new NoSuchElementException("cannot find relAnchorUuid " + body.getRelAnchorUuid())
|
||||
));
|
||||
@ -85,7 +84,7 @@ public class HsOfficeRelationshipController implements HsOfficeRelationshipsApi
|
||||
final var uri =
|
||||
MvcUriComponentsBuilder.fromController(getClass())
|
||||
.path("/api/hs/office/relationships/{id}")
|
||||
.buildAndExpand(entityToSave.getUuid())
|
||||
.buildAndExpand(saved.getUuid())
|
||||
.toUri();
|
||||
final var mapped = mapper.map(saved, HsOfficeRelationshipResource.class,
|
||||
RELATIONSHIP_ENTITY_TO_RESOURCE_POSTMAPPER);
|
||||
|
@ -6,6 +6,7 @@ import lombok.experimental.FieldNameConstants;
|
||||
import net.hostsharing.hsadminng.stringify.Stringify;
|
||||
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactEntity;
|
||||
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntity;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
import org.hibernate.annotations.Type;
|
||||
import org.hibernate.annotations.TypeDef;
|
||||
|
||||
@ -34,7 +35,10 @@ public class HsOfficeRelationshipEntity {
|
||||
.withProp(Fields.relHolder, HsOfficeRelationshipEntity::getRelHolder)
|
||||
.withProp(Fields.contact, HsOfficeRelationshipEntity::getContact);
|
||||
|
||||
private @Id UUID uuid;
|
||||
@Id
|
||||
@GeneratedValue(generator = "UUID")
|
||||
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
||||
private UUID uuid;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "relanchoruuid")
|
||||
|
@ -1,12 +1,11 @@
|
||||
package net.hostsharing.hsadminng.hs.office.sepamandate;
|
||||
|
||||
import com.vladmihalcea.hibernate.type.range.Range;
|
||||
import net.hostsharing.hsadminng.mapper.Mapper;
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficeSepaMandatesApi;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeSepaMandateInsertResource;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeSepaMandatePatchResource;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeSepaMandateResource;
|
||||
import net.hostsharing.hsadminng.mapper.Mapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@ -62,14 +61,13 @@ public class HsOfficeSepaMandateController implements HsOfficeSepaMandatesApi {
|
||||
context.define(currentUser, assumedRoles);
|
||||
|
||||
final var entityToSave = mapper.map(body, HsOfficeSepaMandateEntity.class, SEPA_MANDATE_RESOURCE_TO_ENTITY_POSTMAPPER);
|
||||
entityToSave.setUuid(UUID.randomUUID());
|
||||
|
||||
final var saved = SepaMandateRepo.save(entityToSave);
|
||||
|
||||
final var uri =
|
||||
MvcUriComponentsBuilder.fromController(getClass())
|
||||
.path("/api/hs/office/SepaMandates/{id}")
|
||||
.buildAndExpand(entityToSave.getUuid())
|
||||
.path("/api/hs/office/sepamandates/{id}")
|
||||
.buildAndExpand(saved.getUuid())
|
||||
.toUri();
|
||||
final var mapped = mapper.map(saved, HsOfficeSepaMandateResource.class,
|
||||
SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER);
|
||||
|
@ -8,6 +8,7 @@ import net.hostsharing.hsadminng.stringify.Stringify;
|
||||
import net.hostsharing.hsadminng.stringify.Stringifyable;
|
||||
import net.hostsharing.hsadminng.hs.office.bankaccount.HsOfficeBankAccountEntity;
|
||||
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
import org.hibernate.annotations.TypeDef;
|
||||
|
||||
import javax.persistence.*;
|
||||
@ -37,7 +38,10 @@ public class HsOfficeSepaMandateEntity implements Stringifyable {
|
||||
.withSeparator(", ")
|
||||
.quotedValues(false);
|
||||
|
||||
private @Id UUID uuid;
|
||||
@Id
|
||||
@GeneratedValue(generator = "UUID")
|
||||
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
||||
private UUID uuid;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "debitoruuid")
|
||||
|
@ -1,7 +1,14 @@
|
||||
package net.hostsharing.hsadminng.mapper;
|
||||
|
||||
import net.hostsharing.hsadminng.errors.DisplayName;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.validation.ValidationException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.stream.Collectors;
|
||||
@ -11,6 +18,9 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
public class Mapper extends ModelMapper {
|
||||
|
||||
@Autowired
|
||||
EntityManager em;
|
||||
|
||||
public Mapper() {
|
||||
getConfiguration().setAmbiguityIgnored(true);
|
||||
}
|
||||
@ -32,6 +42,45 @@ public class Mapper extends ModelMapper {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public <D> D map(final Object source, final Class<D> destinationType) {
|
||||
final var target = super.map(source, destinationType);
|
||||
for (Field f : destinationType.getDeclaredFields()) {
|
||||
if (f.getAnnotation(ManyToOne.class) == null) {
|
||||
continue;
|
||||
}
|
||||
ReflectionUtils.makeAccessible(f);
|
||||
final var subEntity = ReflectionUtils.getField(f, target);
|
||||
if (subEntity == null) {
|
||||
continue;
|
||||
}
|
||||
final var subEntityUuidField = ReflectionUtils.findField(f.getType(), "uuid");
|
||||
if (subEntityUuidField == null) {
|
||||
continue;
|
||||
}
|
||||
ReflectionUtils.makeAccessible(subEntityUuidField);
|
||||
final var subEntityUuid = ReflectionUtils.getField(subEntityUuidField, subEntity);
|
||||
if (subEntityUuid == null) {
|
||||
continue;
|
||||
}
|
||||
ReflectionUtils.setField(f, target, findEntityById(f.getType(), subEntityUuid));
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
private Object findEntityById(final Class<?> entityClass, final Object subEntityUuid) {
|
||||
// using getReference would be more efficent, but results in very technical error messages
|
||||
final var entity = em.find(entityClass, subEntityUuid);
|
||||
if (entity != null) {
|
||||
return entity;
|
||||
}
|
||||
final var displayNameAnnot = entityClass.getAnnotation(DisplayName.class);
|
||||
final var displayName = displayNameAnnot != null ? displayNameAnnot.value() : entityClass.getSimpleName();
|
||||
throw new ValidationException("Unable to find %s with uuid %s".formatted(
|
||||
displayName, subEntityUuid
|
||||
));
|
||||
}
|
||||
|
||||
public <S, T> T map(final S source, final Class<T> targetClass, final BiConsumer<S, T> postMapper) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
|
@ -2,6 +2,7 @@ package net.hostsharing.hsadminng.rbac.rbacrole;
|
||||
|
||||
import lombok.*;
|
||||
import org.hibernate.annotations.Formula;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
import org.springframework.data.annotation.Immutable;
|
||||
|
||||
import javax.persistence.*;
|
||||
@ -18,6 +19,8 @@ import java.util.UUID;
|
||||
public class RbacRoleEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(generator = "UUID")
|
||||
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
||||
private UUID uuid;
|
||||
|
||||
@Column(name = "objectuuid")
|
||||
|
@ -1,9 +1,11 @@
|
||||
package net.hostsharing.hsadminng.rbac.rbacuser;
|
||||
|
||||
import lombok.*;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
import org.springframework.data.annotation.Immutable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import java.time.LocalDateTime;
|
||||
@ -25,6 +27,8 @@ public class RbacUserEntity {
|
||||
private static DateTimeFormatter DATE_FORMAT_WITH_FULLHOUR = DateTimeFormatter.ofPattern("MM-dd-yyyy HH");
|
||||
|
||||
@Id
|
||||
@GeneratedValue(generator = "UUID")
|
||||
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
||||
private UUID uuid;
|
||||
|
||||
private String name;
|
||||
|
@ -48,16 +48,12 @@ public class TestCustomerController implements TestCustomersApi {
|
||||
|
||||
context.define(currentUser, assumedRoles);
|
||||
|
||||
if (customer.getUuid() == null) {
|
||||
customer.setUuid(UUID.randomUUID());
|
||||
}
|
||||
|
||||
final var saved = testCustomerRepository.save(mapper.map(customer, TestCustomerEntity.class));
|
||||
|
||||
final var uri =
|
||||
MvcUriComponentsBuilder.fromController(getClass())
|
||||
.path("/api/test/customers/{id}")
|
||||
.buildAndExpand(customer.getUuid())
|
||||
.buildAndExpand(saved.getUuid())
|
||||
.toUri();
|
||||
return ResponseEntity.created(uri).body(mapper.map(saved, TestCustomerResource.class));
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.UUID;
|
||||
@ -15,8 +16,14 @@ import java.util.UUID;
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class TestCustomerEntity {
|
||||
private @Id UUID uuid;
|
||||
@Id
|
||||
@GeneratedValue(generator = "UUID")
|
||||
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
||||
private UUID uuid;
|
||||
|
||||
private String prefix;
|
||||
private int reference;
|
||||
private @Column(name="adminusername")String adminUserName;
|
||||
|
||||
@Column(name="adminusername")
|
||||
private String adminUserName;
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import net.hostsharing.hsadminng.test.cust.TestCustomerEntity;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.UUID;
|
||||
@ -17,7 +18,10 @@ import java.util.UUID;
|
||||
@AllArgsConstructor
|
||||
public class TestPackageEntity {
|
||||
|
||||
private @Id UUID uuid;
|
||||
@Id
|
||||
@GeneratedValue(generator = "UUID")
|
||||
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
||||
private UUID uuid;
|
||||
|
||||
@Version
|
||||
private int version;
|
||||
|
Reference in New Issue
Block a user