All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.aem.formsndocuments.util.TemplateUtils Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2018 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/

package com.adobe.aem.formsndocuments.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Nonnull;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.query.Query;

import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.aemds.guide.utils.GuideConstants;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.wcm.api.NameConstants;

public class TemplateUtils {

    /** Default log. */
    private static final Logger log = LoggerFactory.getLogger(TemplateUtils.class);

	/**
	* This API returns a list of configuration browsers present in /conf folder
	* @param resolver
	* @return List
	*/
	public static List getConfigurations(ResourceResolver resolver) {
		List configurations = new ArrayList();
		// the configuration subpath we are looking for inside '/conf'
		String pathExpression = (FMConstants.CONF_ROOT + "/%/" + FMConstants.CONF_TEMPLATES_REL_PATH).replaceAll("'","''");
		String sql = "SELECT * FROM [cq:Page] AS p WHERE ISDESCENDANTNODE([" + FMConstants.CONF_ROOT + "]) AND p.[jcr:path] LIKE '" + pathExpression + "' ORDER BY [jcr:path]";
		Iterator iterator = resolver.findResources(sql, Query.JCR_SQL2);
		while (iterator.hasNext()) {
			// the templates setting folder
			// (e.g.'/conf/we-retail/settings/wcm/templates')
			Resource templates = iterator.next();
			String path = templates.getPath();
			// find path of the configuration resource (e.g. '/conf/we-retail')
			String configurationPath = path.substring(0, path.length() - FMConstants.CONF_TEMPLATES_REL_PATH.length());
			// try to read the resource
			Resource configuration = resolver.getResource(configurationPath);
			if (configuration == null) {
				// the user doesn't have read permissions, so we use the
				// templates setting resource instead
				configuration = templates;
			}
			configurations.add(configuration);
		}
		return configurations;
	}

	/**
	* This API returns all the valid Interactive Communication web channel templates.
	* A template is valid when it satisfies following conditions:
	* 1. allowedPaths property for template availability matches assetPathSuffix
	* 2. template should be enabled
	* @param confResource - Configuration browser resource
	* @param assetPathSuffix - Destination path suffix for IC
	* @return List
	*/
	public static List getValidICTemplates(Resource confResource, String assetPathSuffix) {
		List resources = new ArrayList();
		Resource templates;
		if (confResource != null) {
			templates = confResource.getChild(FMConstants.CONF_TEMPLATES_REL_PATH);
			if (templates != null) {
				for (Resource templateResource : templates.getChildren()) {
					if (isValidICTemplate(templateResource, assetPathSuffix)) {
						resources.add(templateResource);
					}
				}
			}
		}
		return resources;
	}

	/**
	* This API filters AF templates based on the allowedPaths property for template availability
	* @param it - An iterator for AF templates
	* @param assetPathSuffix - Destination path suffix for AF
	* @return
	*/
	public static List getFilteredAFTemplates(Iterator it, String assetPathSuffix) {
		List templatesList = IteratorUtils.toList(it);
		List tempTemplatesList = new ArrayList(templatesList);
		Node tempNode;
			for (Resource tempRes : tempTemplatesList) {
				tempNode = tempRes.adaptTo(Node.class);
				try {
					if (tempNode != null && tempNode.hasNode(FMConstants.JCR_CONTENT_NODE_NAME)
							&& !isTemplateAllowed(tempNode.getNode(FMConstants.JCR_CONTENT_NODE_NAME), assetPathSuffix)) {
						templatesList.remove(tempRes);
					}
				} catch (RepositoryException  e) {
					log.error("Exception occured in getFilteredAFTemplates function while fetching jcr:content from : " + tempNode, e);
				}
			}
		return templatesList;
	}

	/**
	* This API returns all the valid IC templates with the given title
	* A template is valid when it satisfies following conditions:
	* 1. allowedPaths property for template availability matches assetPathSuffix
	* 2. template should be enabled
	* @param resolver
	* @param title
	* @param assetPathSuffix
	* @return List
	*/
	public static List findTemplates(ResourceResolver resolver, String title, String assetPathSuffix) {
		List resources = new ArrayList();
		String sql = "SELECT * FROM [cq:Template] AS t WHERE lower(t.[jcr:title]) LIKE '%" + title
				+ "%' OR lower(t.[jcr:content/jcr:title]) LIKE '%" + title + "%' ORDER BY [jcr:path]";
		Iterator iterator = resolver.findResources(sql, Query.JCR_SQL2);
		while (iterator.hasNext()) {
			Resource resource = iterator.next();
			if (isValidICTemplate(resource, assetPathSuffix)) {
				resources.add(resource);
			}
		}
		return resources;
	}

	/*
	 * This function validates whether a given template is valid or not.
	 * A template is valid when it satisfies following conditions:
	 * 1. allowedPaths property for template availability matches assetPathSuffix
	 * 2. template should be enabled
	 */
	private static boolean isValidICTemplate(Resource templateResource, String assetPathSuffix) {
		boolean isValid = false;
		Node tempNode = templateResource.adaptTo(Node.class);
		try {
			if (tempNode != null && tempNode.hasNode(JcrConstants.JCR_CONTENT)) {
				Node jcrContentNode = tempNode.getNode(JcrConstants.JCR_CONTENT);
				if (jcrContentNode != null && isTemplateAllowed(jcrContentNode, assetPathSuffix)) {
					if (jcrContentNode.hasProperty(FMConstants.PROPERTYNAME_STATUS)
							&& FMConstants.STR_ENABLED.equals(jcrContentNode.getProperty(FMConstants.PROPERTYNAME_STATUS).getString())
							&& tempNode.hasNode(FMConstants.TEMPLATE_INITIAL_NODE + FMConstants.DELIMITER_SLASH+ JcrConstants.JCR_CONTENT)) {
						jcrContentNode = tempNode.getNode(FMConstants.TEMPLATE_INITIAL_NODE + FMConstants.DELIMITER_SLASH + JcrConstants.JCR_CONTENT);
						if (jcrContentNode != null && jcrContentNode.hasProperty(GuideConstants.FD_GUIDE_COMPONENT_TYPE)
								&& FMConstants.IC_TEMPLATE_GUIDE_COMPONENT_TYPE.equals(jcrContentNode.getProperty(GuideConstants.FD_GUIDE_COMPONENT_TYPE).getString())) {
							isValid = true;
						}
					}
				}
			}
		} catch (RepositoryException e) {
			log.error("Exception occured in isValidICTemplate function while reading : " + tempNode, e);
		}
		return isValid;
	}

	/*
	 * This function validates whether a template with given path is allowed at given assetPathSuffix.
	 */
	public static boolean isTemplateAllowed(@Nonnull Session session, String templatePath, String assetPathSuffix) {
		boolean isAllowed = false;
		Node templateNode;
		try {
			if (session.nodeExists(templatePath)) {
				templateNode = session.getNode(templatePath);
				if (templateNode != null && templateNode.hasNode(JcrConstants.JCR_CONTENT)) {
					isAllowed = isTemplateAllowed(templateNode.getNode(JcrConstants.JCR_CONTENT), assetPathSuffix);
				}
			}
		} catch (RepositoryException e) {
			log.error("Exception occured in isTemplateAllowed function while reading node from : " + templatePath, e);
		}
		return isAllowed;
	}

	/*
	 * This function validates whether a given template is allowed at given assetPathSuffix.
	 */
	private static boolean isTemplateAllowed(Node node, String assetPathSuffix) {
		boolean isAllowed = false;
		Value[] allowedPathValues;
		try {
			// Check if allowedPaths property is present on node, if its not present then template is valid.
			if (node != null && node.hasProperty(NameConstants.PN_ALLOWED_PATHS)) {
				allowedPathValues = node.getProperty(NameConstants.PN_ALLOWED_PATHS).getValues();
				for (Value v : allowedPathValues) {
					if (StringUtils.isEmpty(v.getString())
							|| (assetPathSuffix != null && assetPathSuffix.matches(v.getString()))) {
						isAllowed = true;
						break;
					}
				}
			} else {
				isAllowed = true;
			}
		} catch (RepositoryException e) {
			log.error("Exception occured in isTemplateAllowed function while fetching allowed path property from : "
					+ node, e);
		}
		return isAllowed;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy