AssetDetails.java 8.01 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.LabeledResource
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.api.AssetReferenceResolver
 *  com.day.cq.dam.api.Rendition
 *  com.day.cq.dam.commons.util.DamUtil
 *  com.day.cq.dam.commons.util.UIHelper
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.commons.ReferenceSearch
 *  com.day.cq.wcm.commons.ReferenceSearch$Info
 *  com.day.text.Text
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.RepositoryException
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.json.JSONObject
 */
package com.day.cq.wcm.resource.details;

import com.day.cq.commons.LabeledResource;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.AssetReferenceResolver;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.commons.util.DamUtil;
import com.day.cq.dam.commons.util.UIHelper;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.commons.ReferenceSearch;
import com.day.cq.wcm.resource.details.ResourceDetails;
import com.day.text.Text;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONObject;

public class AssetDetails
implements ResourceDetails {
    final String ASSET_PROPERTY_METADATA = "jcr:content/metadata";
    final String ASSET_PROPERTY_COMMENTS = "jcr:content/comments";
    private Resource resource;
    private Asset asset;
    private Node assetNode;

    public AssetDetails(Resource resource) {
        if (resource == null) {
            throw new IllegalArgumentException("Resource may not be null!");
        }
        this.resource = resource;
        this.assetNode = (Node)resource.adaptTo(Node.class);
        this.asset = (Asset)resource.adaptTo(Asset.class);
    }

    public Node getAssetNode() {
        return this.assetNode;
    }

    public Asset getAsset() {
        return this.asset;
    }

    @Override
    public String getName() throws RepositoryException {
        return this.asset.getName();
    }

    @Override
    public long getLastModified() throws RepositoryException {
        long assetLastModification = this.asset.getLastModified();
        ValueMap vm = (ValueMap)this.resource.adaptTo(ValueMap.class);
        if (assetLastModification == 0) {
            Calendar created = (Calendar)vm.get("jcr:created", Calendar.class);
            assetLastModification = null != created ? created.getTimeInMillis() : 0;
        }
        return assetLastModification;
    }

    public String getMimeType() {
        return this.asset.getMimeType() != null ? this.asset.getMimeType() : null;
    }

    @Override
    public int getReferencesSize(AssetReferenceResolver customResolver) throws RepositoryException {
        int assetReferencesCount = 0;
        Collection resultSet = new ReferenceSearch().search(this.resource.getResourceResolver(), this.assetNode.getPath()).values();
        if (resultSet != null) {
            Iterator it = resultSet.iterator();
            while (it.hasNext()) {
                ReferenceSearch.Info infoItem = (ReferenceSearch.Info)it.next();
                Resource contentRes = infoItem.getPage().getContentResource();
                if (contentRes == null || !contentRes.getResourceType().equals("mac/components/boardpage")) continue;
                it.remove();
            }
            assetReferencesCount = resultSet.size();
        }
        if (customResolver != null) {
            HashMap referencesInfo = new HashMap();
            assetReferencesCount += customResolver.getReferences(this.assetNode.getPath(), this.resource.getResourceResolver()).size();
        }
        return assetReferencesCount;
    }

    @Override
    public int getCommentsSize() throws RepositoryException {
        int commentsCount = 0;
        if (this.assetNode.hasNode("jcr:content/comments")) {
            Node commentsNode = this.assetNode.getNode("jcr:content/comments");
            NodeIterator it = commentsNode.getNodes();
            while (it.hasNext()) {
                ++commentsCount;
                it.next();
            }
        }
        return commentsCount;
    }

    public String getThumbnailUrl() {
        String thumbnailUrl = "";
        Rendition thumbnailRendition = UIHelper.getBestfitRendition((Asset)this.asset, (int)319);
        thumbnailUrl = this.getMimeType() != null && this.getMimeType().startsWith("Multipart/Related") ? this.asset.getPath() + ".folderthumbnail.jpg" : (thumbnailRendition != null ? thumbnailRendition.getPath() : this.asset.getPath() + ".thumb.319.319.png");
        thumbnailUrl = Text.escapePath((String)thumbnailUrl);
        return thumbnailUrl;
    }

    public long getWidth() throws RepositoryException {
        long width = 0;
        if (this.assetNode.hasNode("jcr:content/metadata")) {
            Node metadataNode = this.assetNode.getNode("jcr:content/metadata");
            try {
                width = Long.valueOf(DamUtil.getValue((Node)metadataNode, (String)"tiff:ImageWidth", (String)DamUtil.getValue((Node)metadataNode, (String)"exif:PixelXDimension", (String)"")));
            }
            catch (Exception e) {
                // empty catch block
            }
        }
        return width;
    }

    public long getHeight() throws RepositoryException {
        long height = 0;
        if (this.assetNode.hasNode("jcr:content/metadata")) {
            Node metadataNode = this.assetNode.getNode("jcr:content/metadata");
            try {
                height = Long.valueOf(DamUtil.getValue((Node)metadataNode, (String)"tiff:ImageLength", (String)DamUtil.getValue((Node)metadataNode, (String)"exif:PixelYDimension", (String)"")));
            }
            catch (Exception e) {
                // empty catch block
            }
        }
        return height;
    }

    public String getSize() {
        return this.asset.getOriginal() != null ? UIHelper.getSizeLabel((double)this.asset.getOriginal().getSize()) : "0.0 B";
    }

    public String getResolution() throws RepositoryException {
        long width = this.getWidth();
        long height = this.getHeight();
        return width != 0 && height != 0 ? "" + width + " x " + height : "";
    }

    public String getDescription() throws RepositoryException {
        Asset asset = (Asset)this.resource.adaptTo(Asset.class);
        if (asset != null) {
            Node metadataNode = this.assetNode.getNode("jcr:content/metadata");
            return DamUtil.getValue((Node)metadataNode, (String)"dc:description", (String)this.resource.getName());
        }
        LabeledResource lr = (LabeledResource)this.resource.adaptTo(LabeledResource.class);
        if (lr != null) {
            return lr.getDescription() != null ? lr.getDescription() : "";
        }
        return this.resource.getName();
    }

    public String getParamJSON() throws RepositoryException {
        JSONObject param = new JSONObject();
        if (this.assetNode.hasNode("jcr:content/metadata")) {
            Node metadataNode = this.assetNode.getNode("jcr:content/metadata");
            try {
                String imageMap = DamUtil.getValue((Node)metadataNode, (String)"imageMap", (String)"");
                if (StringUtils.isEmpty((String)imageMap)) {
                    param.put("./imageMap@Delete", (Object)"");
                } else {
                    param.put("./imageMap", (Object)imageMap);
                }
                param.put("./imageCrop@Delete", (Object)"");
                param.put("./imageRotate@Delete", (Object)"");
            }
            catch (Exception e) {
                // empty catch block
            }
        }
        return param.toString();
    }
}