AssetIDProvider.java 5.26 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  aQute.bnd.annotation.ProviderType
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.commons.util.DamUtil
 *  javax.jcr.Node
 *  javax.jcr.PathNotFoundException
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Workspace
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.dam.core.impl;

import aQute.bnd.annotation.ProviderType;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.commons.util.DamUtil;
import java.util.Calendar;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import org.apache.commons.lang.StringUtils;
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.Resource;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(immediate=1)
@Service(value={AssetIDProvider.class})
@ProviderType
public class AssetIDProvider {
    private static final Logger log = LoggerFactory.getLogger(AssetIDProvider.class);
    private static final String ASSET_ID_HELPER_SUBSERVICE = "assetidhelper";
    private static final String DAM_COPY_TIMESTAMP = "dam:copiedAt";
    @Reference
    private SlingRepository repository;

    public String getAssetID(Asset asset) throws PathNotFoundException, RepositoryException {
        try {
            return ((Node)asset.adaptTo(Node.class)).getProperty("jcr:uuid").getString();
        }
        catch (PathNotFoundException e) {
            log.error("Error while getting Asset ID for asset at {}", (Object)asset.getPath(), (Object)e);
            throw e;
        }
        catch (RepositoryException e) {
            log.error("Error while getting Asset ID for asset at {}", (Object)asset.getPath(), (Object)e);
            throw e;
        }
    }

    public void establishParentage(Asset asset) {
        String relPathFromJcrContent = "";
        String assetsRoot = DamUtil.getTenantAssetsRoot((Resource)((Resource)asset.adaptTo(Resource.class)));
        try {
            Node assetNode = (Node)asset.adaptTo(Node.class);
            Session userSession = assetNode.getSession();
            Node assetJcrContent = null;
            assetJcrContent = assetNode.getNode("jcr:content");
            try {
                relPathFromJcrContent = assetJcrContent.getProperty("dam:relativePath").getString();
            }
            catch (PathNotFoundException e) {
                relPathFromJcrContent = "";
            }
            String assetRelPath = DamUtil.findRelativePathOfAssetNode((Node)assetNode, (String)assetsRoot);
            if (!StringUtils.equals((String)assetRelPath, (String)relPathFromJcrContent)) {
                String parentAssetID = this.findParentAsset(userSession, assetsRoot, relPathFromJcrContent);
                this.updateAssetsRelativePath(userSession, assetJcrContent, assetRelPath, parentAssetID);
            }
        }
        catch (Exception e) {
            log.warn("Couldn't establish parentage for asset at {}", (Object)asset.getPath());
        }
    }

    private String findParentAsset(Session userSession, String assetsRoot, String assetRelPathFromJcrContent) throws RepositoryException {
        String assetPathFromJcrContent = assetsRoot + "/" + assetRelPathFromJcrContent;
        Node parentAssetNode = null;
        try {
            parentAssetNode = userSession.getNode(assetPathFromJcrContent);
            return parentAssetNode.getProperty("jcr:uuid").getString();
        }
        catch (PathNotFoundException ign) {
            return "";
        }
    }

    private void updateAssetsRelativePath(Session userSession, Node assetJcrContent, String assetRelPath, String parentAssetID) {
        try {
            Session serviceSession = this.repository.loginService("assetidhelper", userSession.getWorkspace().getName());
            Node jcrContentForWrite = serviceSession.getNode(assetJcrContent.getPath());
            jcrContentForWrite.setProperty("dam:relativePath", assetRelPath);
            if (!parentAssetID.isEmpty()) {
                jcrContentForWrite.setProperty("dam:parentAssetID", parentAssetID);
                jcrContentForWrite.setProperty("dam:copiedAt", Calendar.getInstance());
            }
            serviceSession.save();
            serviceSession.logout();
            userSession.refresh(true);
        }
        catch (RepositoryException unauthorizedException) {
            log.error("Session doesn't have appropriate privileges", (Throwable)unauthorizedException);
        }
    }

    protected void bindRepository(SlingRepository slingRepository) {
        this.repository = slingRepository;
    }

    protected void unbindRepository(SlingRepository slingRepository) {
        if (this.repository == slingRepository) {
            this.repository = null;
        }
    }
}