|
18 | 18 | package ru.mystamps.web.controller.dto;
|
19 | 19 |
|
20 | 20 | import javax.validation.GroupSequence;
|
21 |
| -import javax.validation.constraints.NotNull; |
| 21 | + |
| 22 | +import org.apache.commons.lang3.StringUtils; |
| 23 | + |
| 24 | +import org.hibernate.validator.constraints.URL; |
22 | 25 |
|
23 | 26 | import org.springframework.web.multipart.MultipartFile;
|
24 | 27 |
|
25 | 28 | import lombok.Getter;
|
26 | 29 | import lombok.Setter;
|
27 | 30 |
|
28 | 31 | import ru.mystamps.web.service.dto.AddImageDto;
|
| 32 | +import ru.mystamps.web.support.beanvalidation.HasImageOrImageUrl; |
29 | 33 | import ru.mystamps.web.support.beanvalidation.ImageFile;
|
30 | 34 | import ru.mystamps.web.support.beanvalidation.MaxFileSize;
|
31 | 35 | import ru.mystamps.web.support.beanvalidation.MaxFileSize.Unit;
|
32 | 36 | import ru.mystamps.web.support.beanvalidation.NotEmptyFile;
|
33 | 37 | import ru.mystamps.web.support.beanvalidation.NotEmptyFilename;
|
| 38 | +import ru.mystamps.web.support.beanvalidation.RequireImageOrImageUrl; |
34 | 39 |
|
35 | 40 | import static ru.mystamps.web.validation.ValidationRules.MAX_IMAGE_SIZE;
|
36 | 41 |
|
37 | 42 | @Getter
|
38 | 43 | @Setter
|
39 |
| -@GroupSequence({ |
40 |
| - AddImageForm.class, |
41 |
| - Group.Level1.class, |
42 |
| - Group.Level2.class, |
43 |
| - Group.Level3.class, |
44 |
| - Group.Level4.class, |
45 |
| - Group.Level5.class |
46 |
| -}) |
47 |
| -public class AddImageForm implements AddImageDto { |
48 |
| - |
49 |
| - @NotNull(groups = Group.Level1.class) |
50 |
| - @NotEmptyFilename(groups = Group.Level2.class) |
51 |
| - @NotEmptyFile(groups = Group.Level3.class) |
52 |
| - @MaxFileSize(value = MAX_IMAGE_SIZE, unit = Unit.Kbytes, groups = Group.Level4.class) |
53 |
| - @ImageFile(groups = Group.Level5.class) |
| 44 | +@RequireImageOrImageUrl(groups = AddImageForm.ImageUrl1Checks.class) |
| 45 | +public class AddImageForm implements AddImageDto, HasImageOrImageUrl, NullableImageUrl { |
| 46 | + |
| 47 | + // Name of this field should match with the value of |
| 48 | + // DownloadImageInterceptor.UPLOADED_IMAGE_FIELD_NAME. |
| 49 | + @NotEmptyFilename(groups = RequireImageCheck.class) |
| 50 | + @NotEmptyFile(groups = Group.Level1.class) |
| 51 | + @MaxFileSize(value = MAX_IMAGE_SIZE, unit = Unit.Kbytes, groups = Group.Level2.class) |
| 52 | + @ImageFile(groups = Group.Level2.class) |
54 | 53 | private MultipartFile image;
|
55 | 54 |
|
| 55 | + // Name of this field must match with the value of DownloadImageInterceptor.URL_PARAMETER_NAME. |
| 56 | + @URL(groups = ImageUrl2Checks.class) |
| 57 | + private String imageUrl; |
| 58 | + |
| 59 | + // This field holds a file that was downloaded from imageUrl. |
| 60 | + // Name of this field must match with the value of |
| 61 | + // DownloadImageInterceptor.DOWNLOADED_IMAGE_FIELD_NAME. |
| 62 | + @NotEmptyFile(groups = Group.Level1.class) |
| 63 | + @MaxFileSize(value = MAX_IMAGE_SIZE, unit = Unit.Kbytes, groups = Group.Level2.class) |
| 64 | + @ImageFile(groups = Group.Level2.class) |
| 65 | + private MultipartFile downloadedImage; |
| 66 | + |
| 67 | + @Override |
| 68 | + public MultipartFile getImage() { |
| 69 | + if (hasImage()) { |
| 70 | + return image; |
| 71 | + } |
| 72 | + |
| 73 | + return downloadedImage; |
| 74 | + } |
| 75 | + |
| 76 | + // This method has to be implemented to satisfy HasImageOrImageUrl requirements. |
| 77 | + // The latter is being used by RequireImageOrImageUrl validator. |
| 78 | + @Override |
| 79 | + public boolean hasImage() { |
| 80 | + return image != null && StringUtils.isNotEmpty(image.getOriginalFilename()); |
| 81 | + } |
| 82 | + |
| 83 | + // This method has to be implemented to satisfy HasImageOrImageUrl requirements. |
| 84 | + // The latter is being used by RequireImageOrImageUrl validator. |
| 85 | + @Override |
| 86 | + public boolean hasImageUrl() { |
| 87 | + return StringUtils.isNotEmpty(imageUrl); |
| 88 | + } |
| 89 | + |
| 90 | + public interface RequireImageCheck { |
| 91 | + } |
| 92 | + |
| 93 | + @GroupSequence({ |
| 94 | + ImageUrl1Checks.class, |
| 95 | + ImageUrl2Checks.class, |
| 96 | + }) |
| 97 | + public interface ImageUrlChecks { |
| 98 | + } |
| 99 | + |
| 100 | + public interface ImageUrl1Checks { |
| 101 | + } |
| 102 | + |
| 103 | + public interface ImageUrl2Checks { |
| 104 | + } |
| 105 | + |
| 106 | + @GroupSequence({ |
| 107 | + Group.Level1.class, |
| 108 | + Group.Level2.class |
| 109 | + }) |
| 110 | + public interface ImageChecks { |
| 111 | + } |
| 112 | + |
56 | 113 | }
|
0 commit comments