GraniteAssetWrapper.java 3.47 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.asset.api.Asset
 *  com.scene7.is.util.callbacks.Option
 *  javax.jcr.RepositoryException
 *  org.apache.sling.api.adapter.Adaptable
 *  org.apache.sling.api.resource.Resource
 */
package com.adobe.cq.dam.s7imaging.impl.jcr.adapters;

import com.adobe.cq.dam.s7imaging.impl.catalog.JcrUtil;
import com.adobe.cq.dam.s7imaging.impl.jcr.adapters.Relation;
import com.adobe.cq.dam.s7imaging.impl.jcr.props.JcrProps;
import com.adobe.cq.dam.s7imaging.impl.jcr.props.PropExtractor;
import com.adobe.cq.dam.s7imaging.impl.jcr.props.PropKey;
import com.adobe.cq.dam.s7imaging.impl.jcr.props.StandardExtractors;
import com.adobe.granite.asset.api.Asset;
import com.scene7.is.util.callbacks.Option;
import java.util.ArrayList;
import java.util.Iterator;
import javax.jcr.RepositoryException;
import org.apache.sling.api.adapter.Adaptable;
import org.apache.sling.api.resource.Resource;

public class GraniteAssetWrapper {
    private static final String SLING_COLLECTION = "sling/collection";
    private static final String SLING_MEMBERS = "sling:members";
    private static final String RELATIONS_PATH = "jcr:content/related";
    private static final String SLING_RESOURCES = "sling:resources";
    private static final PropKey<Option<String>> SlingResourceType = PropKey.optional("sling:resourceType", StandardExtractors.toString);
    private static final PropKey<String[]> References = PropKey.optional("sling:resources", new String[0], StandardExtractors.toStringArray);
    private final Asset asset;

    public static Option<GraniteAssetWrapper> graniteAssetWrapper(Adaptable asset) {
        Iterator i$ = JcrUtil.adaptTo(Asset.class, asset).iterator();
        if (i$.hasNext()) {
            Asset a = (Asset)i$.next();
            return Option.some((Object)GraniteAssetWrapper.graniteAssetWrapper(a));
        }
        return Option.none();
    }

    public static GraniteAssetWrapper graniteAssetWrapper(Asset asset) {
        return new GraniteAssetWrapper(asset);
    }

    public Iterable<Asset> related(final String name) {
        return new Iterable<Asset>(){

            @Override
            public Iterator<Asset> iterator() {
                return GraniteAssetWrapper.this.asset.listRelated(name);
            }
        };
    }

    public Iterable<Relation> relatedRefs(String name) throws RepositoryException {
        ArrayList<Relation> refs = new ArrayList<Relation>();
        for (Resource relations : JcrUtil.getChild((Resource)this.asset, "jcr:content/related/" + name)) {
            JcrProps props = JcrProps.jcrProps(relations);
            if (Option.some((Object)"sling/collection").equals(props.get(SlingResourceType))) {
                for (Resource members : JcrUtil.getChild(relations, "sling:members")) {
                    reference i$ = (reference)members.getChildren().iterator();
                    while (i$.hasNext()) {
                        Resource child = (Resource)i$.next();
                        for (Relation relation : Relation.relation((Adaptable)child)) {
                            refs.add(relation);
                        }
                    }
                }
                continue;
            }
            for (String ref : JcrProps.jcrProps((Resource)this.asset).get(References)) {
                refs.add(Relation.relation(ref));
            }
        }
        return refs;
    }

    private GraniteAssetWrapper(Asset asset) {
        this.asset = asset;
    }

}