Skip to content

Commit 44d5115

Browse files
committed
refactor(LinkEntityDtoGenericConverter): split converter to two separate converters that are part of the different packages.
Addressed to #927 No functional changes.
1 parent ca51b7f commit 44d5115

File tree

5 files changed

+118
-33
lines changed

5 files changed

+118
-33
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@
4040
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
4141
import org.springframework.web.servlet.resource.VersionResourceResolver;
4242
import ru.mystamps.web.Url;
43-
import ru.mystamps.web.controller.converter.LinkEntityDtoGenericConverter;
4443
import ru.mystamps.web.feature.account.AccountConfig;
4544
import ru.mystamps.web.feature.category.CategoryConfig;
45+
import ru.mystamps.web.feature.category.CategoryLinkEntityDtoConverter;
4646
import ru.mystamps.web.feature.category.CategoryService;
4747
import ru.mystamps.web.feature.collection.CollectionConfig;
4848
import ru.mystamps.web.feature.country.CountryConfig;
49+
import ru.mystamps.web.feature.country.CountryLinkEntityDtoConverter;
4950
import ru.mystamps.web.feature.country.CountryService;
5051
import ru.mystamps.web.feature.image.ImageConfig;
5152
import ru.mystamps.web.feature.participant.ParticipantConfig;
@@ -88,7 +89,8 @@ public class MvcConfig extends WebMvcConfigurerAdapter {
8889

8990
@Override
9091
public void addFormatters(FormatterRegistry registry) {
91-
registry.addConverter(new LinkEntityDtoGenericConverter(categoryService, countryService));
92+
registry.addConverter(new CategoryLinkEntityDtoConverter(categoryService));
93+
registry.addConverter(new CountryLinkEntityDtoConverter(countryService));
9294
}
9395

9496
@Override

src/main/java/ru/mystamps/web/controller/converter/package-info.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/main/java/ru/mystamps/web/controller/package-info.java

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (C) 2009-2019 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.feature.category;
19+
20+
import lombok.RequiredArgsConstructor;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
import org.springframework.core.convert.TypeDescriptor;
24+
import org.springframework.core.convert.converter.ConditionalGenericConverter;
25+
import ru.mystamps.web.common.LinkEntityDto;
26+
import ru.mystamps.web.common.LocaleUtils;
27+
28+
import java.util.HashSet;
29+
import java.util.Set;
30+
31+
@RequiredArgsConstructor
32+
public class CategoryLinkEntityDtoConverter implements ConditionalGenericConverter {
33+
34+
private static final Logger LOG = LoggerFactory.getLogger(CategoryLinkEntityDtoConverter.class);
35+
36+
private final CategoryService categoryService;
37+
38+
@Override
39+
public Set<ConvertiblePair> getConvertibleTypes() {
40+
Set<ConvertiblePair> pairs = new HashSet<>();
41+
pairs.add(new ConvertiblePair(String.class, LinkEntityDto.class));
42+
pairs.add(new ConvertiblePair(LinkEntityDto.class, String.class));
43+
return pairs;
44+
}
45+
46+
@Override
47+
public Object convert(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) {
48+
if (value == null) {
49+
return null;
50+
}
51+
52+
if (isDto(sourceType) && isString(targetType)) {
53+
LinkEntityDto dto = (LinkEntityDto)value;
54+
return String.valueOf(dto.getId());
55+
}
56+
57+
if (isString(sourceType) && isDto(targetType)) {
58+
String slug = value.toString();
59+
if (slug.isEmpty()) {
60+
return null;
61+
}
62+
63+
if (hasCategoryAnnotation(targetType)) {
64+
String lang = LocaleUtils.getCurrentLanguageOrNull();
65+
return categoryService.findOneAsLinkEntity(slug, lang);
66+
}
67+
68+
LOG.warn(
69+
"Can't convert type '{}' because it doesn't contain @Category annotation",
70+
targetType
71+
);
72+
73+
return null;
74+
}
75+
76+
LOG.warn("Attempt to convert unsupported types: from {} to {}", sourceType, targetType);
77+
return null;
78+
}
79+
80+
@Override
81+
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
82+
if (sourceType == null || targetType == null) {
83+
return false;
84+
}
85+
86+
// LinkEntityDto -> String
87+
if (isDto(sourceType) && isString(targetType)) {
88+
return true;
89+
}
90+
91+
// String -> @Category LinkEntityDto
92+
return isString(sourceType) && isDto(targetType) && hasCategoryAnnotation(targetType);
93+
}
94+
95+
private static boolean isString(TypeDescriptor type) {
96+
return String.class.equals(type.getType());
97+
}
98+
99+
private static boolean isDto(TypeDescriptor type) {
100+
return LinkEntityDto.class.equals(type.getType());
101+
}
102+
103+
private static boolean hasCategoryAnnotation(TypeDescriptor type) {
104+
return type.hasAnnotation(Category.class);
105+
}
106+
107+
}

src/main/java/ru/mystamps/web/controller/converter/LinkEntityDtoGenericConverter.java renamed to src/main/java/ru/mystamps/web/feature/country/CountryLinkEntityDtoConverter.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* along with this program; if not, write to the Free Software
1616
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1717
*/
18-
package ru.mystamps.web.controller.converter;
18+
package ru.mystamps.web.feature.country;
1919

2020
import lombok.RequiredArgsConstructor;
2121
import org.slf4j.Logger;
@@ -24,20 +24,15 @@
2424
import org.springframework.core.convert.converter.ConditionalGenericConverter;
2525
import ru.mystamps.web.common.LinkEntityDto;
2626
import ru.mystamps.web.common.LocaleUtils;
27-
import ru.mystamps.web.feature.category.Category;
28-
import ru.mystamps.web.feature.category.CategoryService;
29-
import ru.mystamps.web.feature.country.Country;
30-
import ru.mystamps.web.feature.country.CountryService;
3127

3228
import java.util.HashSet;
3329
import java.util.Set;
3430

3531
@RequiredArgsConstructor
36-
public class LinkEntityDtoGenericConverter implements ConditionalGenericConverter {
32+
public class CountryLinkEntityDtoConverter implements ConditionalGenericConverter {
3733

38-
private static final Logger LOG = LoggerFactory.getLogger(LinkEntityDtoGenericConverter.class);
34+
private static final Logger LOG = LoggerFactory.getLogger(CountryLinkEntityDtoConverter.class);
3935

40-
private final CategoryService categoryService;
4136
private final CountryService countryService;
4237

4338
@Override
@@ -65,17 +60,13 @@ public Object convert(Object value, TypeDescriptor sourceType, TypeDescriptor ta
6560
return null;
6661
}
6762

68-
String lang = LocaleUtils.getCurrentLanguageOrNull();
6963
if (hasCountryAnnotation(targetType)) {
64+
String lang = LocaleUtils.getCurrentLanguageOrNull();
7065
return countryService.findOneAsLinkEntity(slug, lang);
7166
}
7267

73-
if (hasCategoryAnnotation(targetType)) {
74-
return categoryService.findOneAsLinkEntity(slug, lang);
75-
}
76-
7768
LOG.warn(
78-
"Can't convert type '{}' because it doesn't contain supported annotations",
69+
"Can't convert type '{}' because it doesn't contain @Country annotation",
7970
targetType
8071
);
8172

@@ -97,10 +88,8 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
9788
return true;
9889
}
9990

100-
// String -> @Category/@Country LinkEntityDto
101-
return isString(sourceType)
102-
&& isDto(targetType)
103-
&& (hasCategoryAnnotation(targetType) || hasCountryAnnotation(targetType));
91+
// String -> @Country LinkEntityDto
92+
return isString(sourceType) && isDto(targetType) && hasCountryAnnotation(targetType);
10493
}
10594

10695
private static boolean isString(TypeDescriptor type) {
@@ -111,10 +100,6 @@ private static boolean isDto(TypeDescriptor type) {
111100
return LinkEntityDto.class.equals(type.getType());
112101
}
113102

114-
private static boolean hasCategoryAnnotation(TypeDescriptor type) {
115-
return type.hasAnnotation(Category.class);
116-
}
117-
118103
private static boolean hasCountryAnnotation(TypeDescriptor type) {
119104
return type.hasAnnotation(Country.class);
120105
}

0 commit comments

Comments
 (0)