Skip to content

Commit 4ed41b4

Browse files
committed
Import series: gracefully handle the case when parsed value don't match database.
Fix #684
1 parent afc42c4 commit 4ed41b4

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.slf4j.LoggerFactory;
2323

24+
import org.springframework.context.ApplicationEventPublisher;
2425
import org.springframework.context.MessageSource;
2526
import org.springframework.context.annotation.Bean;
2627
import org.springframework.context.annotation.Configuration;
@@ -44,6 +45,7 @@ public class ServicesConfig {
4445
private final JavaMailSender mailSender;
4546
private final Environment env;
4647
private final MessageSource messageSource;
48+
private final ApplicationEventPublisher eventPublisher;
4749

4850
@Bean
4951
public SuspiciousActivityService getSuspiciousActivityService() {
@@ -166,7 +168,8 @@ public SeriesImportService getSeriesImportService() {
166168
LoggerFactory.getLogger(SeriesImportServiceImpl.class),
167169
daoConfig.getSeriesImportDao(),
168170
getSeriesService(),
169-
getSeriesInfoExtractorService()
171+
getSeriesInfoExtractorService(),
172+
eventPublisher
170173
);
171174
}
172175

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

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323

2424
/**
2525
* Event occurs when parsing of a file from import request has been failed.
26+
*
27+
* It could occurs by 2 reasons:
28+
* - when we couldn't extract anything meaningful
29+
* (elements specified by locators don't contain info)
30+
* - when we extract information but were unable to match it with database
31+
* (for example, we extracted country named "Italy" but it doesn't exist in database)
2632
*/
2733
@Getter
2834
public class ParsingFailed extends ApplicationEvent {

src/main/java/ru/mystamps/web/service/SeriesImportServiceImpl.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525

2626
import org.slf4j.Logger;
2727

28+
import org.springframework.context.ApplicationEventPublisher;
2829
import org.springframework.transaction.annotation.Transactional;
2930

3031
import org.springframework.security.access.prepost.PreAuthorize;
3132

3233
import lombok.RequiredArgsConstructor;
3334

3435
import ru.mystamps.web.Db.SeriesImportRequestStatus;
36+
import ru.mystamps.web.controller.event.ParsingFailed;
3537
import ru.mystamps.web.dao.SeriesImportDao;
3638
import ru.mystamps.web.dao.dto.ImportRequestDto;
3739
import ru.mystamps.web.dao.dto.ImportSeriesDbDto;
@@ -51,6 +53,7 @@ public class SeriesImportServiceImpl implements SeriesImportService {
5153
private final SeriesImportDao seriesImportDao;
5254
private final SeriesService seriesService;
5355
private final SeriesInfoExtractorService extractorService;
56+
private final ApplicationEventPublisher eventPublisher;
5457

5558
@Override
5659
@Transactional
@@ -175,8 +178,13 @@ public void saveParsedData(Integer requestId, RawParsedDataDto data) {
175178
processedData.setReleaseYear(releaseYear);
176179
}
177180

178-
// TODO: handle it gracefully by publishing ParsingFailed event
179-
Validate.validState(processedData.hasAtLeastOneFieldFilled(), "");
181+
// IMPORTANT: don't add code that modifies database above this line!
182+
// @todo #684 Series import: add integration test
183+
// for the case when parsed value don't match database
184+
if (!processedData.hasAtLeastOneFieldFilled()) {
185+
eventPublisher.publishEvent(new ParsingFailed(this, requestId));
186+
return;
187+
}
180188

181189
seriesImportDao.addParsedContent(requestId, processedData);
182190

0 commit comments

Comments
 (0)