Skip to content

Commit a7d77af

Browse files
First attempt to make improvements of validation : it is draft .
1 parent 0892f93 commit a7d77af

File tree

6 files changed

+95
-7
lines changed

6 files changed

+95
-7
lines changed

src/main/java/ru/mystamps/web/feature/series/sale/AddSeriesSalesForm.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,29 @@
1717
*/
1818
package ru.mystamps.web.feature.series.sale;
1919

20+
import javax.validation.GroupSequence;
21+
import javax.validation.constraints.NotNull;
22+
import javax.validation.constraints.Size;
2023
import lombok.Getter;
2124
import lombok.Setter;
2225
import org.hibernate.validator.constraints.URL;
2326
import org.springframework.format.annotation.DateTimeFormat;
2427
import ru.mystamps.web.dao.dto.Currency;
28+
import ru.mystamps.web.support.beanvalidation.FieldsMismatch;
2529
import ru.mystamps.web.support.beanvalidation.Group;
2630
import ru.mystamps.web.validation.ValidationRules;
27-
28-
import javax.validation.GroupSequence;
29-
import javax.validation.constraints.NotNull;
30-
import javax.validation.constraints.Size;
3131
import java.math.BigDecimal;
3232
import java.util.Date;
3333

3434
@Getter
3535
@Setter
36+
//@SellerAndBuyerSellSeriesMatch(
37+
// message = "The sellers and buyers should be different"
38+
//)
39+
@FieldsMismatch(
40+
first = "sellerId",
41+
second ="buyerId"
42+
)
3643
public class AddSeriesSalesForm implements AddSeriesSalesDto {
3744

3845
@DateTimeFormat(pattern = "dd.MM.yyyy")

src/main/java/ru/mystamps/web/feature/series/sale/JdbcSeriesSalesDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
import org.apache.commons.lang3.Validate;
2222
import org.springframework.beans.factory.annotation.Value;
2323
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
24-
2524
import java.util.HashMap;
2625
import java.util.Map;
2726

2827
@RequiredArgsConstructor
28+
2929
public class JdbcSeriesSalesDao implements SeriesSalesDao {
3030

3131
private final NamedParameterJdbcTemplate jdbcTemplate;

src/main/java/ru/mystamps/web/feature/series/sale/SeriesSalesServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import org.springframework.security.access.prepost.PreAuthorize;
2424
import org.springframework.transaction.annotation.Transactional;
2525
import ru.mystamps.web.support.spring.security.HasAuthority;
26-
2726
import java.util.Date;
2827

2928
@RequiredArgsConstructor
29+
3030
public class SeriesSalesServiceImpl implements SeriesSalesService {
3131

3232
private final Logger log;

src/main/java/ru/mystamps/web/support/beanvalidation/FieldsMatchValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public boolean isValid(Object value, ConstraintValidatorContext ctx) {
5959

6060
// FIXME: check fields only when both fields are equals
6161

62-
if (!firstFieldValue.equals(secondFieldValue)) {
62+
if (firstFieldValue.equals(secondFieldValue)) {
6363
// bind error message to 2nd field
6464
ConstraintViolationUtils.recreate(
6565
ctx,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2009-2019 Slava Semushin <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.support.beanvalidation;
19+
20+
21+
import javax.validation.Constraint;
22+
import javax.validation.Payload;
23+
import java.lang.annotation.Documented;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.Target;
26+
27+
28+
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
29+
import static java.lang.annotation.ElementType.TYPE;
30+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
31+
32+
@Target({ TYPE, ANNOTATION_TYPE })
33+
@Retention(RUNTIME)
34+
@Constraint(validatedBy = SellerAndBuyerSellSeriesValidator.class)
35+
@Documented
36+
public @interface SellerAndBuyerSellSeriesMatch {
37+
String message();
38+
Class<?>[] groups() default { };
39+
Class<? extends Payload>[] payload() default { };
40+
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ru.mystamps.web.support.beanvalidation;
2+
3+
import javax.validation.ConstraintValidator;
4+
import javax.validation.ConstraintValidatorContext;
5+
import org.springframework.beans.BeanWrapperImpl;
6+
import org.springframework.beans.PropertyAccessor;
7+
8+
public class SellerAndBuyerSellSeriesValidator implements ConstraintValidator<SellerAndBuyerSellSeriesMatch,Object> {
9+
10+
private Integer sellerId;
11+
private Integer buyerId;
12+
13+
@Override
14+
public void initialize(SellerAndBuyerSellSeriesMatch sellerAndBuyerSellSeriesMatch) {
15+
}
16+
17+
@Override
18+
public boolean isValid(Object form, ConstraintValidatorContext ctx) {
19+
if (form == null) {
20+
return true;
21+
}
22+
PropertyAccessor wrapper = new BeanWrapperImpl(form);
23+
sellerId= (Integer) wrapper.getPropertyValue("sellerId");
24+
25+
buyerId=(Integer) wrapper.getPropertyValue("buyerId");
26+
27+
28+
if (sellerId.intValue() == buyerId.intValue()) {
29+
30+
ConstraintViolationUtils.recreate(
31+
ctx,
32+
String.valueOf(buyerId),
33+
ctx.getDefaultConstraintMessageTemplate()
34+
);
35+
return false;
36+
}
37+
38+
return false;
39+
}
40+
}

0 commit comments

Comments
 (0)