Skip to content

Commit 0814b35

Browse files
committed
Import series: add support for extracting info about price and known seller.
This commit adds new configuration properties: - app.site-parser[x].seller-locator - app.site-parser[x].price-locator - app.site-parser[x].currency-value Addressed to #695
1 parent 335291a commit 0814b35

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1181
-43
lines changed

src/main/java/ru/mystamps/web/config/ControllersConfig.java

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ public SeriesController getSeriesController() {
116116
public SeriesImportController getSeriesImportController() {
117117
return new SeriesImportController(
118118
servicesConfig.getSeriesImportService(),
119+
servicesConfig.getSeriesSalesService(),
120+
servicesConfig.getSeriesSalesImportService(),
119121
getSeriesController(),
120122
eventPublisher
121123
);

src/main/java/ru/mystamps/web/config/DaoConfig.java

+5
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public SeriesImportDao getSeriesImportDao() {
105105
return new JdbcSeriesImportDao(jdbcTemplate);
106106
}
107107

108+
@Bean
109+
public SeriesSalesImportDao getSeriesSalesImportDao() {
110+
return new JdbcSeriesSalesImportDao(jdbcTemplate);
111+
}
112+
108113
@Bean
109114
public SeriesSalesDao getSeriesSalesDao() {
110115
return new JdbcSeriesSalesDao(jdbcTemplate);

src/main/java/ru/mystamps/web/config/ServicesConfig.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ public SeriesImportService getSeriesImportService() {
178178
LoggerFactory.getLogger(SeriesImportServiceImpl.class),
179179
daoConfig.getSeriesImportDao(),
180180
getSeriesService(),
181+
getSeriesSalesService(),
182+
getSeriesSalesImportService(),
181183
getSeriesInfoExtractorService(),
182184
eventPublisher
183185
);
@@ -190,7 +192,8 @@ public SeriesInfoExtractorService getSeriesInfoExtractorService() {
190192
new SeriesInfoExtractorServiceImpl(
191193
LoggerFactory.getLogger(SeriesInfoExtractorServiceImpl.class),
192194
getCategoryService(),
193-
getCountryService()
195+
getCountryService(),
196+
getTransactionParticipantService()
194197
)
195198
);
196199
}
@@ -203,6 +206,14 @@ public SeriesSalesService getSeriesSalesService() {
203206
);
204207
}
205208

209+
@Bean
210+
public SeriesSalesImportService getSeriesSalesImportService() {
211+
return new SeriesSalesImportServiceImpl(
212+
LoggerFactory.getLogger(SeriesSalesImportServiceImpl.class),
213+
daoConfig.getSeriesSalesImportDao()
214+
);
215+
}
216+
206217
@Bean
207218
public SiteService getSiteService() {
208219
return new SiteServiceImpl(

src/main/java/ru/mystamps/web/controller/SeriesImportController.java

+38-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package ru.mystamps.web.controller;
1919

2020
import java.io.IOException;
21+
import java.util.Date;
2122
import java.util.Locale;
2223

2324
import javax.servlet.http.HttpServletRequest;
@@ -40,11 +41,15 @@
4041
import ru.mystamps.web.Url;
4142
import ru.mystamps.web.controller.converter.annotation.CurrentUser;
4243
import ru.mystamps.web.controller.dto.ImportSeriesForm;
44+
import ru.mystamps.web.controller.dto.ImportSeriesSalesForm;
4345
import ru.mystamps.web.controller.dto.RequestImportForm;
4446
import ru.mystamps.web.controller.event.ImportRequestCreated;
4547
import ru.mystamps.web.dao.dto.ImportRequestDto;
4648
import ru.mystamps.web.dao.dto.SeriesParsedDataDto;
49+
import ru.mystamps.web.dao.dto.SeriesSaleParsedDataDto;
4750
import ru.mystamps.web.service.SeriesImportService;
51+
import ru.mystamps.web.service.SeriesSalesImportService;
52+
import ru.mystamps.web.service.SeriesSalesService;
4853
import ru.mystamps.web.util.LocaleUtils;
4954

5055
import static ru.mystamps.web.controller.ControllerUtils.redirectTo;
@@ -54,6 +59,8 @@
5459
public class SeriesImportController {
5560

5661
private final SeriesImportService seriesImportService;
62+
private final SeriesSalesService seriesSalesService;
63+
private final SeriesSalesImportService seriesSalesImportService;
5764
private final SeriesController seriesController;
5865
private final ApplicationEventPublisher eventPublisher;
5966

@@ -127,6 +134,20 @@ public String showRequestAndImportSeriesForm(
127134
}
128135
}
129136

137+
SeriesSaleParsedDataDto seriesSale = seriesSalesImportService.getParsedData(requestId);
138+
if (seriesSale != null) {
139+
ImportSeriesSalesForm seriesSaleForm = new ImportSeriesSalesForm();
140+
seriesSaleForm.setSellerId(seriesSale.getSellerId());
141+
seriesSaleForm.setPrice(seriesSale.getPrice());
142+
seriesSaleForm.setCurrency(seriesSale.getCurrency());
143+
144+
form.setSeriesSale(seriesSaleForm);
145+
146+
if (seriesSale.getSellerId() != null) {
147+
seriesController.addSellersToModel(model);
148+
}
149+
}
150+
130151
model.addAttribute("importSeriesForm", form);
131152
model.addAttribute("showForm", hasParsedData);
132153

@@ -175,11 +196,27 @@ public String processImportSeriesForm(
175196
seriesController.addCountriesToModel(model, lang);
176197
seriesController.addYearToModel(model);
177198

199+
ImportSeriesSalesForm seriesSaleForm = form.getSeriesSale();
200+
if (seriesSaleForm != null) {
201+
seriesController.addSellersToModel(model);
202+
}
203+
178204
if (result.hasErrors()) {
179205
return "series/import/info";
180206
}
181207

182-
Integer seriesId = seriesImportService.addSeries(form, requestId, currentUserId);
208+
if (seriesSaleForm != null) {
209+
// fill required values prior passing the form into the service.
210+
seriesSaleForm.setDate(new Date());
211+
seriesSaleForm.setUrl(request.getUrl());
212+
}
213+
214+
Integer seriesId = seriesImportService.addSeries(
215+
form,
216+
seriesSaleForm,
217+
requestId,
218+
currentUserId
219+
);
183220

184221
return redirectTo(Url.INFO_SERIES_PAGE, seriesId);
185222
}

src/main/java/ru/mystamps/web/controller/TestController.java

+23
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,27 @@ public void simpleHtml(HttpServletResponse response) throws IOException {
6767
);
6868
}
6969

70+
@GetMapping("/valid/series-info/existing-seller")
71+
public void seriesInfoWithExistingSeller(HttpServletResponse response) throws IOException {
72+
response.setContentType("text/html");
73+
response.setCharacterEncoding("UTF-8");
74+
response.getWriter().println(
75+
"<!DOCTYPE html>"
76+
+ "<html>"
77+
+ "<head>"
78+
+ "<title>Series info (existing seller)</title>"
79+
+ "</head>"
80+
+ "<body>"
81+
// CheckStyle: ignore LineLength for next 2 lines
82+
+ "Image: <a id=\"series-image-link-1\" href=\"/image/1\">series image</a><br />"
83+
+ "Seller: <a id=\"test-seller\" href=\"http://example.com/eicca-toppinen\">Eicca Toppinen</a><br />"
84+
+ "Price: <span id=\"test-price\">111</span> RUB<br />"
85+
// this is needed to simplify an integration test
86+
// (required fields "category" and "quantity" will be filled automatically)
87+
+ "Info: <span class=\"dl-horizontal\">Спорт, 3 марки</span>"
88+
+ "</body>"
89+
+ "</html>"
90+
);
91+
}
92+
7093
}

src/main/java/ru/mystamps/web/controller/dto/ImportSeriesForm.java

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

2020
import java.math.BigDecimal;
2121

22+
import javax.validation.Valid;
2223
import javax.validation.constraints.Max;
2324
import javax.validation.constraints.Min;
2425
import javax.validation.constraints.NotNull;
@@ -84,6 +85,9 @@ public class ImportSeriesForm implements AddSeriesDto, NullableImageUrl {
8485
@NotNull
8586
private MultipartFile downloadedImage;
8687

88+
@Valid
89+
private ImportSeriesSalesForm seriesSale;
90+
8791
//
8892
// The methods bellow required for AddSeriesDto interface.
8993
// They are no-op methods because we don't support all values during series import.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (C) 2009-2018 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.controller.dto;
19+
20+
import java.math.BigDecimal;
21+
import java.util.Date;
22+
23+
import javax.validation.constraints.NotNull;
24+
25+
import org.springframework.format.annotation.NumberFormat;
26+
27+
import lombok.Getter;
28+
import lombok.Setter;
29+
30+
import ru.mystamps.web.dao.dto.Currency;
31+
import ru.mystamps.web.service.dto.AddSeriesSalesDto;
32+
33+
// @todo #695 /series/import/request/{id}(seriesSale):
34+
// add integration test for validation of required fields
35+
@Getter
36+
@Setter
37+
public class ImportSeriesSalesForm implements AddSeriesSalesDto {
38+
39+
@NotNull
40+
private Integer sellerId;
41+
42+
@NotNull
43+
@NumberFormat(pattern = "###.##")
44+
private BigDecimal price;
45+
46+
@NotNull
47+
private Currency currency;
48+
49+
// We don't expose these fields to a form because we know already what
50+
// values should be. Even if user will try to provide its own values, it's not
51+
// a problem as we always rewrite them in the controller.
52+
private Date date;
53+
private String url;
54+
55+
//
56+
// The methods bellow required for AddSeriesSalesDto interface.
57+
// They are no-op methods because we don't support all values during series import.
58+
//
59+
60+
@Override
61+
public BigDecimal getAltPrice() {
62+
return null;
63+
}
64+
65+
@Override
66+
public Currency getAltCurrency() {
67+
return null;
68+
}
69+
70+
@Override
71+
public Integer getBuyerId() {
72+
return null;
73+
}
74+
75+
}

src/main/java/ru/mystamps/web/controller/event/DownloadingSucceededEventListener.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ public void onApplicationEvent(DownloadingSucceeded event) {
9292
info.getImageUrl(),
9393
info.getIssueDate(),
9494
info.getQuantity(),
95-
info.getPerforated()
95+
info.getPerforated(),
96+
info.getSellerName(),
97+
info.getSellerUrl(),
98+
info.getPrice(),
99+
info.getCurrency()
96100
);
97101
importService.saveParsedData(requestId, data);
98102
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (C) 2009-2018 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.dao;
19+
20+
import ru.mystamps.web.dao.dto.SeriesSaleParsedDataDto;
21+
import ru.mystamps.web.dao.dto.SeriesSalesParsedDataDbDto;
22+
23+
public interface SeriesSalesImportDao {
24+
void addParsedData(Integer requestId, SeriesSalesParsedDataDbDto data);
25+
SeriesSaleParsedDataDto findParsedDataByRequestId(Integer requestId);
26+
}

src/main/java/ru/mystamps/web/dao/TransactionParticipantDao.java

+1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ public interface TransactionParticipantDao {
2727
void add(AddParticipantDbDto participant);
2828
List<EntityWithParentDto> findBuyersWithParents();
2929
List<EntityWithParentDto> findSellersWithParents();
30+
Integer findSellerId(String name, String url);
3031
List<EntityWithIdDto> findAllGroups();
3132
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (C) 2009-2018 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.dao.dto;
19+
20+
import java.math.BigDecimal;
21+
22+
import lombok.Getter;
23+
import lombok.RequiredArgsConstructor;
24+
25+
@Getter
26+
@RequiredArgsConstructor
27+
public class SeriesSaleParsedDataDto {
28+
private final Integer sellerId;
29+
private final BigDecimal price;
30+
private final Currency currency;
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) 2009-2018 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.dao.dto;
19+
20+
import java.math.BigDecimal;
21+
import java.util.Date;
22+
23+
import lombok.Getter;
24+
import lombok.Setter;
25+
import lombok.ToString;
26+
27+
@Getter
28+
@Setter
29+
@ToString(exclude = { "createdAt", "updatedAt" })
30+
public class SeriesSalesParsedDataDbDto {
31+
private Integer sellerId;
32+
private BigDecimal price;
33+
private String currency;
34+
private Date createdAt;
35+
private Date updatedAt;
36+
37+
// they aren't useless
38+
@SuppressWarnings("PMD.UselessParentheses")
39+
public boolean hasAtLeastOneFieldFilled() {
40+
return sellerId != null || (price != null && currency != null);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)