FragmentManagerImpl.java 10.3 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.dam.cfm.ContentElement
 *  com.adobe.cq.dam.cfm.ContentFragment
 *  com.adobe.cq.dam.cfm.ContentFragmentException
 *  com.adobe.cq.dam.cfm.ContentFragmentManager
 *  com.adobe.cq.dam.cfm.ContentVariation
 *  com.adobe.cq.dam.cfm.ElementTemplate
 *  com.adobe.cq.dam.cfm.FragmentTemplate
 *  com.adobe.cq.dam.cfm.VariationTemplate
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.api.AssetManager
 *  org.apache.commons.io.IOUtils
 *  org.apache.commons.io.input.ReaderInputStream
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  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
 *  org.apache.sling.resource.collection.ResourceCollection
 *  org.apache.sling.resource.collection.ResourceCollectionManager
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.dam.cfm.impl;

import com.adobe.cq.dam.cfm.ContentElement;
import com.adobe.cq.dam.cfm.ContentFragment;
import com.adobe.cq.dam.cfm.ContentFragmentException;
import com.adobe.cq.dam.cfm.ContentFragmentManager;
import com.adobe.cq.dam.cfm.ContentVariation;
import com.adobe.cq.dam.cfm.ElementTemplate;
import com.adobe.cq.dam.cfm.FragmentTemplate;
import com.adobe.cq.dam.cfm.VariationTemplate;
import com.adobe.cq.dam.cfm.impl.CFMUtils;
import com.adobe.cq.dam.cfm.impl.ElementTemplateImpl;
import com.adobe.cq.dam.cfm.impl.FragmentImpl;
import com.adobe.cq.dam.cfm.impl.FragmentTemplateImpl;
import com.adobe.cq.dam.cfm.impl.FragmentWriteException;
import com.adobe.cq.dam.cfm.impl.InvalidFragmentException;
import com.adobe.cq.dam.cfm.impl.InvalidTemplateException;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.AssetManager;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReaderInputStream;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
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;
import org.apache.sling.resource.collection.ResourceCollection;
import org.apache.sling.resource.collection.ResourceCollectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
@Component(metatype=0)
public class FragmentManagerImpl
implements ContentFragmentManager {
    private final Logger log;
    @Reference
    private ResourceCollectionManager collectionManager;

    public FragmentManagerImpl() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public ContentFragment create(Resource parent, Resource template, String name, String title) throws ContentFragmentException {
        FragmentImpl fragment;
        ResourceResolver resolver = parent.getResourceResolver();
        Asset asset = null;
        boolean oldBatchMode = false;
        try {
            FragmentTemplateImpl fragmentTemplate;
            ResourceCollection associatedContent;
            try {
                fragmentTemplate = new FragmentTemplateImpl(template);
            }
            catch (InvalidTemplateException ite) {
                throw new FragmentWriteException("Provided template is invalid.", (Throwable)((Object)ite));
            }
            AssetManager assetManager = (AssetManager)resolver.adaptTo(AssetManager.class);
            if (assetManager == null) {
                throw new FragmentWriteException("No asset manager available.");
            }
            String assetPath = null;
            int suffix = 0;
            while (assetPath == null) {
                String toCheck = parent.getPath() + "/" + name + (suffix > 0 ? new StringBuilder().append("-").append(Integer.toString(suffix)).toString() : "");
                ++suffix;
                if (resolver.getResource(toCheck) != null) continue;
                assetPath = toCheck;
            }
            ElementTemplateImpl masterTpl = fragmentTemplate.getMasterElement();
            String content = masterTpl.getDefaultContent();
            String contentType = masterTpl.getInitialContentType();
            ReaderInputStream is = new ReaderInputStream((Reader)new StringReader(content));
            try {
                asset = assetManager.createAsset(assetPath, (InputStream)is, contentType, false);
            }
            finally {
                IOUtils.closeQuietly((InputStream)is);
            }
            if (asset == null) {
                throw new FragmentWriteException("Could not create underlying asset.");
            }
            oldBatchMode = asset.isBatchMode();
            asset.setBatchMode(true);
            Resource assetResource = (Resource)asset.adaptTo(Resource.class);
            ModifiableValueMap contentProps = (ModifiableValueMap)assetResource.getChild("jcr:content").adaptTo(ModifiableValueMap.class);
            contentProps.put((Object)"contentFragment", (Object)true);
            contentProps.put((Object)"jcr:title", (Object)title);
            try {
                associatedContent = CFMUtils.createEmptyAssocContent(assetResource, this.collectionManager);
                Iterator<String> initialAssocContent = fragmentTemplate.getInitialAssociatedContent();
                while (initialAssocContent.hasNext()) {
                    String path = initialAssocContent.next();
                    Resource contentRsc = resolver.getResource(path);
                    associatedContent.add(contentRsc);
                }
                fragmentTemplate.copyTo(assetResource);
            }
            catch (PersistenceException pe) {
                throw new FragmentWriteException("Could not add collection to fragment.", (Throwable)pe);
            }
            try {
                fragment = new FragmentImpl(asset, associatedContent, true);
                fragmentTemplate = (FragmentTemplateImpl)fragment.getTemplate();
                masterTpl = fragmentTemplate.getMasterElement();
                masterTpl.linkToAsset(asset, true);
                ContentElement master = fragment.getMasterElement();
                Iterator<VariationTemplate> variations = fragmentTemplate.getVariations();
                while (variations.hasNext()) {
                    VariationTemplate variation = variations.next();
                    master.createVariation(variation);
                }
            }
            catch (InvalidFragmentException ife) {
                throw new FragmentWriteException("Could not read the newly created fragment.", (Throwable)((Object)ife));
            }
            try {
                Iterator<ElementTemplate> elements;
                fragment = new FragmentImpl(asset, associatedContent, false);
                fragmentTemplate = (FragmentTemplateImpl)fragment.getTemplate();
                if (fragmentTemplate.precreateElements() && (elements = fragmentTemplate.getElements()).hasNext()) {
                    elements.next();
                    while (elements.hasNext()) {
                        fragment.createElement(elements.next());
                    }
                }
            }
            catch (InvalidFragmentException ife) {
                throw new FragmentWriteException("Could not read the newly created fragment.", (Throwable)((Object)ife));
            }
        }
        finally {
            if (asset != null) {
                asset.setBatchMode(oldBatchMode);
            }
        }
        return fragment;
    }

    public Map<Resource, List<Resource>> resolveAssociatedContent(ContentFragment fragment) {
        HashMap<Resource, List<Resource>> resolvedAssociatedContent = new HashMap<Resource, List<Resource>>();
        Iterator associatedContent = fragment.getAssociatedContent();
        while (associatedContent.hasNext()) {
            String nodeType;
            ValueMap contentProps;
            Resource contentResource = (Resource)associatedContent.next();
            ArrayList resourcesPerColl = new ArrayList(2);
            resolvedAssociatedContent.put(contentResource, resourcesPerColl);
            if (contentResource.isResourceType("sling/collection")) {
                ResourceCollection coll = this.collectionManager.getCollection(contentResource);
                if (coll == null) continue;
                Iterator collResources = coll.getResources();
                while (collResources.hasNext()) {
                    resourcesPerColl.add(collResources.next());
                }
                continue;
            }
            if (contentResource.getName().equals("jcr:content")) {
                contentResource = contentResource.getParent();
            }
            if (!"dam:Asset".equals(nodeType = (String)(contentProps = (ValueMap)contentResource.adaptTo(ValueMap.class)).get("jcr:primaryType", String.class))) continue;
            resourcesPerColl.add((Resource)contentResource);
        }
        return resolvedAssociatedContent;
    }

    public List<Resource> resolveAssociatedContentFlat(ContentFragment fragment) {
        ArrayList<Resource> resolvedContentFlat = new ArrayList<Resource>();
        Map<Resource, List<Resource>> resolvedContent = this.resolveAssociatedContent(fragment);
        for (Resource key : resolvedContent.keySet()) {
            resolvedContentFlat.addAll((Collection)resolvedContent.get((Object)key));
        }
        return resolvedContentFlat;
    }

    protected void bindCollectionManager(ResourceCollectionManager resourceCollectionManager) {
        this.collectionManager = resourceCollectionManager;
    }

    protected void unbindCollectionManager(ResourceCollectionManager resourceCollectionManager) {
        if (this.collectionManager == resourceCollectionManager) {
            this.collectionManager = null;
        }
    }
}