JcrImageSet.java 10.3 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.asset.api.Asset
 *  com.adobe.granite.asset.api.Rendition
 *  com.day.cq.dam.api.Asset
 *  com.scene7.is.provider.ImageSet
 *  com.scene7.is.ps.provider.fvctx.AssetType
 *  com.scene7.is.ps.provider.parsers.ImageSetConverter
 *  com.scene7.is.util.callbacks.Option
 *  com.scene7.is.util.collections.CollectionUtil
 *  javax.jcr.RepositoryException
 *  org.apache.sling.api.adapter.Adaptable
 *  org.apache.sling.api.resource.Resource
 *  org.jetbrains.annotations.NotNull
 */
package com.adobe.cq.dam.s7imaging.impl.catalog;

import com.adobe.cq.dam.s7imaging.impl.catalog.JcrUtil;
import com.adobe.cq.dam.s7imaging.impl.jcr.adapters.GraniteAssetWrapper;
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.Rendition;
import com.day.cq.dam.api.Asset;
import com.scene7.is.provider.ImageSet;
import com.scene7.is.ps.provider.fvctx.AssetType;
import com.scene7.is.ps.provider.parsers.ImageSetConverter;
import com.scene7.is.util.callbacks.Option;
import com.scene7.is.util.collections.CollectionUtil;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.jcr.RepositoryException;
import org.apache.sling.api.adapter.Adaptable;
import org.apache.sling.api.resource.Resource;
import org.jetbrains.annotations.NotNull;

public class JcrImageSet {
    public final Type setType;
    public final AssetType assetType;
    public final String value;
    private static final String S7_SET = "s7Set";
    private static final String DAM_S7_SWATCH = "dam:s7Swatch";
    private static final String MANUAL_THUMBNAIL = "manualThumbnail";
    private static final String ROW_RELATIONS = "rowRelations";
    private static final String METADATA = "metadata";
    private static final PropKey<String> S7Type = PropKey.optional("jcr:content/dam:s7damType", "", StandardExtractors.toString);
    private static final PropKey<String[]> RowRelations = PropKey.optional("jcr:content/rowRelations", new String[0], StandardExtractors.toStringArray);
    private static final PropKey<Option<String>> Thumbnail = PropKey.optional("manualThumbnail", StandardExtractors.toString);
    private static final PropKey<Option<String>> SWATCH = PropKey.optional("dam:s7Swatch", StandardExtractors.toString);

    public static Option<JcrImageSet> jcrImageSet(@NotNull String rootId, @NotNull Resource resource) throws RepositoryException {
        Iterator i$ = JcrImageSet.imageSetType(resource).iterator();
        if (i$.hasNext()) {
            Type type = (Type)((Object)i$.next());
            switch (type) {
                case VIDEO_SET: {
                    return Option.some((Object)JcrImageSet.buildVideoSet(resource));
                }
                case VIDEO_GROUP: {
                    return Option.some((Object)JcrImageSet.buildVideoGroup(resource));
                }
                case IMAGE_SET: 
                case CAROUSEL_SET: {
                    return Option.some((Object)JcrImageSet.buildImageSet(rootId, resource));
                }
                case MIXED_MEDIA_SET: {
                    return Option.some((Object)JcrImageSet.buildMixedMediaSet(rootId, resource));
                }
                case SPIN_SET: {
                    return Option.some((Object)JcrImageSet.buildSpinSet(rootId, resource));
                }
            }
            throw new AssertionError((Object)("Unsupported image set type: " + (Object)((Object)type)));
        }
        return Option.none();
    }

    private static JcrImageSet buildSpinSet(String rootId, Resource resource) throws RepositoryException {
        GraniteAssetWrapper graniteAsset = GraniteAssetWrapper.graniteAssetWrapper(JcrUtil.as(com.adobe.granite.asset.api.Asset.class, (Adaptable)resource));
        ArrayList<String> rowAcc = new ArrayList<String>();
        for (String row : JcrProps.jcrProps(resource).get(RowRelations)) {
            ArrayList<String> acc = new ArrayList<String>();
            for (Relation relation : graniteAsset.relatedRefs(row)) {
                acc.add(JcrImageSet.imageSetId(rootId, relation.path));
            }
            rowAcc.add(CollectionUtil.mkString(acc, (String)"{", (String)",", (String)"}"));
        }
        String value = CollectionUtil.mkString(rowAcc, (String)",");
        return new JcrImageSet(Type.SPIN_SET, value);
    }

    private static JcrImageSet buildVideoSet(Resource resource) {
        ArrayList<String> buffer = new ArrayList<String>();
        for (Rendition r : JcrImageSet.videoRenditions(JcrUtil.as(com.adobe.granite.asset.api.Asset.class, (Adaptable)resource))) {
            buffer.add(JcrImageSet.urlEncode(r.getPath()));
        }
        return new JcrImageSet(Type.VIDEO_SET, CollectionUtil.mkString(buffer, (String)","));
    }

    private static JcrImageSet buildVideoGroup(Resource resource) {
        ArrayList<String> buffer = new ArrayList<String>();
        for (Rendition r : JcrImageSet.videoRenditions(JcrUtil.as(com.adobe.granite.asset.api.Asset.class, (Adaptable)resource))) {
            buffer.add(JcrImageSet.urlEncode(r.getPath()));
        }
        return new JcrImageSet(Type.VIDEO_GROUP, CollectionUtil.mkString(buffer, (String)","));
    }

    private static JcrImageSet buildImageSet(String rootId, Resource resource) throws RepositoryException {
        ArrayList<String> acc = new ArrayList<String>();
        GraniteAssetWrapper graniteAsset = GraniteAssetWrapper.graniteAssetWrapper(JcrUtil.as(com.adobe.granite.asset.api.Asset.class, (Adaptable)resource));
        for (Relation ref : graniteAsset.relatedRefs("s7Set")) {
            String item = JcrImageSet.imageSetId(rootId, ref.path);
            String swatch = ref.props.get(SWATCH, item);
            acc.add(item + ";" + swatch);
        }
        return new JcrImageSet(Type.IMAGE_SET, CollectionUtil.mkString(acc, (String)","));
    }

    private static String imageSetId(String rootId, String path) {
        assert (!rootId.startsWith("/"));
        assert (path.startsWith("/"));
        Pattern sameRoot = Pattern.compile("^/" + rootId + "/(.*)$$");
        Matcher matcher = sameRoot.matcher(path);
        if (matcher.matches()) {
            return matcher.group(1);
        }
        return path;
    }

    private static JcrImageSet buildMixedMediaSet(String rootId, Resource resource) throws RepositoryException {
        ArrayList<String> acc = new ArrayList<String>();
        for (GraniteAssetWrapper asset : GraniteAssetWrapper.graniteAssetWrapper((Adaptable)resource)) {
            for (com.adobe.granite.asset.api.Asset member : asset.related("s7Set")) {
                String path = JcrImageSet.imageSetId(rootId, member.getPath());
                String thumb = (String)JcrImageSet.thumbPath(member).getOrElse((Object)path);
                acc.add(path + ';' + thumb);
            }
        }
        String value = CollectionUtil.mkString(acc, (String)",");
        return new JcrImageSet(Type.MIXED_MEDIA_SET, value);
    }

    private static Option<Type> imageSetType(Resource resource) throws RepositoryException {
        String s7damType;
        Iterator i$;
        Asset damAsset = (Asset)resource.adaptTo(Asset.class);
        if (damAsset != null && (i$ = Type.forS7DamType(s7damType = JcrProps.jcrProps(resource).get(S7Type)).iterator()).hasNext()) {
            Type t = (Type)((Object)i$.next());
            return Option.some((Object)((Object)t));
        }
        return Option.none();
    }

    private static List<Rendition> videoRenditions(com.adobe.granite.asset.api.Asset asset) {
        ArrayList<Rendition> result = new ArrayList<Rendition>();
        Iterator it = asset.listRenditions();
        while (it.hasNext()) {
            Rendition r = (Rendition)it.next();
            String name = r.getName();
            if (!name.startsWith("cqdam.video.dm.") || name.endsWith(".m3u8") || name.endsWith(".f4m")) continue;
            result.add(r);
        }
        return result;
    }

    private static Option<String> thumbPath(com.adobe.granite.asset.api.Asset asset) throws RepositoryException {
        for (Resource n : JcrUtil.getChild((Resource)asset, "jcr:content/metadata")) {
            Iterator i$ = JcrProps.jcrProps(n).get(Thumbnail).iterator();
            if (!i$.hasNext()) continue;
            String thumbnail = (String)i$.next();
            return Option.some((Object)thumbnail);
        }
        return Option.none();
    }

    private static ImageSet toImageSet(List<String> items) {
        String imageSetString = CollectionUtil.mkString(items, (String)",");
        return (ImageSet)ImageSetConverter.imageSetConverter().convert((Object)imageSetString);
    }

    private static String urlEncode(String str) {
        try {
            return URLEncoder.encode(str, "UTF-8");
        }
        catch (UnsupportedEncodingException e) {
            throw new AssertionError(e);
        }
    }

    private JcrImageSet(Type setType, String value) {
        this.setType = setType;
        this.assetType = setType.assetType;
        this.value = value;
    }

    static enum Type {
        VIDEO_SET("VideoAVS", AssetType.VIDEOSET),
        VIDEO_GROUP("Video", AssetType.VIDEOGROUP),
        IMAGE_SET("ImageSet", AssetType.IMAGESET),
        MIXED_MEDIA_SET("MixedMediaSet", AssetType.MEDIASET),
        SPIN_SET("SpinSet", AssetType.SPINSET),
        CAROUSEL_SET("CarouselSet", AssetType.IMAGESET);
        
        private static final Map<String, Type> s7damTypeTable;
        private final String s7damType;
        private final AssetType assetType;

        private static Option<Type> forS7DamType(String s7damType) {
            return Option.some((Object)((Object)s7damTypeTable.get(s7damType)));
        }

        public String toString() {
            return this.s7damType;
        }

        private Type(String name, AssetType assetType) {
            this.s7damType = name;
            this.assetType = assetType;
        }

        static {
            s7damTypeTable = CollectionUtil.lookupTable(Type.class);
        }
    }

}