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

com.adobe.aemds.guide.utils.GuideStyleMigrationUtils Maven / Gradle / Ivy

/*
 * **********************************************************************
 *  ADOBE CONFIDENTIAL
 *  __________________
 *
 *  Copyright 2016 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.aemds.guide.utils;

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.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.resourcemerger.api.ResourceMergerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.RepositoryException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @pad.exclude Exclude from Published API.
 */
public class GuideStyleMigrationUtils {
    private static Logger logger = LoggerFactory.getLogger(GuideStyleMigrationUtils.class);

    public static Map getSelectorResourceMap(Resource styleConfig, Map map, String pathPrefix) {
        if (styleConfig != null) {
            Iterable children = styleConfig.getChildren();
            Iterator iterator = children.iterator();
            while (iterator.hasNext()) {
                String prefix = pathPrefix;
                Resource childResource = iterator.next();
                Resource actualChildResource = getTargetResource(childResource);
                prefix += "/" + childResource.getName();
                Map tempMap = getSelectorResourceMap(actualChildResource,map,prefix);
                map.putAll(tempMap);
            }

            ValueMap values = styleConfig.getValueMap();
            String id = values.get("id", String.class);
            if (id != null) {
                pathPrefix = pathPrefix.replaceAll("/items/", "/");
                if(pathPrefix.startsWith("/")) {
                    pathPrefix = pathPrefix.substring(1);
                }
                map.put(id, pathPrefix);
            }
        }
        return map;
    }

    public static Map getSelectorResourceMap(Resource styleConfig, Map map) {
        if (styleConfig != null) {
            Iterable children = styleConfig.getChildren();
            Iterator iterator = children.iterator();
            while (iterator.hasNext()) {
                Resource childResource = iterator.next();
                Resource actualChildResource = getTargetResource(childResource);
                Map tempMap = getSelectorResourceMap(actualChildResource, map);
                map.putAll(tempMap);
            }
            ValueMap values = styleConfig.getValueMap();
            String id = values.get("id", String.class);

            if (id != null) {
                map.put(id, styleConfig.getPath());
            }
        }
        return map;
    }


    private static String removeConfigPrefix(String fullPathString) {
        String selectorPath = fullPathString;
        //remove the part - cq:styleConfig/items/
        if(fullPathString.contains("cq:styleConfig")) {
            selectorPath = fullPathString.substring(fullPathString.indexOf("cq:styleConfig") + 21);
        }
        if(fullPathString.contains("cq:themeConfig")) {
            selectorPath = fullPathString.substring(fullPathString.indexOf("cq:themeConfig") + 21);
        }
        return selectorPath;
    }

    //if target is set return it, else return the resource itself
    private static boolean isTargetResource(Resource resource) {
        ValueMap values = resource.getValueMap();
        return values.containsKey("target");
    }

    //if target is set return it, else return the resource itself
    private static Resource getTargetResource(Resource resource) {
        ValueMap values = resource.getValueMap();
        String target = values.get("target", String.class);
        if (target != null) {
            ResourceResolver resolver = resource.getResourceResolver();
            return resolver.getResource(target);
        }
        return resource;
    }

    public static void migrateBreakpointJson(JSONObject styleJSON, Resource styleResource, String componentResourceType, ResourceMergerService resourceMergerService) throws PersistenceException, RepositoryException, JSONException {
        ResourceResolver resolver = styleResource.getResourceResolver();
        JSONObject selectorsParent = styleJSON.getJSONObject("selectors");
        Iterator selectorKeys = selectorsParent.keys();


        Map selectorMap = getSelectorIDMap(componentResourceType, resolver, resourceMergerService, "cq:styleConfig");
        while(selectorKeys.hasNext()){
            String selectorKey = selectorKeys.next();
            String updatedKey = getIDBasedSelector(selectorMap, selectorKey);
            Map selectorProperties = new HashMap();
            JSONObject selectorObject = selectorsParent.getJSONObject(selectorKey);
            JSONObject breakpointParent = selectorObject.getJSONObject("breakpoints");
            Iterator breakpointsKeys = breakpointParent.keys();
            while(breakpointsKeys.hasNext()){
                String breakpointName = breakpointsKeys.next();
                JSONObject breakpointJson = breakpointParent.getJSONObject(breakpointName);
                JSONObject statesParent = breakpointJson.getJSONObject("states");
                Iterator statesKeys = statesParent.keys();
                while(statesKeys.hasNext()){
                    String stateName = statesKeys.next();
                    Map stateProperties = getStatePropertiesMap(breakpointName, stateName, statesParent.getJSONObject(stateName));
                    selectorProperties.putAll(stateProperties);
                }
            }
            resolver.create(styleResource, updatedKey, selectorProperties);
        }
    }

    public static String getIDBasedSelector(Map selectorMap, String selectorKey){
        String updatedKey = selectorKey;
        if(!selectorMap.containsKey(selectorKey)) {
            //if key is found - it means path to ID migration has been done, else to the migration
            if(selectorMap.containsValue(selectorKey)) {
                Iterator selectorMapKeysIter = selectorMap.keySet().iterator();
                while(selectorMapKeysIter.hasNext()) {
                    String key = selectorMapKeysIter.next();
                    String value = selectorMap.get(key);
                    if(selectorKey.equals(value)) {
                        updatedKey =  key;
                        break;
                    }
                }
            }
        }
        return updatedKey.replace("/", "_");
    }

    /**
     * Save the theme information for the state.
     *
     * @param breakpointName      state name for which the theme definition is to be saved
     * @param stateName      state name for which the theme definition is to be saved
     * @param stateJSON      theme definition for the state
     * @throws RepositoryException
     * @throws JSONException
     */
    public static Map getStatePropertiesMap(String breakpointName,
                                            String stateName,
                                            JSONObject stateJSON) throws RepositoryException, JSONException {
        HashMap stateMap = new HashMap();
        String cssPropertyName = breakpointName + "#" + stateName;
        String uiPropertyName = breakpointName + "#" + stateName + "#ui";

        if (stateJSON.has("cssProperties")) {
            JSONObject uiPropertiesJSON = (JSONObject) stateJSON.get("cssProperties");
            Iterator keys = uiPropertiesJSON.keys();
            List properties = new ArrayList();
            while (keys.hasNext()) {
                String key = keys.next();
                properties.add(key+":"+uiPropertiesJSON.getString(key));
            }
            if(properties.size() > 0) {
                stateMap.put(cssPropertyName, properties.toArray(new String[properties.size()]));
            }
        }
        if (stateJSON.has("uiProperties")) {
            JSONObject uiPropertiesJSON = (JSONObject) stateJSON.get("uiProperties");
            Iterator keys = uiPropertiesJSON.keys();
            List properties = new ArrayList();
            while (keys.hasNext()) {
                String key = keys.next();
                properties.add(key+":"+uiPropertiesJSON.getString(key));
            }
            if(properties.size() > 0) {
                stateMap.put(uiPropertyName, properties.toArray(new String[properties.size()]));
            }
        }
        return stateMap;
    }

    public static Map getSelectorMap(String componentType,
                                     ResourceResolver resolver,
                                     ResourceMergerService resourceMergerService,
                                     String configNodeName) {
        Map selectorResourceMap = new HashMap();
        if(componentType.endsWith("fd/af/components/rootPanel")) {
            componentType = "fd/af/components/panel";
        } else if(componentType.endsWith("fd/af/components/guideContainerWrapper")){
            componentType = "fd/af/components/guideContainer";
        }
        String componentPath = resolver.getResource(componentType).getPath();
        Resource componentRes = resolver.getResource("/mnt/override/" + componentPath);
        if (componentRes != null) {
            Resource styleConfigMergedRes = componentRes.getChild(configNodeName);
            selectorResourceMap = getSelectorResourceMap(styleConfigMergedRes, selectorResourceMap);
        }
        return selectorResourceMap;
    }

    public static Map getSelectorIDMap(String componentType,
                                     ResourceResolver resolver,
                                     ResourceMergerService resourceMergerService,
                                     String configNodeName) {
        Map selectorResourceMap = new HashMap();
        String componentPath = resolver.getResource(componentType).getPath();
        Resource componentRes = resolver.getResource("/mnt/override/" + componentPath);
        if (componentRes != null) {
            Resource styleConfigMergedRes = componentRes.getChild(configNodeName);
            selectorResourceMap = getSelectorResourceMap(styleConfigMergedRes, selectorResourceMap, "");
        }
        return selectorResourceMap;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy