Skip to content

Commit 8bdb2f2

Browse files
committed
refactor: remove code for parsing site parsers configuration from file.
Addressed to #975
1 parent 2658b09 commit 8bdb2f2

File tree

6 files changed

+5
-474
lines changed

6 files changed

+5
-474
lines changed

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

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,23 @@
1717
*/
1818
package ru.mystamps.web.feature.series.importing.event;
1919

20-
import java.util.HashMap;
21-
import java.util.Map;
22-
23-
import javax.annotation.PostConstruct;
24-
25-
import org.apache.commons.lang3.StringUtils;
26-
2720
import org.slf4j.Logger;
2821
import org.slf4j.LoggerFactory;
2922

30-
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
3123
import org.springframework.context.ApplicationEventPublisher;
3224
import org.springframework.context.ApplicationListener;
3325
import org.springframework.context.annotation.Bean;
3426
import org.springframework.context.annotation.Configuration;
35-
import org.springframework.core.env.ConfigurableEnvironment;
36-
import org.springframework.core.env.EnumerablePropertySource;
37-
import org.springframework.core.env.PropertySource;
3827
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
3928

4029
import lombok.RequiredArgsConstructor;
4130

4231
import ru.mystamps.web.config.ServicesConfig;
4332
import ru.mystamps.web.feature.series.importing.SeriesImportService;
4433
import ru.mystamps.web.feature.series.importing.extractor.JdbcSiteParserDao;
45-
import ru.mystamps.web.feature.series.importing.extractor.JsoupSiteParser;
46-
import ru.mystamps.web.feature.series.importing.extractor.SiteParser;
4734
import ru.mystamps.web.feature.series.importing.extractor.SiteParserDao;
4835
import ru.mystamps.web.feature.series.importing.extractor.SiteParserService;
4936
import ru.mystamps.web.feature.series.importing.extractor.SiteParserServiceImpl;
50-
import ru.mystamps.web.feature.series.importing.extractor.TimedSiteParser;
5137

5238
@Configuration
5339
@RequiredArgsConstructor
@@ -58,25 +44,8 @@ public class EventsConfig {
5844
private final SeriesImportService seriesImportService;
5945
private final ServicesConfig servicesConfig;
6046
private final ApplicationEventPublisher eventPublisher;
61-
private final ConfigurableBeanFactory beanFactory;
62-
private final ConfigurableEnvironment env;
6347
private final NamedParameterJdbcTemplate jdbcTemplate;
6448

65-
@PostConstruct
66-
public void init() {
67-
Map<Integer, SiteParser> parsers = createSiteParsers();
68-
for (Map.Entry<Integer, SiteParser> entry : parsers.entrySet()) {
69-
Integer num = entry.getKey();
70-
SiteParser parser = entry.getValue();
71-
if (!parser.isFullyInitialized()) {
72-
LOG.warn("Ignored non-fully initialized site parser (app.site-parser[{}])", num);
73-
continue;
74-
}
75-
LOG.trace("Registering site parser for '{}'", parser);
76-
beanFactory.registerSingleton("siteParser" + num, parser);
77-
}
78-
}
79-
8049
@Bean
8150
public SiteParserService siteParserService(SiteParserDao siteParserDao) {
8251
return new SiteParserServiceImpl(
@@ -121,74 +90,4 @@ public ApplicationListener<ParsingFailed> getParsingFailedEventListener() {
12190
);
12291
}
12392

124-
@SuppressWarnings({
125-
"PMD.AvoidInstantiatingObjectsInLoops",
126-
"PMD.ModifiedCyclomaticComplexity" // TODO: deal with it someday
127-
})
128-
private Map<Integer, SiteParser> createSiteParsers() {
129-
boolean foundSiteParserProps = false;
130-
Map<Integer, SiteParser> parsers = new HashMap<>();
131-
132-
for (PropertySource<?> source : env.getPropertySources()) {
133-
// while we expect that properties will be in PropertiesPropertySource, we use
134-
// EnumerablePropertySource to also handle systemProperties and systemEnvironment
135-
// that are MapPropertySource and SystemEnvironmentPropertySource respectively
136-
if (!(source instanceof EnumerablePropertySource<?>)) {
137-
LOG.trace("Ignored property source: {} ({})", source.getName(), source.getClass());
138-
continue;
139-
}
140-
141-
LOG.trace("Inspecting property source: {} ({})", source.getName(), source.getClass());
142-
143-
for (String name : ((EnumerablePropertySource<?>)source).getPropertyNames()) {
144-
if (!name.startsWith("app.site-parser")) {
145-
continue;
146-
}
147-
148-
String propertyValue = (String)source.getProperty(name);
149-
LOG.trace("Detected property '{}' with value {}", name, propertyValue);
150-
151-
// extract parser number (app.site-parser[2].name -> 2)
152-
String strNum = StringUtils.substringBetween(name, "[", "]");
153-
if (StringUtils.isBlank(strNum)) {
154-
LOG.warn("Ignored property '{}': could not extract index", name);
155-
continue;
156-
}
157-
158-
Integer num = Integer.valueOf(strNum);
159-
if (!parsers.containsKey(num)) {
160-
// @todo #801 EventsConfig.createSiteParsers(): split the logic for properties
161-
// parsing and the object instantiation
162-
SiteParser parser =
163-
new TimedSiteParser(
164-
LoggerFactory.getLogger(TimedSiteParser.class),
165-
new JsoupSiteParser()
166-
);
167-
parsers.put(num, parser);
168-
}
169-
170-
// extract parser property (app.site-parser[2].name -> name)
171-
String fieldName = StringUtils.substringAfterLast(name, ".");
172-
if (StringUtils.isBlank(fieldName)) {
173-
LOG.warn("Ignored property '{}': could not extract property name", name);
174-
continue;
175-
}
176-
177-
SiteParser parser = parsers.get(num);
178-
boolean validProperty = parser.setField(fieldName, propertyValue);
179-
if (!validProperty) {
180-
LOG.warn("Ignored property '{}': unknown or unsupported", name);
181-
continue;
182-
}
183-
foundSiteParserProps = true;
184-
}
185-
// we shouldn't process others to be able to override a property
186-
if (foundSiteParserProps) {
187-
break;
188-
}
189-
}
190-
191-
return parsers;
192-
}
193-
19493
}

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

Lines changed: 5 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,23 @@
3030

3131
import lombok.AccessLevel;
3232
import lombok.Getter;
33-
import lombok.NoArgsConstructor;
33+
import lombok.RequiredArgsConstructor;
3434
import lombok.Setter;
3535

36-
// Getters/setters are being used in unit tests
36+
// Getters/setters/no-arg constructor are being used in unit tests
3737
@Getter(AccessLevel.PROTECTED)
3838
@Setter(AccessLevel.PROTECTED)
39+
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
3940
@SuppressWarnings({
4041
"PMD.GodClass",
4142
"PMD.TooManyMethods",
42-
// false positive because setField() modifies the fields
43+
// the fields are effectively final but I keep them non-final to simplify testing
4344
"PMD.ImmutableField"
4445
})
45-
// TODO: consider to remove no-arg constructor after removing code for creating parsers from file
46-
@NoArgsConstructor
4746
public class JsoupSiteParser implements SiteParser {
4847
private static final Logger LOG = LoggerFactory.getLogger(JsoupSiteParser.class);
4948

50-
// When you're adding a new field don't forget to also update:
51-
// - JsoupSiteParser.setField()
52-
// - JsoupSiteParser.isFullyInitialized() (optionally)
53-
// - JsoupSiteParserTest.describe()
54-
// - JsoupSiteParser constructor
55-
// - SiteParserConfiguration
49+
// When you add a new field, don't forget to also update SiteParserConfiguration.
5650
private String name;
5751
private String matchedUrl;
5852
private String categoryLocator;
@@ -80,83 +74,6 @@ public JsoupSiteParser(SiteParserConfiguration cfg) {
8074
currencyValue = cfg.getCurrencyValue();
8175
}
8276

83-
@Override
84-
public boolean setField(String name, String value) {
85-
Validate.validState(StringUtils.isNotBlank(name), "Field name must be non-blank");
86-
Validate.validState(StringUtils.isNotBlank(value), "Field value must be non-blank");
87-
88-
boolean valid = true;
89-
90-
switch (name) {
91-
92-
case "name":
93-
setName(value);
94-
break;
95-
96-
case "matched-url":
97-
setMatchedUrl(value);
98-
break;
99-
100-
case "category-locator":
101-
setCategoryLocator(value);
102-
break;
103-
104-
case "country-locator":
105-
setCountryLocator(value);
106-
break;
107-
108-
case "short-description-locator":
109-
setShortDescriptionLocator(value);
110-
break;
111-
112-
case "image-url-locator":
113-
setImageUrlLocator(value);
114-
break;
115-
116-
case "image-url-attribute":
117-
setImageUrlAttribute(value);
118-
break;
119-
120-
case "issue-date-locator":
121-
setIssueDateLocator(value);
122-
break;
123-
124-
case "seller-locator":
125-
setSellerLocator(value);
126-
break;
127-
128-
case "price-locator":
129-
setPriceLocator(value);
130-
break;
131-
132-
case "currency-value":
133-
// @todo #695 Series import: validate app.site-parser[x].currency-value
134-
setCurrencyValue(value);
135-
break;
136-
137-
default:
138-
valid = false;
139-
break;
140-
}
141-
142-
return valid;
143-
}
144-
145-
@Override
146-
public boolean isFullyInitialized() {
147-
return name != null
148-
&& matchedUrl != null
149-
&& (
150-
categoryLocator != null
151-
|| countryLocator != null
152-
|| shortDescriptionLocator != null
153-
|| imageUrlLocator != null
154-
|| issueDateLocator != null
155-
|| sellerLocator != null
156-
|| priceLocator != null
157-
);
158-
}
159-
16077
/**
16178
* Parse HTML document to get info about series.
16279
*

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,5 @@
1818
package ru.mystamps.web.feature.series.importing.extractor;
1919

2020
public interface SiteParser {
21-
// TODO: remove because it's only needed for migrating configuration from file to database
22-
boolean setField(String name, String value);
23-
24-
boolean isFullyInitialized();
25-
2621
SeriesInfo parse(String htmlPage);
2722
}

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@ public class TimedSiteParser implements SiteParser {
3131
private final Logger log;
3232
private final SiteParser parser;
3333

34-
@Override
35-
public boolean setField(String name, String value) {
36-
return parser.setField(name, value);
37-
}
38-
39-
@Override
40-
public boolean isFullyInitialized() {
41-
return parser.isFullyInitialized();
42-
}
43-
4434
@Override
4535
public SeriesInfo parse(String htmlPage) {
4636
// Why we don't use Spring's StopWatch?

0 commit comments

Comments
 (0)