S7Preset.java 2.75 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.PathNotFoundException
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.tenant.Tenant
 */
package com.day.cq.dam.commons.preset;

import com.day.cq.dam.commons.preset.PresetType;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.tenant.Tenant;

public class S7Preset {
    private static final String DEFAULT_PRESET_ROOT_PATH = "/etc/dam/";
    private static final String TENANTS_BASE_PATH = "tenants/";
    private static final String PRESET_BASE_PATH = "/presets/";
    private static final String JCR_PRESET_ROOT_PATH = "/etc/dam/dynamicmediaconfig";
    private static final String JCR_PRESET_ROOT_PROP = "dynamicMediaPresetRoot";
    private PresetType presetType = null;
    private String tenantId = "";
    private String presetRootPath = "";

    public S7Preset(String presetTypeName, ResourceResolver resourceResolver) {
        Tenant tenant;
        this.presetType = PresetType.getPresetTypeByName(presetTypeName);
        this.presetRootPath = "/etc/dam/";
        if (resourceResolver != null && (tenant = (Tenant)resourceResolver.adaptTo(Tenant.class)) != null) {
            this.tenantId = tenant.getId();
            this.presetRootPath = this.getPresetRootPath(resourceResolver);
        }
    }

    private String getPresetRootPath(ResourceResolver resourceResolver) {
        String presetRootPath = "/etc/dam/";
        try {
            Session session = (Session)resourceResolver.adaptTo(Session.class);
            Node jcrRootPathNode = session.getNode("/etc/dam/dynamicmediaconfig");
            if (jcrRootPathNode != null && jcrRootPathNode.hasProperty("dynamicMediaPresetRoot") && !(presetRootPath = jcrRootPathNode.getProperty("dynamicMediaPresetRoot").getString()).endsWith("/")) {
                presetRootPath = presetRootPath + "/";
            }
        }
        catch (PathNotFoundException pnfe) {
        }
        catch (RepositoryException re) {
            // empty catch block
        }
        return presetRootPath;
    }

    public String getPresetPath() {
        StringBuilder path = new StringBuilder(this.presetRootPath);
        if (!this.tenantId.isEmpty()) {
            path.append("tenants/");
            path.append(this.tenantId);
            path.append("/presets/");
            path.append(this.presetType.getName());
        } else {
            path.append(this.presetType.getDefaultPresetPath());
        }
        return path.toString();
    }
}