
com.day.cq.dam.commons.util.SchemaFormHelper Maven / Gradle / Ivy
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2013 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 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.day.cq.dam.commons.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.jcr.RepositoryException;
import com.adobe.granite.confmgr.Conf;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.jcr.resource.api.JcrResourceConstants;
import org.apache.sling.tenant.Tenant;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.dam.api.DamConstants;
import com.day.cq.dam.commons.schemaforms.internal.TabList;
import com.day.text.Text;
/**
* this is an internal class to be used by dam code.
*/
public class SchemaFormHelper {
/**
* Given current form, returns a list of master.
* Looks for master forms up in the path heirarchy,
* while traversing it takes into account any override
* available in apps
*
* @param currentForm current form resource
* @return list of master form resource, empty list if no master is found
* @throws RepositoryException
* @deprecated Use getMasterForms(Resource resource) instead
*/
@Deprecated
public static List getMasterForms(Resource currentForm, String formsBaseDirPath) throws RepositoryException {
throw new UnsupportedOperationException("This API has been deprecated. Please use getMasterForms(Resource resource) instead.");
}
/**
* @return Metadata Schema provides a OOTB form. And User can overlay this form as per use case.
* It returns list of form path homes (Overlay, OOTB) in the order. e.g. [/conf/metdataschema, /libs/metadataschema]
*/
public static String[] getBaseFormPaths(ResourceResolver resourceResolver){
// if user is admin the ResourceResolver#adaptTo(Tenant.class) call logs a NPE
// check for admin to avoid this loggin
Tenant tenant = null;
User user = resourceResolver.adaptTo(User.class);
if(user != null && !user.isAdmin()) {
tenant = resourceResolver.adaptTo(Tenant.class);
}
return getBaseFormPathsFromTenant(resourceResolver, tenant);
}
private static String[] getBaseFormPathsFromTenant(ResourceResolver resourceResolver, Tenant tenant) {
String schemaExtHome = (null != tenant)? (String)tenant.getProperty(DamConfigurationConstants.METADATA_SCHEMA_HOME) : DamConfigurationConstants.DEFAULT_METADATA_SCHEMA_HOME;
String tenantAssetsRoot = (null != tenant)? (String)tenant.getProperty(DamConfigurationConstants.DAM_ASSETS_ROOT): DamConstants.MOUNTPOINT_ASSETS;
String schemaHome = DamConfigurationConstants.OOTB_METADATA_SCHEMA_FORM_HOME;
Resource schemaExtHomeRes = resourceResolver.getResource(schemaExtHome);
if(null == schemaExtHome || schemaExtHome.trim().isEmpty() || schemaExtHomeRes == null){
schemaExtHome = DamConfigurationConstants.DEFAULT_METADATA_SCHEMA_HOME;
}else{
Conf conf = resourceResolver.getResource(tenantAssetsRoot).adaptTo(Conf.class);
schemaHome = conf.getItem(DamConfigurationConstants.ADMIN_UI_OOTB_CONF_RELPATH).get(DamConfigurationConstants.METADATA_SCHEMA_HOME, DamConfigurationConstants.OOTB_METADATA_SCHEMA_FORM_HOME);
}
return new String[]{schemaExtHome, schemaHome};
}
/**
* retruns relatibe path of the form '/default/image'
* @param formPath
* @param baseFormPaths
* @return
*/
private static String getRelativeFormPath(String formPath, String[] baseFormPaths){
for(String baseFormPath : baseFormPaths){
if (formPath.startsWith(baseFormPath)){
String relativePath = formPath.substring(baseFormPath.length());
relativePath = relativePath.startsWith("/") ? relativePath : relativePath + "/";
relativePath = relativePath.endsWith("/") ? relativePath.substring(relativePath.length() -1 ) : relativePath;
return relativePath;
}
}
return formPath;
}
/**
* Given current form, returns a list of master.
* Looks for master forms up in the path heirarchy,
* while traversing it takes into account any override
* available in apps
*
* @param currentForm current form resource
* @return list of master form resource, empty list if no master is found
* @throws RepositoryException
* @throws java.lang.NullPointerException
*/
public static List getMasterForms(Resource currentForm) throws RepositoryException {
ResourceResolver resourceResolver = currentForm.getResourceResolver();
String[] baseFormPaths = getBaseFormPathsFromTenant(resourceResolver, currentForm.adaptTo(Tenant.class));
String relativePath = getRelativeFormPath(currentForm.getPath(), baseFormPaths);
List masterFormsList = new ArrayList();
relativePath = relativePath.trim().isEmpty() ? "" : relativePath.substring(0, relativePath.lastIndexOf('/'));
while(!relativePath.trim().isEmpty()){
for(int i=0; i resourceList = new ArrayList();
String[] baseFormPaths = getBaseFormPaths(resolver);
suffix = null == suffix ? "" : suffix;
suffix = getRelativeFormPath(suffix, baseFormPaths);
//create a concatenated tree out of APPS_DIR & LIBS_DIR
String appsResPath = baseFormPaths[0] + suffix;
String libsResPath = baseFormPaths[1] + suffix;
Resource appRes = resolver.getResource(appsResPath);
Resource libsRes = resolver.getResource(libsResPath);
// fill all the resources in /apps which are not in /libs
if (appRes != null) {
for (Iterator appIter = appRes.listChildren(); appIter.hasNext(); ) {
Resource ar = appIter.next();
String resName = Text.getName(ar.getPath());
if (libsRes == null || libsRes.getChild(resName) == null) {
if (ar.isResourceType(JcrConstants.NT_FOLDER) ||
ar.isResourceType(JcrResourceConstants.NT_SLING_ORDERED_FOLDER) ||
ar.isResourceType(JcrResourceConstants.NT_SLING_FOLDER))
resourceList.add(ar);
}
}
}
// for all resources common in /apps and /libs, take those that are in /apps
// else take those that are in /libs
if (libsRes != null) {
for (Iterator libsIter = libsRes.listChildren(); libsIter.hasNext(); ) {
Resource lr = libsIter.next();
String resName = Text.getName(lr.getPath());
if (appRes != null && appRes.getChild(resName) != null && appRes.getChild(resName).getChild("items/tabs") != null) {
Resource ar = resolver.getResource(appRes.getPath() + "/" + resName);
if (ar.isResourceType(JcrConstants.NT_FOLDER) ||
ar.isResourceType(JcrResourceConstants.NT_SLING_ORDERED_FOLDER) ||
ar.isResourceType(JcrResourceConstants.NT_SLING_FOLDER)) {
resourceList.add(ar);
}
} else {
if (lr.getChild("items/tabs") != null && lr.isResourceType(JcrConstants.NT_FOLDER) ||
lr.isResourceType(JcrResourceConstants.NT_SLING_ORDERED_FOLDER) ||
lr.isResourceType(JcrResourceConstants.NT_SLING_FOLDER))
resourceList.add(lr);
}
}
}
return resourceList.iterator();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy