25
25
import javax .validation .constraints .NotNull ;
26
26
import javax .validation .constraints .Size ;
27
27
28
+ import org .apache .commons .lang3 .StringUtils ;
29
+
28
30
import org .hibernate .validator .constraints .Range ;
31
+ import org .hibernate .validator .constraints .URL ;
29
32
30
33
import org .springframework .web .multipart .MultipartFile ;
31
34
37
40
import ru .mystamps .web .dao .dto .LinkEntityDto ;
38
41
import ru .mystamps .web .service .dto .AddSeriesDto ;
39
42
import ru .mystamps .web .support .beanvalidation .CatalogNumbers ;
43
+ import ru .mystamps .web .support .beanvalidation .HasImageOrImageUrl ;
40
44
import ru .mystamps .web .support .beanvalidation .ImageFile ;
41
45
import ru .mystamps .web .support .beanvalidation .MaxFileSize ;
42
46
import ru .mystamps .web .support .beanvalidation .MaxFileSize .Unit ;
45
49
import ru .mystamps .web .support .beanvalidation .NotNullIfFirstField ;
46
50
import ru .mystamps .web .support .beanvalidation .Price ;
47
51
import ru .mystamps .web .support .beanvalidation .ReleaseDateIsNotInFuture ;
52
+ import ru .mystamps .web .support .beanvalidation .RequireImageOrImageUrl ;
48
53
49
54
import static ru .mystamps .web .validation .ValidationRules .MAX_DAYS_IN_MONTH ;
50
55
import static ru .mystamps .web .validation .ValidationRules .MAX_IMAGE_SIZE ;
58
63
@ Setter
59
64
// TODO: combine price with currency to separate class
60
65
@ SuppressWarnings ({ "PMD.TooManyFields" , "PMD.AvoidDuplicateLiterals" })
66
+ @ RequireImageOrImageUrl (groups = AddSeriesForm .ImageUrl1Checks .class )
61
67
@ NotNullIfFirstField .List ({
62
68
@ NotNullIfFirstField (
63
69
first = "month" , second = "year" , message = "{month.requires.year}" ,
69
75
)
70
76
})
71
77
@ ReleaseDateIsNotInFuture (groups = AddSeriesForm .ReleaseDate3Checks .class )
72
- public class AddSeriesForm implements AddSeriesDto {
78
+ public class AddSeriesForm implements AddSeriesDto , HasImageOrImageUrl {
73
79
74
80
// FIXME: change type to plain Integer
75
81
@ NotNull
@@ -128,13 +134,48 @@ public class AddSeriesForm implements AddSeriesDto {
128
134
@ Size (max = MAX_SERIES_COMMENT_LENGTH , message = "{value.too-long}" )
129
135
private String comment ;
130
136
131
- @ NotNull
132
- @ NotEmptyFilename (groups = Image1Checks .class )
133
- @ NotEmptyFile (groups = Image2Checks .class )
134
- @ MaxFileSize (value = MAX_IMAGE_SIZE , unit = Unit .Kbytes , groups = Image3Checks .class )
135
- @ ImageFile (groups = Image3Checks .class )
137
+ // Name of this field should match with the value of
138
+ // DownloadImageInterceptor.UPLOADED_IMAGE_FIELD_NAME.
139
+ @ NotEmptyFilename (groups = RequireImageCheck .class )
140
+ @ NotEmptyFile (groups = Image1Checks .class )
141
+ @ MaxFileSize (value = MAX_IMAGE_SIZE , unit = Unit .Kbytes , groups = Image2Checks .class )
142
+ @ ImageFile (groups = Image2Checks .class )
136
143
private MultipartFile image ;
137
144
145
+ // Name of this field must match with the value of DownloadImageInterceptor.URL_PARAMETER_NAME.
146
+ @ URL (groups = AddSeriesForm .ImageUrl2Checks .class )
147
+ private String imageUrl ;
148
+
149
+ // This field holds a file that was downloaded from imageUrl.
150
+ // Name of this field must match with the value of
151
+ // DownloadImageInterceptor.DOWNLOADED_IMAGE_FIELD_NAME.
152
+ @ NotEmptyFile (groups = Image1Checks .class )
153
+ @ MaxFileSize (value = MAX_IMAGE_SIZE , unit = Unit .Kbytes , groups = Image2Checks .class )
154
+ @ ImageFile (groups = Image2Checks .class )
155
+ private MultipartFile downloadedImage ;
156
+
157
+ @ Override
158
+ public MultipartFile getImage () {
159
+ if (hasImage ()) {
160
+ return image ;
161
+ }
162
+
163
+ return downloadedImage ;
164
+ }
165
+
166
+ // This method has to be implemented to satisfy HasImageOrImageUrl requirements.
167
+ // The latter is being used by RequireImageOrImageUrl validator.
168
+ @ Override
169
+ public boolean hasImage () {
170
+ return image != null && StringUtils .isNotEmpty (image .getOriginalFilename ());
171
+ }
172
+
173
+ // This method has to be implemented to satisfy HasImageOrImageUrl requirements.
174
+ // The latter is being used by RequireImageOrImageUrl validator.
175
+ @ Override
176
+ public boolean hasImageUrl () {
177
+ return StringUtils .isNotEmpty (imageUrl );
178
+ }
138
179
139
180
@ GroupSequence ({
140
181
ReleaseDate1Checks .class ,
@@ -153,10 +194,25 @@ public interface ReleaseDate2Checks {
153
194
public interface ReleaseDate3Checks {
154
195
}
155
196
197
+ public interface RequireImageCheck {
198
+ }
199
+
200
+ @ GroupSequence ({
201
+ ImageUrl1Checks .class ,
202
+ ImageUrl2Checks .class ,
203
+ })
204
+ public interface ImageUrlChecks {
205
+ }
206
+
207
+ public interface ImageUrl1Checks {
208
+ }
209
+
210
+ public interface ImageUrl2Checks {
211
+ }
212
+
156
213
@ GroupSequence ({
157
214
Image1Checks .class ,
158
- Image2Checks .class ,
159
- Image3Checks .class
215
+ Image2Checks .class
160
216
})
161
217
public interface ImageChecks {
162
218
}
@@ -167,7 +223,4 @@ public interface Image1Checks {
167
223
public interface Image2Checks {
168
224
}
169
225
170
- public interface Image3Checks {
171
- }
172
-
173
226
}
0 commit comments