From 43210fdaa3b08a9b892b142cd39ee05857131941 Mon Sep 17 00:00:00 2001 From: "Valeriy.Vyrva" Date: Mon, 24 Jul 2017 17:50:55 +0300 Subject: [PATCH] OGRN: Implement constraint validator --- .../validator/russian/ogrn/Ogrn.java | 40 +++++++++++++++++++ .../russian/ogrn/OgrnConstraintValidator.java | 29 ++++++++++++++ .../resources/ValidationMessages.properties | 1 + .../ValidationMessages_ru_RU.properties | 1 + 4 files changed, 71 insertions(+) create mode 100644 src/main/java/name/valery1707/validator/russian/ogrn/Ogrn.java create mode 100644 src/main/java/name/valery1707/validator/russian/ogrn/OgrnConstraintValidator.java diff --git a/src/main/java/name/valery1707/validator/russian/ogrn/Ogrn.java b/src/main/java/name/valery1707/validator/russian/ogrn/Ogrn.java new file mode 100644 index 0000000..8728ef1 --- /dev/null +++ b/src/main/java/name/valery1707/validator/russian/ogrn/Ogrn.java @@ -0,0 +1,40 @@ +package name.valery1707.validator.russian.ogrn; + +import javax.validation.Payload; + +public @interface Ogrn { + /** + * @return the error message template + */ + String message() default "{name.valery1707.validator.russian.ogrn.Ogrn.message}"; + + /** + * @return the check mode + */ + CheckMode mode() default CheckMode.ANY; + + /** + * @return the groups the constraint belongs to + */ + Class[] groups() default {}; + + /** + * @return the payload associated to the constraint + */ + Class[] payload() default {}; + + enum CheckMode { + /** + * Support any variant + */ + ANY, + /** + * Support only juridical values + */ + JURIDICAL, + /** + * Support only individual values + */ + INDIVIDUAL; + } +} diff --git a/src/main/java/name/valery1707/validator/russian/ogrn/OgrnConstraintValidator.java b/src/main/java/name/valery1707/validator/russian/ogrn/OgrnConstraintValidator.java new file mode 100644 index 0000000..b2404a7 --- /dev/null +++ b/src/main/java/name/valery1707/validator/russian/ogrn/OgrnConstraintValidator.java @@ -0,0 +1,29 @@ +package name.valery1707.validator.russian.ogrn; + +import javax.annotation.Nonnull; +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; + +public class OgrnConstraintValidator implements ConstraintValidator { + private Ogrn.CheckMode mode; + + @Override + public void initialize(Ogrn constraintAnnotation) { + mode = constraintAnnotation.mode(); + } + + @Override + public boolean isValid(CharSequence value, ConstraintValidatorContext context) { + return value == null || value.length() == 0 || (OgrnValidator.isValid(value).isValid() && isValidType(value, mode)); + } + + private boolean isValidType(@Nonnull CharSequence value, @Nonnull Ogrn.CheckMode mode) { + if (mode.equals(Ogrn.CheckMode.JURIDICAL)) { + return value.length() == OgrnValidator.LEN_JURIDICAL; + } else if (mode.equals(Ogrn.CheckMode.INDIVIDUAL)) { + return value.length() == OgrnValidator.LEN_INDIVIDUAL; + } else { + return true; + } + } +} diff --git a/src/main/resources/ValidationMessages.properties b/src/main/resources/ValidationMessages.properties index 36a3db9..a0abc6c 100644 --- a/src/main/resources/ValidationMessages.properties +++ b/src/main/resources/ValidationMessages.properties @@ -1,2 +1,3 @@ name.valery1707.validator.russian.inn.Inn.message=INN must be correct name.valery1707.validator.russian.kpp.Kpp.message=KPP must be correct +name.valery1707.validator.russian.ogrn.Ogrn.message=OGRN must be correct diff --git a/src/main/resources/ValidationMessages_ru_RU.properties b/src/main/resources/ValidationMessages_ru_RU.properties index aa4c565..0ddd066 100644 --- a/src/main/resources/ValidationMessages_ru_RU.properties +++ b/src/main/resources/ValidationMessages_ru_RU.properties @@ -1,2 +1,3 @@ name.valery1707.validator.russian.inn.Inn.message=\u0418\u041D\u041D \u0434\u043E\u043B\u0436\u0435\u043D \u0431\u044B\u0442\u044C \u043A\u043E\u0440\u0440\u0435\u043A\u0442\u043D\u044B\u043C name.valery1707.validator.russian.kpp.Kpp.message=\u041A\u041F\u041F \u0434\u043E\u043B\u0436\u0435\u043D \u0431\u044B\u0442\u044C \u043A\u043E\u0440\u0440\u0435\u043A\u0442\u043D\u044B\u043C +name.valery1707.validator.russian.ogrn.Ogrn.message=\u041E\u0413\u0420\u041D/\u041E\u0413\u0420\u041D\u0418\u041F \u0434\u043E\u043B\u0436\u0435\u043D \u0431\u044B\u0442\u044C \u043A\u043E\u0440\u0440\u0435\u043A\u0442\u043D\u044B\u043C