From 9bc9bcbb9258dfc036a982dd522663aa677e9fb1 Mon Sep 17 00:00:00 2001 From: Huynh Thanh Binh <1653006@student.hcmus.edu.vn> Date: Mon, 3 Aug 2020 15:41:24 +0700 Subject: [PATCH] [SERVER] implement LicensePlateValidator to validate licensePlate --- documents/documents/references.txt | 10 +++++-- .../annotation/LicensePlateValidation.java | 28 +++++++++++++++++++ .../annotation/LicensePlateValidator.java | 18 ++++++++++++ .../service/booking/entity/BookingEntity.java | 5 ++-- 4 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 service/booking-service/src/main/java/com/bht/saigonparking/service/booking/annotation/LicensePlateValidation.java create mode 100644 service/booking-service/src/main/java/com/bht/saigonparking/service/booking/annotation/LicensePlateValidator.java diff --git a/documents/documents/references.txt b/documents/documents/references.txt index 370838f1..9676543d 100644 --- a/documents/documents/references.txt +++ b/documents/documents/references.txt @@ -1,6 +1,6 @@ Document's name: Saigon Parking Project References Author: Huynh Thanh Binh -Last updated: Sat, Aug 2nd, 2020 +Last updated: Mon, Aug 3rd, 2020 ======================================================================================================================== 001. gRPC error handling: https://grpc.io/docs/guides/error/ @@ -390,4 +390,10 @@ Last updated: Sat, Aug 2nd, 2020 https://www.danvega.dev/blog/2016/01/13/sending-async-emails-in-spring/ 126. How To Do @Async in Spring - https://www.baeldung.com/spring-async \ No newline at end of file + https://www.baeldung.com/spring-async + +127. Spring DTO validation using ConstraintValidator + https://stackoverflow.com/questions/56381693/spring-dto-validation-using-constraintvalidator + +128. Dynamic DTO Validation Config - Retrieved from the Database + https://www.baeldung.com/spring-dynamic-dto-validation \ No newline at end of file diff --git a/service/booking-service/src/main/java/com/bht/saigonparking/service/booking/annotation/LicensePlateValidation.java b/service/booking-service/src/main/java/com/bht/saigonparking/service/booking/annotation/LicensePlateValidation.java new file mode 100644 index 00000000..37b03d0b --- /dev/null +++ b/service/booking-service/src/main/java/com/bht/saigonparking/service/booking/annotation/LicensePlateValidation.java @@ -0,0 +1,28 @@ +package com.bht.saigonparking.service.booking.annotation; + +import static java.lang.annotation.ElementType.*; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.validation.Constraint; +import javax.validation.Payload; + +/** + * + * @author bht + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Constraint(validatedBy = LicensePlateValidator.class) +@Target({FIELD, METHOD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER}) +public @interface LicensePlateValidation { + + String message() default "Number license plate is in wrong format !"; + + Class[] groups() default {}; + + Class[] payload() default {}; +} \ No newline at end of file diff --git a/service/booking-service/src/main/java/com/bht/saigonparking/service/booking/annotation/LicensePlateValidator.java b/service/booking-service/src/main/java/com/bht/saigonparking/service/booking/annotation/LicensePlateValidator.java new file mode 100644 index 00000000..e00f33c5 --- /dev/null +++ b/service/booking-service/src/main/java/com/bht/saigonparking/service/booking/annotation/LicensePlateValidator.java @@ -0,0 +1,18 @@ +package com.bht.saigonparking.service.booking.annotation; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; + +/** + * + * @author bht + */ +public final class LicensePlateValidator implements ConstraintValidator { + + @Override + public boolean isValid(String numberLicensePlate, ConstraintValidatorContext constraintValidatorContext) { + return numberLicensePlate + .replaceAll("\\s+|\\.+", "") // remove all space and dot characters exist in string + .matches("^[0-9]{1,2}[A-Za-z][0-9]?-[0-9]{4,5}$"); // example: 59H1-76217, 54L6-2908, 51B-1234, 86B-56789 + } +} \ No newline at end of file diff --git a/service/booking-service/src/main/java/com/bht/saigonparking/service/booking/entity/BookingEntity.java b/service/booking-service/src/main/java/com/bht/saigonparking/service/booking/entity/BookingEntity.java index 5343e5a4..35bedd52 100644 --- a/service/booking-service/src/main/java/com/bht/saigonparking/service/booking/entity/BookingEntity.java +++ b/service/booking-service/src/main/java/com/bht/saigonparking/service/booking/entity/BookingEntity.java @@ -21,6 +21,7 @@ import org.hibernate.annotations.Type; import com.bht.saigonparking.common.base.BaseEntity; +import com.bht.saigonparking.service.booking.annotation.LicensePlateValidation; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; @@ -47,8 +48,7 @@ @Table(name = "[BOOKING]") public final class BookingEntity extends BaseEntity { - public static final Comparator> SORT_BY_CREATED_AT_THEN_BY_BOOKING_ID - = new SortByCreatedAt().thenComparing(new SortById()); + public static final Comparator> SORT_BY_CREATED_AT_THEN_BY_BOOKING_ID = new SortByCreatedAt().thenComparing(new SortById()); @NaturalId @Type(type = "uuid-char") @@ -61,6 +61,7 @@ public final class BookingEntity extends BaseEntity { @Column(name = "[CUSTOMER_ID]", nullable = false) private Long customerId; + @LicensePlateValidation @Column(name = "[CUSTOMER_LICENSE_PLATE]", nullable = false) private String customerLicensePlate;