|
19 | 19 |
|
20 | 20 | import lombok.RequiredArgsConstructor;
|
21 | 21 | import org.apache.commons.lang3.Validate;
|
| 22 | +import org.springframework.security.access.prepost.PreAuthorize; |
22 | 23 | import org.springframework.transaction.annotation.Transactional;
|
| 24 | +import ru.mystamps.web.feature.series.DownloadResult; |
| 25 | +import ru.mystamps.web.feature.series.DownloaderService; |
| 26 | +import ru.mystamps.web.feature.series.importing.RawParsedDataDto; |
| 27 | +import ru.mystamps.web.feature.series.importing.SeriesExtractedInfo; |
| 28 | +import ru.mystamps.web.feature.series.importing.SeriesInfoExtractorService; |
| 29 | +import ru.mystamps.web.feature.series.importing.extractor.SeriesInfo; |
| 30 | +import ru.mystamps.web.feature.series.importing.extractor.SiteParser; |
| 31 | +import ru.mystamps.web.feature.series.importing.extractor.SiteParserService; |
| 32 | +import ru.mystamps.web.support.spring.security.HasAuthority; |
23 | 33 |
|
24 | 34 | @RequiredArgsConstructor
|
25 | 35 | public class SeriesSalesImportServiceImpl implements SeriesSalesImportService {
|
26 | 36 |
|
27 | 37 | private final SeriesSalesImportDao seriesSalesImportDao;
|
| 38 | + private final DownloaderService downloaderService; |
| 39 | + private final SiteParserService siteParserService; |
| 40 | + private final SeriesInfoExtractorService extractorService; |
| 41 | + |
| 42 | + // @todo #995 SeriesSalesImportServiceImpl.downloadAndParse(): add unit tests |
| 43 | + @Override |
| 44 | + @Transactional(readOnly = true) |
| 45 | + @PreAuthorize(HasAuthority.IMPORT_SERIES) |
| 46 | + @SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes") |
| 47 | + public SeriesSaleExtractedInfo downloadAndParse(String url) { |
| 48 | + SiteParser parser = siteParserService.findForUrl(url); |
| 49 | + if (parser == null) { |
| 50 | + throw new RuntimeException("could not find an appropriate parser"); |
| 51 | + } |
| 52 | + |
| 53 | + DownloadResult result = downloaderService.download(url); |
| 54 | + if (result.hasFailed()) { |
| 55 | + String message = "could not download: " + result.getCode(); |
| 56 | + throw new RuntimeException(message); |
| 57 | + } |
| 58 | + |
| 59 | + String content = result.getDataAsString(); |
| 60 | + |
| 61 | + // @todo #995 SiteParser: introduce a method for parsing only sales-related info |
| 62 | + SeriesInfo info = parser.parse(content); |
| 63 | + if (info.isEmpty()) { |
| 64 | + throw new RuntimeException("could not parse the page"); |
| 65 | + } |
| 66 | + |
| 67 | + RawParsedDataDto data = new RawParsedDataDto( |
| 68 | + null, |
| 69 | + null, |
| 70 | + null, |
| 71 | + null, |
| 72 | + null, |
| 73 | + null, |
| 74 | + null, |
| 75 | + info.getSellerName(), |
| 76 | + info.getSellerUrl(), |
| 77 | + info.getPrice(), |
| 78 | + info.getCurrency() |
| 79 | + ); |
| 80 | + |
| 81 | + // CheckStyle: ignore LineLength for next 1 line |
| 82 | + // @todo #995 SeriesInfoExtractorService: introduce a method for parsing only sales-related info |
| 83 | + SeriesExtractedInfo seriesInfo = extractorService.extract(url, data); |
| 84 | + |
| 85 | + return new SeriesSaleExtractedInfo( |
| 86 | + seriesInfo.getSellerId(), |
| 87 | + seriesInfo.getPrice(), |
| 88 | + seriesInfo.getCurrency() |
| 89 | + ); |
| 90 | + } |
28 | 91 |
|
29 | 92 | // @todo #834 SeriesSalesImportServiceImpl.saveParsedData(): introduce dto without dates
|
30 | 93 | @Override
|
|
0 commit comments