ClassificationCategoryImpl.java
14.2 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.RangeIterator
* com.day.cq.search.Predicate
* com.day.cq.search.SimpleSearch
* com.day.cq.search.result.Hit
* com.day.cq.search.result.SearchResult
* com.day.cq.tagging.TagManager
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.query.Query
* javax.jcr.query.QueryManager
* javax.jcr.query.QueryResult
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.commerce.impl.classification;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.Product;
import com.adobe.cq.commerce.api.classification.Classification;
import com.adobe.cq.commerce.api.classification.ClassificationAttribute;
import com.adobe.cq.commerce.api.classification.ClassificationCategory;
import com.adobe.cq.commerce.impl.classification.ClassificationAttributeImpl;
import com.adobe.cq.commerce.impl.classification.ClassificationImpl;
import com.day.cq.commons.RangeIterator;
import com.day.cq.search.Predicate;
import com.day.cq.search.SimpleSearch;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;
import com.day.cq.tagging.TagManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ClassificationCategoryImpl
implements ClassificationCategory {
protected final Logger log = LoggerFactory.getLogger(ClassificationCategoryImpl.class);
protected Resource resource;
protected ValueMap properties;
protected ModifiableValueMap modifiableProperties;
protected ResourceResolver resolver;
private static final String DEFAULT_PRODUCTS_START_PATH = "/etc/commerce/products";
protected static final String NN_ROOT_CATEGORY = "rootcategory";
protected static final String NN_ATTRIBUTES = "attributes";
protected static final String NN_CATEGORIES = "categories";
protected static final String NN_PRODUCTS = "products";
protected static final String COMMERCE_TYPE_CLASSIFICATION = "classification";
protected static final String COMMERCE_TYPE_ATTRIBUTE = "classification.attribute";
protected static final String COMMERCE_TYPE_CATEGORY = "classification.category";
protected static final String PN_BASE_PATH = "basePath";
protected static final String PN_MATCH_TAGS = "matchTags";
protected static final String PN_QUERY_TEXT = "search";
protected static final String PN_QUERY_TYPE = "searchType";
public ClassificationCategoryImpl(Resource resource) throws CommerceException {
if (!this.isCategory(resource)) {
throw new CommerceException("Resource is not a classification category.");
}
this.resource = resource;
this.properties = (ValueMap)resource.adaptTo(ValueMap.class);
this.modifiableProperties = (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class);
this.resolver = resource.getResourceResolver();
}
@Override
public String getTitle() {
return (String)this.properties.get("jcr:title", (Object)"");
}
@Override
public String getDescription() {
return (String)this.properties.get("jcr:description", (Object)"");
}
@Override
public String getPath() {
return this.resource.getPath();
}
@Override
public Classification getClassification() {
ClassificationCategory tmpCategory;
for (tmpCategory = this; tmpCategory != null && !tmpCategory.isRootCategory(); tmpCategory = tmpCategory.getParentCategory()) {
}
if (tmpCategory == null) {
return null;
}
Resource rootCategoryRes = this.resolver.getResource(tmpCategory.getPath());
Resource classificationRes = rootCategoryRes.getParent();
try {
return new ClassificationImpl(classificationRes);
}
catch (CommerceException e) {
this.log.error("The resource at " + classificationRes.getPath() + " is not a classification", (Throwable)e);
return null;
}
}
@Override
public boolean isRootCategory() {
Resource parentRes = this.resource.getParent();
return this.isCommerceType(parentRes, "classification");
}
@Override
public List<ClassificationAttribute> getDirectAttributes() {
ArrayList<ClassificationAttribute> attributes = new ArrayList<ClassificationAttribute>();
Resource attributesRes = this.resource.getChild("attributes");
if (attributesRes != null) {
Iterator resources = attributesRes.listChildren();
while (resources.hasNext()) {
Resource r = (Resource)resources.next();
try {
ClassificationAttributeImpl attribute = new ClassificationAttributeImpl(r);
attributes.add(attribute);
}
catch (CommerceException e) {
this.log.error("The resource at " + r.getPath() + " is not an attribute", (Throwable)e);
}
}
}
return attributes;
}
@Override
public List<ClassificationAttribute> getAttributes() {
List<ClassificationAttribute> attributes = this.getDirectAttributes();
if (this.isRootCategory()) {
return attributes;
}
ClassificationCategory parentCategory = this.getParentCategory();
while (!parentCategory.isRootCategory()) {
attributes.addAll(parentCategory.getDirectAttributes());
parentCategory = parentCategory.getParentCategory();
}
attributes.addAll(parentCategory.getDirectAttributes());
return attributes;
}
@Override
public ClassificationAttribute getAttribute(String name) {
Resource attributeRes = this.resource.getChild("attributes/" + name);
try {
return new ClassificationAttributeImpl(attributeRes);
}
catch (CommerceException e) {
this.log.error("There is no attribute with name: " + name, (Throwable)e);
return null;
}
}
@Override
public ClassificationCategory getParentCategory() {
ClassificationCategoryImpl parentCategory = null;
Resource categoriesRes = this.resource.getParent();
if (categoriesRes != null && categoriesRes.getName().equals("categories")) {
Resource parentCategoryRes = categoriesRes.getParent();
try {
parentCategory = new ClassificationCategoryImpl(parentCategoryRes);
}
catch (CommerceException e) {
this.log.error("The resource at " + parentCategoryRes.getPath() + " is not a classification category", (Throwable)e);
}
}
return parentCategory;
}
@Override
public List<ClassificationCategory> getSubCategories() {
ArrayList<ClassificationCategory> categories = new ArrayList<ClassificationCategory>();
Resource categoriesRes = this.resource.getChild("categories");
if (categoriesRes == null) {
return null;
}
Iterator resources = categoriesRes.listChildren();
while (resources.hasNext()) {
Resource r = (Resource)resources.next();
try {
ClassificationCategoryImpl category = new ClassificationCategoryImpl(r);
categories.add(category);
}
catch (CommerceException e) {
this.log.error("The resource at " + r.getPath() + " is not a category", (Throwable)e);
}
}
return categories;
}
@Override
public ClassificationCategory getSubCategory(String name) {
Resource categoryRes = this.resource.getChild("categories/" + name);
try {
return new ClassificationCategoryImpl(categoryRes);
}
catch (CommerceException e) {
this.log.error("There is no category with name: " + name, (Throwable)e);
return null;
}
}
@Override
public Iterable<Product> getDirectProducts() {
List products = new ArrayList<Product>();
Resource productsRes = this.resource.getChild("products");
if (productsRes == null) {
return products;
}
ValueMap properties = (ValueMap)productsRes.adaptTo(ValueMap.class);
String basePath = (String)properties.get("basePath", (Object)"/etc/commerce/products");
String[] matchTags = (String[])properties.get("matchTags", (Object)new String[0]);
String searchQuery = (String)properties.get("search", (Object)"");
String queryType = (String)properties.get("searchType", (Object)"simple");
if (matchTags.length == 0 && searchQuery.length() == 0) {
return products;
}
products = matchTags.length > 0 ? this.findProductsByTag(basePath, matchTags) : (queryType.equals("simple") ? this.findProductsBySearch(basePath, searchQuery) : this.findProductsByQuery(queryType, searchQuery));
return products;
}
@Override
public Iterable<Product> getProducts() {
Iterable<Product> directProducts = this.getDirectProducts();
if (directProducts.iterator().hasNext()) {
return directProducts;
}
HashSet<Product> products = new HashSet<Product>();
List<ClassificationCategory> subCategories = this.getSubCategories();
if (subCategories.isEmpty()) {
return products;
}
for (ClassificationCategory subCategory : subCategories) {
Iterable<Product> subCategoryProducts = subCategory.getProducts();
for (Product subCategoryProduct : subCategoryProducts) {
products.add(subCategoryProduct);
}
}
return products;
}
@Override
public boolean hasDirectProduct(Product product) {
Iterable<Product> directProducts = this.getDirectProducts();
for (Product p : directProducts) {
if (!p.getPath().equals(product.getPath())) continue;
return true;
}
return false;
}
@Override
public boolean hasProduct(Product product) {
Iterable<Product> products = this.getProducts();
for (Product p : products) {
if (!p.getPath().equals(product.getPath())) continue;
return true;
}
return false;
}
private boolean isCategory(Resource resource) {
return this.isCommerceType(resource, "classification.category");
}
private boolean isCommerceType(Resource res, String value) {
ValueMap properties = (ValueMap)res.adaptTo(ValueMap.class);
String commerceType = (String)properties.get("cq:commerceType", String.class);
return StringUtils.equals((String)commerceType, (String)value);
}
private List<Product> findProductsByTag(String basePath, String[] matchTags) {
TagManager tagManager = (TagManager)this.resolver.adaptTo(TagManager.class);
RangeIterator results = tagManager.find(basePath, matchTags);
ArrayList<Product> products = new ArrayList<Product>();
while (results != null && results.hasNext()) {
Resource r = (Resource)results.next();
Product p = (Product)r.adaptTo(Product.class);
if (p == null) continue;
products.add(p);
}
return products;
}
private List<Product> findProductsBySearch(String basePath, String searchString) {
ArrayList<Product> products = new ArrayList<Product>();
Resource root = this.resolver.getResource(basePath);
if (root == null) {
this.log.error("Product base path {} not found.", (Object)basePath);
return products;
}
try {
SimpleSearch search = (SimpleSearch)root.adaptTo(SimpleSearch.class);
if (search != null) {
search.setQuery(searchString);
search.setSearchIn(basePath);
search.setHitsPerPage(100000);
Predicate typePredicate = new Predicate("type", "type");
typePredicate.set("type", "nt:unstructured");
search.addPredicate(typePredicate);
SearchResult result = search.getResult();
if (result != null) {
for (Hit hit : result.getHits()) {
Product p = (Product)hit.getResource().adaptTo(Product.class);
if (p == null || p.getBaseProduct() != p) continue;
products.add(p);
}
}
}
}
catch (Exception e) {
this.log.error("Product data search failed", (Throwable)e);
}
return products;
}
private List<Product> findProductsByQuery(String queryType, String query) {
Session session = (Session)this.resolver.adaptTo(Session.class);
ArrayList<Product> products = new ArrayList<Product>();
try {
Query jcrQuery = session.getWorkspace().getQueryManager().createQuery(query, queryType);
NodeIterator iterator = jcrQuery.execute().getNodes();
while (iterator != null && iterator.hasNext()) {
Resource r = this.resolver.getResource(iterator.nextNode().getPath());
Product p = (Product)r.adaptTo(Product.class);
if (p == null) continue;
products.add(p);
}
}
catch (Exception e) {
this.log.error("Product data search failed", (Throwable)e);
}
return products;
}
}