ProductAssetManagerImpl.java
17.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.dam.api.Asset
* com.day.cq.dam.commons.util.DamUtil
* org.apache.commons.codec.DecoderException
* org.apache.commons.codec.net.URLCodec
* org.apache.commons.lang.StringUtils
* 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.asset;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.Product;
import com.adobe.cq.commerce.api.asset.ProductAssetHandler;
import com.adobe.cq.commerce.api.asset.ProductAssetHandlerProvider;
import com.adobe.cq.commerce.api.asset.ProductAssetManager;
import com.adobe.cq.commerce.common.AbstractJcrProduct;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.commons.util.DamUtil;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.net.URLCodec;
import org.apache.commons.lang.StringUtils;
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 ProductAssetManagerImpl
implements ProductAssetManager {
private static final Logger log = LoggerFactory.getLogger(ProductAssetManagerImpl.class);
private static final String ASSET_REFERENCE_PLACEHOLDER = "/libs/dam/gui/components/admin/resources/emptydir.png";
private static final String NN_PRODUCT_ASSET_BASE = "assets";
private static final String NN_PRODUCT_ASSET = "asset";
private static final String NN_PRODUCT_IMAGE = "image";
private static final String PN_PRODUCT_REFERENCE = "cq:productReference";
private static final String PN_PRODUCT_ID = "dam:productID";
private ResourceResolver resolver;
private List<ProductAssetHandler> productAssetHandlers;
private String productAssetHandlerFallback;
public ProductAssetManagerImpl(ResourceResolver resolver, ProductAssetHandlerProvider productAssetHandlerProvider) {
this.resolver = resolver;
this.productAssetHandlers = productAssetHandlerProvider.getProductAssetHandlers();
this.productAssetHandlerFallback = productAssetHandlerProvider.getFallbackHandler();
}
@Override
public boolean isSupportedReferencedAsset(String assetReference) {
ProductAssetHandler handler = this.getHandler(assetReference);
if (handler != null) {
return handler.isSupportedReferencedAsset(this.resolver, assetReference);
}
return false;
}
@Override
public boolean isSupportedAsset(String productAssetPath) {
Resource productAssetResource = this.resolver.getResource(productAssetPath);
if (!this.isProductAsset(productAssetResource)) {
return false;
}
ProductAssetHandler handler = this.getHandler(productAssetResource);
if (handler != null) {
return handler.isSupportedAsset(productAssetResource);
}
return false;
}
@Override
public String getReferencedAsset(String productAssetPath) {
Resource productAssetResource = this.resolver.getResource(productAssetPath);
if (!this.isProductAsset(productAssetResource)) {
return null;
}
ProductAssetHandler handler = this.getHandler(productAssetResource);
if (handler != null) {
return handler.getReferencedAsset(productAssetResource);
}
return null;
}
@Override
public Resource getAsset(Product product) {
if (product == null) {
throw new IllegalArgumentException("The product is not defined.");
}
return product.getAsset();
}
@Override
public List<Resource> getAssets(Product product) {
if (product == null) {
throw new IllegalArgumentException("The product is not defined.");
}
return product.getAssets();
}
@Override
public String getThumbnailUrl(Product product, String selectorString) {
Resource productAssetRes = this.getAsset(product);
if (productAssetRes != null) {
return this.getThumbnailUrl(productAssetRes.getPath(), selectorString);
}
return null;
}
@Override
public String getThumbnailUrl(String productAssetPath, String selectorString) {
Resource productAssetResource = this.resolver.getResource(productAssetPath);
if (!this.isProductAsset(productAssetResource)) {
return null;
}
ProductAssetHandler handler = this.getHandler(productAssetResource);
if (handler != null) {
return handler.getThumbnailUrl(productAssetResource, selectorString);
}
return null;
}
@Override
public Resource addAsset(Product product, String assetReference) throws CommerceException {
if (product == null) {
throw new IllegalArgumentException("The product is not defined.");
}
Map<String, Object> assetProperties = null;
if (StringUtils.isEmpty((String)assetReference)) {
ProductAssetHandler fallbackHandler = this.getHandlerByName(this.productAssetHandlerFallback);
if (fallbackHandler == null) {
throw new CommerceException("Failed to add an asset to product at " + product.getPath());
}
assetProperties = fallbackHandler.getAssetProperties(this.resolver, "/libs/dam/gui/components/admin/resources/emptydir.png");
} else {
ProductAssetHandler handler = this.getHandler(assetReference);
if (handler == null) {
throw new CommerceException("No product asset handler found: cannot add the asset to the product at: " + product.getPath());
}
assetProperties = handler.getAssetProperties(this.resolver, assetReference);
}
return this.addAsset(product, assetProperties);
}
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);
}
@Override
public Resource addAsset(Product product, Map<String, Object> productAssetProperties) throws CommerceException {
Resource productAssetRes;
if (product == null) {
throw new IllegalArgumentException("The product is not defined.");
}
try {
Resource productRes = (Resource)product.adaptTo(Resource.class);
Resource productAssetBase = productRes.getChild("assets");
if (productAssetBase == null) {
productAssetBase = this.resolver.create(productRes, "assets", null);
this.resolver.commit();
}
productAssetProperties.put("jcr:lastModifiedBy", this.resolver.getUserID());
productAssetProperties.put("jcr:lastModified", Calendar.getInstance());
productAssetRes = this.createUniqueResource(productAssetBase, "asset", productAssetProperties);
String productAssetPath = productAssetRes.getPath();
String productPath = product.getPath();
String assetReference = this.getReferencedAsset(productAssetPath);
this.addProductRefToDAMAsset(productPath, assetReference);
this.resolver.commit();
}
catch (PersistenceException e) {
throw new CommerceException("Failed to add an asset to product at " + product.getPath(), (Throwable)e);
}
return productAssetRes;
}
@Override
public Resource updateAsset(String productAssetPath, String assetReference) throws CommerceException {
ProductAssetHandler handler = this.getHandler(assetReference);
if (handler == null) {
throw new CommerceException("No product asset handler found: cannot update the product asset at: " + productAssetPath);
}
Map<String, Object> assetProperties = handler.getAssetProperties(this.resolver, assetReference);
return this.updateAsset(productAssetPath, assetProperties);
}
@Override
public Resource updateAsset(String productAssetPath, Map<String, Object> productAssetProperties) throws CommerceException {
Resource productAssetResource = this.resolver.getResource(productAssetPath);
if (productAssetResource == null) {
throw new CommerceException("Failed to update product asset at " + productAssetPath);
}
if (!this.isProductAsset(productAssetResource)) {
throw new CommerceException("This is not a product asset: " + productAssetPath);
}
String oldAssetReference = this.getReferencedAsset(productAssetPath);
ModifiableValueMap currentProps = (ModifiableValueMap)productAssetResource.adaptTo(ModifiableValueMap.class);
currentProps.putAll(productAssetProperties);
HashSet keys = new HashSet(currentProps.keySet());
for (String key : keys) {
if (productAssetProperties.containsKey(key) || key.contains("jcr:")) continue;
currentProps.remove((Object)key);
}
currentProps.put((Object)"jcr:lastModifiedBy", (Object)this.resolver.getUserID());
currentProps.put((Object)"jcr:lastModified", (Object)Calendar.getInstance());
String productPath = this.getProductPath(productAssetPath);
String newAssetReference = this.getReferencedAsset(productAssetPath);
this.removeProductRefFromDAMAsset(productPath, oldAssetReference);
this.addProductRefToDAMAsset(productPath, newAssetReference);
try {
this.resolver.commit();
}
catch (PersistenceException e) {
throw new CommerceException("Failed to update product asset at " + productAssetPath, (Throwable)e);
}
return productAssetResource;
}
@Override
public void removeAsset(String productAssetPath) throws CommerceException {
Resource productAssetResource = this.resolver.getResource(productAssetPath);
if (productAssetResource == null) {
throw new CommerceException("Failed to remove product asset at " + productAssetPath);
}
if (!this.isProductAsset(productAssetResource)) {
throw new CommerceException("This is not a product asset: " + productAssetPath);
}
try {
String productPath = this.getProductPath(productAssetPath);
String assetReference = this.getReferencedAsset(productAssetPath);
this.removeProductRefFromDAMAsset(productPath, assetReference);
this.resolver.delete(productAssetResource);
this.resolver.commit();
}
catch (PersistenceException e) {
throw new CommerceException("Failed to remove product asset at " + productAssetPath, (Throwable)e);
}
}
private void addProductRefToDAMAsset(String productPath, String assetReference) {
Asset asset = this.getAsset(this.resolver, assetReference);
if (asset == null) {
return;
}
Product product = this.getProduct(productPath);
if (product != null) {
String productId = product.getSKU();
Resource assetRes = (Resource)asset.adaptTo(Resource.class);
Resource metadata = assetRes.getChild("jcr:content/metadata");
ModifiableValueMap vm = (ModifiableValueMap)metadata.adaptTo(ModifiableValueMap.class);
vm.put((Object)"cq:productReference", (Object)productPath);
vm.put((Object)"dam:productID", (Object)productId);
}
}
private void removeProductRefFromDAMAsset(String productPath, String assetReference) {
Asset asset = this.getAsset(this.resolver, assetReference);
if (asset == null) {
return;
}
Resource assetRes = (Resource)asset.adaptTo(Resource.class);
Resource metadata = assetRes.getChild("jcr:content/metadata");
ModifiableValueMap vm = (ModifiableValueMap)metadata.adaptTo(ModifiableValueMap.class);
String productReference = (String)vm.get("cq:productReference", String.class);
if (productReference != null && productReference.equals(productPath)) {
vm.remove((Object)"cq:productReference");
vm.remove((Object)"dam:productID");
}
}
private Product getProduct(String productPath) {
Resource res = this.resolver.getResource(productPath);
if (res != null) {
Product product = (Product)res.adaptTo(Product.class);
return product;
}
return null;
}
private Asset getAsset(ResourceResolver resolver, String assetReference) {
if (StringUtils.isEmpty((String)assetReference)) {
return null;
}
try {
assetReference = new URLCodec().decode(assetReference);
}
catch (DecoderException e) {
log.error("Error while decoding fileReference: {}", (Object)assetReference);
}
Resource assetRes = resolver.getResource(assetReference);
if (assetRes == null) {
return null;
}
return DamUtil.resolveToAsset((Resource)assetRes);
}
private String getProductPath(String productAssetPath) {
Resource productAssetRes = this.resolver.getResource(productAssetPath);
Resource parent = productAssetRes.getParent();
Product product = (Product)parent.adaptTo(Product.class);
if (product != null) {
return product.getPath();
}
Resource grandParent = parent.getParent();
product = (Product)grandParent.adaptTo(Product.class);
if (product != null) {
return product.getPath();
}
return null;
}
private ProductAssetHandler getHandler(String assetReference) {
if (StringUtils.isEmpty((String)assetReference)) {
throw new IllegalArgumentException("The asset reference is not defined.");
}
log.debug("Looking for handler for asset: " + assetReference);
for (ProductAssetHandler handler : this.productAssetHandlers) {
if (!handler.isActive() || !handler.isSupportedReferencedAsset(this.resolver, assetReference)) continue;
log.debug("Handler found for asset: " + assetReference);
return handler;
}
log.debug("No handler found for asset: " + assetReference);
log.debug("Using the fallback handler (name = " + this.productAssetHandlerFallback + ")");
return this.getHandlerByName(this.productAssetHandlerFallback);
}
private ProductAssetHandler getHandler(Resource productAssetResource) {
if (productAssetResource == null) {
throw new IllegalArgumentException("The product asset is not defined.");
}
if (!this.isProductAsset(productAssetResource)) {
throw new IllegalArgumentException("This is not a product asset: " + productAssetResource.getPath());
}
log.debug("Looking for handler for asset: " + productAssetResource.getPath());
for (ProductAssetHandler handler : this.productAssetHandlers) {
if (!handler.isActive() || !handler.isSupportedAsset(productAssetResource)) continue;
log.debug("Handler found for asset: " + productAssetResource.getPath());
return handler;
}
log.debug("No handler found for asset: " + productAssetResource.getPath());
log.debug("Using the fallback handler (name = " + this.productAssetHandlerFallback + ")");
return this.getHandlerByName(this.productAssetHandlerFallback);
}
private ProductAssetHandler getHandlerByName(String name) {
log.debug("Looking for handler with name: " + name);
for (ProductAssetHandler handler : this.productAssetHandlers) {
if (!handler.isActive() || !StringUtils.isNotEmpty((String)name) || !name.equals(handler.getHandlerName())) continue;
log.debug("Handler found for name: " + name);
return handler;
}
log.error("No handler found for name: " + name);
return null;
}
private boolean isProductAsset(Resource resource) {
if (resource == null) {
return false;
}
String path = resource.getPath();
Resource parent = resource.getParent();
if (path == null || parent == null) {
return false;
}
return path.endsWith("/image") && AbstractJcrProduct.isAProductOrVariant(parent) || path.contains("/assets/asset") && AbstractJcrProduct.isAProductOrVariant(parent.getParent());
}
}