ClassificationManagerImpl.java
23.1 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.nodetype.NodeType
* javax.jcr.query.Query
* javax.jcr.query.QueryManager
* javax.jcr.query.QueryResult
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* 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.api.classification.ClassificationManager;
import com.adobe.cq.commerce.impl.classification.ClassificationAttributeImpl;
import com.adobe.cq.commerce.impl.classification.ClassificationCategoryImpl;
import com.adobe.cq.commerce.impl.classification.ClassificationImpl;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.nodetype.NodeType;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ClassificationManagerImpl
implements ClassificationManager {
protected static final String DEFAULT_CLASSIFICATION_ROOT = "/etc/commerce/classifications";
protected final Logger log = LoggerFactory.getLogger(ClassificationManagerImpl.class);
private ResourceResolver resolver;
public ClassificationManagerImpl(ResourceResolver resolver) {
this.resolver = resolver;
}
@Override
public Classification getClassification(String path) {
ClassificationImpl classification = null;
Resource classificationRes = this.resolver.getResource(path);
if (classificationRes == null) {
return null;
}
try {
classification = new ClassificationImpl(classificationRes);
}
catch (CommerceException e) {
this.log.error("The resource at " + classificationRes.getPath() + " is not a classification", (Throwable)e);
}
return classification;
}
@Override
public ClassificationCategory getCategory(String path) {
ClassificationCategoryImpl category = null;
Resource categoryRes = this.resolver.getResource(path);
try {
category = new ClassificationCategoryImpl(categoryRes);
}
catch (CommerceException e) {
this.log.error("The resource at " + path + " is not a classification category", (Throwable)e);
}
return category;
}
@Override
public ClassificationAttribute getAttribute(String path) {
ClassificationAttributeImpl attribute = null;
Resource attributeRes = this.resolver.getResource(path);
try {
attribute = new ClassificationAttributeImpl(attributeRes);
}
catch (CommerceException e) {
this.log.error("The resource at " + path + " is not a classification category", (Throwable)e);
}
return attribute;
}
@Override
public Classification createClassification(String parentPath, String name, Map<String, Object> properties) throws CommerceException {
Resource classificationRes;
Resource parentRes = this.resolver.getResource(parentPath);
try {
properties.put("cq:commerceType", "classification");
if (properties.get("jcr:primaryType") == null) {
properties.put("jcr:primaryType", "nt:unstructured");
}
classificationRes = this.resolver.create(parentRes, name, properties);
this.updateLastModProps(classificationRes);
this.resolver.commit();
}
catch (PersistenceException e) {
throw new CommerceException("Failed to create a classification below " + parentPath, (Throwable)e);
}
return new ClassificationImpl(classificationRes);
}
@Override
public void removeClassification(String path) throws CommerceException {
Classification classification = this.getClassification(path);
if (classification == null) {
throw new CommerceException("The resource at " + path + " is not a classification");
}
Resource classificationRes = this.resolver.getResource(path);
try {
this.resolver.delete(classificationRes);
this.resolver.commit();
}
catch (PersistenceException e) {
throw new CommerceException("Failed to remove the classification at " + path, (Throwable)e);
}
}
@Override
public void updateClassification(String path, Map<String, Object> properties) throws CommerceException {
Classification classification = this.getClassification(path);
if (classification == null) {
throw new CommerceException("The resource at " + path + " is not a classification");
}
Resource classificationRes = this.resolver.getResource(path);
ModifiableValueMap currentProps = (ModifiableValueMap)classificationRes.adaptTo(ModifiableValueMap.class);
currentProps.putAll(properties);
this.updateLastModProps(classificationRes);
try {
this.resolver.commit();
}
catch (PersistenceException pe) {
throw new CommerceException("Failed to update the classification at " + path, (Throwable)pe);
}
}
@Override
public ClassificationCategory addRootCategory(String path, Map<String, Object> properties) throws CommerceException {
Classification classification = this.getClassification(path);
if (classification == null) {
throw new CommerceException("The resource at " + path + " is not a classification");
}
Resource classificationRes = this.resolver.getResource(path);
Resource rootCategoryRes = classificationRes.getChild("rootcategory");
if (rootCategoryRes != null) {
throw new CommerceException("The classification at " + path + " has already a root category.");
}
try {
properties.put("cq:commerceType", "classification.category");
rootCategoryRes = this.resolver.create(classificationRes, "rootcategory", properties);
this.updateLastModProps(rootCategoryRes);
this.updateLastModProps(classificationRes);
this.resolver.commit();
}
catch (PersistenceException pe) {
throw new CommerceException("Failed to add the root category to the classification at " + path, (Throwable)pe);
}
return new ClassificationCategoryImpl(rootCategoryRes);
}
@Override
public ClassificationCategory addCategory(String parentPath, String name, Map<String, Object> properties) throws CommerceException {
Resource categoryRes;
ClassificationCategory parentCategory = this.getCategory(parentPath);
if (parentCategory == null) {
this.log.error("The resource at {} is not a category.", (Object)parentPath);
return null;
}
Resource parentCategoryRes = this.resolver.getResource(parentPath);
Resource categoriesRes = parentCategoryRes.getChild("categories");
try {
if (categoriesRes == null) {
categoriesRes = this.resolver.create(parentCategoryRes, "categories", null);
}
properties.put("cq:commerceType", "classification.category");
categoryRes = this.createUniqueResource(categoriesRes, name, properties);
this.updateLastModProps(categoryRes);
if (parentCategory.getClassification() != null) {
Resource classificationRes = this.resolver.getResource(parentCategory.getClassification().getPath());
this.updateLastModProps(classificationRes);
}
this.resolver.commit();
}
catch (PersistenceException pe) {
throw new CommerceException("Failed to add a sub-category to the category at " + parentPath, (Throwable)pe);
}
return new ClassificationCategoryImpl(categoryRes);
}
@Override
public void removeCategory(String path) throws CommerceException {
ClassificationCategory category = this.getCategory(path);
if (category == null) {
this.log.error("The resource at {} is not a category.", (Object)path);
return;
}
Resource categoryRes = this.resolver.getResource(path);
try {
if (category.getClassification() != null) {
Resource classificationRes = this.resolver.getResource(category.getClassification().getPath());
this.updateLastModProps(classificationRes);
}
this.resolver.delete(categoryRes);
this.resolver.commit();
}
catch (PersistenceException pe) {
throw new CommerceException("Failed to remove the category at " + path, (Throwable)pe);
}
}
@Override
public void updateCategory(String path, Map<String, Object> properties) throws CommerceException {
ClassificationCategory category = this.getCategory(path);
if (category == null) {
this.log.error("The resource at {} is not a category.", (Object)path);
return;
}
Resource categoryRes = this.resolver.getResource(path);
ModifiableValueMap currentProps = (ModifiableValueMap)categoryRes.adaptTo(ModifiableValueMap.class);
currentProps.putAll(properties);
this.updateLastModProps(categoryRes);
if (category.getClassification() != null) {
Resource classificationRes = this.resolver.getResource(category.getClassification().getPath());
this.updateLastModProps(classificationRes);
}
try {
this.resolver.commit();
}
catch (PersistenceException pe) {
throw new CommerceException("Failed to update the category at " + path, (Throwable)pe);
}
}
@Override
public ClassificationAttribute addAttribute(String parentPath, String name, Map<String, Object> properties) throws CommerceException {
Resource attributeRes;
ClassificationCategory category = this.getCategory(parentPath);
if (category == null) {
this.log.error("The resource at {} is not a category.", (Object)parentPath);
return null;
}
Resource categoryRes = this.resolver.getResource(parentPath);
Resource attributesRes = categoryRes.getChild("attributes");
try {
if (attributesRes == null) {
attributesRes = this.resolver.create(categoryRes, "attributes", null);
}
properties.put("cq:commerceType", "classification.attribute");
attributeRes = this.createUniqueResource(attributesRes, name, properties);
this.updateLastModProps(attributeRes);
if (category.getClassification() != null) {
Resource classificationRes = this.resolver.getResource(category.getClassification().getPath());
this.updateLastModProps(classificationRes);
}
this.resolver.commit();
}
catch (PersistenceException pe) {
throw new CommerceException("Failed to add an attribute to the category at {}" + parentPath, (Throwable)pe);
}
return new ClassificationAttributeImpl(attributeRes);
}
@Override
public void removeAttribute(String path) throws CommerceException {
ClassificationAttribute attribute = this.getAttribute(path);
if (attribute == null) {
this.log.error("The resource at {} is not an attribute.", (Object)path);
return;
}
Resource attributeRes = this.resolver.getResource(path);
try {
if (this.getClassification(attribute) != null) {
Resource classificationRes = this.resolver.getResource(this.getClassification(attribute).getPath());
this.updateLastModProps(classificationRes);
}
this.resolver.delete(attributeRes);
this.resolver.commit();
}
catch (PersistenceException pe) {
throw new CommerceException("Failed to remove the attribute at " + path, (Throwable)pe);
}
}
@Override
public void updateAttribute(String path, Map<String, Object> properties) throws CommerceException {
ClassificationAttribute attribute = this.getAttribute(path);
if (attribute == null) {
this.log.error("The resource at {} is not an attribute.", (Object)path);
return;
}
Resource attributeRes = this.resolver.getResource(path);
ModifiableValueMap currentProps = (ModifiableValueMap)attributeRes.adaptTo(ModifiableValueMap.class);
currentProps.putAll(properties);
this.updateLastModProps(attributeRes);
if (this.getClassification(attribute) != null) {
Resource classificationRes = this.resolver.getResource(this.getClassification(attribute).getPath());
this.updateLastModProps(classificationRes);
}
try {
this.resolver.commit();
}
catch (PersistenceException pe) {
throw new CommerceException("Failed to update the attribute at " + path, (Throwable)pe);
}
}
@Override
public void orderBefore(String srcAttributePath, String destAttributePath) throws CommerceException {
try {
Session session = (Session)this.resolver.adaptTo(Session.class);
Node srcNode = session.getNode(srcAttributePath);
Node destNode = session.getNode(destAttributePath);
Node parentNode = srcNode.getParent();
String destName = destNode.getName();
if (!parentNode.getPrimaryNodeType().hasOrderableChildNodes()) {
throw new CommerceException("Folder does not support ordering: " + parentNode.getPath());
}
if (destName == null || parentNode.hasNode(destName)) {
parentNode.orderBefore(srcNode.getName(), destName);
session.save();
}
}
catch (Exception e) {
throw new CommerceException("Error while ordering " + srcAttributePath + " before " + destAttributePath, e);
}
}
@Override
public void assignProducts(String path, Map<String, Object> productListDefinition) throws CommerceException {
ClassificationCategory category = this.getCategory(path);
if (category == null) {
this.log.error("The resource at {} is not a category.", (Object)path);
return;
}
Resource categoryRes = this.resolver.getResource(path);
Resource productsRes = categoryRes.getChild("products");
try {
if (productsRes == null) {
productsRes = this.resolver.create(categoryRes, "products", null);
}
ModifiableValueMap currentProps = (ModifiableValueMap)productsRes.adaptTo(ModifiableValueMap.class);
currentProps.putAll(productListDefinition);
this.updateLastModProps(productsRes);
if (category.getClassification() != null) {
Resource classificationRes = this.resolver.getResource(category.getClassification().getPath());
this.updateLastModProps(classificationRes);
}
this.resolver.commit();
}
catch (PersistenceException pe) {
throw new CommerceException("Failed to assign products to the category at " + path, (Throwable)pe);
}
}
@Override
public Collection<Classification> getClassifications(Product product) {
HashSet<Classification> classifications = new HashSet<Classification>();
Collection<Classification> allClassifications = this.getAllClassifications();
for (Classification classification : allClassifications) {
if (!this.isAssigned(product, classification)) continue;
classifications.add(classification);
}
return classifications;
}
@Override
public Collection<ClassificationCategory> getClassificationCategories(Product product) {
HashSet<ClassificationCategory> classificationCategories = new HashSet<ClassificationCategory>();
Collection<Classification> allClassifications = this.getAllClassifications();
for (Classification classification : allClassifications) {
ClassificationCategory category = this.getClassificationCategory(product, classification);
if (category == null) continue;
classificationCategories.add(category);
}
return classificationCategories;
}
@Override
public Collection<Classification> getClassifications(List<Product> products) {
HashSet<String> classificationIds = new HashSet<String>();
for (Product product : products) {
Collection<Classification> productClassifications = this.getClassifications(product);
for (Classification classification : productClassifications) {
String classificationId = classification.getPath();
if (classificationIds.contains(classificationId)) continue;
classificationIds.add(classificationId);
}
}
HashSet<Classification> classifications = new HashSet<Classification>();
for (String classificationId : classificationIds) {
Classification c = this.getClassification(classificationId);
classifications.add(c);
}
return classifications;
}
@Override
public Collection<ClassificationCategory> getClassificationCategories(List<Product> products) {
HashSet<String> categoryIds = new HashSet<String>();
for (Product product : products) {
Collection<ClassificationCategory> productClassificationCategories = this.getClassificationCategories(product);
for (ClassificationCategory category : productClassificationCategories) {
String categoryId = category.getPath();
if (categoryIds.contains(categoryId)) continue;
categoryIds.add(categoryId);
}
}
HashSet<ClassificationCategory> classificationCategories = new HashSet<ClassificationCategory>();
for (String categoryId : categoryIds) {
ClassificationCategory c = this.getCategory(categoryId);
classificationCategories.add(c);
}
return classificationCategories;
}
private boolean isAssigned(Product product, Classification classification) {
ClassificationCategory category = this.getClassificationCategory(product, classification);
return category != null;
}
private ClassificationCategory getClassificationCategory(Product product, Classification classification) {
ClassificationCategory rootCategory = classification.getRootCategory();
ClassificationCategory category = this.getMatchingCategory(product, rootCategory);
return category;
}
private ClassificationCategory getMatchingCategory(Product product, ClassificationCategory baseCategory) {
ClassificationCategory matchingCategory = null;
if (baseCategory != null) {
if (baseCategory.hasDirectProduct(product)) {
return baseCategory;
}
List<ClassificationCategory> subCategories = baseCategory.getSubCategories();
if (subCategories != null) {
for (ClassificationCategory subCategory : subCategories) {
if (matchingCategory != null) continue;
matchingCategory = this.getMatchingCategory(product, subCategory);
}
}
}
return matchingCategory;
}
private Collection<Classification> getAllClassifications() {
HashSet<Classification> classifications = new HashSet<Classification>();
String searchQuery = "/jcr:root/etc/commerce/classifications//element(*,nt:base)[@cq:commerceType='classification']";
Session session = (Session)this.resolver.adaptTo(Session.class);
try {
Query jcrQuery = session.getWorkspace().getQueryManager().createQuery(searchQuery, "xpath");
NodeIterator iterator = jcrQuery.execute().getNodes();
while (iterator != null && iterator.hasNext()) {
Resource r = this.resolver.getResource(iterator.nextNode().getPath());
Classification c = (Classification)r.adaptTo(Classification.class);
if (c == null) continue;
classifications.add(c);
}
}
catch (Exception e) {
this.log.error("Classification data search failed", (Throwable)e);
}
return classifications;
}
private Resource createUniqueResource(Resource parent, String nameHint, Map<String, Object> properties) throws PersistenceException {
String childName;
Resource child = parent.getChild(nameHint);
if (child == null) {
return this.resolver.create(parent, nameHint, properties);
}
int i = 0;
do {
childName = nameHint + String.valueOf(i);
++i;
} while (parent.getChild(childName) != null);
return this.resolver.create(parent, childName, properties);
}
private void updateLastModProps(Resource resource) {
if (resource == null) {
return;
}
ModifiableValueMap currentProps = (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class);
currentProps.put((Object)"jcr:lastModifiedBy", (Object)this.resolver.getUserID());
currentProps.put((Object)"jcr:lastModified", (Object)Calendar.getInstance());
}
private Classification getClassification(ClassificationAttribute attribute) {
if (attribute == null) {
this.log.error("The attribute does not exist");
return null;
}
String attributePath = attribute.getPath();
Resource attributeRes = this.resolver.getResource(attributePath);
if (attributeRes == null || attributeRes.getParent() == null || attributeRes.getParent().getParent() == null) {
this.log.error("The attribute does not belong to a category");
return null;
}
Resource categoryRes = attributeRes.getParent().getParent();
ClassificationCategory category = this.getCategory(categoryRes.getPath());
if (category == null) {
this.log.error("The category of the attribute at {} is not defined", (Object)attribute.getPath());
return null;
}
return category.getClassification();
}
}