S7SetHelper.java 5.11 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.asset.api.Asset
 *  com.adobe.granite.asset.api.AssetManager
 *  com.day.cq.dam.api.s7dam.set.ImageSet
 *  com.day.cq.dam.api.s7dam.set.MediaSet
 *  com.day.cq.dam.api.s7dam.set.SpinSet
 *  com.day.cq.dam.api.s7dam.set.SwatchSet
 *  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.apache.sling.api.resource.ValueMap
 */
package com.day.cq.dam.commons.util;

import com.adobe.granite.asset.api.Asset;
import com.adobe.granite.asset.api.AssetManager;
import com.day.cq.dam.api.s7dam.set.ImageSet;
import com.day.cq.dam.api.s7dam.set.MediaSet;
import com.day.cq.dam.api.s7dam.set.SpinSet;
import com.day.cq.dam.api.s7dam.set.SwatchSet;
import java.util.Map;
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.apache.sling.api.resource.ValueMap;

public class S7SetHelper {
    private static String MIME_TYPE = "Multipart/Related; type=application/x-";

    public static ImageSet createS7ImageSet(Resource parent, String name, Map<String, Object> props) throws PersistenceException {
        Resource resource = S7SetHelper.createS7Set(parent, name, props, "ImageSet");
        return (ImageSet)resource.adaptTo(ImageSet.class);
    }

    private static Resource createS7Set(Resource parent, String name, Map<String, Object> props, String type) throws PersistenceException {
        return S7SetHelper.createS7Set(parent, name, props, type, null);
    }

    private static Resource createS7Set(Resource parent, String name, Map<String, Object> props, String type, Map<String, Object> metadata) throws PersistenceException {
        if (parent != null && name != null) {
            ResourceResolver resolver = parent.getResourceResolver();
            String path = parent.getPath() + "/" + name;
            AssetManager am = (AssetManager)resolver.adaptTo(AssetManager.class);
            Asset asset = am.createAsset(path);
            ValueMap vm = (ValueMap)asset.adaptTo(ValueMap.class);
            vm.put((Object)"dam:s7damType", (Object)type);
            if (props != null) {
                vm.putAll(props);
            }
            Resource metadataResource = resolver.getResource((Resource)asset, "jcr:content/metadata");
            ModifiableValueMap metadataVm = (ModifiableValueMap)metadataResource.adaptTo(ModifiableValueMap.class);
            metadataVm.put((Object)"dc:format", (Object)(MIME_TYPE + type));
            if (metadata != null) {
                metadataVm.putAll(metadata);
            }
            resolver.commit();
            return resolver.getResource(path);
        }
        throw new IllegalArgumentException("parent resource or name can not be null");
    }

    public static SwatchSet createS7SwatchSet(Resource parent, String name, Map<String, Object> props) throws PersistenceException {
        Resource resource = S7SetHelper.createS7Set(parent, name, props, "SwatchSet");
        return (SwatchSet)resource.adaptTo(SwatchSet.class);
    }

    public static MediaSet createS7VideoSet(Resource parent, String name, Map<String, Object> props) throws PersistenceException {
        Resource resource = S7SetHelper.createS7Set(parent, name, props, "VideoSet");
        return (MediaSet)resource.adaptTo(MediaSet.class);
    }

    public static MediaSet createS7MixedMediaSet(Resource parent, String name, Map<String, Object> props) throws PersistenceException {
        Resource resource = S7SetHelper.createS7Set(parent, name, props, "MixedMediaSet");
        return (MediaSet)resource.adaptTo(MediaSet.class);
    }

    public static SpinSet createS7SpinSet(Resource parent, String name, Map<String, Object> props) throws PersistenceException {
        Resource resource = S7SetHelper.createS7Set(parent, name, props, "SpinSet");
        return (SpinSet)resource.adaptTo(SpinSet.class);
    }

    public static boolean isS7Set(Resource resource) {
        String s7Type;
        ValueMap vm;
        Resource contentResource = resource.getChild("jcr:content");
        if (contentResource != null && ("ImageSet".equals(s7Type = (String)(vm = (ValueMap)contentResource.adaptTo(ValueMap.class)).get("dam:s7damType", (Object)"")) || "SwatchSet".equals(s7Type) || "VideoSet".equals(s7Type) || "MixedMediaSet".equals(s7Type) || "SpinSet".equals(s7Type) || "CarouselSet".equals(s7Type) || "ECatalog".equals(s7Type))) {
            return true;
        }
        return false;
    }

    public static boolean isS7Video(Resource resource) {
        Resource contentResource = resource.getChild("jcr:content");
        if (contentResource != null) {
            ValueMap vm = (ValueMap)contentResource.adaptTo(ValueMap.class);
            String s7Type = (String)vm.get("dam:s7damType", (Object)"");
            boolean isVideo = "Video".equals(s7Type) || "VideoAVS".equals(s7Type);
            return isVideo;
        }
        return false;
    }
}