1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
package com.blog.sap.core.service.impl;
import com.blog.sap.core.model.AbstractSapStringAttributeParserModel;
import com.blog.sap.core.model.ParsedValueMapperModel;
import com.blog.sap.core.model.ProductAttributeParserModel;
import com.blog.sap.core.sap.attribute.ParsedValueMapperDAO;
import com.blog.sap.core.service.ParsedValueMapperService;
import com.blog.sap.core.utils.MethodHandleUtils;
import de.hybris.platform.core.model.ItemModel;
import de.hybris.platform.core.model.product.ProductModel;
import de.hybris.platform.servicelayer.i18n.I18NService;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
public class ProductAttributeMapperService implements ParsedValueMapperService {
private static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
private static final Logger LOG = LoggerFactory.getLogger(ProductAttributeMapperService.class);
private static final String CODE_ATTRIBUTE = "code";
private static final String CATALOG_VERSION_ATTRIBUTE = "catalogVersion";
@Resource
private I18NService i18NService;
@Resource
private ParsedValueMapperDAO parsedValueMapperDAO;
@Resource
private ModelService modelService;
@Resource
private FlexibleSearchService flexibleSearchService;
@Override
public void clearProductAttribute(ProductModel product, Object attribute) {
if (attribute instanceof String) {
try {
if (MethodHandleUtils.hasSetter(product, (String) attribute, Collection.class)) {
MethodHandleUtils.invokeSetterMethod(product, (String) attribute, new ArrayList<>());
}
} catch (Exception exception) {
LOG.warn("Product {} is not change. Attribute {} was not cleared.", product.getCode(), attribute);
if (LOG.isDebugEnabled()) {
LOG.warn("Product {} is not change. Attribute {} was not cleared.", product.getCode(), attribute, exception);
}
}
}
}
@Override
public void mapParsedValue(ProductModel product, AbstractSapStringAttributeParserModel abstractParser, String attributeValue) {
if (abstractParser instanceof ProductAttributeParserModel) {
ProductAttributeParserModel parser = (ProductAttributeParserModel) abstractParser;
try {
setProductAttribute(product, parser, attributeValue);
} catch (Exception exception) {
LOG.warn("Product {} is not change. Attribute {} with value {} is not set.", product.getCode(), parser.getCode(), attributeValue);
if (LOG.isDebugEnabled()) {
LOG.debug("Product {} is not change. Attribute {} with value {} is not set.", product.getCode(), parser.getCode(), attributeValue, exception);
}
}
}
}
private void setProductAttribute(ProductModel product, ProductAttributeParserModel parser, String attributeValue) throws ClassNotFoundException {
String attribute = parser.getProductAttribute();
Class attributeType = Class.forName(parser.getProductAttributeClassType());
ParsedValueMapperModel mapper = parsedValueMapperDAO.getParsedValueMapperByCodeAndParsedValue(parser.getCode(), attributeValue);
// Set string into product attribute
if (attributeType == String.class) {
setStringAttribute(product, mapper, attribute, attributeValue);
} else if (Boolean.class == attributeType) {
// Set boolean into product attribute
setBooleanAttribute(product, mapper, attribute, attributeValue);
} else if (ItemModel.class.isAssignableFrom(attributeType)) {
// Set Item type into product attribute
boolean isCollection = parser.getIsCollectionAttribute();
if (!isCollection) {
Class setterSignatureClass = Class.forName(parser.getProductAttributeClassType());
if (MethodHandleUtils.hasSetter(product, attribute, setterSignatureClass)) {
ItemModel attributeValueModel = getAttributeModel(product.getCatalogVersion(), attributeType, mapper, attributeValue);
setItemModelAttribute(product, attribute, attributeValueModel);
}
} else {
// Set collection of item types into product attribute
if (MethodHandleUtils.hasSetter(product, attribute, Collection.class)) {
ItemModel attributeValueModel = getAttributeModel(product.getCatalogVersion(), attributeType, mapper, attributeValue);
setCollectionItemModelAttribute(product, attribute, attributeValueModel);
}
}
}
}
private void setBooleanAttribute(ProductModel product, ParsedValueMapperModel mapper, String attribute, String attributeValue) {
if (MethodHandleUtils.hasSetter(product, attribute, Boolean.class)) {
if (mapper == null) {
MethodHandleUtils.invokeSetterMethod(product, attribute, Boolean.valueOf(attributeValue));
} else {
MethodHandleUtils.invokeSetterMethod(product, attribute, Boolean.valueOf(mapper.getMappedValue(DEFAULT_LOCALE)));
}
}
}
private void setStringAttribute(ProductModel product, ParsedValueMapperModel mapper, String attribute, String attributeValue) {
if (MethodHandleUtils.hasSetter(product, attribute, String.class)) {
if (mapper == null) {
MethodHandleUtils.invokeSetterMethod(product, attribute, attributeValue);
} else {
if (MethodHandleUtils.hasLocalizedSetter(product, attribute, String.class)) {
for (Locale locale : i18NService.getSupportedLocales()) {
MethodHandleUtils.invokeSetterMethod(product, attribute, mapper.getMappedValue(locale), locale);
}
} else {
MethodHandleUtils.invokeSetterMethod(product, attribute, mapper.getMappedValue(DEFAULT_LOCALE));
}
}
}
}
private void setItemModelAttribute(ProductModel product, String attribute, ItemModel attributeValueModel) {
if (attributeValueModel != null) {
MethodHandleUtils.invokeSetterMethod(product, attribute, attributeValueModel);
}
}
private void setCollectionItemModelAttribute(ProductModel product, String attribute, ItemModel attributeValueModel) {
if (attributeValueModel != null) {
Collection<ItemModel> oldValues = MethodHandleUtils.invokeGetterMethod(product, attribute, Collection.class);
Collection<ItemModel> newValues;
if (CollectionUtils.isEmpty(oldValues)) {
newValues = new HashSet<>();
} else {
newValues = new HashSet<>(oldValues);
}
newValues.add(attributeValueModel);
MethodHandleUtils.invokeSetterMethod(product, attribute, newValues);
}
}
private ItemModel getAttributeModel(CatalogVersionModel productCatalogVersion, Class modelType, ParsedValueMapperModel mapper, String defaultModelCode) {
ItemModel exampleAttributeValueModel = modelService.create(modelType);
// Set catalog version if attribute supports it
if (MethodHandleUtils.hasSetter(exampleAttributeValueModel, CATALOG_VERSION_ATTRIBUTE, CatalogVersionModel.class) && productCatalogVersion != null) {
MethodHandleUtils.invokeSetterMethod(exampleAttributeValueModel, CATALOG_VERSION_ATTRIBUTE, productCatalogVersion);
}
// Set item code and execute sear
if (MethodHandleUtils.hasSetter(exampleAttributeValueModel, CODE_ATTRIBUTE, String.class)) {
String mappedAttributeValue;
if (mapper != null) {
mappedAttributeValue = mapper.getMappedValue(DEFAULT_LOCALE);
} else {
mappedAttributeValue = defaultModelCode;
}
MethodHandleUtils.invokeSetterMethod(exampleAttributeValueModel, CODE_ATTRIBUTE, mappedAttributeValue);
List<ItemModel> attributeValueModels = flexibleSearchService.getModelsByExample(exampleAttributeValueModel);
if (CollectionUtils.isNotEmpty(attributeValueModels)) {
return attributeValueModels.get(0);
}
}
return null;
}
}
|