AbstractJcrProduct.java 17.8 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
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  aQute.bnd.annotation.ConsumerType
 *  com.day.cq.commons.ImageResource
 *  com.day.cq.commons.inherit.ComponentInheritanceValueMap
 *  com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap
 *  com.day.cq.commons.inherit.InheritanceValueMap
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.commons.util.DamUtil
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  org.apache.commons.codec.DecoderException
 *  org.apache.commons.codec.net.URLCodec
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.adapter.SlingAdaptable
 *  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.common;

import aQute.bnd.annotation.ConsumerType;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.CommerceService;
import com.adobe.cq.commerce.api.Product;
import com.adobe.cq.commerce.api.VariantFilter;
import com.adobe.cq.commerce.api.asset.ProductAssetManager;
import com.day.cq.commons.ImageResource;
import com.day.cq.commons.inherit.ComponentInheritanceValueMap;
import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap;
import com.day.cq.commons.inherit.InheritanceValueMap;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.commons.util.DamUtil;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.net.URLCodec;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.adapter.SlingAdaptable;
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;

@ConsumerType
public abstract class AbstractJcrProduct
extends SlingAdaptable
implements Product {
    private static final String[] IMG_MIME_TYPES = new String[]{"image/png", "image/jpeg", "image/tiff", "image/png", "image/bmp", "image/gif", "image/pipeg", "image/x-portable-anymap", "image/x-portable-bitmap", "image/x-portable-graymap", "image/x-portable-pixmap", "image/x-rgb", "image/x-xbitmap", "image/x-xpixmap", "image/x-icon", "image/photoshop", "image/x-photoshop", "image/psd", "application/photoshop", "application/psd", "image/vnd.adobe.photoshop"};
    private static final List<String> IMG_MIME_TYPE = Arrays.asList(IMG_MIME_TYPES);
    protected static final String PN_PRODUCT_TYPE = "cq:commerceType";
    protected static final String PN_PRODUCT_VARIANT_AXES = "cq:productVariantAxes";
    protected static final String PN_PRODUCT_DATA = "productData";
    protected static final String PN_PRODUCT_TITLE = "jcr:title";
    protected static final String PN_PRODUCT_DESCRIPTION = "jcr:description";
    protected static final String PN_PRODUCT_ASSET_CATEGORY = "assetCategory";
    protected static final Logger log = LoggerFactory.getLogger(AbstractJcrProduct.class);
    protected Resource resource;
    protected Product baseProduct;
    protected ProductAssetManager productAssetManager;

    public AbstractJcrProduct(Resource resource) {
        this.resource = resource;
        this.productAssetManager = (ProductAssetManager)resource.getResourceResolver().adaptTo(ProductAssetManager.class);
    }

    @Override
    public String getPath() {
        return this.resource.getPath();
    }

    @Override
    public String getPagePath() {
        PageManager pm = (PageManager)this.resource.getResourceResolver().adaptTo(PageManager.class);
        Page page = pm.getContainingPage(this.resource);
        return page != null ? page.getPath() + ".html#" + this.getSKU() : null;
    }

    @Override
    public String getTitle() {
        return this.getProperty("jcr:title", String.class);
    }

    @Override
    public String getTitle(String selectorString) {
        return this.getProperty("jcr:title", selectorString, String.class);
    }

    @Override
    public String getDescription() {
        return this.getProperty("jcr:description", String.class);
    }

    @Override
    public String getDescription(String selectorString) {
        return this.getProperty("jcr:description", selectorString, String.class);
    }

    @Deprecated
    @Override
    public String getImagePath() {
        ImageResource image = this.getImage();
        if (image != null) {
            return image.getPath();
        }
        return "";
    }

    @Deprecated
    @Override
    public String getImageUrl() {
        return this.getImagePath();
    }

    @Deprecated
    @Override
    public ImageResource getThumbnail() {
        ImageResource thumbnail = this.getImage();
        if (thumbnail != null) {
            thumbnail.setSelector(".thumbnail");
        }
        return thumbnail;
    }

    @Override
    public String getThumbnailUrl() {
        return this.getThumbnailUrl("");
    }

    @Override
    public String getThumbnailUrl(int width) {
        String selector = Integer.toString(width);
        return this.getThumbnailUrl(selector);
    }

    @Override
    public String getThumbnailUrl(String selectorString) {
        String[] arrstring;
        if (selectorString != null) {
            arrstring = selectorString.split("\\.");
        } else {
            String[] arrstring2 = new String[1];
            arrstring = arrstring2;
            arrstring2[0] = "";
        }
        String[] selectors = arrstring;
        ImageResource assetRes = Arrays.asList(selectors).contains("image") ? this.getImage() : this.getAsset();
        return assetRes == null ? null : this.productAssetManager.getThumbnailUrl(assetRes.getPath(), selectorString);
    }

    @Override
    public ImageResource getImage() {
        Resource image = this.getAsset(IMG_MIME_TYPE);
        if (image != null) {
            return new ImageResource(image);
        }
        return null;
    }

    @Override
    public Resource getAsset() {
        return this.getAsset(null);
    }

    protected Resource getAsset(List<String> mimeTypes) {
        List<Resource> assets = this.getAssets(mimeTypes);
        if (assets.size() == 1) {
            return assets.get(0);
        }
        if (assets.size() > 1) {
            PageManager pm;
            Page page;
            String assetCategoryToDisplay = (String)this.resource.getValueMap().get("assetCategory", String.class);
            if (StringUtils.isEmpty((String)assetCategoryToDisplay) && AbstractJcrProduct.isAVariant(this.resource)) {
                try {
                    this.baseProduct = this.getBaseProduct();
                    if (this.baseProduct != null) {
                        assetCategoryToDisplay = this.baseProduct.getProperty("assetCategory", String.class);
                    }
                }
                catch (CommerceException e) {
                    log.error("Error while retrieving the base product.", (Throwable)e);
                }
            }
            if (StringUtils.isEmpty((String)assetCategoryToDisplay) && (page = (pm = (PageManager)this.resource.getResourceResolver().adaptTo(PageManager.class)).getContainingPage(this.resource)) != null) {
                Resource pageContent = page.getContentResource();
                HierarchyNodeInheritanceValueMap dataMap = new HierarchyNodeInheritanceValueMap(pageContent);
                assetCategoryToDisplay = (String)dataMap.getInherited("assetCategory", String.class);
            }
            if (StringUtils.isEmpty((String)assetCategoryToDisplay)) {
                assetCategoryToDisplay = this.getProperty("assetCategory", String.class);
            }
            for (Resource asset : assets) {
                String category = (String)asset.getValueMap().get("assetCategory", String.class);
                if (!StringUtils.isNotEmpty((String)category) || !category.equals(assetCategoryToDisplay)) continue;
                return asset;
            }
            return assets.get(0);
        }
        return null;
    }

    @Override
    public List<ImageResource> getImages() {
        ArrayList<ImageResource> result = new ArrayList<ImageResource>();
        List<Resource> images = this.getAssets(IMG_MIME_TYPE);
        if (images == null || images.isEmpty()) {
            return result;
        }
        for (Resource assetRes : images) {
            result.add(new ImageResource(assetRes));
        }
        return result;
    }

    @Override
    public List<Resource> getAssets() {
        Resource ancestor;
        List<Resource> assets = this.getAssets(this.resource);
        if (assets.size() > 0) {
            return assets;
        }
        String productDataPath = (String)this.resource.getValueMap().get("productData", String.class);
        if (StringUtils.isNotEmpty((String)productDataPath)) {
            ancestor = this.resource.getResourceResolver().getResource(productDataPath);
            if (ancestor == null) {
                log.warn("Product data not found at [{}].", (Object)productDataPath);
                return Collections.emptyList();
            }
        } else {
            ancestor = this.resource.getParent();
        }
        while (ancestor != null && AbstractJcrProduct.isAProductOrVariant(ancestor)) {
            assets = this.getAssets(ancestor);
            if (assets.size() > 0) {
                return assets;
            }
            ancestor = ancestor.getParent();
        }
        return Collections.emptyList();
    }

    public List<Resource> getAssets(List<String> mimeTypes) {
        List<Resource> assets = this.getAssets();
        return this.filterResources(assets, mimeTypes);
    }

    protected List<Resource> getAssets(Resource resource) {
        Resource assetsResource;
        ArrayList<Resource> result = new ArrayList<Resource>();
        Resource legacyImageResource = resource.getChild("image");
        if (legacyImageResource != null) {
            result.add(legacyImageResource);
        }
        if ((assetsResource = resource.getChild("assets")) != null) {
            Iterator assets = assetsResource.listChildren();
            while (assets.hasNext()) {
                result.add((Resource)assets.next());
            }
        }
        return result;
    }

    protected List<Resource> getAssets(Resource resource, List<String> mimeTypes) {
        List<Resource> assets = this.getAssets(resource);
        return this.filterResources(assets, mimeTypes);
    }

    private List<Resource> filterResources(List<Resource> assetResources, List<String> mimeTypes) {
        ArrayList<Resource> result = new ArrayList<Resource>();
        if (assetResources == null || assetResources.isEmpty()) {
            return result;
        }
        if (mimeTypes == null || mimeTypes.isEmpty()) {
            return assetResources;
        }
        for (Resource assetRes : assetResources) {
            String assetReference = this.productAssetManager.getReferencedAsset(assetRes.getPath());
            Asset asset = this.mapToAsset(assetReference);
            if (asset == null || !mimeTypes.contains(asset.getMimeType())) continue;
            result.add(assetRes);
        }
        return result;
    }

    private Asset mapToAsset(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 = this.resource.getResourceResolver().getResource(assetReference);
        if (assetRes == null) {
            return null;
        }
        return DamUtil.resolveToAsset((Resource)assetRes);
    }

    protected List<ImageResource> getImages(Resource resource) {
        ArrayList<ImageResource> result = new ArrayList<ImageResource>();
        List<Resource> images = this.getAssets(resource, IMG_MIME_TYPE);
        if (images == null || images.isEmpty()) {
            return result;
        }
        for (Resource assetRes : images) {
            result.add(new ImageResource(assetRes));
        }
        return result;
    }

    @Override
    public <T> T getProperty(String name, Class<T> type) {
        Resource ancestor;
        ValueMap properties = this.resource.getValueMap();
        if (properties.containsKey((Object)name)) {
            return (T)properties.get(name, type);
        }
        if (properties.containsKey((Object)"productData")) {
            String productDataPath = (String)properties.get("productData", String.class);
            ancestor = this.resource.getResourceResolver().getResource(productDataPath);
            if (ancestor == null) {
                log.warn("Product data not found at [{}].", (Object)productDataPath);
                return null;
            }
        } else {
            ancestor = this.resource.getParent();
        }
        return (T)new ComponentInheritanceValueMap(ancestor).getInherited(name, type);
    }

    @Override
    public <T> T getProperty(String name, String selectorString, Class<T> type) {
        T selectorSpecificValue;
        if (StringUtils.isNotEmpty((String)selectorString) && (selectorSpecificValue = this.getProperty(name + "." + selectorString, type)) != null) {
            return selectorSpecificValue;
        }
        return this.getProperty(name, type);
    }

    @Override
    public Iterator<Product> getVariants() throws CommerceException {
        return this.getVariants(null);
    }

    @Override
    public boolean axisIsVariant(String axis) {
        Iterator<String> axes = this.getVariantAxes();
        while (axes.hasNext()) {
            if (!axes.next().equals(axis)) continue;
            return true;
        }
        return false;
    }

    @Override
    public Iterator<Product> getVariants(VariantFilter filter) throws CommerceException {
        ArrayList<Product> variants = new ArrayList<Product>();
        this.collectVariants(this.getBaseProduct(), filter, variants);
        return variants.iterator();
    }

    @Override
    public Iterator<String> getVariantAxes() {
        String[] axes = this.getProperty("cq:productVariantAxes", String[].class);
        if (axes != null) {
            return Arrays.asList(axes).iterator();
        }
        return new ArrayList().iterator();
    }

    @Override
    public Product getBaseProduct() throws CommerceException {
        if (this.baseProduct == null) {
            if (!AbstractJcrProduct.isAProductOrVariant(this.resource)) {
                log.error("Node isn't a product: " + this.resource.getPath());
                return null;
            }
            Resource tempResource = this.resource;
            while (AbstractJcrProduct.isAVariant(tempResource)) {
                tempResource = tempResource.getParent();
            }
            if (AbstractJcrProduct.isABaseProduct(tempResource)) {
                CommerceService service = (CommerceService)tempResource.adaptTo(CommerceService.class);
                this.baseProduct = service.getProduct(tempResource.getPath());
            } else {
                log.error("Variant product node [{}] didn't have a product parent.", (Object)this.resource.getPath());
                return null;
            }
        }
        return this.baseProduct;
    }

    @Override
    public Product getPIMProduct() throws CommerceException {
        String productDataPath = (String)this.resource.getValueMap().get("productData", String.class);
        if (StringUtils.isNotEmpty((String)productDataPath)) {
            Resource productData = this.resource.getResourceResolver().getResource(productDataPath);
            if (productData == null) {
                log.warn("Product data not found at [{}].", (Object)productDataPath);
                return null;
            }
            return (Product)productData.adaptTo(Product.class);
        }
        return null;
    }

    public static boolean isAVariant(Resource resource) {
        return ((String)resource.getValueMap().get("cq:commerceType", (Object)"")).equals("variant");
    }

    public static boolean isABaseProduct(Resource resource) {
        return ((String)resource.getValueMap().get("cq:commerceType", (Object)"")).equals("product");
    }

    public static boolean isAProductOrVariant(Resource resource) {
        String commerceType = (String)resource.getValueMap().get("cq:commerceType", (Object)"");
        return commerceType.equals("product") || commerceType.equals("variant");
    }

    protected void collectVariants(Product p, VariantFilter filter, List<Product> variants) {
        boolean isLeaf = true;
        Iterator i = this.resource.getResourceResolver().listChildren((Resource)p.adaptTo(Resource.class));
        while (i != null && i.hasNext()) {
            Resource child = (Resource)i.next();
            if (!AbstractJcrProduct.isAVariant(child)) continue;
            isLeaf = false;
            this.collectVariants((Product)child.adaptTo(Product.class), filter, variants);
        }
        if (isLeaf && (filter == null || filter.includes(p))) {
            variants.add(p);
        }
    }

    public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
        if (type == Resource.class) {
            return (AdapterType)this.resource;
        }
        if (type == InheritanceValueMap.class) {
            return (AdapterType)new ComponentInheritanceValueMap(this.resource);
        }
        Object ret = super.adaptTo(type);
        if (ret == null) {
            ret = this.resource.adaptTo(type);
        }
        return (AdapterType)ret;
    }

    public boolean equals(Object obj) {
        return obj instanceof Product && ((Product)obj).getPath().equals(this.getPath());
    }

    public int hashCode() {
        return this.getPath().hashCode();
    }
}