ProductReferenceProvider.java 2.34 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.reference.Reference
 *  com.day.cq.wcm.api.reference.ReferenceProvider
 *  javax.jcr.Node
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 */
package com.adobe.cq.commerce.impl;

import com.adobe.cq.commerce.common.AbstractJcrProduct;
import com.day.cq.wcm.api.reference.Reference;
import com.day.cq.wcm.api.reference.ReferenceProvider;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import javax.jcr.Node;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;

@Component
@Service(value={ReferenceProvider.class})
public class ProductReferenceProvider
implements ReferenceProvider {
    public List<Reference> findReferences(Resource resource) {
        return this.recurse(resource, new ArrayList<Reference>());
    }

    private List<Reference> recurse(Resource resource, ArrayList<Reference> references) {
        Node node = (Node)resource.adaptTo(Node.class);
        if (node == null) {
            return references;
        }
        if (AbstractJcrProduct.isAProductOrVariant(resource)) {
            Resource pimProduct;
            String productDataPath = (String)ResourceUtil.getValueMap((Resource)resource).get("productData", String.class);
            Resource resource2 = pimProduct = productDataPath != null ? resource.getResourceResolver().getResource(productDataPath) : null;
            if (pimProduct != null) {
                Calendar mod = (Calendar)ResourceUtil.getValueMap((Resource)pimProduct).get("jcr:lastModified", Calendar.class);
                long lastModified = mod != null ? mod.getTimeInMillis() : -1;
                references.add(new Reference("product", pimProduct.getName(), pimProduct, lastModified));
            }
        }
        Iterator it = resource.listChildren();
        while (it.hasNext()) {
            this.recurse((Resource)it.next(), references);
        }
        return references;
    }
}