VariationImpl.java 5.12 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.dam.cfm.ContentFragmentException
 *  com.adobe.cq.dam.cfm.ContentVariation
 *  com.adobe.cq.dam.cfm.SyncStatus
 *  com.adobe.cq.dam.cfm.VariationTemplate
 *  com.adobe.cq.dam.cfm.VersionDef
 *  com.adobe.cq.dam.cfm.VersionedContent
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.api.Rendition
 *  com.day.cq.dam.api.Revision
 */
package com.adobe.cq.dam.cfm.impl;

import com.adobe.cq.dam.cfm.ContentFragmentException;
import com.adobe.cq.dam.cfm.ContentVariation;
import com.adobe.cq.dam.cfm.SyncStatus;
import com.adobe.cq.dam.cfm.VariationTemplate;
import com.adobe.cq.dam.cfm.VersionDef;
import com.adobe.cq.dam.cfm.VersionedContent;
import com.adobe.cq.dam.cfm.impl.CFMUtils;
import com.adobe.cq.dam.cfm.impl.ElementImpl;
import com.adobe.cq.dam.cfm.impl.FragmentWriteException;
import com.adobe.cq.dam.cfm.impl.InvalidFragmentException;
import com.adobe.cq.dam.cfm.impl.VersionDefImpl;
import com.adobe.cq.dam.cfm.impl.VersioningException;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.api.Revision;
import java.util.Calendar;
import java.util.Iterator;

public class VariationImpl
implements ContentVariation {
    private Rendition rendition;
    private final ElementImpl element;
    private final Asset mainAsset;
    private final long lastModified;
    private final String subPath;
    private final VariationTemplate template;

    public VariationImpl(Config cfg) throws InvalidFragmentException {
        this.rendition = cfg.rendition;
        this.element = cfg.element;
        this.mainAsset = this.element.getMainAsset();
        this.subPath = cfg.subPath;
        this.template = cfg.template;
        if (this.template == null && !cfg.allowInvalidTemplate) {
            throw new InvalidFragmentException("Missing template for variation '" + cfg.rendition.getName() + "'.");
        }
        Calendar modifiedCal = CFMUtils.getRenditionLastModified(this.rendition);
        this.lastModified = modifiedCal != null ? modifiedCal.getTimeInMillis() : 0;
    }

    public String getName() {
        return this.template.getName();
    }

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

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

    public String getContentType() {
        return CFMUtils.getContentType(this.rendition);
    }

    public String getContent() {
        return CFMUtils.getContentAsString(this.rendition);
    }

    public void setContent(String content, String contentType) throws FragmentWriteException {
        CFMUtils.setContent(this.rendition, content, contentType);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public Calendar syncContent(String content, String contentType) throws FragmentWriteException {
        Asset asset = this.rendition.getAsset();
        boolean oldBatchMode = asset.isBatchMode();
        asset.setBatchMode(true);
        try {
            Rendition newRendition;
            this.rendition = newRendition = CFMUtils.setContent(this.rendition, content, contentType);
            Calendar calendar = CFMUtils.getRenditionLastModified(newRendition);
            return calendar;
        }
        finally {
            asset.setBatchMode(oldBatchMode);
        }
    }

    public SyncStatus getSyncStatus() {
        return this.element.getSyncStatus(this);
    }

    public void synchronize() throws ContentFragmentException {
        this.element.synchronize(this);
    }

    public VersionDef createVersion(String label, String comment) throws VersioningException {
        VersionDefImpl versionCreated;
        try {
            Revision revision = this.mainAsset.createRevision(label, comment);
            versionCreated = new VersionDefImpl(revision);
        }
        catch (Exception e) {
            throw new VersioningException("Could not create revision of the fragment asset.", e);
        }
        return versionCreated;
    }

    public Iterator<VersionDef> listVersions() throws VersioningException {
        return CFMUtils.listVersions(this.mainAsset, this.lastModified, this.rendition.getName(), this.subPath);
    }

    public VersionedContent getVersionedContent(VersionDef version) throws VersioningException {
        return CFMUtils.getVersionedContent(version, this.mainAsset, this.rendition.getName(), this.subPath);
    }

    public Calendar getModification() {
        return CFMUtils.getRenditionLastModified(this.rendition);
    }

    static class Config {
        public final Rendition rendition;
        public final ElementImpl element;
        public final String subPath;
        public final VariationTemplate template;
        public final boolean allowInvalidTemplate;

        public Config(Rendition rendition, ElementImpl element, String subPath, VariationTemplate template, boolean allowInvalidTemplate) {
            this.rendition = rendition;
            this.element = element;
            this.subPath = subPath;
            this.template = template;
            this.allowInvalidTemplate = allowInvalidTemplate;
        }
    }

}