|
| 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 | +} |
0 commit comments