PIMPostProcessor.java
13.4 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.commerce.api.CommerceException
* com.adobe.cq.commerce.api.Product
* com.adobe.cq.commerce.api.asset.ProductAssetManager
* com.adobe.cq.commerce.common.AbstractJcrProduct
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.commons.osgi.PropertiesUtil
* org.apache.sling.servlets.post.Modification
* org.apache.sling.servlets.post.ModificationType
* org.apache.sling.servlets.post.SlingPostProcessor
* org.osgi.service.component.ComponentContext
* org.osgi.service.event.Event
* org.osgi.service.event.EventAdmin
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.commerce.pim.impl;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.Product;
import com.adobe.cq.commerce.api.asset.ProductAssetManager;
import com.adobe.cq.commerce.common.AbstractJcrProduct;
import java.util.Calendar;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Activate;
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.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.ModificationType;
import org.apache.sling.servlets.post.SlingPostProcessor;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="Adobe CQ Commerce PIM Post Processor", description="Updates lastMod dates after POST operations on product data.")
@Service(value={SlingPostProcessor.class})
@Property(name="service.description", value={"Updates lastMod dates after POST operations on product data"})
public class PIMPostProcessor
implements SlingPostProcessor {
private Logger logger;
@Reference
private ResourceResolverFactory resolverFactory;
@Reference
EventAdmin eventAdmin;
private String[] searchRoots;
private static final String[] DEFAULT_PATHS = new String[]{"/etc/commerce/products"};
@Property(label="Paths", description="Paths under which product data will be processed", value={"/etc/commerce/products"})
public static final String PROPERTY_PATHS = "cq.commerce.pimpostprocessor.searchRoots";
public PIMPostProcessor() {
this.logger = LoggerFactory.getLogger(this.getClass());
this.resolverFactory = null;
}
@Activate
protected void activate(ComponentContext context) {
String[] configuredPaths = PropertiesUtil.toStringArray(context.getProperties().get("cq.commerce.pimpostprocessor.searchRoots"), (String[])DEFAULT_PATHS);
this.searchRoots = new String[configuredPaths.length];
for (int i = 0; i < configuredPaths.length; ++i) {
String path = configuredPaths[i];
this.searchRoots[i] = path.endsWith("/") ? path : path + "/";
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void process(SlingHttpServletRequest request, List<Modification> changes) throws Exception {
ResourceResolver postResolver = request.getResourceResolver();
Session postSession = (Session)postResolver.adaptTo(Session.class);
ResourceResolver serviceResolver = null;
try {
HashMap<String, String> authenticationInfo = new HashMap<String, String>();
authenticationInfo.put("sling.service.subservice", "backend");
serviceResolver = this.resolverFactory.getServiceResourceResolver(authenticationInfo);
Session serviceSession = (Session)serviceResolver.adaptTo(Session.class);
HashMap<String, ModificationType> changedNodes = new HashMap<String, ModificationType>();
for (Modification mod : changes) {
switch (mod.getType()) {
case CREATE: {
this.processPath(mod.getSource(), postSession, changedNodes, ModificationType.CREATE);
break;
}
case MODIFY:
case ORDER: {
this.processPath(mod.getSource(), postSession, changedNodes, ModificationType.MODIFY);
break;
}
case MOVE: {
this.processPath(mod.getSource(), serviceSession, changedNodes, ModificationType.DELETE);
this.processPath(mod.getDestination(), postSession, changedNodes, ModificationType.CREATE);
break;
}
case COPY: {
this.processPath(mod.getDestination(), postSession, changedNodes, ModificationType.CREATE);
break;
}
case DELETE: {
this.processPath(mod.getSource(), serviceSession, changedNodes, mod.getType());
}
}
}
for (Map.Entry change : changedNodes.entrySet()) {
Resource baseProduct;
String changedPath = (String)change.getKey();
ModificationType modificationType = (ModificationType)change.getValue();
this.logger.debug("post-processing modification to " + changedPath);
Resource changedResource = postResolver.getResource(changedPath);
if (changedResource == null) {
changedResource = serviceResolver.getResource(changedPath);
}
if ((baseProduct = this.getBaseProduct(changedResource)) == null) continue;
try {
Calendar now = Calendar.getInstance();
ProductAssetManager productAssetManager = (ProductAssetManager)postResolver.adaptTo(ProductAssetManager.class);
if (changedPath.equals(baseProduct.getPath())) {
this.generateProductEvents(baseProduct, modificationType);
} else if (this.deleteEmptyProductAsset(postResolver, changedResource, changes, productAssetManager)) {
this.generateProductEvents(baseProduct, ModificationType.MODIFY);
} else {
this.setLastModDate(postResolver, changedResource, now, ModificationType.MODIFY, changes);
this.deleteEmptyProductAssets(postResolver, changedResource, changes, productAssetManager);
this.generateProductEvents(baseProduct, ModificationType.MODIFY);
}
this.setLastModDate(postResolver, baseProduct, now, modificationType, changes);
}
catch (RepositoryException e) {
this.logger.error("failed to post-process modification to " + changedPath, (Throwable)e);
}
}
}
finally {
if (serviceResolver != null && serviceResolver.isLive()) {
serviceResolver.close();
}
}
}
private void processPath(String path, Session session, Map<String, ModificationType> changedNodes, ModificationType type) throws Exception {
ModificationType existing;
Item item;
boolean underSearchPaths = false;
for (String searchRoot : this.searchRoots) {
if (!path.startsWith(searchRoot)) continue;
underSearchPaths = true;
break;
}
if (!underSearchPaths) {
return;
}
Item item2 = item = session.itemExists(path) ? session.getItem(path) : null;
while (item != null && !item.isNode()) {
item = item.getParent();
}
if (item != null && (existing = changedNodes.get(item.getPath())) != ModificationType.CREATE && existing != ModificationType.DELETE) {
changedNodes.put(item.getPath(), type);
}
}
private void setLastModDate(ResourceResolver postResolver, Resource resource, Calendar now, ModificationType modificationType, List<Modification> changes) throws RepositoryException {
if ((resource = postResolver.getResource(resource.getPath())) != null) {
Node node = (Node)resource.adaptTo(Node.class);
this.logger.debug("setting lastMod on:", (Object)node.getPath());
if (modificationType == ModificationType.CREATE && !node.hasProperty("jcr:created")) {
node.setProperty("jcr:created", now);
} else {
node.setProperty("jcr:lastModified", now);
}
changes.add(Modification.onModified((String)node.getPath()));
}
}
private void deleteEmptyProductAssets(ResourceResolver postResolver, Resource productResource, List<Modification> changes, ProductAssetManager productAssetManager) throws RepositoryException {
Product product = (Product)productResource.adaptTo(Product.class);
if (product == null) {
return;
}
List productAssets = product.getAssets();
for (Resource productAsset : productAssets) {
this.deleteEmptyProductAsset(postResolver, productAsset, changes, productAssetManager);
}
}
private boolean deleteEmptyProductAsset(ResourceResolver postResolver, Resource productAsset, List<Modification> changes, ProductAssetManager productAssetManager) throws RepositoryException {
if (productAsset != null) {
productAsset = postResolver.getResource(productAsset.getPath());
}
if (productAsset != null && productAssetManager != null) {
String productAssetPath = productAsset.getPath();
String referencedAsset = productAssetManager.getReferencedAsset(productAssetPath);
if (productAssetManager.isSupportedAsset(productAssetPath) && StringUtils.isEmpty((String)referencedAsset)) {
try {
productAssetManager.removeAsset(productAssetPath);
changes.add(Modification.onDeleted((String)productAssetPath));
return true;
}
catch (CommerceException e) {
this.logger.error("Could not delete the empty product asset at: {}", (Object)productAssetPath);
return false;
}
}
}
return false;
}
private void generateProductEvents(Resource baseProduct, ModificationType modificationType) throws RepositoryException {
String baseProductPath = baseProduct.getPath();
Hashtable<String, String> properties = new Hashtable<String, String>();
properties.put("path", baseProductPath);
if (modificationType == ModificationType.CREATE) {
this.logger.debug("posting com/adobe/cq/commerce/pim/PRODUCT_ADDED event for: " + baseProductPath);
this.eventAdmin.postEvent(new Event("com/adobe/cq/commerce/pim/PRODUCT_ADDED", properties));
} else if (modificationType == ModificationType.DELETE) {
this.logger.debug("posting com/adobe/cq/commerce/pim/PRODUCT_DELETED event for: " + baseProductPath);
this.eventAdmin.postEvent(new Event("com/adobe/cq/commerce/pim/PRODUCT_DELETED", properties));
} else {
this.logger.debug("posting com/adobe/cq/commerce/pim/PRODUCT_MODIFIED event for: " + baseProductPath);
this.eventAdmin.postEvent(new Event("com/adobe/cq/commerce/pim/PRODUCT_MODIFIED", properties));
}
}
protected Resource getBaseProduct(Resource resource) throws RepositoryException {
while (resource != null && !AbstractJcrProduct.isABaseProduct((Resource)resource)) {
resource = resource.getParent();
}
return resource;
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
protected void bindEventAdmin(EventAdmin eventAdmin) {
this.eventAdmin = eventAdmin;
}
protected void unbindEventAdmin(EventAdmin eventAdmin) {
if (this.eventAdmin == eventAdmin) {
this.eventAdmin = null;
}
}
}