ProjectUtils.java 8.43 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.projects.api.Project
 *  com.adobe.cq.projects.api.ProjectException
 *  com.adobe.granite.asset.api.Asset
 *  com.adobe.granite.asset.api.AssetManager
 *  com.adobe.granite.asset.api.Rendition
 *  com.day.cq.commons.jcr.JcrUtil
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.jackrabbit.commons.JcrUtils
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.projects.impl.util;

import com.adobe.cq.projects.api.Project;
import com.adobe.cq.projects.api.ProjectException;
import com.adobe.granite.asset.api.Asset;
import com.adobe.granite.asset.api.AssetManager;
import com.adobe.granite.asset.api.Rendition;
import com.day.cq.commons.jcr.JcrUtil;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class ProjectUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectUtils.class);
    private static final String PATH_NODENAME_DASHBOARD = "dashboard";
    private static final String PATH_NODENAME_GADGETS = "gadgets";

    private ProjectUtils() {
    }

    public static Asset createProjectAsset(Project project, String mimeType, InputStream thumbnailStream, ResourceResolver resourceResolver, String coverAssetName) throws PersistenceException {
        AssetManager assetManager = (AssetManager)resourceResolver.adaptTo(AssetManager.class);
        return ProjectUtils.createOrReplaceAsset(project, assetManager, coverAssetName, mimeType, thumbnailStream, resourceResolver);
    }

    public static Asset createOrReplaceAsset(Project project, AssetManager assetManager, String name, String mimeType, InputStream source, ResourceResolver resourceResolver) {
        Asset asset;
        Resource targetFolder = project.getAssetFolder();
        String targetPath = targetFolder.getPath();
        if (!targetPath.endsWith("/")) {
            targetPath = targetPath + "/";
        }
        if ((asset = assetManager.getAsset(targetPath = targetPath + name)) == null) {
            asset = assetManager.createAsset(targetPath);
        } else {
            assetManager.removeAsset(asset.getPath());
            asset = assetManager.createAsset(asset.getPath());
        }
        HashMap<String, String> map = new HashMap<String, String>();
        if (mimeType != null) {
            map.put("rendition.mime", mimeType);
        }
        asset.setRendition("original", source, map);
        return asset;
    }

    public static void setCoverImage(Resource targetResource, Project project, String mimeType, InputStream stream, String name) {
        try {
            Asset thumbnailAsset = ProjectUtils.createProjectAsset(project, mimeType, stream, targetResource.getResourceResolver(), name);
            ProjectUtils.setStringProperty(targetResource, "coverUrl", thumbnailAsset.getPath());
        }
        catch (Exception e) {
            throw new ProjectException("Failed to update project cover", (Throwable)e);
        }
    }

    public static Resource getCoverImage(Resource resource) {
        Resource imageResource;
        String coverPath = ProjectUtils.getProjectProperty(resource, "coverUrl", String.class);
        if (!StringUtils.isBlank((CharSequence)coverPath) && (imageResource = resource.getResourceResolver().getResource(coverPath)) != null && !ResourceUtil.isNonExistingResource((Resource)imageResource)) {
            return imageResource;
        }
        return null;
    }

    public static String getStringProperty(Resource resource, String name) {
        ValueMap map = (ValueMap)resource.adaptTo(ValueMap.class);
        return (String)map.get(name, String.class);
    }

    public static String getStringProperty(Node node, String name) throws RepositoryException {
        Property property = node.getProperty(name);
        if (property != null) {
            return property.getString();
        }
        return null;
    }

    public static void setStringProperty(Resource resource, String name, String value) throws RepositoryException {
        Node node = (Node)resource.adaptTo(Node.class);
        node.setProperty(name, value);
    }

    public static void setProjectProperty(Resource project, String name, Object value) throws RepositoryException {
        Resource content = project.getChild("jcr:content");
        ModifiableValueMap map = (ModifiableValueMap)content.adaptTo(ModifiableValueMap.class);
        map.put((Object)name, value);
    }

    public static <T> T getProjectProperty(Resource project, String property, Class<T> type) {
        Resource content = project.getChild("jcr:content");
        ValueMap contentVM = (ValueMap)content.adaptTo(ValueMap.class);
        return (T)contentVM.get(property, type);
    }

    public static Resource getGadgetFolder(Resource projectResource) {
        Resource projectContent = projectResource.getChild("jcr:content");
        Resource dashboardResource = projectContent.getChild("dashboard");
        if (dashboardResource == null) {
            return null;
        }
        return dashboardResource.getChild("gadgets");
    }

    public static Resource getOrCreateGadgetFolder(Resource projectResource) throws PersistenceException {
        Resource gadgetResource;
        ResourceResolver resolver = projectResource.getResourceResolver();
        Resource projectContent = projectResource.getChild("jcr:content");
        Resource dashboardResource = projectContent.getChild("dashboard");
        if (dashboardResource == null) {
            HashMap dashboardResourceProps = new HashMap();
            dashboardResource = resolver.create(projectContent, "dashboard", dashboardResourceProps);
        }
        if ((gadgetResource = dashboardResource.getChild("gadgets")) == null) {
            HashMap gadgetsResourceProps = new HashMap();
            gadgetResource = resolver.create(dashboardResource, "gadgets", gadgetsResourceProps);
        }
        return gadgetResource;
    }

    public static String createFolder(String parentPath, String title, String nameHint, String nodeType, String strThumbnailPath, ResourceResolver resourceResolver) {
        String resultingPath = "";
        try {
            Node parentFolderNode = ((Session)resourceResolver.adaptTo(Session.class)).getNode(parentPath);
            String uniqueName = JcrUtil.createValidName((String)nameHint);
            Node folderNode = JcrUtils.getOrCreateByPath((Node)parentFolderNode, (String)uniqueName, (boolean)true, (String)nodeType, (String)nodeType, (boolean)false);
            resultingPath = folderNode.getPath();
            folderNode.setProperty("jcr:title", title);
            folderNode.setProperty("sling:resourceType", "cq/gui/components/projects/admin/card/foldercard");
            if (!StringUtils.isEmpty((CharSequence)strThumbnailPath)) {
                folderNode.setProperty("folderthumbnailurl", strThumbnailPath);
            }
        }
        catch (RepositoryException exception) {
            throw new ProjectException("Could not create project folder: " + resultingPath, (Throwable)exception);
        }
        return resultingPath;
    }

    public static boolean isEmptyFolder(Resource folderResource) {
        Iterator children = folderResource.listChildren();
        while (children.hasNext()) {
            Resource child = (Resource)children.next();
            if (!child.isResourceType("cq/gui/components/projects/admin/card/projectcard") && !child.isResourceType("cq/gui/components/projects/admin/card/foldercard")) continue;
            return false;
        }
        return true;
    }
}