FragmentTemplateImpl.java 11.3 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.dam.cfm.ContentElement
 *  com.adobe.cq.dam.cfm.ContentVariation
 *  com.adobe.cq.dam.cfm.ElementTemplate
 *  com.adobe.cq.dam.cfm.FragmentTemplate
 *  com.adobe.cq.dam.cfm.MetaDataDefinition
 *  com.adobe.cq.dam.cfm.VariationTemplate
 *  com.day.cq.commons.jcr.JcrUtil
 *  com.day.cq.dam.api.Asset
 *  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.adobe.cq.dam.cfm.impl;

import com.adobe.cq.dam.cfm.ContentElement;
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.MetaDataDefinition;
import com.adobe.cq.dam.cfm.VariationTemplate;
import com.adobe.cq.dam.cfm.impl.ElementTemplateImpl;
import com.adobe.cq.dam.cfm.impl.FragmentWriteException;
import com.adobe.cq.dam.cfm.impl.InvalidTemplateException;
import com.adobe.cq.dam.cfm.impl.MetaDataDefinitionImpl;
import com.adobe.cq.dam.cfm.impl.VariationTemplateImpl;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.dam.api.Asset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
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 FragmentTemplateImpl
implements FragmentTemplate {
    private final Resource resource;
    private final String title;
    private final String description;
    private final List<ElementTemplate> elements;
    private final List<VariationTemplate> variations;
    private final boolean precreateElements;
    private final List<String> initialAssociatedContent;
    private final MetaDataDefinition metaData;

    public FragmentTemplateImpl(Resource resource) throws InvalidTemplateException {
        this.resource = resource;
        ValueMap values = (ValueMap)resource.adaptTo(ValueMap.class);
        Long version = (Long)values.get("version", Long.class);
        if (version == null) {
            throw new InvalidTemplateException("Invalid template format; missing 'version' attribute.");
        }
        if (version > 1) {
            throw new InvalidTemplateException("Unsupported template version: " + version);
        }
        this.title = (String)values.get("jcr:title", (Object)resource.getName());
        this.description = (String)values.get("jcr:description", (Object)"");
        this.elements = new ArrayList<ElementTemplate>(4);
        Resource elements = resource.getChild("elements");
        if (elements == null) {
            throw new InvalidTemplateException("Template missing element definition.");
        }
        Iterator elementDefs = elements.listChildren();
        if (!elementDefs.hasNext()) {
            throw new InvalidTemplateException("Template requires at least one element definition.");
        }
        this.variations = new ArrayList<VariationTemplate>(4);
        Resource variations = resource.getChild("variations");
        if (variations != null) {
            Iterator variationDefs = variations.listChildren();
            while (variationDefs.hasNext()) {
                this.variations.add(new VariationTemplateImpl((Resource)variationDefs.next()));
            }
        }
        this.precreateElements = (Boolean)values.get("precreateElements", (Object)false);
        this.initialAssociatedContent = new ArrayList<String>();
        String[] initialAssociatedContent = (String[])values.get("initialAssociatedContent", String[].class);
        if (initialAssociatedContent != null) {
            Collections.addAll(this.initialAssociatedContent, initialAssociatedContent);
        }
        while (elementDefs.hasNext()) {
            this.elements.add(new ElementTemplateImpl(this, (Resource)elementDefs.next()));
        }
        this.metaData = new MetaDataDefinitionImpl(resource.getChild("metadata"));
    }

    public String getTitle() {
        return this.title;
    }

    public String getDescription() {
        return this.description;
    }

    public Iterator<ElementTemplate> getElements() {
        return this.elements.iterator();
    }

    public ElementTemplate getForElement(ContentElement element) {
        String elementName = element.getName();
        for (ElementTemplate toCheck : this.elements) {
            if (!elementName.equals(toCheck.getName())) continue;
            return toCheck;
        }
        return null;
    }

    public ElementTemplateImpl getMasterElement() {
        if (this.elements.size() < 1) {
            throw new IllegalStateException("No element templates available.");
        }
        return (ElementTemplateImpl)this.elements.get(0);
    }

    public Iterator<VariationTemplate> getVariations() {
        return this.variations.iterator();
    }

    public VariationTemplate getForVariation(ContentVariation variation) {
        String variationName = variation.getName();
        for (VariationTemplate toCheck : this.variations) {
            if (!variationName.equals(toCheck.getName())) continue;
            return toCheck;
        }
        return null;
    }

    public VariationTemplate getVariation(String variationName) {
        for (VariationTemplate toCheck : this.variations) {
            if (!toCheck.getName().equals(variationName)) continue;
            return toCheck;
        }
        return null;
    }

    public Iterator<String> getInitialAssociatedContent() {
        return this.initialAssociatedContent.iterator();
    }

    public MetaDataDefinition getMetaDataDefinition() {
        return this.metaData;
    }

    private void copy(Resource src, Resource dest) throws PersistenceException {
        ResourceResolver resolver = dest.getResourceResolver();
        ValueMap srcProps = (ValueMap)src.adaptTo(ValueMap.class);
        ModifiableValueMap destProps = (ModifiableValueMap)dest.adaptTo(ModifiableValueMap.class);
        Set keys = srcProps.keySet();
        for (String key : keys) {
            if ("jcr:primaryType".equals(key)) continue;
            destProps.put((Object)key, srcProps.get((Object)key));
        }
        Iterator children = src.listChildren();
        while (children.hasNext()) {
            Resource child = (Resource)children.next();
            HashMap<String, Object> primaryTypeDef = new HashMap<String, Object>(4);
            primaryTypeDef.put("jcr:primaryType", ((ValueMap)child.adaptTo(ValueMap.class)).get((Object)"jcr:primaryType"));
            Resource childCopy = resolver.create(dest, child.getName(), primaryTypeDef);
            this.copy(child, childCopy);
        }
    }

    protected void copyTo(Resource target) throws FragmentWriteException {
        ResourceResolver resolver = this.resource.getResourceResolver();
        Resource modelResource = target.getChild("model");
        try {
            if (modelResource != null) {
                Iterator children = modelResource.listChildren();
                while (children.hasNext()) {
                    resolver.delete((Resource)children.next());
                }
                ModifiableValueMap properties = (ModifiableValueMap)modelResource.adaptTo(ModifiableValueMap.class);
                properties.clear();
            } else {
                HashMap<String, String> primaryTypeDef = new HashMap<String, String>(4);
                primaryTypeDef.put("jcr:primaryType", "nt:unstructured");
                modelResource = resolver.create(target, "model", primaryTypeDef);
            }
            this.copy(this.resource, modelResource);
        }
        catch (PersistenceException pe) {
            throw new FragmentWriteException("Could not copy template to '" + target.getPath() + "'.", (Throwable)pe);
        }
    }

    public VariationTemplate createModelFor(String name, String title, String description) throws FragmentWriteException {
        VariationTemplateImpl theModel;
        name = name == null ? JcrUtil.createValidName((String)title) : name;
        ResourceResolver resolver = this.resource.getResourceResolver();
        try {
            Resource varsRsc = this.resource.getChild("variations");
            if (varsRsc == null) {
                HashMap<String, String> varsProps = new HashMap<String, String>();
                varsProps.put("jcr:primaryType", "nt:unstructured");
                varsRsc = resolver.create(this.resource, "variations", varsProps);
            }
            HashMap<String, String> tplProps = new HashMap<String, String>();
            tplProps.put("jcr:primaryType", "nt:unstructured");
            tplProps.put("name", name);
            tplProps.put("jcr:title", title);
            tplProps.put("jcr:description", description);
            Resource varTplRsc = resolver.create(varsRsc, name, tplProps);
            theModel = new VariationTemplateImpl(varTplRsc);
        }
        catch (PersistenceException pe) {
            throw new FragmentWriteException("Could not create variant template '" + name + "'.", (Throwable)pe);
        }
        catch (InvalidTemplateException ite) {
            throw new FragmentWriteException("Could not create variant template '" + name + "'.", (Throwable)((Object)ite));
        }
        return theModel;
    }

    public void removeModelFor(String variationName) throws FragmentWriteException {
        Resource varsRsc = this.resource.getChild("variations");
        if (varsRsc == null) {
            throw new FragmentWriteException("No definition for variations found.");
        }
        Iterator variations = varsRsc.listChildren();
        Resource variationToRemove = null;
        while (variations.hasNext() && variationToRemove == null) {
            Resource variationToCheck = (Resource)variations.next();
            ValueMap props = variationToCheck.getValueMap();
            String name = (String)props.get("name", String.class);
            if (!variationName.equals(name)) continue;
            variationToRemove = variationToCheck;
        }
        if (variationToRemove == null) {
            throw new FragmentWriteException("Invalid variation: " + variationName);
        }
        ResourceResolver resolver = variationToRemove.getResourceResolver();
        try {
            resolver.delete(variationToRemove);
        }
        catch (PersistenceException pe) {
            throw new FragmentWriteException("Could not delete '" + variationToRemove.getPath() + "'.");
        }
    }

    public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
        if (type == Resource.class) {
            return (AdapterType)this.resource;
        }
        return null;
    }

    public boolean precreateElements() {
        return this.precreateElements;
    }

    public ElementTemplateImpl getForAsset(Asset asset) {
        String assetName = asset.getName();
        for (ElementTemplate toCheck : this.elements) {
            ElementTemplateImpl tplToCheck = (ElementTemplateImpl)toCheck;
            if (!assetName.equals(tplToCheck.getAssetName())) continue;
            return tplToCheck;
        }
        return null;
    }
}