Skip to content

Commit

Permalink
[SERVER] implement LicensePlateValidator to validate licensePlate
Browse files Browse the repository at this point in the history
  • Loading branch information
Huynh Thanh Binh committed Aug 3, 2020
1 parent 4bc5bcb commit 9bc9bcb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
10 changes: 8 additions & 2 deletions documents/documents/references.txt
Original file line number Diff line number Diff line change
@@ -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/
Expand Down Expand Up @@ -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
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
Original file line number Diff line number Diff line change
@@ -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<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -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<LicensePlateValidation, String> {

@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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,8 +48,7 @@
@Table(name = "[BOOKING]")
public final class BookingEntity extends BaseEntity {

public static final Comparator<Map.Entry<BookingEntity, String>> SORT_BY_CREATED_AT_THEN_BY_BOOKING_ID
= new SortByCreatedAt().thenComparing(new SortById());
public static final Comparator<Map.Entry<BookingEntity, String>> SORT_BY_CREATED_AT_THEN_BY_BOOKING_ID = new SortByCreatedAt().thenComparing(new SortById());

@NaturalId
@Type(type = "uuid-char")
Expand All @@ -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;

Expand Down

0 comments on commit 9bc9bcb

Please sign in to comment.