FDMResourcePropertyTransformer.java 6.38 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.guide.transformer.api.ResourcePropertyTransformer
 *  com.adobe.aemds.guide.utils.GuideContainerThreadLocal
 *  com.adobe.aemds.guide.utils.GuideFragmentHolder
 *  com.adobe.aemds.guide.utils.GuideUtils
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.guide.addon.fdm.prefill;

import com.adobe.aemds.guide.transformer.api.ResourcePropertyTransformer;
import com.adobe.aemds.guide.utils.GuideContainerThreadLocal;
import com.adobe.aemds.guide.utils.GuideFragmentHolder;
import com.adobe.aemds.guide.utils.GuideUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FDMResourcePropertyTransformer
implements ResourcePropertyTransformer {
    private JSONObject result = null;
    private Map<String, JSONArray> map = new HashMap<String, JSONArray>();
    private final Logger logger;

    public FDMResourcePropertyTransformer() {
        this.logger = LoggerFactory.getLogger(this.getClass());
    }

    private String getFormDataModelName(String bindRef) {
        if (StringUtils.isEmpty((CharSequence)bindRef)) {
            return null;
        }
        return StringUtils.substringAfterLast((String)bindRef, (String)"/");
    }

    private List<String> getAllPrefixes(String bindRef) {
        ArrayList<String> prefixes = new ArrayList<String>();
        if (!StringUtils.isEmpty((CharSequence)bindRef)) {
            prefixes.add(bindRef);
            String subStringBeforeSlash = StringUtils.substringBeforeLast((String)bindRef, (String)"/");
            while (StringUtils.isNotEmpty((CharSequence)subStringBeforeSlash)) {
                prefixes.add(subStringBeforeSlash);
                subStringBeforeSlash = StringUtils.substringBeforeLast((String)subStringBeforeSlash, (String)"/");
            }
        }
        return prefixes;
    }

    public JSONObject getResult() {
        return this.result;
    }

    public JSONObject transform(Resource resource, String parentSOM) throws JSONException {
        JSONObject obj = new JSONObject();
        ValueMap props = (ValueMap)resource.adaptTo(ValueMap.class);
        if ("layout".equals(resource.getName())) {
            return null;
        }
        String nodeClass = (String)props.get((Object)"guideNodeClass");
        if (StringUtils.isEmpty((CharSequence)nodeClass)) {
            return obj;
        }
        String bindRef = (String)props.get((Object)"bindRef");
        if (StringUtils.isEmpty((CharSequence)bindRef)) {
            return obj;
        }
        obj.put("bindRef", (Object)bindRef);
        GuideFragmentHolder guideFragmentHolder = null;
        try {
            guideFragmentHolder = GuideContainerThreadLocal.getGuideFragmentHolder();
        }
        catch (Exception e) {
            this.logger.error("Unable to get fragment holder", (Throwable)e);
        }
        if (guideFragmentHolder != null) {
            String bindRefPrefixForFragment = guideFragmentHolder.getBindRefPrefixForFragment();
            String fragmentRoot = guideFragmentHolder.getFragmentModelRoot();
            bindRef = GuideUtils.manipulateBindRefForFragments((String)bindRefPrefixForFragment, (String)bindRef, (String)fragmentRoot);
        }
        if (GuideUtils.isGuideFieldModel((String)nodeClass) || GuideUtils.isGuidePanelModel((String)nodeClass)) {
            String stringBeforeSlash = StringUtils.substringBeforeLast((String)bindRef, (String)"/");
            if (StringUtils.isNotEmpty((CharSequence)stringBeforeSlash)) {
                int i;
                String parentModelNameKey = stringBeforeSlash;
                JSONArray arr = this.map.get(parentModelNameKey);
                if (arr == null) {
                    JSONArray modelArr = null;
                    List<String> models = this.getAllPrefixes(stringBeforeSlash);
                    for (i = models.size() - 1; i >= 0; --i) {
                        String modelKey = models.get(i);
                        if (!this.map.containsKey(modelKey)) {
                            this.addPanelToResult(modelKey, modelArr);
                        }
                        modelArr = this.map.get(modelKey);
                    }
                    arr = modelArr;
                }
                if (GuideUtils.isGuidePanelModel((String)nodeClass)) {
                    this.addPanelToResult(bindRef, arr);
                } else {
                    String name = this.getFormDataModelName(bindRef);
                    boolean isKeyPresent = false;
                    for (i = 0; i < arr.length(); ++i) {
                        if (!arr.getString(i).equals(name)) continue;
                        isKeyPresent = true;
                        break;
                    }
                    if (!isKeyPresent) {
                        arr.put((Object)name);
                    }
                }
            } else {
                this.addPanelToResult(bindRef, null);
            }
        }
        return obj;
    }

    private void addPanelToResult(String bindRef, JSONArray arr) throws JSONException {
        if (!this.map.containsKey(bindRef)) {
            String dataModelName = this.getFormDataModelName(bindRef);
            if (StringUtils.isEmpty((CharSequence)dataModelName)) {
                return;
            }
            JSONObject current = new JSONObject();
            JSONArray child = new JSONArray();
            current.put(dataModelName, (Object)child);
            if (this.result == null) {
                this.result = current;
            } else if (arr != null) {
                arr.put((Object)current);
            } else {
                child = new JSONArray();
                this.result.put(dataModelName, (Object)child);
            }
            this.map.put(bindRef, child);
        }
    }
}