CommerceAdapterFactory.java
14.8 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.social.ugc.api.UgcSearch
* com.adobe.cq.social.ugcbase.SocialUtils
* com.day.cq.commons.inherit.ComponentInheritanceValueMap
* com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.adapter.AdapterFactory
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.osgi.service.event.EventAdmin
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.commerce.impl;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.CommerceProvider;
import com.adobe.cq.commerce.api.CommerceService;
import com.adobe.cq.commerce.api.CommerceServiceFactory;
import com.adobe.cq.commerce.api.PaymentMethod;
import com.adobe.cq.commerce.api.Product;
import com.adobe.cq.commerce.api.ShippingMethod;
import com.adobe.cq.commerce.api.asset.ProductAssetHandlerProvider;
import com.adobe.cq.commerce.api.asset.ProductAssetManager;
import com.adobe.cq.commerce.api.classification.Classification;
import com.adobe.cq.commerce.api.classification.ClassificationManager;
import com.adobe.cq.commerce.api.collection.ProductCollection;
import com.adobe.cq.commerce.api.collection.ProductCollectionManager;
import com.adobe.cq.commerce.api.promotion.Promotion;
import com.adobe.cq.commerce.api.promotion.PromotionHandler;
import com.adobe.cq.commerce.api.promotion.PromotionManager;
import com.adobe.cq.commerce.api.promotion.Voucher;
import com.adobe.cq.commerce.api.smartlist.SmartList;
import com.adobe.cq.commerce.api.smartlist.SmartListManager;
import com.adobe.cq.commerce.impl.JcrPaymentMethodImpl;
import com.adobe.cq.commerce.impl.JcrShippingMethodImpl;
import com.adobe.cq.commerce.impl.asset.ProductAssetManagerImpl;
import com.adobe.cq.commerce.impl.classification.ClassificationImpl;
import com.adobe.cq.commerce.impl.classification.ClassificationManagerImpl;
import com.adobe.cq.commerce.impl.collection.ProductCollectionManagerImpl;
import com.adobe.cq.commerce.impl.promotion.JcrPromotionImpl;
import com.adobe.cq.commerce.impl.promotion.JcrVoucherImpl;
import com.adobe.cq.commerce.impl.smartlist.SmartListImpl;
import com.adobe.cq.commerce.impl.smartlist.SmartListManagerImpl;
import com.adobe.cq.social.ugc.api.UgcSearch;
import com.adobe.cq.social.ugcbase.SocialUtils;
import com.day.cq.commons.inherit.ComponentInheritanceValueMap;
import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(specVersion="1.1")
@Service
@Property(name="service.description", value={"Default adapter factory for commerce entities"})
public class CommerceAdapterFactory
implements AdapterFactory,
CommerceProvider {
private final Logger log = LoggerFactory.getLogger(CommerceAdapterFactory.class);
private static final Class<SlingHttpServletRequest> REQUEST_CLASS = SlingHttpServletRequest.class;
private static final Class<Resource> RESOURCE_CLASS = Resource.class;
private static final Class<ResourceResolver> RESOURCE_RESOLVER_CLASS = ResourceResolver.class;
private static final Class<CommerceService> COMMERCE_SERVICE_CLASS = CommerceService.class;
private static final Class<Product> PRODUCT_CLASS = Product.class;
private static final Class<Promotion> PROMOTION_CLASS = Promotion.class;
private static final Class<PromotionHandler> PROMOTION_HANDLER_CLASS = PromotionHandler.class;
private static final Class<PromotionManager> PROMOTION_MANAGER_CLASS = PromotionManager.class;
private static final Class<ProductAssetManager> PRODUCT_ASSET_MANAGER_CLASS = ProductAssetManager.class;
private static final Class<Voucher> VOUCHER_CLASS = Voucher.class;
private static final Class<PaymentMethod> PAYMENT_METHOD_CLASS = PaymentMethod.class;
private static final Class<ShippingMethod> SHIPPING_METHOD_CLASS = ShippingMethod.class;
private static final Class<ProductCollection> PRODUCT_COLLECTION_CLASS = ProductCollection.class;
private static final Class<ProductCollectionManager> PRODUCT_COLLECTION_MANAGER_CLASS = ProductCollectionManager.class;
private static final Class<SmartListManager> SMART_LIST_MANAGER_CLASS = SmartListManager.class;
private static final Class<SmartList> SMART_LIST_CLASS = SmartList.class;
private static final Class<Classification> CLASSIFICATION_CLASS = Classification.class;
private static final Class<ClassificationManager> CLASSIFICATION_MANAGER_CLASS = ClassificationManager.class;
private static final String COMMERCE_PROVIDER_PROPERTY = "cq:commerceProvider";
@Reference(referenceInterface=CommerceServiceFactory.class, bind="bindFactory", unbind="unbindFactory", cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
private Map<String, CommerceServiceFactory> factories = Collections.synchronizedMap(new HashMap());
@Property(name="adapters")
protected static final String[] ADAPTER_CLASSES = new String[]{COMMERCE_SERVICE_CLASS.getName(), PRODUCT_CLASS.getName(), PROMOTION_CLASS.getName(), PROMOTION_HANDLER_CLASS.getName(), PROMOTION_MANAGER_CLASS.getName(), VOUCHER_CLASS.getName(), PAYMENT_METHOD_CLASS.getName(), SHIPPING_METHOD_CLASS.getName(), PRODUCT_ASSET_MANAGER_CLASS.getName(), PRODUCT_COLLECTION_CLASS.getName(), PRODUCT_COLLECTION_MANAGER_CLASS.getName(), SMART_LIST_CLASS.getName(), SMART_LIST_MANAGER_CLASS.getName(), CLASSIFICATION_CLASS.getName(), CLASSIFICATION_MANAGER_CLASS.getName()};
@Property(name="adaptables")
protected static final String[] ADAPTABLE_CLASSES = new String[]{RESOURCE_CLASS.getName(), RESOURCE_RESOLVER_CLASS.getName(), REQUEST_CLASS.getName()};
@Reference
private PromotionManager promotionManager;
@Reference
private ProductAssetHandlerProvider productAssetHandlerProvider;
@Reference
private SocialUtils socialUtils;
@Reference
private UgcSearch ugcSearch;
@Reference
EventAdmin eventAdmin;
protected void bindFactory(CommerceServiceFactory factory, Map<?, ?> properties) {
this.factories.put((String)properties.get("commerceProvider"), factory);
}
protected void unbindFactory(CommerceServiceFactory factory, Map<?, ?> properties) {
this.factories.remove((String)properties.get("commerceProvider"));
}
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
if (adaptable instanceof Resource) {
return this.getAdapter((Resource)adaptable, type);
}
if (adaptable instanceof ResourceResolver) {
return this.getAdapter((ResourceResolver)adaptable, type);
}
if (adaptable instanceof SlingHttpServletRequest) {
return this.getAdapter((SlingHttpServletRequest)adaptable, type);
}
this.log.warn("Unable to handle adaptable {}", (Object)adaptable.getClass().getName());
return null;
}
private <AdapterType> AdapterType getAdapter(Resource resource, Class<AdapterType> type) {
if (type == PRODUCT_CLASS || type == COMMERCE_SERVICE_CLASS) {
CommerceServiceFactory factory;
Page page = ((PageManager)resource.getResourceResolver().adaptTo(PageManager.class)).getContainingPage(resource);
ComponentInheritanceValueMap properties = page != null ? new HierarchyNodeInheritanceValueMap(page.getContentResource()) : new ComponentInheritanceValueMap(resource);
String provider = (String)properties.getInherited("cq:commerceProvider", String.class);
if (provider == null && !this.factories.isEmpty()) {
provider = this.factories.keySet().iterator().next();
}
if ((factory = this.factories.get(provider)) != null) {
try {
CommerceService cs = factory.getCommerceService(resource);
if (type == COMMERCE_SERVICE_CLASS) {
return (AdapterType)cs;
}
return (AdapterType)cs.getProduct(resource.getPath());
}
catch (CommerceException e) {
this.log.error("Could not adapt resource: ", (Throwable)e);
}
}
} else if (type == PROMOTION_CLASS) {
try {
return (AdapterType)new JcrPromotionImpl(resource);
}
catch (CommerceException e) {
this.log.error("Could not adapt resource: ", (Throwable)e);
}
} else if (type == VOUCHER_CLASS) {
try {
return (AdapterType)new JcrVoucherImpl(resource);
}
catch (CommerceException e) {
this.log.error("Could not adapt resource: ", (Throwable)e);
}
} else if (type == PROMOTION_HANDLER_CLASS) {
String promoType;
Promotion promo = (Promotion)resource.adaptTo(Promotion.class);
if (promo != null && (promoType = promo.getType()) != null) {
return (AdapterType)this.promotionManager.getHandler(promoType);
}
} else if (type == PAYMENT_METHOD_CLASS) {
try {
return (AdapterType)new JcrPaymentMethodImpl(resource);
}
catch (CommerceException e) {
this.log.error("Could not adapt resource: ", (Throwable)e);
}
} else if (type == SHIPPING_METHOD_CLASS) {
try {
return (AdapterType)new JcrShippingMethodImpl(resource);
}
catch (CommerceException e) {
this.log.error("Could not adapt resource: ", (Throwable)e);
}
} else {
if (type == PRODUCT_COLLECTION_CLASS) {
ProductCollectionManager productCollectionManager = (ProductCollectionManager)resource.getResourceResolver().adaptTo(ProductCollectionManager.class);
return (AdapterType)productCollectionManager.getCollection(resource.getPath());
}
if (type == SMART_LIST_CLASS) {
try {
return (AdapterType)new SmartListImpl(resource, this.eventAdmin);
}
catch (CommerceException e) {
this.log.error("Could not adapt resource: ", (Throwable)e);
}
} else if (type == CLASSIFICATION_CLASS) {
try {
return (AdapterType)new ClassificationImpl(resource);
}
catch (CommerceException e) {
this.log.error("Could not adapt resource: ", (Throwable)e);
}
}
}
this.log.debug("Unable to adapt resource to type {}", (Object)type.getName());
return null;
}
private <AdapterType> AdapterType getAdapter(ResourceResolver resolver, Class<AdapterType> type) {
if (type == PROMOTION_MANAGER_CLASS) {
return (AdapterType)this.promotionManager;
}
if (type == PRODUCT_ASSET_MANAGER_CLASS) {
return (AdapterType)new ProductAssetManagerImpl(resolver, this.productAssetHandlerProvider);
}
if (type == PRODUCT_COLLECTION_MANAGER_CLASS) {
return (AdapterType)new ProductCollectionManagerImpl(resolver);
}
if (type == CLASSIFICATION_MANAGER_CLASS) {
return (AdapterType)new ClassificationManagerImpl(resolver);
}
this.log.warn("Unable to adapt resolver to requested type {}", (Object)type.getName());
return null;
}
private <AdapterType> AdapterType getAdapter(SlingHttpServletRequest request, Class<AdapterType> type) {
if (type == SMART_LIST_MANAGER_CLASS) {
try {
return (AdapterType)new SmartListManagerImpl(request, this.socialUtils, this.ugcSearch, this.eventAdmin);
}
catch (CommerceException e) {
this.log.error("Could not adapt resource resolver: ", (Throwable)e);
return null;
}
}
this.log.warn("Unable to adapt resolver to requested type {}", (Object)type.getName());
return null;
}
@Override
public CommerceServiceFactory getServiceFactory(String name) {
return this.factories.get(name);
}
protected void bindPromotionManager(PromotionManager promotionManager) {
this.promotionManager = promotionManager;
}
protected void unbindPromotionManager(PromotionManager promotionManager) {
if (this.promotionManager == promotionManager) {
this.promotionManager = null;
}
}
protected void bindProductAssetHandlerProvider(ProductAssetHandlerProvider productAssetHandlerProvider) {
this.productAssetHandlerProvider = productAssetHandlerProvider;
}
protected void unbindProductAssetHandlerProvider(ProductAssetHandlerProvider productAssetHandlerProvider) {
if (this.productAssetHandlerProvider == productAssetHandlerProvider) {
this.productAssetHandlerProvider = null;
}
}
protected void bindSocialUtils(SocialUtils socialUtils) {
this.socialUtils = socialUtils;
}
protected void unbindSocialUtils(SocialUtils socialUtils) {
if (this.socialUtils == socialUtils) {
this.socialUtils = null;
}
}
protected void bindUgcSearch(UgcSearch ugcSearch) {
this.ugcSearch = ugcSearch;
}
protected void unbindUgcSearch(UgcSearch ugcSearch) {
if (this.ugcSearch == ugcSearch) {
this.ugcSearch = null;
}
}
protected void bindEventAdmin(EventAdmin eventAdmin) {
this.eventAdmin = eventAdmin;
}
protected void unbindEventAdmin(EventAdmin eventAdmin) {
if (this.eventAdmin == eventAdmin) {
this.eventAdmin = null;
}
}
}