AbstractJcrCommerceService.java
7.78 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* aQute.bnd.annotation.ConsumerType
* com.day.cq.wcm.api.Page
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
*/
package com.adobe.cq.commerce.common;
import aQute.bnd.annotation.ConsumerType;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.CommerceQuery;
import com.adobe.cq.commerce.api.CommerceResult;
import com.adobe.cq.commerce.api.CommerceService;
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.collection.ProductCollection;
import com.adobe.cq.commerce.api.promotion.Promotion;
import com.adobe.cq.commerce.api.promotion.PromotionManager;
import com.adobe.cq.commerce.api.promotion.Voucher;
import com.adobe.cq.commerce.common.AbstractJcrCommerceServiceFactory;
import com.adobe.cq.commerce.common.CommerceSearchProvider;
import com.adobe.cq.commerce.common.CommerceSearchProviderManager;
import com.adobe.cq.commerce.common.DefaultJcrCartEntry;
import com.adobe.cq.commerce.common.ServiceContext;
import com.adobe.cq.commerce.common.VendorJcrPlacedOrder;
import com.day.cq.wcm.api.Page;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
@ConsumerType
public abstract class AbstractJcrCommerceService
implements CommerceService {
private final ServiceContext serviceContext;
protected Map<String, Object> context = new HashMap<String, Object>();
protected CommerceSearchProvider searchProvider;
protected ResourceResolver resolver;
@Deprecated
public AbstractJcrCommerceServiceFactory.Services services;
protected AbstractJcrCommerceService(ServiceContext serviceContext, Resource resource) {
this.serviceContext = serviceContext;
this.resolver = resource.getResourceResolver();
}
public ServiceContext serviceContext() {
return this.serviceContext;
}
@Override
public void setContext(Map<String, Object> context) {
this.context = context;
}
@Override
public Map<String, Object> getContext() {
return this.context;
}
@Override
public String getServer() {
return null;
}
@Override
public boolean isActivated(Product product) throws CommerceException {
return true;
}
@Override
public Promotion getPromotion(String path) throws CommerceException {
Resource resource = this.resolver.getResource(path);
return resource != null ? (Promotion)resource.adaptTo(Promotion.class) : null;
}
@Override
public Voucher getVoucher(String path) throws CommerceException {
Resource resource = this.resolver.getResource(path);
return resource != null ? (Voucher)resource.adaptTo(Voucher.class) : null;
}
@Override
public ProductCollection getProductCollection(String path) throws CommerceException {
Resource resource = this.resolver.getResource(path);
return resource != null ? (ProductCollection)resource.adaptTo(ProductCollection.class) : null;
}
public VendorJcrPlacedOrder getPlacedOrder(String orderId, Locale locale) {
return new VendorJcrPlacedOrder(this, orderId, locale);
}
@Override
public void catalogRolloutHook(Page blueprint, Page catalog) throws CommerceException {
}
@Override
public void sectionRolloutHook(Page blueprint, Page section) {
}
@Override
public void productRolloutHook(Product productData, Page productPage, Product productReference) throws CommerceException {
}
@Override
public CommerceResult search(CommerceQuery query) throws CommerceException {
if (this.searchProvider == null) {
this.searchProvider = this.getSearchProvider();
}
if (this.searchProvider != null) {
return this.searchProvider.search(query, this);
}
return null;
}
protected CommerceSearchProvider getSearchProvider() throws CommerceException {
String providerName = this.getSearchProviderName();
if (providerName != null) {
return this.serviceContext.searchProviderManager.getSearchProvider(providerName);
}
return null;
}
protected String getSearchProviderName() {
return null;
}
@Override
public List<Promotion> getAvailablePromotions(ResourceResolver resourceResolver) throws CommerceException {
PromotionManager promotionManager = (PromotionManager)resourceResolver.adaptTo(PromotionManager.class);
return promotionManager.getAvailablePromotions(resourceResolver);
}
public List<ShippingMethod> getAvailableShippingMethods() throws CommerceException {
return this.enumerateMethods("/etc/commerce/shipping-methods", ShippingMethod.class);
}
public List<PaymentMethod> getAvailablePaymentMethods() throws CommerceException {
return this.enumerateMethods("/etc/commerce/payment-methods", PaymentMethod.class);
}
protected <T> List<T> enumerateMethods(String path, Class<T> type) {
ArrayList<Object> methods = new ArrayList<Object>();
Resource dir = this.resolver.getResource(path);
if (dir != null) {
for (Resource resource : dir.getChildren()) {
Object method;
if (resource.getName().equals("jcr:content") || (method = resource.adaptTo(type)) == null) continue;
methods.add(method);
}
}
return methods;
}
public DefaultJcrCartEntry newCartEntryImpl(int index, Product product, int quantity) {
return new DefaultJcrCartEntry(index, product, quantity);
}
public String serializeCartEntryData(String productPath, int quantity, ValueMap properties) {
StringBuilder sb = new StringBuilder();
sb.append(productPath).append(';').append(quantity);
if (properties == null || properties.isEmpty()) {
return sb.toString();
}
sb.append(';');
for (Map.Entry entry : properties.entrySet()) {
String key = (String)entry.getKey();
String value = String.valueOf(entry.getValue());
sb.append(key).append('=').append(value).append('\f');
}
return sb.toString();
}
public Object[] deserializeCartEntryData(String str) throws CommerceException {
String[] propertyFields;
Object[] entryData = new Object[3];
String[] entryFields = str.split(";", 3);
Product product = this.getProduct(entryFields[0]);
entryData[0] = product;
int quantity = Integer.parseInt(entryFields[1]);
entryData[1] = quantity;
if (entryFields.length == 2) {
return entryData;
}
HashMap<String, String> properties = new HashMap<String, String>();
for (String field : propertyFields = entryFields[2].split("\f")) {
if (!StringUtils.isNotBlank((String)field)) continue;
String[] property = field.split("=", 2);
properties.put(property[0], property[1]);
}
entryData[2] = properties;
return entryData;
}
@Deprecated
public AbstractJcrCommerceService(AbstractJcrCommerceServiceFactory.Services services) {
this((ServiceContext)null);
this.services = services;
}
@Deprecated
protected AbstractJcrCommerceService(ServiceContext serviceContext) {
this.serviceContext = serviceContext;
}
}