ProductCollectionManagerImpl.java
7.12 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* 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.apache.sling.api.resource.ValueMap
* org.apache.sling.resource.collection.ResourceCollection
* org.apache.sling.resource.collection.ResourceCollectionManager
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.commerce.impl.collection;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.Product;
import com.adobe.cq.commerce.api.collection.ProductCollection;
import com.adobe.cq.commerce.api.collection.ProductCollectionManager;
import com.adobe.cq.commerce.impl.collection.CollectionBasedProductCollection;
import com.adobe.cq.commerce.impl.collection.ProductBasedProductCollection;
import com.adobe.cq.commerce.impl.collection.QueryBasedProductCollection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
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.apache.sling.api.resource.ValueMap;
import org.apache.sling.resource.collection.ResourceCollection;
import org.apache.sling.resource.collection.ResourceCollectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ProductCollectionManagerImpl
implements ProductCollectionManager {
protected static final String DEFAULT_COLLECTION_ROOT = "/etc/commerce/collections";
protected static final String PRODUCT_COLLECTION_RESOURCE_TYPE_VALUE = "commerce/collection";
protected static final String PN_PRODUCT_COLLECTION_TYPE = "commerce.collection.type";
private final Logger log = LoggerFactory.getLogger(ProductCollectionManagerImpl.class);
private ResourceResolver resolver;
private ResourceCollectionManager resourceCollectionManager;
public ProductCollectionManagerImpl(ResourceResolver resolver) {
this.resolver = resolver;
this.resourceCollectionManager = (ResourceCollectionManager)resolver.adaptTo(ResourceCollectionManager.class);
}
@Override
public ProductCollection getCollection(String path) {
Resource resource = this.resolver.getResource(path);
if (resource == null) {
return null;
}
if (!resource.isResourceType("commerce/collection")) {
return null;
}
ValueMap properties = (ValueMap)resource.adaptTo(ValueMap.class);
String collectionType = (String)properties.get("commerce.collection.type", String.class);
if ("query-based".equals(collectionType)) {
return new QueryBasedProductCollection(resource);
}
if ("product-based".equals(collectionType)) {
return new ProductBasedProductCollection(resource);
}
if ("collection-based".equals(collectionType)) {
return new CollectionBasedProductCollection(resource);
}
return null;
}
@Override
public Iterator<ProductCollection> getProductCollections(Product product) {
ArrayList<ProductCollection> collections = new ArrayList<ProductCollection>();
String query = "/jcr:root/etc/commerce/collections//element(*, nt:base)[@sling:resourceType='commerce/collection']";
Iterator collectionResources = this.resolver.findResources(query, "xpath");
while (collectionResources.hasNext()) {
ProductCollection collection;
Resource collectionRes = (Resource)collectionResources.next();
if (collectionRes == null || (collection = (ProductCollection)collectionRes.adaptTo(ProductCollection.class)) == null || !collection.contains(product)) continue;
collections.add(collection);
}
return collections.iterator();
}
@Override
public ProductCollection createCollection(String parentPath, String name) throws CommerceException {
return this.createCollection(parentPath, name, null);
}
@Override
public ProductCollection createCollection(String parentPath, String name, Map<String, Object> properties) throws CommerceException {
Resource parentResource = this.resolver.getResource(parentPath);
try {
if (properties == null) {
properties = new HashMap<String, Object>();
}
properties.put("sling:resourceType", "commerce/collection");
if (properties.get("commerce.collection.type") == null) {
properties.put("commerce.collection.type", "product-based");
}
if (properties.get("jcr:primaryType") == null) {
properties.put("jcr:primaryType", "nt:unstructured");
}
properties.put("jcr:lastModifiedBy", this.resolver.getUserID());
properties.put("jcr:lastModified", Calendar.getInstance());
ResourceCollection resourceCollection = this.resourceCollectionManager.createCollection(parentResource, name, properties);
this.resolver.commit();
return this.getCollection(resourceCollection.getPath());
}
catch (PersistenceException e) {
throw new CommerceException("Cannot create a collection below " + parentPath, (Throwable)e);
}
}
@Override
public void updateCollection(ProductCollection collection, Map<String, Object> properties) throws CommerceException {
if (collection == null) {
throw new CommerceException("Cannot update the collection as it is undefined.");
}
Resource resource = this.resolver.getResource(collection.getPath());
try {
ModifiableValueMap currentProps = (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class);
currentProps.putAll(properties);
currentProps.put((Object)"jcr:lastModifiedBy", (Object)this.resolver.getUserID());
currentProps.put((Object)"jcr:lastModified", (Object)Calendar.getInstance());
this.resolver.commit();
}
catch (PersistenceException e) {
throw new CommerceException("Cannot delete the collection at " + collection.getPath(), (Throwable)e);
}
}
@Override
public void deleteCollection(ProductCollection collection) throws CommerceException {
if (collection == null) {
throw new CommerceException("Cannot delete the collection as it is undefined.");
}
Resource resource = this.resolver.getResource(collection.getPath());
boolean deleted = false;
try {
deleted = this.resourceCollectionManager.deleteCollection(resource);
this.resolver.commit();
}
catch (PersistenceException e) {
throw new CommerceException("Cannot delete the collection at " + collection.getPath(), (Throwable)e);
}
if (!deleted) {
throw new CommerceException("Cannot delete the collection at " + collection.getPath());
}
}
}