Skip to content

Commit 28eab06

Browse files
committed
task: implement extraction of MNH/MVLH conditions for a series sale import and creation.
Part of #1326
1 parent 487ef61 commit 28eab06

24 files changed

+142
-10
lines changed

src/main/frontend/src/components/SeriesSaleImportForm.js

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ class SeriesSaleImportForm extends React.Component {
8181
document.getElementById('seller').value = data.sellerId;
8282
}
8383

84+
if (data.condition) {
85+
document.getElementById('condition').value = data.condition;
86+
}
87+
8488
this.setState({ isDisabled: false, url: '' });
8589

8690
}).catch(error => {

src/main/java/ru/mystamps/web/feature/series/importing/ImportSeriesSalesForm.java

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.format.annotation.NumberFormat;
2323
import ru.mystamps.web.common.Currency;
2424
import ru.mystamps.web.feature.series.sale.AddSeriesSalesDto;
25+
import ru.mystamps.web.feature.series.sale.SeriesCondition;
2526

2627
import javax.validation.constraints.NotNull;
2728
import java.math.BigDecimal;
@@ -64,4 +65,9 @@ public Integer getBuyerId() {
6465
return null;
6566
}
6667

68+
@Override
69+
public SeriesCondition getCondition() {
70+
return null;
71+
}
72+
6773
}

src/main/java/ru/mystamps/web/feature/series/importing/RawParsedDataDto.java

+1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ public class RawParsedDataDto {
3636
private final String currency;
3737
private final String altPrice;
3838
private final String altCurrency;
39+
private final String condition;
3940
}

src/main/java/ru/mystamps/web/feature/series/importing/SeriesExtractedInfo.java

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import lombok.Getter;
2121
import lombok.RequiredArgsConstructor;
22+
import ru.mystamps.web.feature.series.sale.SeriesCondition;
2223

2324
import java.math.BigDecimal;
2425
import java.util.List;
@@ -44,4 +45,5 @@ public class SeriesExtractedInfo {
4445
private final String currency;
4546
private final BigDecimal altPrice;
4647
private final String altCurrency;
48+
private final SeriesCondition condition;
4749
}

src/main/java/ru/mystamps/web/feature/series/importing/SeriesInfoExtractorServiceImpl.java

+31-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import ru.mystamps.web.feature.country.CountryValidation;
2929
import ru.mystamps.web.feature.participant.ParticipantService;
3030
import ru.mystamps.web.feature.series.SeriesValidation;
31+
import ru.mystamps.web.feature.series.sale.SeriesCondition;
3132

3233
import java.math.BigDecimal;
3334
import java.net.MalformedURLException;
@@ -116,6 +117,7 @@ public SeriesExtractedInfo extract(String pageUrl, RawParsedDataDto data) {
116117
String currency = extractCurrency(data.getCurrency());
117118
BigDecimal altPrice = extractPrice(data.getAltPrice());
118119
String altCurrency = extractCurrency(data.getAltCurrency());
120+
SeriesCondition condition = extractCondition(data.getCondition());
119121

120122
return new SeriesExtractedInfo(
121123
categoryIds,
@@ -133,7 +135,8 @@ public SeriesExtractedInfo extract(String pageUrl, RawParsedDataDto data) {
133135
price,
134136
currency,
135137
altPrice,
136-
altCurrency
138+
altCurrency,
139+
condition
137140
);
138141
}
139142

@@ -494,6 +497,33 @@ public SeriesExtractedInfo extract(String pageUrl, RawParsedDataDto data) {
494497
return null;
495498
}
496499

500+
// @todo #1326 SeriesInfoExtractorServiceImpl.extractCondition(): add unit tests
501+
@SuppressWarnings({
502+
"checkstyle:missingswitchdefault",
503+
"PMD.TooFewBranchesForASwitchStatement",
504+
"PMD.SwitchStmtsShouldHaveDefault"
505+
})
506+
/* default */ SeriesCondition extractCondition(String fragment) {
507+
if (StringUtils.isBlank(fragment)) {
508+
return null;
509+
}
510+
511+
String[] candidates = StringUtils.split(fragment, ' ');
512+
for (String candidate : candidates) {
513+
switch(candidate) {
514+
case "MNH":
515+
case "MVLH":
516+
SeriesCondition condition = SeriesCondition.valueOf(candidate);
517+
log.debug("Condition is {}", condition);
518+
return condition;
519+
}
520+
}
521+
522+
log.debug("Could not extract condition from a fragment");
523+
524+
return null;
525+
}
526+
497527
private Integer extractSellerByNameAndUrl(String name, String url) {
498528
log.debug("Determine seller by name '{}' and url '{}'", name, url);
499529

src/main/java/ru/mystamps/web/feature/series/importing/event/DownloadingSucceededEventListener.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ public void onApplicationEvent(DownloadingSucceeded event) {
9898
info.getPrice(),
9999
info.getCurrency(),
100100
info.getAltPrice(),
101-
info.getAltCurrency()
101+
info.getAltCurrency(),
102+
info.getCondition()
102103
);
103104

104105
SeriesExtractedInfo seriesInfo = extractorService.extract(event.getUrl(), data);

src/main/java/ru/mystamps/web/feature/series/importing/extractor/JsoupSiteParser.java

+12
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public SeriesInfo parse(String htmlPage) {
107107
info.setCurrency(extractCurrency(body));
108108
info.setAltPrice(extractAltPrice(body));
109109
info.setAltCurrency(extractAltCurrency(body));
110+
info.setCondition(extractCondition(body));
110111

111112
return info;
112113
}
@@ -271,6 +272,17 @@ protected String extractAltCurrency(Element body) {
271272
return currency;
272273
}
273274

275+
// @todo #1326 JsoupSiteParser.extractCondition(): add unit tests
276+
protected String extractCondition(Element body) {
277+
String description = getTextOfTheFirstElement(body, shortDescriptionLocator);
278+
if (description == null) {
279+
return null;
280+
}
281+
282+
LOG.debug("Extracted condition: '{}'", description);
283+
return description;
284+
}
285+
274286
private static Element getFirstElement(Element body, String locator) {
275287
if (locator == null) {
276288
return null;

src/main/java/ru/mystamps/web/feature/series/importing/extractor/SeriesInfo.java

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class SeriesInfo {
4343
private String currency;
4444
private String altPrice;
4545
private String altCurrency;
46+
private String condition;
4647

4748
/**
4849
* Check whether any info about a series is available.

src/main/java/ru/mystamps/web/feature/series/importing/sale/SeriesSaleExtractedInfo.java

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import lombok.Getter;
2121
import lombok.RequiredArgsConstructor;
22+
import ru.mystamps.web.feature.series.sale.SeriesCondition;
2223

2324
import java.math.BigDecimal;
2425

@@ -30,4 +31,5 @@ public class SeriesSaleExtractedInfo {
3031
private final String currency;
3132
private final BigDecimal altPrice;
3233
private final String altCurrency;
34+
private final SeriesCondition condition;
3335
}

src/main/java/ru/mystamps/web/feature/series/importing/sale/SeriesSalesImportServiceImpl.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ public SeriesSaleExtractedInfo downloadAndParse(String url) {
7777
info.getPrice(),
7878
info.getCurrency(),
7979
info.getAltPrice(),
80-
info.getAltCurrency()
80+
info.getAltCurrency(),
81+
info.getCondition()
8182
);
8283

8384
// CheckStyle: ignore LineLength for next 1 line
@@ -89,7 +90,8 @@ public SeriesSaleExtractedInfo downloadAndParse(String url) {
8990
seriesInfo.getPrice(),
9091
seriesInfo.getCurrency(),
9192
seriesInfo.getAltPrice(),
92-
seriesInfo.getAltCurrency()
93+
seriesInfo.getAltCurrency(),
94+
seriesInfo.getCondition()
9395
);
9496
}
9597

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

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class AddSeriesSalesDbDto {
3535
private BigDecimal altPrice;
3636
private String altCurrency;
3737
private Integer buyerId;
38+
private String condition;
3839
private Date createdAt;
3940
private Integer createdBy;
4041
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ public interface AddSeriesSalesDto {
3232
BigDecimal getAltPrice();
3333
Currency getAltCurrency();
3434
Integer getBuyerId();
35+
SeriesCondition getCondition();
3536
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public class AddSeriesSalesForm implements AddSeriesSalesDto {
7070

7171
private Integer buyerId;
7272

73+
// @todo #1326 Series sale import: add integration test for series condition
74+
private SeriesCondition condition;
75+
7376
@GroupSequence({
7477
Group.Level1.class,
7578
Group.Level2.class,

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

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public void add(AddSeriesSalesDbDto sale) {
4545
params.put("alt_price", sale.getAltPrice());
4646
params.put("alt_currency", sale.getAltCurrency());
4747
params.put("buyer_id", sale.getBuyerId());
48+
params.put("condition", sale.getCondition());
4849
params.put("created_at", sale.getCreatedAt());
4950
params.put("created_by", sale.getCreatedBy());
5051

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (C) 2009-2020 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+
19+
package ru.mystamps.web.feature.series.sale;
20+
21+
public enum SeriesCondition {
22+
MNH,
23+
MVLH,
24+
CANCELLED;
25+
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public void add(AddSeriesSalesDto dto, Integer seriesId, Integer userId) {
5454
sale.setAltCurrency(dto.getAltCurrency().toString());
5555
}
5656
sale.setBuyerId(dto.getBuyerId());
57+
if (dto.getCondition() != null) {
58+
sale.setCondition(dto.getCondition().toString());
59+
}
5760

5861
Date now = new Date();
5962
sale.setCreatedAt(now);

src/main/java/ru/mystamps/web/feature/site/ResourceUrl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public final class ResourceUrl {
3232
public static final String STATIC_RESOURCES_URL = "https://stamps.filezz.ru";
3333

3434
// MUST be updated when any of our resources were modified
35-
public static final String RESOURCES_VERSION = "v0.4.3.4";
35+
public static final String RESOURCES_VERSION = "v0.4.3.5";
3636

3737
// CheckStyle: ignore LineLength for next 15 lines
3838
private static final String CATALOG_UTILS_JS = "/public/js/" + RESOURCES_VERSION + "/CatalogUtils.min.js";

src/main/resources/ru/mystamps/i18n/Messages.properties

+2
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ t_today = Today
155155
t_seller = Seller
156156
t_add_new_seller = Add a new seller
157157
t_price = Price
158+
t_condition = Condition
159+
t_cancelled = Cancelled
158160
t_alternative_price = Alternative price
159161
t_buyer = Buyer
160162
t_add_new_buyer = Add a new buyer

src/main/resources/ru/mystamps/i18n/Messages_ru.properties

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ t_today = Сегодня
154154
t_seller = Продавец
155155
t_add_new_seller = Добавить нового продавца
156156
t_price = Цена
157+
t_condition = Состояние
158+
t_cancelled = Гашеная
157159
t_alternative_price = Альтернативная цена
158160
t_buyer = Покупатель
159161
t_add_new_buyer = Добавить нового покупателя

src/main/resources/sql/series_sales_dao_queries.properties

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ INSERT \
1010
, second_price \
1111
, second_currency \
1212
, buyer_id \
13+
, condition \
1314
, created_at \
1415
, created_by \
1516
) \
@@ -23,6 +24,7 @@ VALUES \
2324
, :alt_price \
2425
, :alt_currency \
2526
, :buyer_id \
27+
, :condition \
2628
, :created_at \
2729
, :created_by \
2830
)

src/main/webapp/WEB-INF/views/series/info.html

+17
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,23 @@ <h5 th:text="#{t_add_info_who_selling_series}">Add info about selling/buying thi
756756
</div>
757757
</div>
758758

759+
<div class="form-group form-group-sm" th:classappend="${#fields.hasErrors('condition') ? 'has-error' : ''}">
760+
<label for="condition" class="control-label col-sm-3" th:text="#{t_condition}">
761+
Condition
762+
</label>
763+
<div class="col-sm-3">
764+
<select id="condition" class="form-control" th:field="*{condition}">
765+
<option value=""></option>
766+
<option value="MNH">MNH</option>
767+
<option value="MVLH">MVLH</option>
768+
<option value="CANCELLED" th:text="#{t_cancelled}">Сancelled</option>
769+
</select>
770+
<!--/*/
771+
<span id="condition.errors" class="help-block" th:if="${#fields.hasErrors('condition')}" th:each="error : ${#fields.errors('condition')}" th:text="${error}"></span>
772+
/*/-->
773+
</div>
774+
</div>
775+
759776
<div class="form-group form-group-sm" th:classappend="${#fields.hasErrors('buyerId') ? 'has-error' : ''}">
760777
<label for="buyer" class="control-label col-sm-3" th:text="#{t_buyer}">
761778
Buyer

src/test/groovy/ru/mystamps/web/feature/series/sale/SeriesSalesServiceImplTest.groovy

+9-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
*/
1818
package ru.mystamps.web.feature.series.sale
1919

20+
import static io.qala.datagen.RandomShortApi.nullOr
21+
2022
import org.slf4j.helpers.NOPLogger
2123
import ru.mystamps.web.common.Currency
2224
import ru.mystamps.web.tests.DateUtils
25+
import ru.mystamps.web.tests.Random
2326
import spock.lang.Specification
2427
import spock.lang.Unroll
2528

@@ -108,6 +111,10 @@ class SeriesSalesServiceImplTest extends Specification {
108111
Integer buyerId) {
109112

110113
given:
114+
Integer expectedSeriesId = 777
115+
Integer expectedUserId = 888
116+
SeriesCondition expectedCondition = nullOr(Random.seriesCondition())
117+
and:
111118
// mandatory fields
112119
form.setSellerId(444)
113120
form.setPrice(new BigDecimal('100'))
@@ -118,9 +125,7 @@ class SeriesSalesServiceImplTest extends Specification {
118125
form.setAltPrice(altPrice)
119126
form.setAltCurrency(altCurrency)
120127
form.setBuyerId(buyerId)
121-
and:
122-
Integer expectedSeriesId = 777
123-
Integer expectedUserId = 888
128+
form.setCondition(expectedCondition)
124129
when:
125130
service.add(form, expectedSeriesId, expectedUserId)
126131
then:
@@ -133,6 +138,7 @@ class SeriesSalesServiceImplTest extends Specification {
133138
assert dto?.altPrice == form.altPrice
134139
assert dto?.altCurrency == form.altCurrency?.toString()
135140
assert dto?.buyerId == form.buyerId
141+
assert dto?.condition == form.condition
136142
assert dto?.createdBy == expectedUserId
137143
assert dto?.seriesId == expectedSeriesId
138144
assert DateUtils.roughlyEqual(dto?.createdAt, new Date())

src/test/java/ru/mystamps/web/service/TestObjects.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ public static RawParsedDataDto createRawParsedDataDto() {
273273
Random.currency().toString(),
274274
String.valueOf(Random.price()),
275275
// FIXME: alternative currency shouldn't match with currency
276-
Random.currency().toString()
276+
Random.currency().toString(),
277+
Random.seriesCondition().toString()
277278
);
278279
}
279280

@@ -353,7 +354,8 @@ public static SeriesExtractedInfo createSeriesExtractedInfo() {
353354
Random.currency().toString(),
354355
Random.price(),
355356
// FIXME: alternative currency shouldn't match with currency
356-
Random.currency().toString()
357+
Random.currency().toString(),
358+
Random.seriesCondition()
357359
);
358360
}
359361

@@ -374,6 +376,7 @@ public static SeriesExtractedInfo createEmptySeriesExtractedInfo() {
374376
null,
375377
null,
376378
null,
379+
null,
377380
null
378381
);
379382
}

0 commit comments

Comments
 (0)