-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathAddSeriesForm.java
244 lines (196 loc) · 7.2 KB
/
AddSeriesForm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Copyright (C) 2009-2018 Slava Semushin <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package ru.mystamps.web.feature.series;
import java.math.BigDecimal;
import javax.validation.GroupSequence;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Range;
import org.hibernate.validator.constraints.URL;
import org.springframework.web.multipart.MultipartFile;
import lombok.Getter;
import lombok.Setter;
import ru.mystamps.web.controller.interceptor.DownloadImageInterceptor;
import ru.mystamps.web.dao.dto.LinkEntityDto;
import ru.mystamps.web.feature.category.Category;
import ru.mystamps.web.feature.country.Country;
import ru.mystamps.web.support.beanvalidation.CatalogNumbers;
import ru.mystamps.web.support.beanvalidation.ImageFile;
import ru.mystamps.web.support.beanvalidation.MaxFileSize;
import ru.mystamps.web.support.beanvalidation.MaxFileSize.Unit;
import ru.mystamps.web.support.beanvalidation.NotEmptyFile;
import ru.mystamps.web.support.beanvalidation.NotEmptyFilename;
import ru.mystamps.web.support.beanvalidation.NotNullIfFirstField;
import ru.mystamps.web.support.beanvalidation.Price;
import static ru.mystamps.web.validation.ValidationRules.MAX_DAYS_IN_MONTH;
import static ru.mystamps.web.validation.ValidationRules.MAX_IMAGE_SIZE;
import static ru.mystamps.web.validation.ValidationRules.MAX_MONTHS_IN_YEAR;
import static ru.mystamps.web.validation.ValidationRules.MAX_SERIES_COMMENT_LENGTH;
import static ru.mystamps.web.validation.ValidationRules.MAX_STAMPS_IN_SERIES;
import static ru.mystamps.web.validation.ValidationRules.MIN_RELEASE_YEAR;
import static ru.mystamps.web.validation.ValidationRules.MIN_STAMPS_IN_SERIES;
@Getter
@Setter
@SuppressWarnings({ "PMD.TooManyFields", "PMD.AvoidDuplicateLiterals" })
@RequireImageOrImageUrl(
imageFieldName = DownloadImageInterceptor.UPLOADED_IMAGE_FIELD_NAME,
imageUrlFieldName = DownloadImageInterceptor.IMAGE_URL_FIELD_NAME,
groups = AddSeriesForm.ImageUrl1Checks.class
)
@NotNullIfFirstField.List({
@NotNullIfFirstField(
first = "month", second = "year", message = "{month.requires.year}",
groups = AddSeriesForm.ReleaseDate1Checks.class
),
@NotNullIfFirstField(
first = "day", second = "month", message = "{day.requires.month}",
groups = AddSeriesForm.ReleaseDate1Checks.class
)
})
@ReleaseDateIsNotInFuture(groups = AddSeriesForm.ReleaseDate3Checks.class)
public class AddSeriesForm implements AddSeriesDto, HasImageOrImageUrl, NullableImageUrl {
// FIXME: change type to plain Integer
@NotNull
@Category
private LinkEntityDto category;
// FIXME: change type to plain Integer
@Country
private LinkEntityDto country;
@Range(min = 1, max = MAX_DAYS_IN_MONTH, message = "{day.invalid}")
private Integer day;
@Range(min = 1, max = MAX_MONTHS_IN_YEAR, message = "{month.invalid}")
private Integer month;
@Min(
value = MIN_RELEASE_YEAR,
message = "{series.too-early-release-year}",
groups = ReleaseDate2Checks.class
)
private Integer year;
@NotNull
@Min(MIN_STAMPS_IN_SERIES)
@Max(MAX_STAMPS_IN_SERIES)
private Integer quantity;
@NotNull
private Boolean perforated;
@CatalogNumbers
private String michelNumbers;
@Price
private BigDecimal michelPrice;
// @todo #671 /series/add: add integration test to check that Scott numbers may contain letters
// @todo #671 /series/add: add integration test for Scott numbers error message
@CatalogNumbers(allowLetters = true)
private String scottNumbers;
@Price
private BigDecimal scottPrice;
@CatalogNumbers
private String yvertNumbers;
@Price
private BigDecimal yvertPrice;
@CatalogNumbers
private String gibbonsNumbers;
@Price
private BigDecimal gibbonsPrice;
// @todo #770 /series/add: validate that Solovyov numbers are specified only for stamps
// from USSR/Russia
@CatalogNumbers
private String solovyovNumbers;
@Price
private BigDecimal solovyovPrice;
// @todo #769 /series/add: validate that Zagorski numbers are specified only for stamps
// from USSR/Russia
@CatalogNumbers
private String zagorskiNumbers;
@Price
private BigDecimal zagorskiPrice;
@Size(max = MAX_SERIES_COMMENT_LENGTH, message = "{value.too-long}")
private String comment;
// Name of this field should match with the value of
// DownloadImageInterceptor.UPLOADED_IMAGE_FIELD_NAME.
@NotEmptyFilename(groups = RequireImageCheck.class)
@NotEmptyFile(groups = Image1Checks.class)
@MaxFileSize(value = MAX_IMAGE_SIZE, unit = Unit.Kbytes, groups = Image2Checks.class)
@ImageFile(groups = Image2Checks.class)
private MultipartFile uploadedImage;
// Name of this field must match with the value of DownloadImageInterceptor.IMAGE_URL_FIELD_NAME
@URL(groups = ImageUrl2Checks.class)
private String imageUrl;
// This field holds a file that was downloaded from imageUrl.
// Name of this field must match with the value of
// DownloadImageInterceptor.DOWNLOADED_IMAGE_FIELD_NAME.
@NotEmptyFile(groups = Image1Checks.class)
@MaxFileSize(value = MAX_IMAGE_SIZE, unit = Unit.Kbytes, groups = Image2Checks.class)
@ImageFile(groups = Image2Checks.class)
private MultipartFile downloadedImage;
@Override
public MultipartFile getImage() {
if (hasImage()) {
return uploadedImage;
}
return downloadedImage;
}
// This method has to be implemented to satisfy HasImageOrImageUrl requirements.
// The latter is being used by RequireImageOrImageUrl validator.
@Override
public boolean hasImage() {
return uploadedImage != null && StringUtils.isNotEmpty(uploadedImage.getOriginalFilename());
}
// This method has to be implemented to satisfy HasImageOrImageUrl requirements.
// The latter is being used by RequireImageOrImageUrl validator.
@Override
public boolean hasImageUrl() {
return StringUtils.isNotEmpty(imageUrl);
}
@GroupSequence({
ReleaseDate1Checks.class,
ReleaseDate2Checks.class,
ReleaseDate3Checks.class
})
public interface ReleaseDateChecks {
}
public interface ReleaseDate1Checks {
}
public interface ReleaseDate2Checks {
}
public interface ReleaseDate3Checks {
}
public interface RequireImageCheck {
}
@GroupSequence({
ImageUrl1Checks.class,
ImageUrl2Checks.class,
})
public interface ImageUrlChecks {
}
public interface ImageUrl1Checks {
}
public interface ImageUrl2Checks {
}
@GroupSequence({
Image1Checks.class,
Image2Checks.class
})
public interface ImageChecks {
}
public interface Image1Checks {
}
public interface Image2Checks {
}
}