AssetLanguageCopyReferenceProvider.java 3.94 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.references.Reference
 *  com.adobe.granite.references.ReferenceProvider
 *  com.day.cq.commons.LanguageUtil
 *  com.day.text.Text
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.dam.core.impl;

import com.adobe.granite.references.Reference;
import com.adobe.granite.references.ReferenceProvider;
import com.day.cq.commons.LanguageUtil;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service(value={ReferenceProvider.class})
public class AssetLanguageCopyReferenceProvider
implements ReferenceProvider {
    public static final String REFERENCE_TYPE = "assetLanguageCopy";
    private static final Logger log = LoggerFactory.getLogger(AssetLanguageCopyReferenceProvider.class);

    public List<Reference> getReferences(Resource resource) {
        ArrayList<Reference> references = new ArrayList<Reference>();
        ResourceResolver resolver = resource.getResourceResolver();
        Session session = (Session)resolver.adaptTo(Session.class);
        Collection<Resource> languageRoots = this.getLanguageRoots(resolver, resource.getPath());
        String path = resource.getPath();
        String langRoot = LanguageUtil.getLanguageRoot((String)path);
        String contentPath = "";
        for (Resource languageRoot2 : languageRoots) {
            String languageRootPath = languageRoot2.getPath();
            if (!path.startsWith(languageRootPath) || langRoot != null && !langRoot.equals(LanguageUtil.getLanguageRoot((String)languageRootPath))) continue;
            contentPath = path.replaceFirst(languageRootPath, "");
            break;
        }
        try {
            for (Resource languageRoot2 : languageRoots) {
                String targetPath = languageRoot2.getPath() + contentPath;
                if (!session.nodeExists(targetPath)) continue;
                references.add(new Reference(resource, resolver.getResource(targetPath), "assetLanguageCopy"));
            }
        }
        catch (RepositoryException e) {
            log.error("unable to get all language copy references for resource: " + path + ". Cause: {}", (Object)e.getMessage(), (Object)e);
        }
        return references;
    }

    private Collection<Resource> getLanguageRoots(ResourceResolver resolver, String path) {
        String parent = null;
        String root = LanguageUtil.getLanguageRoot((String)path);
        if (root == null) {
            return new LinkedList<Resource>();
        }
        parent = Text.getRelativeParent((String)root, (int)1);
        Resource parentResource = resolver.getResource(parent);
        if (parentResource == null) {
            return Collections.emptySet();
        }
        LinkedList<Resource> assets = new LinkedList<Resource>();
        Iterator iter = resolver.listChildren(parentResource);
        while (iter.hasNext()) {
            Resource r = (Resource)iter.next();
            String name = Text.getName((String)r.getPath());
            Locale locale = LanguageUtil.getLocale((String)name);
            if (locale == null) continue;
            assets.add(r);
        }
        return assets;
    }

    public String getType() {
        return "assetLanguageCopy";
    }
}